In [2]:
print("Hello world")
In [3]:
'Single quoted string'
Out[3]:
In [4]:
"double quoted string"
Out[4]:
In [5]:
''' This is is tripple quoted string
contains new lines and
preserves them.'''
Out[5]:
In [6]:
""" THisi is same as above
has new lines"""
Out[6]:
In [7]:
'kasdkjasd
asdlkalsdk'
In [9]:
# integers
1
2
99
Out[9]:
In [10]:
# floats
1.2
0.1
9.4
2.0
Out[10]:
In [11]:
# booleans
True
False
Out[11]:
In [12]:
# Null / nothing
None
In [13]:
1 + 3
Out[13]:
In [14]:
1.0 + 3
Out[14]:
In [15]:
2.3 + 5.6
Out[15]:
In [16]:
'a string ' + 'added later'
Out[16]:
In [17]:
5 - 4
Out[17]:
In [18]:
2 - 3
Out[18]:
In [19]:
1.2 - 1.6
Out[19]:
In [20]:
2 ** 7
Out[20]:
In [21]:
'a string' - 'p'
In [22]:
1 * 2
Out[22]:
In [23]:
2.3 * 6
Out[23]:
In [24]:
'abc' * 3
Out[24]:
In [25]:
5 / 4
Out[25]:
In [26]:
5 // 4
Out[26]:
In [28]:
5.0 // 4
Out[28]:
In [30]:
5 % 2
Out[30]:
if / elif / else
In [31]:
if True:
print("true")
In [32]:
if not False:
print("not false")
In [33]:
if not True:
pass
else:
print("true inside else")
In [35]:
if '':
pass
elif ' ':
print('some string')
else:
print("never here")
In [37]:
ord('1')
Out[37]:
In [38]:
ord('A')
Out[38]:
In [39]:
ord(' ')
Out[39]:
In [40]:
ord('ल')
Out[40]:
In [41]:
ord('')
In [42]:
if 1:
print("true")
In [43]:
if 0:
print("inside if")
else:
print("inside else")
In [44]:
0.0 == True
Out[44]:
In [45]:
0 == True
Out[45]:
In [46]:
'' == True
Out[46]:
In [48]:
' ' == True
Out[48]:
In [49]:
' ' == ' '
Out[49]:
In [50]:
2 > 3
Out[50]:
In [52]:
3 < 4
Out[52]:
In [53]:
'a' < 'b'
Out[53]:
In [54]:
'p' > 'r'
Out[54]:
In [55]:
'a' == 'A'
Out[55]:
In [56]:
'a' < 'A'
Out[56]:
In [57]:
ord('A')
Out[57]:
In [58]:
ord('a')
Out[58]:
In [59]:
2 <= 2
Out[59]:
In [60]:
4 >= 3
Out[60]:
In [61]:
2 < 3 < 4
Out[61]:
In [63]:
4 > 5 > 6
Out[63]:
In [65]:
dir('')
Out[65]:
In [66]:
'Abc'.lower()
Out[66]:
In [67]:
'capital'.upper()
Out[67]:
In [68]:
'kathmandu'.capitalize()
Out[68]:
In [69]:
'KaThMandU'.swapcase()
Out[69]:
H/W test all string methods
In [ ]:
In [70]:
(1 + 2j)
Out[70]:
In [71]:
complex_number = 4 + 7j
In [72]:
complex_number - (5 + 2j)
Out[72]:
In [74]:
complex_number.real
Out[74]:
In [75]:
complex_number.imag
Out[75]:
In [77]:
complex_number.conjugate()
Out[77]:
In [ ]:
In [78]:
city = 'Kathmandu'
In [79]:
city[3]
Out[79]:
In [80]:
city[-1]
Out[80]:
In [81]:
city[len(city) - 1]
Out[81]:
In [82]:
city[0]
Out[82]:
In [83]:
city[-4]
Out[83]:
In [86]:
city[1:4]
Out[86]:
In [85]:
city
Out[85]:
In [87]:
city[3:7]
Out[87]:
In [88]:
city[1:7:2]
Out[88]:
In [89]:
city[:]
Out[89]:
In [90]:
city[::]
Out[90]:
In [91]:
len(city)
Out[91]:
In [92]:
city[0:9]
Out[92]:
In [93]:
city[9]
In [96]:
city[::-1]
Out[96]:
In [97]:
city[-5:-8:-1]
Out[97]:
In [100]:
city[3:-5]
Out[100]:
In [101]:
city[3:-7]
Out[101]:
In [ ]:
In [102]:
for c in city:
print(c)
In [103]:
index = 0
while index < len(city):
print(city[index])
index += 1
In [ ]:
In [104]:
cities = ['kathmandu', 'Biratnagar', 'Pokhara', 'Birgunj', 'Hetauda']
In [105]:
for city in cities:
print(city)
In [106]:
[1, 2, 5]
Out[106]:
In [107]:
[[2, 3], [6, 7]]
Out[107]:
In [108]:
[5.6, 6.7, 9.0]
Out[108]:
In [109]:
['a', 'string', 4, 9, 2.3, 2 + 7j]
Out[109]:
In [ ]:
In [110]:
[2, 3] + [5, 6]
Out[110]:
In [111]:
[3, 4] + [[3, 4], 7]
Out[111]:
In [112]:
cities
Out[112]:
In [113]:
cities.append('Dhankuta')
In [114]:
cities
Out[114]:
In [115]:
cities.pop()
Out[115]:
In [116]:
cities
Out[116]:
In [117]:
cities[0]
Out[117]:
In [118]:
cities[4]
Out[118]:
In [119]:
cities[3:5]
Out[119]:
In [120]:
cities[-1]
Out[120]:
In [121]:
cities
Out[121]:
In [122]:
cities.remove('Pokhara')
In [123]:
cities
Out[123]:
In [124]:
dir(cities)
Out[124]:
In [125]:
cities
Out[125]:
In [127]:
cities.insert(1, 'Pokhara')
In [128]:
cities
Out[128]:
In [129]:
cities.pop(2)
Out[129]:
In [130]:
cities
Out[130]:
In [131]:
cities.extend(['Biratnagar', 'Dhankuta', 'Dharan'])
In [132]:
cities
Out[132]:
In [135]:
cities[1] = 'Pokhara City'
In [136]:
cities
Out[136]:
In [133]:
name = ('john doe', 23)
In [134]:
dir(name)
Out[134]:
In [137]:
name[0] = 'Diwaker'
In [142]:
city
Out[142]:
In [143]:
city[0] = 'P'
In [ ]:
In [ ]:
In [148]:
phones = {'John Doe': '9287389234923', 'Jane Doe': '9234798234224'}
In [149]:
phones['John Doe']
Out[149]:
In [150]:
phones[0]
In [151]:
phones.keys()
Out[151]:
In [152]:
phones.values()
Out[152]:
In [153]:
phones.items()
Out[153]:
In [ ]:
In [154]:
for item in phones:
print(item)
In [155]:
for item in phones:
print(phones[item])
In [ ]:
In [156]:
key = 'abc'
In [157]:
key
Out[157]:
In [158]:
key, value = 'abc', 'xyz'
In [159]:
key
Out[159]:
In [160]:
value
Out[160]:
In [168]:
item = ('abc', 'xyz')
In [169]:
key, value = item
In [161]:
key, value = ('abc', 'xyz')
In [162]:
value
Out[162]:
In [167]:
for item in phones.items():
key, value = item
print(item)
print(key)
In [170]:
for key, value in phones.items():
print(key)
print(value)
In [176]:
dir(phones)
Out[176]:
In [177]:
phones['User 1'] = '293829348724'
In [178]:
phones
Out[178]:
In [180]:
phones.update({'user1': 'ashdkajsdk', 'user5': 'asdhjaksdhkasd'})
In [181]:
phones
Out[181]:
In [182]:
phones.update({'user1': '92384792384792384'})
In [183]:
phones
Out[183]:
In [ ]:
In [ ]:
In [ ]:
In [171]:
chars = {'a', 'b', 'd', 'b', 'e'}
In [172]:
chars
Out[172]:
In [173]:
chars.union({'f', 'e', 'g'})
Out[173]:
In [174]:
chars.intersection({'d', 'e', 'f'})
Out[174]:
In [175]:
dir(chars)
Out[175]:
In [184]:
chars
Out[184]:
In [185]:
chars.add('f')
In [186]:
chars
Out[186]:
In [187]:
chars.add('e')
In [188]:
chars
Out[188]:
In [ ]:
In [191]:
# Empty list
[]
Out[191]:
In [192]:
not []
Out[192]:
In [194]:
# empty tuple
tuple()
Out[194]:
In [195]:
not tuple()
Out[195]:
In [196]:
# tuple with single item
(1,)
Out[196]:
In [197]:
# Empty dictionary
{}
Out[197]:
In [198]:
# Empty set
set()
Out[198]:
In [ ]:
In [204]:
a = [] and True
In [205]:
a
Out[205]:
In [208]:
if [] and True:
print('true')
else:
print('false')
In [206]:
b = [1] and True
In [207]:
b
Out[207]:
In [200]:
[] or ''
Out[200]:
In [202]:
[1] or ''
Out[202]:
In [201]:
if [] and True:
print('is here')
else:
print('false')
In [ ]:
In [209]:
def function_name(arguments):
# function body
pass
In [210]:
def sum(a, b):
return a + b
In [211]:
result = sum(5, 7)
In [212]:
result
Out[212]:
In [213]:
def power(val, by=2):
return val ** by
In [214]:
power(3)
Out[214]:
In [215]:
# positional argument
power(3, 3)
Out[215]:
In [216]:
# named argument
power(val=5, by=4)
Out[216]:
In [217]:
power(by=4, val=5)
Out[217]:
In [218]:
power(6, by=5)
Out[218]:
In [219]:
power(by=4, 6)
In [ ]:
In [231]:
def fib_hundred1():
a, b = 0, 1
while b < 100:
print(b)
a, b = b, a + b
In [232]:
fib_hundred1()
In [233]:
def fib_hundred2():
result = []
a, b = 0, 1
while b < 100:
result.append(b)
a, b = b, a + b
return result
In [234]:
fib_hundred2()
Out[234]:
In [235]:
def fib_hundred3(num):
result = []
a, b = 0, 1
while b < num:
result.append(b)
a, b = b, a + b
return result
In [236]:
fib_hundred3(1000)
Out[236]:
In [ ]:
In [ ]:
In [237]:
if 7 % 2 == 0 or 7 % 3 == 0:
print('7 is divisible by either 2 or 3')
else:
print('not divisible by any of them')
Hint
is_prime
In [248]:
def is_prime(value):
if value < 1:
return False
for i in range(2, value):
if value % i == 0:
return False
return True
In [250]:
is_prime(7)
Out[250]:
In [251]:
primes = set()
num = 1
while len(primes) <= 10:
if is_prime(num):
primes.add(num)
num += 1
In [252]:
primes
Out[252]:
In [258]:
%timeit is_prime(1039)
In [254]:
import math
def is_prime2(value):
if value < 1:
return False
for i in range(2, int(math.sqrt(value)) + 1):
if value % i == 0:
return False
return True
In [255]:
is_prime2(101)
Out[255]:
In [256]:
is_prime(101)
Out[256]:
In [259]:
%timeit is_prime2(1039)
In [260]:
In [ ]:
In [269]:
def sum_in_loop(num, arr):
arr = arr.split()
sum = 0
for each in arr:
sum += int(each)
return sum
In [270]:
sum_in_loop(8, '10 20 30 40 5 6 7 8')
Out[270]:
In [272]:
' '.join(['10', '20', '30', '40'])
Out[272]:
In [273]:
test_data = '''
3830210 1299990 -5253330
-9103720 7965172 4618179
-4156713 2632678 215788
936901 -1932287 -4600085
536108 -9117224 8646093
8804982 8442814 -2933518
-1476911 -631763 -7381075
-6160349 -6630815 9162557
'''
In [274]:
test_data
Out[274]:
In [276]:
test_data.split('\n')
Out[276]:
In [277]:
for data in test_data.split('\n'):
if data.strip():
a, b, c = data.strip().split()
min_of_three(a, b, c)
In [278]:
print?
In [279]:
[0]*5
Out[279]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]: