Variables
Variables are nothing but reserved memory locations to store values. This means that when you create a variable you reserve some space in memory.
Based on the data type of a variable, the interpreter allocates memory and decides what can be stored in the reserved memory. Therefore, by assigning different data types to variables, you can store integers, decimals or characters in these variables.
Assigning Values to Variables
Python variables do not need explicit declaration to reserve memory space. The declaration happens automatically when you assign a value to a variable. The equal sign (=) is used to assign values to variables.
The operand to the left of the = operator is the name of the variable and the operand to the right of the = operator is the value stored in the variable. For example −
>>> Age=25 #integer
>>> percentage=66.6 #Float
>>> Student_name="Mohan" #String
Here, 25, 66.6 and "Mohan" are the values assigned to Age, percentag, and Student_name variables, respectively. This produces the following result −
>>> print(Age)
25
>>> print(percentage)
66.6
>>> print(Student_name)
Mohan
Multiple Assignment
Python allows you to assign a single value to several variables simultaneously. For example −
>>> a=b=c=1
Here, an integer object is created with the value 1, and all three variables are assigned to the same memory location. You can also assign multiple objects to multiple variables. For example −
>>> a, b, c = 10, 20, "Keshav"
Here, two integer objects with values 1 and 2 are assigned to variables a and b respectively, and one string object with the value "Keshav" is assigned to the variable c.
Standard Data Types
The data stored in memory can be of many types. For example, a person's age is stored as a numeric value and his or her address is stored as alphanumeric characters. Python has various standard data types that are used to define the operations possible on them and the storage method for each of them.
Python has five standard data types −
-
Numbers
-
String
-
List
-
Tuple
-
Dictionary
Numbers
String
List
Tuple
Dictionary
Numbers
The interpreter acts as a simple calculator: you can type an expression at it and it will write the value. Expression syntax is straightforward: the operators
+, -, * and / work just like in most other languages (for example, Pascal or C); parentheses (()) can be used for grouping. For example:
The integer numbers (e.g.
2, 4, 20) have type int, the ones with a fractional part (e.g. 5.0, 1.6) have type float. We will see more about numeric types later in the Blog.
Division (
/) always returns a float. To do floor division and get an integer result (discarding any fractional result) you can use the // operator; to calculate the remainder you can use %:
With Python, it is possible to use the
** operator to calculate powers [1]:
The equal sign (
=) is used to assign a value to a variable. Afterwards, no result is displayed before the next interactive prompt:
If a variable is not “defined” (assigned a value), trying to use it will give you an error:
There is full support for floating point; operators with mixed type operands convert the integer operand to floating point:
In interactive mode, the last printed expression is assigned to the variable
_. This means that when you are using Python as a desk calculator, it is somewhat easier to continue calculations, for example:
This variable should be treated as read-only by the user. Don’t explicitly assign a value to it — you would create an independent local variable with the same name masking the built-in variable with its magic behavior.
In addition to
int and float, Python supports other types of numbers, such as Decimal and Fraction. Python also has built-in support for complex numbers, and uses the j or J suffix to indicate the imaginary part (e.g. 3+5j).