In previous we learn about the python basic data type, there we use string literal, number literal, Boolean literal and constant literal. But we need something to store them so that we can manipulate those as required.
This is where variables come into the picture. Variables are exactly what they mean – their value can vary i.e. you can store anything using a variable. Unlike literal constants, you need some method of accessing these variables and hence you give them names.
A variable can have a short name (like x and y) or a more descriptive name (age, car_name, total_volume). Rules for Python variables:
- A variable name must start with a letter of the alphabet (upper or lowercase) or an underscore (‘_’).
- The rest of the variable name can consist of letters (upper or lowercase), underscores (‘_’) or digits (0-9).
- Variable names are case-sensitive. For example, myvar and myVar are not the same. Note the lowercase n in the former and the uppercase V in the latter.
Variable names are case-sensitive.variable naming
Example of invalid identifiers :
>>> 2myVar = 23 File "<stdin>", line 1 2myVar = 23 ^ SyntaxError: invalid syntax >>> my-Var = 23 File "<stdin>", line 1 SyntaxError: can't assign to operator >>> my Var = 23 File "<stdin>", line 1 my Var = 23 ^ SyntaxError: invalid syntax >>>
Example of valid identifiers:
>>> myVar = 23 >>> myvar = 23 >>> my_Var = 23
The most commonly used methods of constructing a multi-word variable name are the last three examples:
- Camel Case: Second and subsequent words are capitalized, to make word boundaries easier to see. (Presumably, it struck someone at some point that the capital letters strewn throughout the variable name vaguely resemble camel humps.)
- Example: nameOfStudent
- Pascal Case: Identical to Camel Case, except the first word is also capitalized.
- Example: NameOfStudent
- Snake Case: Words are separated by underscores.
- Example: name_of_student
It is you who choose to use any one of the conventions in which you are comfortable. The Style Guide for Python Code, also known as PEP 8, contains Naming Conventions that list suggested standards for names of different object types. PEP 8 includes the following recommendations:
Variables can hold values of different types called data types. The basic types are numbers and strings, which we have already discussed.
Example 1: Assigning a value to a Variable in Python:
>>> myMarks=98 >>> print(myMarks) 98 >>> myMarks=myMarks+2 >>> print(myMarks) 100 >>> myName="Singhak" >>> print(myName) Singhak >>>
In the above piece of code. First, we assign the literal constant value 98 to the variable myMarks using the assignment operator (=).
This line is called a statement because it states that something should be done and in this case, we connect the variable name myMarks to the value 98.
Next, we print the value of myMarks using the print statement. Then we add 2 to the value stored in myMarks and store it back. We then print it and expectedly, we get the value 6.
Similarly, we assign the literal string to the variable s and then print it.
Example 2: Changing the value of a variable
>>> myName = "Anil" >>> print(myName) Anil >>> # now change the value of variable ... myName = "Anil Singh" >>> print(myName) Anil Singh >>>
# is used for comment in python.Comment in python
Example 3: Assigning multiple values to multiple variables
>>> x,y,z = 1,2.2,"singhak" >>> print(x,y,z) (1, 2.2, 'singhak') >>> print(x) 1 >>> print(y) 2.2 >>> print(z) singhak >>>
While assigning multiple values to multiple variables, remember that number of variable and number of value should be same, otherwise it will give error.Assigning multiple values to multiple variables
>>> x,y,z = "singhak", 1 Traceback (most recent call last): File "<stdin>", line 1, in <module> ValueError: need more than 2 values to unpack >>>
Example 4: Assign the same value to multiple variables
>>> x = y = z = "singhak" >>> print(x) singhak >>> print(y) singhak >>> print(z) singhak >>>
Variables do not need to be declared with any particular type and can even change type after they have been set
>>> x = 1.2 >>> type(x) <class'float'> >>> x = "singhak" >>> type(x) <class'str'> >>>
id() Return the “identity” of an object. This is an integer which is guaranteed to be unique and constant for this object during its lifetime. Two objects with non-overlapping lifetimes may have the sameBuilt in function id() in python
>>> a = "anil" >>> b = "anil" >>> id(a) 50981792L >>> id(b) 50981792L >>> a = "anil singh" >>> id(a) 50981744L >>> id(b) 50981792L
It’s important to note that everything in Python is an object, even numbers, and Classes.
Hence, string “anil” has a unique id. The id of the string “anil” remains constant during the lifetime. Similar is the case for string “anil singh” and other objects. See below examples
>>> print(id(i)) 46423760 >>> print(id(10)) 46423760 >>> print(id(j)) 46423760 >>> print(id(k)) 46423736
For complex objects like lists, tuples, dictionaries etc. id() would give a unique integer even if the content of those containers is same.id() for complex object
>>> t1 = (1,2,3) >>> t2 = (1,2,3) >>> id(t1) 51004184L >>> id(t2) 50949720L
For purposes of optimization, the interpreter creates objects for the integers in the rangesource: https://realpython.com/python-variables/
[-5, 256]at startup, and then reuses them during program execution. Thus, when you assign separate variables to an integer value in this range, they will actually reference the same object.
>>> i = 257 >>> j = 257 >>> print(id(i)) 50812304L >>> print(id(j)) 50812328L >>> i = 256 >>> j = 256 >>> print(id(i)) 46429808L >>> print(id(j)) 46429808L >>>
In python, there are many reserved keywords that you can not use a variable name. If you will use, it will give an error.
Keywords are case-sensitive. They are all entirely lowercase, except for False, True and None.
>>> if = 1.2 File "<stdin>", line 1 if = 1.2 ^ SyntaxError: invalid syntax >>> If = 1.2 >>>
In this post, you learned about variable creation, variable nomenclature and also learned different other features of a variable. In next post, we will learn about Operators and Expressions in python
Is variable in python case sensitive ?
Yes, variable in python are case-sensitive. for example myName and myname are two different variable.
Can we assigning multiple values to multiple variable in python ?
yes, we can assigning multiple values to multiple variable. example: x,y,z=1,”a”,1.2