Python variables are use to store values temporary in a memory location. Based on the data type of a variable, the interpreter allocates memory and decides what type of value can be stored in the reserved memory. Therefore, by assigning different data types to variables, you can store integers, decimals or characters value.
Python variables do not need explicit declaration to reserve memory space. The declaration happens automatically when you assign a value to a variable. The value is always assign from right to left.
name = "Python" # A string name = 'Python' # also a string name = 'Python @ 3' # also a string name = 'Python # 3.7' # also a string age = 30 # An integer value salary = 25555.5 # A floating point flag = True # Boolean flag2 = 10>15 # Boolean print (name) print (age, end=" ") print (salary) print (flag2) print (name, age, flag, sep=',')
Python allows you to assign a single value to several variables simultaneously. For example:
a = b = c = 1
All three variables are assigned value with 1. You can also assign different values to multiple variables in 1 line.
a,b,c = 1,2,"python"
a is assigned 1, b is assigned value 2, and c is assigned value with python.
Swap two numbers
a,b=2,3 a,b=b,a print(a, b)