In [3]:
None

In [4]:
"""hya
how do you do"""


Out[4]:
'hya\nhow do you do'

In [5]:
2.3+5.6


Out[5]:
7.8999999999999995

In [6]:
import(this)


  File "<ipython-input-6-a42d96b34ff1>", line 1
    import(this)
          ^
SyntaxError: invalid syntax

In [7]:
"Hello"+" World"


Out[7]:
'Hello World'

In [8]:
'abs'*'acs'


---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-8-72e72454ae32> in <module>()
----> 1 'abs'*'acs'

TypeError: can't multiply sequence by non-int of type 'str'

In [9]:
if not True:
    pass
else:
    print("true inside else")


true inside else

In [12]:
if '':
    pass #we have nothing do do inside th eblock
elif '':
    print('some sstring')
else:
    print("never here")


never here

there is a difference in '' annd ' ' ; the first signifies fals the second true


In [13]:
ord('a')


Out[13]:
97

In [14]:
ord("A")


Out[14]:
65

In [16]:
ord("प") #ord-ordinal value


Out[16]:
2346

In [17]:
if 1:
    print("true")


true

In [20]:
if 0:
    print("false")
else:
    print("couldn't print o condition")


couldn't print o condition

In [21]:
0.0==True


Out[21]:
False

In [22]:
0==False


Out[22]:
True

In [23]:
''==True


Out[23]:
False

In [26]:
" "==True #tries to compare boolean


Out[26]:
False

In [27]:
' '==' '


Out[27]:
True

In [28]:
2>=2


Out[28]:
True

In [29]:
'a'>'b'


Out[29]:
False

In [30]:
'p'<'q'


Out[30]:
True

In [31]:
'a'>'A' #checcks the ASCII value ie the ordinal value


Out[31]:
True

In [32]:
dir(1)


Out[32]:
['__abs__',
 '__add__',
 '__and__',
 '__bool__',
 '__ceil__',
 '__class__',
 '__delattr__',
 '__dir__',
 '__divmod__',
 '__doc__',
 '__eq__',
 '__float__',
 '__floor__',
 '__floordiv__',
 '__format__',
 '__ge__',
 '__getattribute__',
 '__getnewargs__',
 '__gt__',
 '__hash__',
 '__index__',
 '__init__',
 '__int__',
 '__invert__',
 '__le__',
 '__lshift__',
 '__lt__',
 '__mod__',
 '__mul__',
 '__ne__',
 '__neg__',
 '__new__',
 '__or__',
 '__pos__',
 '__pow__',
 '__radd__',
 '__rand__',
 '__rdivmod__',
 '__reduce__',
 '__reduce_ex__',
 '__repr__',
 '__rfloordiv__',
 '__rlshift__',
 '__rmod__',
 '__rmul__',
 '__ror__',
 '__round__',
 '__rpow__',
 '__rrshift__',
 '__rshift__',
 '__rsub__',
 '__rtruediv__',
 '__rxor__',
 '__setattr__',
 '__sizeof__',
 '__str__',
 '__sub__',
 '__subclasshook__',
 '__truediv__',
 '__trunc__',
 '__xor__',
 'bit_length',
 'conjugate',
 'denominator',
 'from_bytes',
 'imag',
 'numerator',
 'real',
 'to_bytes']

In [33]:
dir('')


Out[33]:
['__add__',
 '__class__',
 '__contains__',
 '__delattr__',
 '__dir__',
 '__doc__',
 '__eq__',
 '__format__',
 '__ge__',
 '__getattribute__',
 '__getitem__',
 '__getnewargs__',
 '__gt__',
 '__hash__',
 '__init__',
 '__iter__',
 '__le__',
 '__len__',
 '__lt__',
 '__mod__',
 '__mul__',
 '__ne__',
 '__new__',
 '__reduce__',
 '__reduce_ex__',
 '__repr__',
 '__rmod__',
 '__rmul__',
 '__setattr__',
 '__sizeof__',
 '__str__',
 '__subclasshook__',
 'capitalize',
 'casefold',
 'center',
 'count',
 'encode',
 'endswith',
 'expandtabs',
 'find',
 'format',
 'format_map',
 'index',
 'isalnum',
 'isalpha',
 'isdecimal',
 'isdigit',
 'isidentifier',
 'islower',
 'isnumeric',
 'isprintable',
 'isspace',
 'istitle',
 'isupper',
 'join',
 'ljust',
 'lower',
 'lstrip',
 'maketrans',
 'partition',
 'replace',
 'rfind',
 'rindex',
 'rjust',
 'rpartition',
 'rsplit',
 'rstrip',
 'split',
 'splitlines',
 'startswith',
 'strip',
 'swapcase',
 'title',
 'translate',
 'upper',
 'zfill']

In [34]:
'ABC'.lower()


Out[34]:
'abc'

In [35]:
'capital'.upper()


Out[35]:
'CAPITAL'

In [36]:
'kathmandu'.capitalize()


Out[36]:
'Kathmandu'

In [37]:
'KaThMaNdU'.swapcase()


Out[37]:
'kAtHmAnDu'

H/W try all the methods that can be done . docs.pyhton.org[httpp://docs.python.org]


In [ ]:

complex numbers


In [39]:
(1+2j)


Out[39]:
(1+2j)

In [40]:
complex_numbers= 4+7j

In [42]:
complex_numbers - (5+2j)


Out[42]:
(-1+5j)

In [44]:
complex_numbers.real


Out[44]:
4.0

In [45]:
complex_numbers.imag


Out[45]:
7.0

In [46]:
complex_numbers.conjugate()


Out[46]:
(4-7j)

string slicing for/while loop


In [47]:
city = 'Kathmandu'

In [50]:
city[3]


Out[50]:
'h'

In [51]:
city[2:7]


Out[51]:
'thman'

In [52]:
city[-1]


Out[52]:
'u'

In [53]:
len(city)


Out[53]:
9

In [54]:
city[-4]


Out[54]:
'a'

In [58]:
city[1:7:2] #called stepping - goes from 1 to 7 by skkippping by 2 in between


Out[58]:
'aha'

In [59]:
city[1:7:3]


Out[59]:
'am'

In [61]:
city[:]#from starting to the end


Out[61]:
'Kathmandu'

In [62]:
city[::]#still goed to the very last


Out[62]:
'Kathmandu'

In [ ]:


In [63]:
city[0:9]


Out[63]:
'Kathmandu'

In [64]:
city[::-1]


Out[64]:
'udnamhtaK'

In [65]:
city[-5:-8:-1]


Out[65]:
'mht'

In [66]:
city[3:-5]


Out[66]:
'h'

In [67]:
city[3:-7] #move from left to right


Out[67]:
''

In [69]:
city[-7:3]#moves from right to left


Out[69]:
't'

In [ ]:


In [70]:
for c in city:
    print(c)


K
a
t
h
m
a
n
d
u

In [71]:
index = 0
while index<len(city):
    print(city[index])
    index+=1


K
a
t
h
m
a
n
d
u

LISTS


In [72]:
cities = ['kathmnandu','biratnagar','patan','dipayal','pokhara']

In [73]:
for city in cities:
    print(city)


kathmnandu
biratnagar
patan
dipayal
pokhara

In [74]:
['a','kathmmandu',4,5.9,(2+3j)]


Out[74]:
['a', 'kathmmandu', 4, 5.9, (2+3j)]

In [75]:
[2,3,4]+[5,6]


Out[75]:
[2, 3, 4, 5, 6]

In [76]:
[3,4]+[[5,6],7]


Out[76]:
[3, 4, [5, 6], 7]

In [77]:
cities.append("dhankuta")

In [78]:
for city in cities:
    print(city)


kathmnandu
biratnagar
patan
dipayal
pokhara
dhankuta

In [79]:
cities.pop()


Out[79]:
'dhankuta'

In [80]:
cities


Out[80]:
['kathmnandu', 'biratnagar', 'patan', 'dipayal', 'pokhara']

In [82]:
cities[4]


Out[82]:
'pokhara'

In [83]:
cities


Out[83]:
['kathmnandu', 'biratnagar', 'patan', 'dipayal', 'pokhara']

In [84]:
cities.remove('pokhara')

In [85]:
cities


Out[85]:
['kathmnandu', 'biratnagar', 'patan', 'dipayal']

In [86]:
dir(cities)


Out[86]:
['__add__',
 '__class__',
 '__contains__',
 '__delattr__',
 '__delitem__',
 '__dir__',
 '__doc__',
 '__eq__',
 '__format__',
 '__ge__',
 '__getattribute__',
 '__getitem__',
 '__gt__',
 '__hash__',
 '__iadd__',
 '__imul__',
 '__init__',
 '__iter__',
 '__le__',
 '__len__',
 '__lt__',
 '__mul__',
 '__ne__',
 '__new__',
 '__reduce__',
 '__reduce_ex__',
 '__repr__',
 '__reversed__',
 '__rmul__',
 '__setattr__',
 '__setitem__',
 '__sizeof__',
 '__str__',
 '__subclasshook__',
 'append',
 'clear',
 'copy',
 'count',
 'extend',
 'index',
 'insert',
 'pop',
 'remove',
 'reverse',
 'sort']

In [88]:
cities.insert(3,'bhaktapur')

In [89]:
cities


Out[89]:
['kathmnandu', 'biratnagar', 'patan', 'bhaktapur', 'dipayal']

In [90]:
cities.pop(2)


Out[90]:
'patan'

In [91]:
cities


Out[91]:
['kathmnandu', 'biratnagar', 'bhaktapur', 'dipayal']

In [92]:
cities.extend(['Biratnagar','Dhankuta','dharan'])

In [93]:
cities


Out[93]:
['kathmnandu',
 'biratnagar',
 'bhaktapur',
 'dipayal',
 'Biratnagar',
 'Dhankuta',
 'dharan']

Tuple


In [94]:
name = ('john doe', 23)

In [96]:
dir(name)


Out[96]:
['__add__',
 '__class__',
 '__contains__',
 '__delattr__',
 '__dir__',
 '__doc__',
 '__eq__',
 '__format__',
 '__ge__',
 '__getattribute__',
 '__getitem__',
 '__getnewargs__',
 '__gt__',
 '__hash__',
 '__init__',
 '__iter__',
 '__le__',
 '__len__',
 '__lt__',
 '__mul__',
 '__ne__',
 '__new__',
 '__reduce__',
 '__reduce_ex__',
 '__repr__',
 '__rmul__',
 '__setattr__',
 '__sizeof__',
 '__str__',
 '__subclasshook__',
 'count',
 'index']

In [97]:
cities[1] = 'Pokhara City'

In [98]:
cities


Out[98]:
['kathmnandu',
 'Pokhara City',
 'bhaktapur',
 'dipayal',
 'Biratnagar',
 'Dhankuta',
 'dharan']

In [99]:
city


Out[99]:
'dhankuta'

In [100]:
for a in city:
    print(a)


d
h
a
n
k
u
t
a

In [101]:
a


Out[101]:
'a'

In [ ]:


In [102]:
city[0] = 'P'


---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-102-bcabadd3c566> in <module>()
----> 1 city[0] = 'P'

TypeError: 'str' object does not support item assignment

In [103]:
city = 'P'+city[1:]

In [104]:
city


Out[104]:
'Phankuta'

In [105]:
city = city[:] + ' City'

In [106]:
city


Out[106]:
'Phankuta City'

In [ ]:

Dictionary


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]:
dict_keys(['Jane Doe', 'John Doe'])

In [110]:
phones.values()


Out[110]:
dict_values(['9841364010', '9841797088'])

In [111]:
phones.items() #gives list of tuples


Out[111]:
dict_items([('Jane Doe', '9841364010'), ('John Doe', '9841797088')])

In [112]:
for items in phones:
    print(items)


Jane Doe
John Doe

In [113]:
for items in phones:
    print (phones[items])


9841364010
9841797088

In [114]:
key = 'abc'

In [115]:
key


Out[115]:
'abc'

In [116]:
key, value = 'abc','xyz'

In [117]:
key


Out[117]:
'abc'

In [118]:
value


Out[118]:
'xyz'

In [119]:
key, value = ('abc','xyz')# called tuple unpacking

In [120]:
value


Out[120]:
'xyz'

In [121]:
key


Out[121]:
'abc'

In [122]:
key, value = ['a','b']

In [123]:
key


Out[123]:
'a'

In [124]:
value


Out[124]:
'b'

In [129]:
for item in phones.items():
    key , value = item
    print(item)
    print(key)


('Jane Doe', '9841364010')
Jane Doe
('John Doe', '9841797088')
John Doe

In [134]:
for key, value in phones.items():
    print(key)
    print(value)


Jane Doe
9841364010
John Doe
9841797088

In [135]:
del phones['Jane Doe']

In [136]:
phones


Out[136]:
{'John Doe': '9841797088'}

In [ ]:

Sets


In [137]:
chars = {'a','b','d','b','e'}

In [138]:
chars


Out[138]:
{'a', 'b', 'd', 'e'}

In [139]:
dir(chars)


