Python Variables

python variables - A simple and easy to learn tutorial on various python topics such as loops, strings, lists, dictionary, tuples, date, time, files, functions, modules, methods and exceptions.

1. Python Variables

Variable is a name which is used to refer memory location. Variable also known as identifier and used to hold value.


In Python, we don't need to specify the type of variable because Python is a type infer language and smart enough to get variable type.


Variable names can be a group of both letters and digits, but they have to begin with a letter or an underscore.


It is recomended to use lowercase letters for variable name. Rahul and rahul both are two different variables.


Identifier Naming


Variables are the example of identifiers. An Identifier is used to identify the literals used in the program. The rules to name an identifier are given below.

  • 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 bound us to declare variable before using in the application. It allows us to create variable at 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.



  1. >>>   
  2. 10  
  3. ravi  
  4. 20000.67  
  5. >>>  


Multiple Assignment


Python allows us to assign a value to multiple variables in a single statement which is also known as multiple assignment.


We can apply multiple assignments in two ways either by assigning a single value to multiple variables or assigning multiple values to multiple variables. Lets see given examples.


1. Assigning single value to multiple variables


  1. x=y=z=50  
  2. print iple  
  3. print y  
  4. print z  


  1. >>>   
  2. 50  
  3. 50  
  4. 50  
  5. >>>  

2.Assigning multiple values to multiple variables:


  1. a,b,c=5,10,15  
  2. print a  
  3. print b  
  4. print c  


  1. >>>   
  2. 5  
  3. 10  
  4. 15  
  5. >>>  

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


Basic Fundamentals:


This section contains the basic fundamentals of Python like :

i)Tokens and their types.

ii) Comments


  • Tokens can be defined as a punctuator mark, reserved words and each individual word in a statement.
  • Token is the smallest unit inside the given program.

There are following tokens in Python:

  • Keywords.
  • Identifiers.
  • Literals.
  • Operators.



  • Tuple is another form of collection where different type of data can be stored.
  • It is similar to list where data is separated by commas. Only the difference is that list uses square bracket and tuple uses parenthesis.
  • Tuples are enclosed in parenthesis and cannot be changed.


  1. >>> tuple=('rahul',100,60.4,'deepak')  
  2. >>> tuple1=('sanjay',10)  
  3. >>> tuple  
  4. ('rahul'10060.4'deepak')  
  5. >>> tuple[2:]  
  6. (60.4'deepak')  
  7. >>> tuple1[0]  
  8. 'sanjay'  
  9. >>> tuple+tuple1  
  10. ('rahul'10060.4'deepak''sanjay'10)  
  11. >>>  


  • Dictionary is a collection which works on a key-value pair.
  • It works like an associated array where no two keys can be same.
  • Dictionaries are enclosed by curly braces ({}) and values can be retrieved by square bracket([]).


  1. >>> dictionary={'name':'charlie','id':100,'dept':'it'}  
  2. >>> dictionary  
  3. {'dept''it''name''charlie''id'100}  
  4. >>> dictionary.keys()  
  5. ['dept''name''id']  
  6. >>> dictionary.values()  
  7. ['it''charlie'100]  
  8. >>>