Monday, February 17, 2020

Variable In Python ||Python Tutorial Part-1


Variable In Python

 §  Variable is a name which is used to store the value in a memory location. Variable 
       also known as identifier and used to hold value.
 §      Since python is a type infer language, we don't need to specify the type of 
          variable.it is smart enough to get variable type.

Assigning value to variable

 §      With the help of python, we don‘t need to declare the variable first and then to use it. Python allows us to create variable at required time. 
 §    Whenever we assign any value to the variable that variable is declared automatically. 
 §     We used equal (=) operator to assign value to a variable. 

To create a variable in Python, all you need to do is specify the variable name, and then assign a value to it.

   <variable name> = <value>  


Eg:  These are different ways to assign value to a variable

Single Assignment

a=10
b=20.3
c=’hello’
print(a)
print(b)
print(c)

Multiple Assignment

 §      We can assign a value to multiple variable in a single line or statement.
 We can use multiple assignments in two ways:
1.     either by assigning a single value to multiple variables in a single line/statement or 
2.     assigning multiple values to multiple variables. 

Assigning single value to multiple variables
a=10
b=10
c=10
print(a)
print(b)
print(c)

a=b=c=10
print(a)
print(b)
print(c)

2.Assigning multiple values to multiple variables:

a,b,c=10,20,30

print(a)
print(b)
print(c)

Note:-

 §  The values will be assigned in the order in which variables appears.





 §  In python Identifiers can be variable name, class name or method name.

    Rules to define identifiers in Python:-

1. In the name of identifier the only allowed character is -
           §  Alphabet symbols (both upper case and lower case)
           §  digits (0 to 9)
           §  underscore (_)
 By mistake if we are using other symbol python immediate gives error.

amount=10         //valid
          amoun$t =10      //invalid
2.     Every Identifier name should not start with digit.

 abc123 =10       //valid
123abc = 10      //invalid

   3. Identifiers are case sensitive
  total=10
TOTAL=20

4.     No keywords should access as a identifier keywords are not allowed as a identifier.
5. No length limit.
  

     Note:-
1     23hello   //invalid
       hello123   //invalid
       java

   Note:-
123hello   //invalid
hello123   //invalid
java
   Note-
_x ==> private
§  If identifier start with underscore (_) symbol it indicates private identifier.
__x ==> strongly private
§  If identifier start with two underscore (__) symbol it is strongly associated.
__x __ ==> language defined special name
§  If the identifier starts and ends with two underscore symbols then the identifier is language defined special name, which is also known as magic methods.







No comments:

Post a Comment