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.
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.
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.
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
x=y=z=50 print iple print y print z
2.Assigning multiple values to multiple variables:
a,b,c=5,10,15 print a print b print c
The values will be assigned in the order in which variables appears.
This section contains the basic fundamentals of Python like :
i)Tokens and their types.
ii) Comments
a)Tokens:
There are following tokens in Python:
>>> tuple=('rahul',100,60.4,'deepak') >>> tuple1=('sanjay',10) >>> tuple ('rahul', 100, 60.4, 'deepak') >>> tuple[2:] (60.4, 'deepak') >>> tuple1[0] 'sanjay' >>> tuple+tuple1 ('rahul', 100, 60.4, 'deepak', 'sanjay', 10) >>>
Eg:
>>> dictionary={'name':'charlie','id':100,'dept':'it'} >>> dictionary {'dept': 'it', 'name': 'charlie', 'id': 100} >>> dictionary.keys() ['dept', 'name', 'id'] >>> dictionary.values() ['it', 'charlie', 100] >>>