Python Variables Variable is a name that is used to refer to memory location. Python variable is also known as an identifier and used to hold value. In Python, we don't need to specify the type of variable to get variable type. Variable names can be a group of both the letters and digits, but they have to begin with a letter or an underscore. It is recommended to use lowercase letters for the variable name. Malavika and malavika both are two different variables as python is case sensitive language.  |
|
The first character of the variable must be an alphabet or underscore ( _ ). All the characters except the first character may be an alphabet of lower-case(a-z), upper-case (A-Z), underscore, or digit (0-9). Identifier name must not contain any white-space, or special character (!, @, #, %, ^, &, *). Identifier name must not be similar to any keyword defined in the language. Identifier names are case sensitive; for example, my name, and MyName is not the same. Examples of valid identifiers: a123, _n, n_9, etc. Examples of invalid identifiers: 1a, n%4, n 9, etc.
Declaring Variable and Assigning Values Python does not bind us to declare a variable before using it in the application. It allows us to create a variable at the required time. We don't need to declare explicitly variable in Python. When we assign any value to the variable, that variable is declared automatically. The equal (=) operator is used to assign value to a variable. Object References It is necessary to understand how the Python interpreter works when we declare a variable. The process of treating variables is somewhat different from many other programming languages. Python is the highly object-oriented programming language; that's why every data item belongs to a specific type of class. Consider the following example.
Let's check the type of it using the Python built-in type() function type("john") type(10) Output: <class 'str'>
<class 'int'> In Python, variables are a symbolic name that is a reference or pointer to an object. The variables are used to denote objects by that name. Let's understand the following example a = 50
 In the above image, the variable a refers to an integer object. Suppose we assign the integer value 50 to a new variable b. a = 50
b = a
 The variable b refers to the same object that a points to because Python does not create another object. Let's assign the new value to b. Now both variables will refer to the different objects. a = 50
b =100
 Python manages memory efficiently if we assign the same variable to two different values. Object Identity or Reference AddressIn Python, every created object identifies uniquely in Python. Python provides the guaranteed that no two objects will have the same identifier. The built-in id() function, is used to identify the object identifier. Consider the following example. a = 50 b = a print(id(a)) print(id(b)) # Reassigned variable a a = 500 print(id(a)) Output:
140734982691168
140734982691168
2822056960944 We assigned the b = a, a and b both point to the same object. When we checked by the id() function it returned the same number. We reassign a to 500; then it referred to the new object identifier. Variable names can be any length can have uppercase, lowercase (A to Z, a to z), the digit (0-9), and underscore character(_). Consider the following example of valid variables names.name = "Devansh" age = 20 marks = 80.50 print(name) print(age) print(marks) Output:
Devansh
20
80.5
1. Assigning single value to multiple variables Eg: x=y=z=50 print(x) print(y) print(z) Output:
50
50
50 2. Assigning multiple values to multiple variables: Eg:
- # case 1:
- a,b,c=5,10,15
- print(a)
- print(b)
- print(c)
- # case 2:
a,b=5,10,15 Value Error: too many values to unpack(expected 2)
- # case 3:
- a,b,c=5,10
- Value Error: not enough values to unpack (expected 3, got 2)
Output:
5
10
15 The values will be assigned in the order in which variables appear. |
Comments
Post a Comment