Variables in Python
Variables are nothing but a named memory space used to store data. However, when a variable has created some space of memory is reserved. In python based on the type of data of variable, the interpreter decides what should be stored in reserved memory and that variable will carry the data which is assigned. Different types of data such as Integers, Decimals, or characters can be assigned to the variable.
In python there is no need for an external declaration of variables; python automatically declares the variable when you assign the value to a variable in the program.
The left side of the operand is the name of the variable and the right side of the operand of the value which is to be stored in the variable.
For example:
Num1 = 45
Num2 = "Amit"
print(Num1)
print(Num2)
Variables declaration is not allowed any particular type and same variable can be assigned another value of data type.
NumA = 4 #NumA is of type integer
NumA = "Sally" #NumA is now of type String
print(A)
Rules for variable naming convention
Variable names should be user-friendly i.e. car, fruits, mobile, etc.
1. Variable name should start with an underscore character or a letter.
2. Variable name should not suppose to start with a number or numeric digit.
3. Case sensitivity is present in the variable name i.e. (fruits, FRUITS, and Fruits are different due to their cases)
4. Variable name should only consist of underscore (A-z, 0-9, and _) and only alphanumeric characters in it.
Examples:
#correct naming convention
keyname = “Ravi”
key_name = “Ravi”
_key_name = “Ravi”
keyName = “Ravi”
KEYNAME= “Ravi”
keyname34 = “Ravi”
#wrong naming convention
4rights = “Ravi”
My-sports = “Ravi”
My car = “Ravi”
Assign Value to Multiple Variables
Multiple variables can be assign value in one line and you can also assign the same value of data to more that one variables in one line of code.
xa, yb, zc = "Man", "animal", "child"
print(xa)
print(ya)
print(za)
Output Variables
In Python print statement is mostly used to output variables in a program and usually to combine both text and a variable, Python uses the + (addition) character.
Plus operator is used for two way for adding integers and for concatenation of strings.
Example
x = "Ravi"
print("My name is " + x)
line1 = 5
line2 = "Kavita"
print(line1 + line2)
