Python Variables

Python Variables – In this tutorial we shall learn the following things regarding python variables :

Note : Python 3.6.1 is considered

Assigning Values to Variables in Python

“=” (equal to) is the operator used to assign values to variables. An example is given below :

number_1 = 100
Try Online

On the left side (number_1) is the variable. On the right side (100) is the value.

Python Variables Assignment - Python Tutorial - www.tutorailkart.com
Python Variables Assignment

Variable Declaration in Python

There is no explicit declaration required for a variable in Python. When a variable is used in the python code, python interpreter interprets the data type from the actual value that is assigned to the variable.

Consider the python program,

variableDeclaration.py

number_1 = 100
number_2 = 120
total = number_1+number_2
print(total)
Try Online

Output

220
Try Online

In the above program, interpreter considered the python variables number_1 and number_2 as numbers from the values 100, 120 being stored in them respectively. And also total being assigned to the sum of two numbers also considered as a number.

One important thing that has to be kept in mind when writing in python is that, Python is a Dynamically-Typed language. If the variable total is assigned a string value, python considers total as a string value, from the instruction at which total is assigned a string value. This behavior is depicted in the following python program,

variableDeclaration.py

number_1 = 100
number_2 = 120
total = number_1+number_2
total = "hello user"
print(total)
Try Online

Output

hello user

The Python Interpreter considers only data type of value, the variable is holding. If there is any new assignment to the variable by a value that belongs to different data type, from that moment on, Python Interpreter considers the new data type for the variable, until another assignment comes.

Standard Data Types in Python

In prior to knowing the available standard data types in python, let us know the importance of having a data type.

Data Type defines how the variable’s value should be stored in memory. And also what operations could be done on that variable.

For an example, let us take numbers and strings. Numbers (say integers) are stored in 32-bit or 64-bit storage, while strings on the other hand are stored each character in a byte, sequentially in the memory, with number of bytes equal to number of characters in the string. Also some of the meaningful operations like addition or subtraction that can be done on numbers are not meaningful for strings.

Coming to the Standard Data Types in Python, we have the following :

  • Boolean
  • Numeric
    • int
    • float
    • long
    • complex
  • Sequence
    • String
    • List
    • Tuple
  • Set
  • Mapping
    • Dictionary

Assigning Multiple Variables

In Python, we can assign multiple variables a value in a single statement.

example.py – Python Program

jan = mar = may = jul = aug = oct = dec = 31
apr = jun = sep = nov = 30
feb = 28
total = jan+feb+mar+apr+may+jun+jul+aug+sep+oct+nov+dec
print(total)
Try Online

Output

365

Where the months with 31 days are assigned a value 31 in a single statement and the months with 30 days is given a value 30 in a single statement.

Conclusion

To conclude this Python Tutorial, we have learnt about python variables, assigning values to them, the available data types in python, assigning multiple variables a value in a single statement.