Variables in python
start your journey in python programming
what is a variable?
a variable is a place to store the information for that value. The type of a variable can be changed according to future uses.
what is the type of variable: There are many types of variable which can be used in many programming languages to establish the accurate performance of the coding.
for example type of variable can be integer, float, string, etc.,
a = 60 integer type,
a=60.2 float type,
name = 'Datascience'string type
Python variable naming
There are some precise rules to what you can name a variable(called an identifier)
i. Python variables can only begin with a letter A-Z,a-z, or an underscore(_).
ii. The rest of the variables may contain all A-Z,a-z,underscore(_),and numbers(0–9)
iii. Python is case sensitive, Test and test are two different identifiers.
iv. Reserved words (keywords) cannot be used as identifier names.
and def False import not True as del finally in or try assert elif for is pass while break else from lambda print with class except global None raise yield continue exec if nonlocal return
for assign any value to the variable ‘=’(assignment operator )is used,
quantity=45
here quantity is an integer type and the assigned identifier is 45 its value is 45.
45=quantity this is the wrong way of assigning
you cant put an identifier on the right-hand side of the equal to sign if you assign code that causes the error as a syntax error.
Multiple assignments
age,city = 85,'UP'
print(age,city)
output
85 UP
can assign the same value to the multiple variables
>>> age=fav_num=7
>>> print(age,fav_num)
output
7 7
swapping variables
swapping is the interchanging of any values. when comparing other programming languages python programming is very easy to read and write code.
>>> a,b = 1,2
>>> a,b = b,a
>>> print(a,b)
output
2,1
Deleting variables: Deleting variables is possible by using the ‘del’ keyword
>>> a = 1
>>> del a
>>> a
output
NameError: name 'a' is not defined