In [3]:
None
In [4]:
"""hya
how do you do"""
Out[4]:
In [5]:
2.3+5.6
Out[5]:
In [6]:
import(this)
In [7]:
"Hello"+" World"
Out[7]:
In [8]:
'abs'*'acs'
In [9]:
if not True:
pass
else:
print("true inside else")
In [12]:
if '':
pass #we have nothing do do inside th eblock
elif '':
print('some sstring')
else:
print("never here")
there is a difference in '' annd ' ' ; the first signifies fals the second true
In [13]:
ord('a')
Out[13]:
In [14]:
ord("A")
Out[14]:
In [16]:
ord("प") #ord-ordinal value
Out[16]:
In [17]:
if 1:
print("true")
In [20]:
if 0:
print("false")
else:
print("couldn't print o condition")
In [21]:
0.0==True
Out[21]:
In [22]:
0==False
Out[22]:
In [23]:
''==True
Out[23]:
In [26]:
" "==True #tries to compare boolean
Out[26]:
In [27]:
' '==' '
Out[27]:
In [28]:
2>=2
Out[28]:
In [29]:
'a'>'b'
Out[29]:
In [30]:
'p'<'q'
Out[30]:
In [31]:
'a'>'A' #checcks the ASCII value ie the ordinal value
Out[31]:
In [32]:
dir(1)
Out[32]:
In [33]:
dir('')
Out[33]:
In [34]:
'ABC'.lower()
Out[34]:
In [35]:
'capital'.upper()
Out[35]:
In [36]:
'kathmandu'.capitalize()
Out[36]:
In [37]:
'KaThMaNdU'.swapcase()
Out[37]:
H/W try all the methods that can be done . docs.pyhton.org[httpp://docs.python.org]
In [ ]:
In [39]:
(1+2j)
Out[39]:
In [40]:
complex_numbers= 4+7j
In [42]:
complex_numbers - (5+2j)
Out[42]:
In [44]:
complex_numbers.real
Out[44]:
In [45]:
complex_numbers.imag
Out[45]:
In [46]:
complex_numbers.conjugate()
Out[46]:
In [47]:
city = 'Kathmandu'
In [50]:
city[3]
Out[50]:
In [51]:
city[2:7]
Out[51]:
In [52]:
city[-1]
Out[52]:
In [53]:
len(city)
Out[53]:
In [54]:
city[-4]
Out[54]:
In [58]:
city[1:7:2] #called stepping - goes from 1 to 7 by skkippping by 2 in between
Out[58]:
In [59]:
city[1:7:3]
Out[59]:
In [61]:
city[:]#from starting to the end
Out[61]:
In [62]:
city[::]#still goed to the very last
Out[62]:
In [ ]:
In [63]:
city[0:9]
Out[63]:
In [64]:
city[::-1]
Out[64]:
In [65]:
city[-5:-8:-1]
Out[65]:
In [66]:
city[3:-5]
Out[66]:
In [67]:
city[3:-7] #move from left to right
Out[67]:
In [69]:
city[-7:3]#moves from right to left
Out[69]:
In [ ]:
In [70]:
for c in city:
print(c)
In [71]:
index = 0
while index<len(city):
print(city[index])
index+=1
LISTS
In [72]:
cities = ['kathmnandu','biratnagar','patan','dipayal','pokhara']
In [73]:
for city in cities:
print(city)
In [74]:
['a','kathmmandu',4,5.9,(2+3j)]
Out[74]:
In [75]:
[2,3,4]+[5,6]
Out[75]:
In [76]:
[3,4]+[[5,6],7]
Out[76]:
In [77]:
cities.append("dhankuta")
In [78]:
for city in cities:
print(city)
In [79]:
cities.pop()
Out[79]:
In [80]:
cities
Out[80]:
In [82]:
cities[4]
Out[82]:
In [83]:
cities
Out[83]:
In [84]:
cities.remove('pokhara')
In [85]:
cities
Out[85]:
In [86]:
dir(cities)
Out[86]:
In [88]:
cities.insert(3,'bhaktapur')
In [89]:
cities
Out[89]:
In [90]:
cities.pop(2)
Out[90]:
In [91]:
cities
Out[91]:
In [92]:
cities.extend(['Biratnagar','Dhankuta','dharan'])
In [93]:
cities
Out[93]:
In [94]:
name = ('john doe', 23)
In [96]:
dir(name)
Out[96]:
In [97]:
cities[1] = 'Pokhara City'
In [98]:
cities
Out[98]:
In [99]:
city
Out[99]:
In [100]:
for a in city:
print(a)
In [101]:
a
Out[101]:
In [ ]:
In [102]:
city[0] = 'P'
In [103]:
city = 'P'+city[1:]
In [104]:
city
Out[104]:
In [105]:
city = city[:] + ' City'
In [106]:
city
Out[106]:
In [ ]:
In [108]:
phones = {"John Doe":'9841797088', 'Jane Doe':'9841364010'}
#john doe is either a string or tuple:: immutable
#the values is mutable, so can be any thing
In [109]:
phones.keys()
Out[109]:
In [110]:
phones.values()
Out[110]:
In [111]:
phones.items() #gives list of tuples
Out[111]:
In [112]:
for items in phones:
print(items)
In [113]:
for items in phones:
print (phones[items])
In [114]:
key = 'abc'
In [115]:
key
Out[115]:
In [116]:
key, value = 'abc','xyz'
In [117]:
key
Out[117]:
In [118]:
value
Out[118]:
In [119]:
key, value = ('abc','xyz')# called tuple unpacking
In [120]:
value
Out[120]:
In [121]:
key
Out[121]:
In [122]:
key, value = ['a','b']
In [123]:
key
Out[123]:
In [124]:
value
Out[124]:
In [129]:
for item in phones.items():
key , value = item
print(item)
print(key)
In [134]:
for key, value in phones.items():
print(key)
print(value)
In [135]:
del phones['Jane Doe']
In [136]:
phones
Out[136]:
In [ ]:
In [137]:
chars = {'a','b','d','b','e'}
In [138]:
chars
Out[138]:
In [139]:
dir(chars)
Out[139]:
In [140]:
chars.union({'f','d','e'})
Out[140]:
In [141]:
chars.intersection({'b','e','o'})
Out[141]:
In [142]:
phones['User1']='4850604565325'
In [143]:
phones
Out[143]:
In [145]:
phones.update({'user1':'asfsfe','user2':'2467q3478i'})
In [146]:
phones
Out[146]:
In [148]:
chars.add('g')
In [149]:
chars
Out[149]:
In [150]:
chars.add('d')
In [151]:
chars
Out[151]:
In [152]:
""
Out[152]:
In [153]:
[] #empty list
Out[153]:
In [154]:
not []
Out[154]:
In [155]:
#empty tuple
tuple()
Out[155]:
In [156]:
not tuple()
Out[156]:
In [157]:
#tuple with single value
(1,)
Out[157]:
In [159]:
(1)#coz this means that this is an expression
Out[159]:
In [160]:
#empty dictionary
{}
Out[160]:
In [161]:
not {}
Out[161]:
In [163]:
#empty set
set()
Out[163]:
In [164]:
not ()
Out[164]:
In [169]:
[] and True
Out[169]:
In [170]:
[1] and True
Out[170]:
In [166]:
[] or ''
Out[166]:
In [168]:
[1] or ''
Out[168]:
In [171]:
if [] and True:
print("not going here")
else:
print('i am here')
In [173]:
if [] and True:
print("not going here")
else:
print('here')
In [174]:
a = [] and True
In [175]:
a
Out[175]:
In [176]:
b = [1] and True
In [177]:
b
Out[177]:
In [ ]:
In [ ]:
def function_name(arguments):
#function body
pass
In [180]:
def summ(a , b):
return a+b
result=summ(2,4)
print(result)
In [181]:
def power(val, by = 2):
return val ** by
In [182]:
power(3)
Out[182]:
In [183]:
power(3,3)
Out[183]:
In [184]:
#named arguments
power(val = 5, by = 4)
Out[184]:
In [185]:
power (by = 4, val = 5)
Out[185]:
In [186]:
power(6,by = 4)
Out[186]:
In [188]:
# this is not possible power(val = 6, 4)
In [ ]:
In [195]:
def fib_hundred():
a , b = 0,1
while b< 100:
print (b)
a,b = b, a+b #right hand assignment happens first, i.e a+b happens first
fib_hundred()
In [198]:
def fib_hundred():
result = []
a,b = 0,1
while b< 100:
result.append(b)
a,b = b,a+b
return result
In [199]:
fib_hundred()
Out[199]:
In [201]:
def fib_hundred(num):
result=[]
a,b = 0,1
while b<num:
result.append(b)
a,b = b,a+b
return result
fib_hundred(1000)
Out[201]:
In [ ]:
In [215]:
lis=[]
def prime(a):
if len(lis)==10:
return 0
else:
flag = 0
for i in range(2,a-1):
if a%i ==0:
flag = 1
break
if flag==0:
print (lis.append(a))
return prime(a+1)
prime(1)
Out[215]:
In [ ]:
lis=[]
def prime(a):
if len(lis)==10:
return 0
else:
flag = 0
for i in range(2,a-1):
if a%i ==0:
flag = 1
break
if flag==0:
lis.append(a)
return prime(a+1)
prime(1)lis=[]
def prime(a):
if len(lis)==10:
return 0
else:
flag = 0
for i in range(2,a-1):
if a%i ==0:
flag = 1
break
if flag==0:
lis.append(a)
return prime(a+1)
prime(1)
In [228]:
lis=[]
def prime(a):
if len(lis)==10:
return 0
else:
flag = 0
for i in range(2,a-1):
if a%i ==0:
flag = 1
break
if flag==0:
lis.append(a)
return prime(a+1)
%timeit prime(1)
lis
Out[228]:
In [232]:
def is_prime(value):
if value<1:
return False
for i in range(2,value):
if value%i == 0:
return False
return True
In [ ]:
In [223]:
is_prime(7)
Out[223]:
In [225]:
prime = set()
num = 1
while len(prime)<=10:
if is_prime(num):
prime.add(num)
num +=1
In [226]:
prime
Out[226]:
In [227]:
%timeit is_prime(100)
In [229]:
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 [230]:
is_prime2(101)
Out[230]:
In [235]:
%timeit is_prime(101)
In [236]:
%timeit is_prime2(101)
In [ ]:
def sum_in_loop(num,arr):
arr = arr.split()
for each in arr:
print(int(each)
In [237]:
test_data='''4042962 7174134 -249750
6076132 9027559 -7159284
8465019 -8908983 -4508703
6408909 -2344732 9697512
7039541 -1559005 -5839470
5677003 -6584898 -963246
6378559 -4484607 -2643401
-4911562 -9132399 5287749
9768711 2189497 9954075
2836530 1631636 5659725
-1378729 -4325402 2833860
8371520 -8249269 1861419
-8787763 -9784250 2952435
-3296466 6624659 -9392296
-3598953 3664200 -951302
561576 -658796 2463800
9598330 -4280237 7979193
-3045071 808201 8846793
-7757322 576913 1036290
-7803246 -6586557 -7332074
7856479 2034713 -1657476
690339 406234 93254
-7448241 1618471 309003
5504194 8322004 -3066338
6111897 -5276948 -9402137
-4839404 5284627 -60933
7624395 4882958 5658829
5603588 -8162113 -3532969
'''
In [240]:
min?
In [ ]:
In [ ]:
test_data.split('\n')
In [238]:
for data in test_data.split('\n'):
if data.strip():
a,b,c= data.strip().split()
min_of_three(a,b,c)
In [239]:
In [243]:
def min_of_three(a,b,c):
min = []
if b<a and b<c :
min.append(str(b))
elif c<a and c<b:
min.append(str(c))
else :
min.append(str(a))
return ' '.join(min)
l1=[4042962,6076132,8465019,6408909,7039541,5677003,6378559,-4911562,9768711,2836530,-1378729,8371520,-8787763,-3296466,-3598953,561576 ,9598330,-3045071,-7757322,-7803246,7856479,690339,-7448241,5504194,6111897,-4839404,7624395,5603588]
l2=[7174134,9027559,-8908983,-2344732, -1559005,-6584898,-4484607,-9132399,2189497,1631636,-4325402,-8249269,-9784250, 6624659,3664200,-658796,-4280237,808201,576913,-6586557, 2034713, 406234, 1618471,8322004,-5276948,5284627,4882958,-8162113]
l3=[-249750, -7159284, -4508703, 9697512, -5839470, -963246, -2643401, 5287749, 9954075, 5659725, 2833860, 1861419, 2952435, -9392296, -951302, 2463800, 7979193, 8846793, 1036290, -7332074, -1657476, 93254, 309003, -3066338, -9402137, -60933, 5658829, -3532969]
m = min_of_three(l1,l2,l3)
print(m)
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]: