Define Variables in Python
Multiple Assignments
Multiple Assignments
Multiple assignments can be made in a single statement.
Example 1
a, b, c = 5, 3.2, "Hello"
print a,b,c
Result
Example
2
If
we want to assign the same value to multiple variables at once, we can do this
as
x = y = z = "same value"
print x;print y;print z
Result
Variables
Python supports int,float and compile. It also support
String,List,Tuple and Dictionary
Example
a=10
b=-10
c=10.00
d=10.9098
e=-10.98
print a,b,c,d,e
String
Example
a_string=”hello”
print a_string # output hello
List
A list contains items separated by commas and enclosed within
square brackets ([]).
Example
a_list=[‘a’,’b’,’c’]
print
a_list[0] # output a
Tuple
It is readonly. Same as
list contains items separated by commas and enclosed within ().
Example
a_tuple=('a','b','c')
print a_tuple[1]
Dictionary
It is used to define data records.
Dictionaries are enclosed by curly braces ({ }) and values are accessed using
square braces ([]).
Example
employeetable = {'name': 'kumar','empcode':'a01', 'dept': 'sales'}
print employeetable.keys()[2],employeetable.values()[2]
name=(['arun','sharma','arvind'],['sunil','ashok'])
print name[1]
print name[1][1]
Result
No comments:
Post a Comment