Out[139]:
['__and__',
 '__class__',
 '__contains__',
 '__delattr__',
 '__dir__',
 '__doc__',
 '__eq__',
 '__format__',
 '__ge__',
 '__getattribute__',
 '__gt__',
 '__hash__',
 '__iand__',
 '__init__',
 '__ior__',
 '__isub__',
 '__iter__',
 '__ixor__',
 '__le__',
 '__len__',
 '__lt__',
 '__ne__',
 '__new__',
 '__or__',
 '__rand__',
 '__reduce__',
 '__reduce_ex__',
 '__repr__',
 '__ror__',
 '__rsub__',
 '__rxor__',
 '__setattr__',
 '__sizeof__',
 '__str__',
 '__sub__',
 '__subclasshook__',
 '__xor__',
 'add',
 'clear',
 'copy',
 'difference',
 'difference_update',
 'discard',
 'intersection',
 'intersection_update',
 'isdisjoint',
 'issubset',
 'issuperset',
 'pop',
 'remove',
 'symmetric_difference',
 'symmetric_difference_update',
 'union',
 'update']

In [140]:
chars.union({'f','d','e'})


Out[140]:
{'a', 'b', 'd', 'e', 'f'}

In [141]:
chars.intersection({'b','e','o'})


Out[141]:
{'b', 'e'}

In [142]:
phones['User1']='4850604565325'

In [143]:
phones


Out[143]:
{'John Doe': '9841797088', 'User1': '4850604565325'}

In [145]:
phones.update({'user1':'asfsfe','user2':'2467q3478i'})

In [146]:
phones


Out[146]:
{'John Doe': '9841797088',
 'User1': '4850604565325',
 'user1': 'asfsfe',
 'user2': '2467q3478i'}

In [148]:
chars.add('g')

In [149]:
chars


Out[149]:
{'a', 'b', 'd', 'e', 'g'}

In [150]:
chars.add('d')

In [151]:
chars


Out[151]:
{'a', 'b', 'd', 'e', 'g'}

In [152]:
""


Out[152]:
''

Conditionals


In [153]:
[] #empty list


Out[153]:
[]

In [154]:
not []


Out[154]:
True

In [155]:
#empty tuple
tuple()


Out[155]:
()

In [156]:
not tuple()


Out[156]:
True

In [157]:
#tuple with single value
(1,)


Out[157]:
(1,)

In [159]:
(1)#coz this means that this is an expression


Out[159]:
1

In [160]:
#empty dictionary
{}


Out[160]:
{}

In [161]:
not {}


Out[161]:
True

In [163]:
#empty set
set()


Out[163]:
set()

In [164]:
not ()


Out[164]:
True

In [169]:
[] and True


Out[169]:
[]

In [170]:
[1] and True


Out[170]:
True

In [166]:
[] or ''


Out[166]:
''

In [168]:
[1] or ''


Out[168]:
[1]

In [171]:
if [] and True:
    print("not going here")
else:
    print('i am here')


i am here

In [173]:
if [] and True:
    print("not going here")
else:
    print('here')


here

In [174]:
a  = [] and True

In [175]:
a


Out[175]:
[]

In [176]:
b = [1] and True

In [177]:
b


Out[177]:
True

In [ ]:

Functions


In [ ]:
def function_name(arguments):
    #function body
    pass

In [180]:
def summ(a , b):
    return a+b
result=summ(2,4)
print(result)


6

In [181]:
def power(val, by = 2):
    return val ** by

In [182]:
power(3)


Out[182]:
9

In [183]:
power(3,3)


Out[183]:
27

In [184]:
#named arguments
power(val = 5, by = 4)


Out[184]:
625

In [185]:
power (by = 4, val = 5)


Out[185]:
625

In [186]:
power(6,by = 4)


Out[186]:
1296

In [188]:
# this is not possible power(val = 6, 4)

In [ ]:

Fibonacci Series


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()


1
1
2
3
5
8
13
21
34
55
89

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]:
[1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]

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]:
[1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987]

In [ ]:

find first 10 prinme numbers


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)


None
None
None
None
None
None
None
None
None
None
Out[215]:
0

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


The slowest run took 175.34 times longer than the fastest. This could mean that an intermediate result is being cached.
1000000 loops, best of 3: 358 ns per loop
Out[228]:
[1, 2, 3, 5, 7, 11, 13, 17, 19, 23]

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]:
True

In [225]:
prime = set()
num = 1
while len(prime)<=10:
    if is_prime(num):
        prime.add(num)
    num +=1

In [226]:
prime


Out[226]:
{3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23}

In [227]:
%timeit is_prime(100)


100000 loops, best of 3: 1.73 µs per loop

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]:
True

In [235]:
%timeit is_prime(101)


The slowest run took 12.77 times longer than the fastest. This could mean that an intermediate result is being cached.
1000000 loops, best of 3: 1.38 µs per loop

In [236]:
%timeit is_prime2(101)


The slowest run took 4.86 times longer than the fastest. This could mean that an intermediate result is being cached.
100000 loops, best of 3: 3.17 µs per loop

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]:



5603588

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)


[-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]

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 [ ]: