Python Basics

Variable and Assignment Statement

In [13]:
sum = 20.5
x = 9
z = x + 5
x = 20*x + 22
In [14]:
print(sum)
20.5
In [15]:
print(z)
14
In [16]:
print(x)
202

data types: numbers, strings, booleans

In [10]:
s = 'Tony'
t = "John's Tavern"
u = '''
hello
how are you?
'''
In [11]:
print(s)
Tony
In [12]:
print(u)
hello
how are you?

In [17]:
b1 = True
b2 = False
print(b1,b2)
True False
In [18]:
print(x/5)
40.4
In [19]:
print(x//5)
40
In [20]:
print(x%5)  # this is the modulus operator
2

list structure

In [23]:
numlist = [ 10, 20, 30, 40, 50 ]
namelist = ["john","tony","anna"]
emptylist = []
In [24]:
print(numlist)
[10, 20, 30, 40, 50]
In [25]:
print(emptylist)
[]

how to read elements from a list, how to add an element to list, how to delete, slice

In [26]:
numlist.append(60)
In [27]:
print(numlist)
[10, 20, 30, 40, 50, 60]
In [36]:
print(numlist[1], numlist[-5])
20 20
In [37]:
print(numlist.index(50))
4
In [38]:
numlist2 = [ 10, 20, 50, 40, 50 ]
In [40]:
numlist2.index(50)
Out[40]:
2
In [41]:
numlist2[2:4] # slice
Out[41]:
[50, 40]
In [42]:
numlist2[2:5]
Out[42]:
[50, 40, 50]
In [43]:
numlist2[2:]
Out[43]:
[50, 40, 50]
In [44]:
numlist2[:4]
Out[44]:
[10, 20, 50, 40]

boolean expressions, conditional statement

In [45]:
True and True
Out[45]:
True
In [46]:
True or False
Out[46]:
True
In [47]:
not True
Out[47]:
False
In [48]:
x
Out[48]:
202
In [49]:
x > 200 # >, >=, <, <=, ==, !=
Out[49]:
True
In [50]:
x != 202
Out[50]:
False
In [53]:
if x > 200:
    print("x more than 200")
    print("hahaha")
else:
    print("x is not more than 200")
    print("ohohoh")
x more than 200
hahaha
In [54]:
if x < 200:
    print("x more than 200")
    print("hahaha")
else:
    print("x is not more than 200")
    print("ohohoh")
x is not more than 200
ohohoh

range function, traversing a list, for loop statement

In [58]:
range(10)
Out[58]:
range(0, 10)
In [57]:
list(range(0,10))
Out[57]:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
In [59]:
list(range(10,20))
Out[59]:
[10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
In [62]:
list(range(10,20,2))
Out[62]:
[10, 12, 14, 16, 18]

Hello

numlist2 = [10,20,30,40,50]
In [67]:
len(numlist2)
Out[67]:
5
In [68]:
list(range(len(numlist2)))
Out[68]:
[0, 1, 2, 3, 4]
In [72]:
numlist2 = [10,20,30,40,50]
for x in numlist2:
    y = x+x
    print(x,y)
10 20
20 40
30 60
40 80
50 100
In [73]:
for index in range(len(numlist2)):
    print(index,numlist2[index])
0 10
1 20
2 30
3 40
4 50

strings (nothing but a list)

In [74]:
s = "Hello, How are you?"
In [75]:
s[3:5]
Out[75]:
'lo'
In [76]:
s[5]
Out[76]:
','
In [77]:
for x in s:
    print(x)
H
e
l
l
o
,
 
H
o
w
 
a
r
e
 
y
o
u
?
In [78]:
len(s)
Out[78]:
19

Dictionary (key-value store) key should be a string or a number or boolean

In [79]:
d1 = { "john": 23, "tony": 32, "anna": 22}
In [80]:
d1["john"]
Out[80]:
23
In [83]:
d1["anna"]
Out[83]:
22
In [84]:
print(d1)
{'john': 23, 'tony': 32, 'anna': 22}
In [86]:
for x in d1:
    print(x,d1[x])
john 23
tony 32
anna 22
In [87]:
d1["raj"] = 62
In [88]:
print(d1)
{'john': 23, 'tony': 32, 'anna': 22, 'raj': 62}
In [89]:
d1["anna"] = 42
In [91]:
print(d1)
{'john': 23, 'tony': 32, 'anna': 42, 'raj': 62}
In [97]:
d2 = { "raj": [7, 17, 27], "jim": [1,2]}
In [98]:
d2["raj"]
Out[98]:
[7, 17, 27]
In [99]:
len(d2["jim"])
Out[99]:
2
In [ ]: