1. Creating variables In Python
1.1. Simple assignment
As python is interpreter language, so here there is no such keyword for declaring a variable. The interpreter allocate memory at runtime and decide what to stored in reserved location of memory.
varNum = 20 varName = "OnlineTutorials" print(varNum) # prints 20 print(varName) # prints OnlineTutorials
A variable of string type may be created with either single quotes and inverted comma, both
varStringSingleQuotes = 'OnlineTutorials' varStringDoubleQuotes = "OnlineTutorials" print(varStringSingleQuotes) # prints OnlineTutorials print(varStringDoubleQuotes) # prints OnlineTutorials
1.2. Chained assignment In Python
Python also allows chained assignment, which makes it possible to assign the identical value to many variables simultaneously.
a = b = c = 20 print(a) # prints 20 print(b) # prints 20 print(c) # prints 20
1.3. Multiple assignments in single line In Python
Python allows you to assign values to multiple variables in one line
a, b, c = "A", "B", 100 print(a) # prints A print(b) # prints B print(c) # prints 100
1.4. Variable re-declaration In Python
As variables don’t need datatype information, we will reassign a brand-new value of any type with none problem. In Python, a variable could also be assigned a worth of 1 type so later re-assigned a worth of a unique type.
varReDeclare = 10 varReDeclare = 20 varReDeclare = "NA" print(varReDeclare) # prints NA
2. Naming Conventions In Python
The main foundations for creating variables in Python are:
- A variable name must start with a letter or the underscore character.
- A variable name cannot start with a number.
- A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ ).
- Variable names are case-sensitive. For example – onlineturorials, Onlinetutorials and ONLINETUTORIALS are three different variables.
3. Local vs global variables In Python
3.1. Creating local and global variables in python
A variable created inside a function is named local variable.
variable created outside a function is global variable. Global variables may be utilized by everyone, both inside functions and outdoors.
a = 10 # here scope is global def func(): b = 10 # local variable print("The summation of a and b = " + str(a + b)) #The summation of a and b = 20 func() print("The summation of a and b = " + str(a + b)) #NameError: name 'b' is not defined
3.2. Local variables are bounded within function scope in python
If you create a variable with the identical name inside a function, this variable is local, and may only be used inside the function. the global variable with the identical name will remain because it was, global and with the initial value.
3.3. The ‘global’ keyword in python
To make a global variable inside a function, we will use the worldwide keyword.
a = 10 # here scope is global def func(): global b b = 10 # creating global variable inside function print("The summation of a and b = " + str(a + b)) # The summation of a and b = 20 func() print("The summation of a and b = " + str(a + b)) # The summation of a and b = 20