A variable is simply a reserved memory location used to store values. This means that when a variable is created, it retains some space in memory.
Depending on the data type of a variable, the interpreter allocates memory and determines how it can be stored in the reserved memory. So, by assigning variables of different data types, you can store integers, decimals, or characters in these variables.
Assignment of variables:
' 123 ' 'ABC'== 123
Variable type:
1. String
2. Boolean type
3, Integer
4. Floating point number
5. Digital
6. List
7, meta-group
8. Dictionaries
9. Date
1. String
' python ' "python"'hellopython'print # pythonprint# pythonprint# Hello python preserves line breaks
2. Boolean type
bool == False
3. Integer
I= = Int ('+')
4. Floating point number
f = 23.4= float ('23.4')
Other types are described in detail in other chapters.
Scope of the variable:
- The code snippet in which Python can change the scope of a variable is Def, class, Lamda.
- If/elif/else, try/except/finally, For/while are not involved in variable scope changes, which means that the variables in their code block are also accessible externally.
- Variable search path is: local variable, global variable
A = 1if a = =1 := []print(a)#>>>[1,2,3] If statements do not affect the scope of the variable = 1def F (): = 2print# 1f () The execution of the Print # 1 function does not affect the value of variable a
If you want to manipulate variables in Def,class,lambda, you need to declare them internally:
A = 1def F (): Global a = 2print# 1f () print# 2 manipulating variables inside a function by declaring global variables
Python Basics-Variables