In [1]:
'''hello
hello its me'''
Out[1]:
In [2]:
"""heyy how
are yuo
byeee"""
Out[2]:
In [3]:
1
2
3
4
5
6
Out[3]:
In [6]:
None
#is null
In [7]:
100/3
Out[7]:
In [8]:
b'01010110101'
Out[8]:
In [9]:
2**7
Out[9]:
In [11]:
"howdy partner "*4
Out[11]:
text here
In [12]:
if not True:
pass
else:
print("True Inside else")
In [17]:
if'':
pass
elif ' ':
print("some string")
else:
print("never here")
In [18]:
ord('1')
Out[18]:
In [21]:
ord('a')
Out[21]:
In [23]:
ord(' ')
Out[23]:
In [26]:
ord('?')
#ord bhaneko ordinal value
Out[26]:
In [27]:
ord('')
In [28]:
if 1:
print("true")
In [29]:
if 0:
print("inside if")
else:
print("inside else")
In [30]:
0.0==True
Out[30]:
In [35]:
1==True
#changed to boolean
Out[35]:
In [32]:
3== True
Out[32]:
In [36]:
' '== True
#tried to compare boolean and string
Out[36]:
In [34]:
''== True
Out[34]:
In [38]:
''==''
#compared string to string
Out[38]:
In [40]:
'hey'=='hey'
#compared string with string
Out[40]:
In [43]:
'a'>'b'
#ordinal value is being compared
Out[43]:
In [42]:
'b'>'a'
Out[42]:
In [47]:
'a'>'A'
#ordinal value is being compared
Out[47]:
In [48]:
2<=2
Out[48]:
In [49]:
2<3<4
Out[49]:
In [50]:
dir(1)
Out[50]:
In [51]:
dir('')
Out[51]:
In [53]:
'ABC'.lower()
Out[53]:
In [54]:
'capital'.upper()
Out[54]:
In [55]:
'suravi'.capitalize()
Out[55]:
In [57]:
'kAThmAnDu'.swapcase()
Out[57]:
HW CHECKOUT ALL THEM METHODS https://docs.python.org/3/
In [60]:
complex_number=2 + 4j
complex_number-1
In [61]:
complex_number-1
Out[61]:
In [62]:
complex_number.real
Out[62]:
In [63]:
complex_number.imag
Out[63]:
In [64]:
complex_number.conjugate()
Out[64]:
In [85]:
city='Kathmandu'
In [69]:
(city[2:5])
Out[69]:
In [68]:
(city[-1])
Out[68]:
In [70]:
city[len(city)-1]
Out[70]:
In [71]:
city[len(city)-8]
Out[71]:
In [72]:
city[0]
Out[72]:
In [74]:
city[1:4]
#till 4 th item not including 4 th item
Out[74]:
In [84]:
city[1:7:2]
# the 2 means it tkes every second item
#the default step is 1
#0 1 2 3 4 5 6 7 8
#k a t h m a n d u
# 1 2 1 2 1 2 1
# soo the second items are ahn
Out[84]:
In [80]:
city[9]
In [82]:
city[::-1]
#revrse steps
Out[82]:
In [83]:
city[::]
Out[83]:
In [87]:
city[-5:-8:-1]
#agadi +ve left to right movement else if front ma -ve bhayee right to left
Out[87]:
In [88]:
city[3:-5]
Out[88]:
In [89]:
city[3:-7]
Out[89]:
In [91]:
for c in city:
print (c)
In [92]:
index=0
while index<len(city):
print(city[index])
index+=1
In [93]:
cities=['kathmandu','biratnagar','pokhara','birgung','hetauda']
In [94]:
for city in cities:
print(city)
In [95]:
[2,3]+[[3,4],7]
Out[95]:
In [96]:
cities
Out[96]:
In [99]:
cities.append('Dhankuta')
In [100]:
cities
Out[100]:
In [103]:
cities.pop()
# did this 3 times to remove all dhankuta
Out[103]:
In [104]:
cities
Out[104]:
In [105]:
cities[0]
Out[105]:
In [106]:
cities[4]
Out[106]:
In [107]:
cities[3:5]
Out[107]:
In [108]:
cities[-1]
Out[108]:
In [109]:
cities.remove('pokhara')
In [110]:
cities
Out[110]:
In [111]:
dir (cities)
Out[111]:
In [112]:
cities
Out[112]:
In [113]:
cities.insert(1,'pokhara')
In [114]:
cities
Out[114]:
In [115]:
cities.pop(2)
Out[115]:
In [117]:
cities
Out[117]:
In [118]:
cities.extend(['biratnagar','dhankuta','dharan'])
In [119]:
cities
Out[119]:
In [123]:
cities[1]='pokara city'
In [124]:
cities
Out[124]:
In [ ]:
In [ ]:
In [ ]:
#tuple is immutable ie cannot be overwritten
In [121]:
name=('john doe',23)
In [122]:
dir(name)
Out[122]:
In [126]:
city
#global name is being used
Out[126]:
In [130]:
for b in city:
print (b)
In [131]:
b
Out[131]:
In [132]:
city[0]="p"
#string is also immutable
In [135]:
city='p'+city[1:]
#used to over write city
In [134]:
city
Out[134]:
In [136]:
city= city[:]+'city'
In [137]:
city
Out[137]:
In [139]:
phones={'john doe':'9808244323','jane doe':'9841234532'}
#key is immutable vallue can be list or tuple or string anything
# key must be tuple or number ie mmutable
In [140]:
phones['john doe']
Out[140]:
In [141]:
phones[0]
In [142]:
phones.keys()
Out[142]:
In [143]:
phones.values()
Out[143]:
In [144]:
phones.items()
Out[144]:
In [145]:
dir(phones)
Out[145]:
In [149]:
for items in phones:
print(items)
In [150]:
key ='abc'
In [151]:
key
Out[151]:
In [153]:
key, value ='abc','xyz'
In [154]:
key
Out[154]:
In [155]:
value
Out[155]:
In [158]:
key, value =('abc','xyz')
#the tuple is unpacked soo 0 value of tupple isassgnedto 0 variable
#this process is calledtupple unpacking
In [164]:
for item in phones .items():
key, value =item
print(item)
print (key)
In [165]:
for key, value in phones.items():
print(key)
print (value)
In [166]:
for item in phones .items():
key, value =item
print(value)
print (key)
In [178]:
phones['user 1']='2342525'
In [179]:
phones
Out[179]:
In [180]:
phones.update({'user 1':'32525434','user 5':' dffdfg'})
In [181]:
phones
Out[181]:
In [184]:
phones.update({'user 1':'3234'})
In [185]:
phones
Out[185]:
In [ ]:
In [183]:
phones
Out[183]:
In [ ]:
In [ ]:
In [175]:
#this is mutable
In [170]:
chars={'a','b','c','b','d','e'}
In [171]:
chars
Out[171]:
In [172]:
chars.union({'f','g','e'})
Out[172]:
In [200]:
chars.intersection({'a','y','z'})
Out[200]:
In [ ]:
In [ ]:
In [186]:
chars
Out[186]:
In [187]:
chars.add('f')
In [188]:
chars
Out[188]:
In [189]:
chars.add('e')
In [190]:
chars
Out[190]:
In [193]:
not []
Out[193]:
In [194]:
#empty tuple
tuple()
Out[194]:
In [192]:
[]
#empty list is always false
Out[192]:
In [195]:
not tuple()
Out[195]:
In [196]:
#tuple with single item thenn comma is compulsary.. comma represnt tuple
(1,)
Out[196]:
In [197]:
#empty dictionart
{}
Out[197]:
In [198]:
#empty set
set()
Out[198]:
In [201]:
[] and True
Out[201]:
In [206]:
[1] and True
Out[206]:
In [202]:
[] or ''
Out[202]:
In [205]:
[1] or ''
Out[205]:
In [ ]:
In [204]:
if [] and True:
print("is here")
else:
print('false')
In [207]:
a=[] and True
In [208]:
a
Out[208]:
In [209]:
b=[1] and True
In [210]:
b
Out[210]:
In [211]:
a= [] or True
In [212]:
a
Out[212]:
In [213]:
if [] and True :
print("true")
else:
print ('false')
In [ ]:
In [214]:
def function_name(argument_list):
#function body
pass
In [215]:
def sum(a,b):
return (a+b)
In [216]:
result = sum(5,7)
print(result)
In [219]:
def power(a, by=2):
return(a**by)
In [221]:
power(3)
Out[221]:
In [222]:
#positional argument
power(3,3)
Out[222]:
In [223]:
#named argument
power(a=4,by=4)
Out[223]:
In [224]:
#named argument
power(by=2,a=3)
Out[224]:
In [225]:
#positional argument must preceed named argument
power(5, by=4)
Out[225]:
In [226]:
power(a=2,3)
In [233]:
def fib_hundred():
a, b = 0,1
while b < 100:
print (b)
a,b = b,a+b# firstly the vlaues of and a+b are assigned...right hand side is evaluated first
In [232]:
fib_hundred()
In [240]:
def fib_hundred():
result =[]
a, b = 0,1
while b < 100:
result. append(b)
a,b = b,a+b
return result
In [241]:
fib_hundred()
Out[241]:
In [242]:
def fib_hundred():
result =[]
a, b = 0,1
while b < 100:
a,b = b,a+b
result. append(b)
return result
In [235]:
fib_hundred()
Out[235]:
In [244]:
def fib_hundred3(num):
result =[]
a, b = 0,1
while b < num:
a,b = b,a+b
result. append(b)
return result
In [245]:
fib_hundred3(1000)
Out[245]:
In [255]:
def prime(a):
if k==0:
return 0
else:
flag=0
for i in range(2,a-1):
if a%i==0:
flag=1
break
if flag==0:
print(a," is prime")
k+=1
return prime(a-1)
In [ ]:
In [256]:
prime(10)
Out[256]:
In [265]:
count=[]
def prime(a):
if len(count)==10:
return 0
else:
flag=0
for i in range(2,a-1):
r=a%i
if r==0:
flag=1
break
if flag==0:
count.append(a)
return prime(a+1)
prime(1)
count
Out[265]:
In [282]:
def is_prime(value):
for i in range (2, value):
if value %i == 0:
return False
return True
In [ ]:
In [267]:
is_prime(7)
Out[267]:
In [268]:
is_prime(-1)
Out[268]:
In [270]:
primes= set()
num=1
while len(primes) <= 10:
if is_prime(num):
primes.add(num)
num+=1
In [271]:
primes
Out[271]:
In [272]:
%timeit is_prime(101)
In [275]:
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 [277]:
is_prime(101)
Out[277]:
In [276]:
is_prime2(101)
Out[276]:
In [279]:
%timeit is_prime2(1039)
In [283]:
%timeit is_prime(1039)
In [281]:
#% then tab gives magic functions
In [284]:
%ls
In [288]:
def sum_in_loop(num ,arr):
arr= arr.split()
sum=0
for each in arr:
sum+=int(each)
return sum
In [ ]:
In [289]:
sum_in_loop(8,'10 20 30 40 50')
Out[289]:
In [290]:
arr=('1 2')
In [291]:
arr
Out[291]:
In [298]:
testdata="""339944 831807
745251 463196
489147 734664
327883 590339
109708 326509
470950 137453
333311 115685
366034 639922
98291 28103
510344 461861
608501 523527
266627 630744
944654 361267
879707 536550"""
In [ ]:
In [301]:
for data in testdata.split('\n'):
if data.strip():
a,b= data.strip().split()
In [ ]:
a
In [304]:
values='''-3172834 3879278
-543468 8398588
-822855 1311568
-7941414 -7317188
5804857 -2961189
-3020836 6778537
2484532 9330548
-1940394 8193555
-3409316 -2375400
-9956179 9525886
7673418 -7243462
-604481 4179370
-5577722 9875565
-8666718 -1207908
-6326421 -8808023
-8496866 500745
5071255 959666
-1100667 -5751599
-7728765 957919
-3068788 8076091
7996729 3910375
4854628 481261
3240924 -7085766
-1325184 9831608
538833 -1281363
9357495 -1787748'''
In [314]:
first = []
second = []
for data in values.split('\n'):
a,b = data.strip().split()
first.append(a)
second.append(b)
print(first)
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [308]:
a
Out[308]:
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 [ ]: