Variable : It is a symbolic name that is a reference or pointer to an object. Once an object is assigned to a variable, you can refer to the object by that name. Example:
a=5
b=10.78
c=2 + 7j
d=”Gargs Academy”
e=True
f=None
a,b,c,d,e,f are variables of different types. We can assign any type of value to a variable in python.
left value or l-value or locator value: It is an assignable object. It is any expression that may occur on the leftside of an assignment. Variables names are l-values. Example:
a=5
b=10.78
c=2 + 7j
d=”Gargs Academy”
e=True
f=None
a,b,c,d,e,f are l-values
right value or r-value: It is any expression that has a value that may appear on the right of an assignment. Example:
a=5
b=10.78
c=2 + 7j
d=”Gargs Academy”
e=True
f=None
5, 10.78, 2+7j, “Gargs Academy”, True, None are r-values
Types of Values in Python: Following types of values are supported in python.
1. Numeric (Integer, Float, Complex)
2. Boolean
3. Sequences/ Collections (List, Tuple, Set, Strings)
4. Mapping ( Dictionary)
5. Special Data Type None
1. Numeric Values: These are values in the form of numbers. Python supports Integer, Float and Complex values.
a) Integer Value It is any combination of digits along with +/- sign. It includes both positive and negative numbers along with 0. It doesn’t include fractional parts.
>>> a=4398 >>> a 4398 >>> a=-34534 >>> a -34534 >>> a=0 >>> a 0
b) Float Values: It includes both positive and negative real numbers. It also includes fractional parts. There are two forms to represent float literals: I) standard form and II) mantissa exponent form.
I) Standard Form: Any number with or without +/- sign having decimal point
>>> a=54.645 >>> a 54.645 >>> a=-324.654 >>> a -324.654 >>> a=9. >>> a 9.0 >>> a=.7 >>> a 0.7
II) Mantissa Exponent Form: mantissa may have any number with or without +/- sign having decimal point, and exponent must be a positive or negative integer followed by e/E sign. Internally python stores the number in normalized form and using e sign for exponent.
>>> a=44.3432e34 >>> a 4.43432e+35 >>> a=0.00324E-45 >>> a 3.24e-48 >>> a=0.00324E-4.5 SyntaxError: invalid syntax
There must not be any space between mantissa, e and exponent part.
>>> a=44.3432 e 34 SyntaxError: invalid syntax >>> a=44.3432 e34 SyntaxError: invalid syntax
c) Complex Values: It is of the form a + bj , here a represents the real part and b represents the complex part. Both j and J are allowed but internally Python stores j.
>>> a=8+4j >>> a=8+4J >>> a (8+4j)
There must not be any space between b and j
>>> a=9+5 j SyntaxError: invalid syntax
2. Boolean Values: There are two Literals True and False which are known as Boolean Values.
>>> a=True >>> a True >>> b=False >>> b False
3. Collections: Python provides 4 different types of collections:
- List : Comma separated list of items (may be of same / different type) enclosed in square brackets [ and ]
Example: [“gargs” ,”academy”, 9467863365] - Tuple : Comma separated list of items (may be of same / different type) enclosed in paranthesis ( and )
Example: (“gargs” ,”academy”, 9467863365) - Set : Comma separated list of unordered and unique items (may be of same / different type) enclosed in braces { and }
Example: {“gargs” ,”academy”, 9467863365} - String : Any combination of characters or text written in single, double, or triple quotes. Internally strings are stored in single quotes.
>>> list1=["gargs" ,"academy", 9467863365] >>> list1 ['gargs', 'academy', 9467863365] >>> tuple1=("gargs" ,"academy", 9467863365) >>> tuple1 ('gargs', 'academy', 9467863365) >>> set1={"gargs" ,"academy", 9467863365} >>> set1 {'academy', 9467863365, 'gargs'} >>> str='gargs academy' >>> str 'gargs academy' >>> str="gargs academy" >>> str 'gargs academy'
Triple quotes ”’ and “”” are used for multi line strings.
>>> str="""gargs academy""" >>> str 'gargs\nacademy' >>> str='''gargs academy''' >>> str 'gargs\nacademy'
For multi line strings, statement continuation mark \ is used. If we want space in between the strings, we must give space explicitly.
>>> str="""gargs\ academy""" >>> str 'gargsacademy' >>> str="""gargs\ academy""" >>> str 'gargs academy'
It can also include binary, decimal, octal, hexadecimal integer values.
>>> a=bin(10) >>> a '0b1010' >>> a=oct(10) >>> a '0o12' >>> a=hex(10) >>> a '0xa'
4. Mapping/ Dictionary: Comma separated list of key : value paired form of unordered items (may be of same / different type) enclosed in braces { and }
Example: {“gargs” : 1 , 2 : “academy”, “phone” : 9467863365}
>>> dict1= {"gargs" : 1 , 2 : "academy", "phone" : 9467863365} >>> dict1 {'gargs': 1, 2: 'academy', 'phone': 9467863365}
5. Special Value None: None is used to define a null or missing value.
>>> a=None >>> a >>> print(a) None
Python supports assigning multiple values to variables in a single statement as:
>>> a,b,c=10,20,30 >>> a 10 >>> b 20 >>> c 30 >>> (a,b,c)=10,20,30 >>> a 10 >>> b 20 >>> c 30 >>> a,b,c=(10,20,30) >>> a 10 >>> b 20 >>> c 30 >>> a,b,c=[10,20,30] >>> a 10 >>> b 20 >>> c 30 >>> a,b,c={10,20,30} >>> a 10 >>> b 20 >>> c 30 >>> a=10;b=20 >>> a=10,b=17 SyntaxError: cannot assign to literal >>> a 10 >>> b 20
But if the count of l-values and r-values doesn’t match, python will report an error as:
>>> a,b=(1,2,3) Traceback (most recent call last): File "<pyshell#21>", line 1, in <module> a,b=(1,2,3) ValueError: too many values to unpack (expected 2)
We can assign all the remaining values in a list form in a variable by placing an * before the variable name as:
>>> a,*b=(1,2,3) >>> a 1 >>> b [2, 3]
We can also combine two or more statements in python as:
>>> a=10;b=20 >>> a 10 >>> b 20
But we can’s use comma to separate two or more expressions as:
>>> a=10,b=20 SyntaxError: cannot assign to literal