Python Variables

Variables in Python refers to reserved memory locations, and are used to store values. In simple words, we can say that a variable is basically a container for a value used in a program.

Python doesn't provide any command to declare a variable. The variable is created at the time of assigning value to it. The type of variable, depends on the type of value assigned to it.

Python is a case-sensitive language. Therefore myname and myName are two different variables. For example:

myname = "codescracker"
myName = "codescracker.com"
MyName = "codes cracker"

print(myname)
print(myName)
print(MyName)

The output is:

codescracker
codescracker.com
codes cracker

Note - The type() function is used to find the type of variable. For example:

x = 10
print(type(x))

x = 10.45
print(type(x))

x = "Python Programming"
print(type(x))

x = True
print(type(x))

x = [12, 23, 453]
print(type(x))

The output is:

<class 'int'>
<class 'float'>
<class 'str'>
<class 'bool'>
<class 'list'>

The variable x belongs to the class int, when it holds the value 10. The same variable, that is, x belongs to the class float, when it holds the value 10.45, and so on.

See, the same variable, that is x changes its class, when a new value of different type gets initialized to it. Means, whatever we initialize a value to a variable, its type depends on the value's type.

Note - This is the big advantage of Python over other languages like Java, C+, C++ etc. As it doesn't require to declare a variable before its use. Also a single variable can be used with all types of values.

Similar to this advantage, there are multiple things where Python dominates. In Python, with less amount of statements or codes, we can do similarly bigger task. Or we can say, use Python to do more with less code.

Variable Naming Rules in Python

The simple rules while naming variables in Python are:

  • A variable must start with an alphabet (A-Z, a-z) or an underscore character (_)
  • And rest characters of the variable can be created using alphanumeric characters (A-Z, a-z, 0-9) including an underscore character (_) too.

Examples of Some Valid Variable Names in Python

Here are the list of some valid variable names in Python:

  • _myval
  • myval
  • _Myval
  • Myval
  • my1val
  • my_1_val
  • myval_
  • My_1_Val_2

Python Variable Examples

All the topics of variables in Python, are covered. Therefore it is the time to take some of the examples, that may boost your skills towards Python. Here is the first example, demonstrates the assign of single value to multiple variables using single statement:

x = y = z = 500
print(x)
print(y)
print(z)

The output is:

500
500
500

Now let's consider the following example:

a = 10
b = 20
c = 30
d = 40
e = 50

print(a)
print(b)
print(c)
print(d)
print(e)

This/above program can also be created in following way:

a, b, c, d, e = 10, 20, 30, 40, 50

print(a)
print(b)
print(c)
print(d)
print(e)

The output of both the above program is:

10
20
30
40
50

Here is another program, where multiple values to multiple variables are initialized using of course a single statement. Also the values of all variables are printed using a single print() statement:

a, b, c, d, e = 10, 20, 30, 40, 50

print(a, b, c, d, e)

Now the output of this program is:

10 20 30 40 50

Here is another program, that receives an input from user to store the entered value to a variable say val and the value of the variable gets printed, back on the output screen:

print("Enter a Value: ")
val = input()
print("\nYou've entered:", val)

The snapshot given below shows the sample run of above program, with user input I love Python as input:

variables in Python

To get the input at the same line of message, use end= parameter, that skips the insertion of an automatic newline at the end of print().

Following is another example, demonstrates the storing of value provided by user at run-time of the program and printing the value of variable, back on the output screen:

print("Who are You ? ", end="")
x = input()

print("\nHello", x)
print("\nWhat is Your Name: ", end="")
x = input()
print("\nSo, You're", x)

print("\nWhy are you here ? ", end="")
x = input()
print("\nOk!\nYou're here", x)

The sample run with user input Student, Julia, and to learn Python is shown in the snapshot given below:

python variables

Python Online Test


« Previous Tutorial Next Tutorial »

Follow/Like Us on Facebook




Subscribe Us on YouTube