Python variables declaration and naming conventions with rules with sample code using google colab.

preview_player
Показать описание
Like any other programming language we can create and use variables to store our data. There are some rules or conventions we have to follow while creating variables in Python.
Name must start with lower or upper case char or with underscore (only ) . We can’t use number as first char of the variable.
We can use number or underscore within the variable name.
There is no length restriction
Can assign different types to same variable
Variables are case sensitive
Reserved words can’t be used
Must assign before using
We can do operation with similar variables
Scope is limited to page only , it is destroyed once the execution is over , we are discussing only normal variables not session variables

Declaring variable
a=45
my_name="Raju"
_mark=65
Address="World"
print ( Address,my_name,_mark,a )

We can’t use number as first char of the variable
4t=56
We can convert string to integer by using int() function , similarly we can convert one integer variable to string variable by using str() function. We will use this to convert the variables before adding them.
a='45' # a is a string variable, it is not an integer
b=42
#print(a+b) # error as we can't add one string with integer variable
print(a+str(b)) # this is fine
print(int(a)+b) # this is fine

Usually life of a variable remain till the execution of the page is over.
Рекомендации по теме
welcome to shbcf.ru