Skip to content

Instantly share code, notes, and snippets.

View Chap9Q1.py
def find_in_list(lst, v):
for i in range(len(lst)):
if lst[i] == v:
return i
break
return -1
l = [1, 2, 3, 4]
print(find_in_list(l, 3)) # 2
print(find_in_list(l, 5)) # -1
View Chap4Q3.py
Conversion
__init__.py
Length
__init__.py
Lengthconversion.py
Mass
__init__.py
Massconversion.py
View Chap4Q2.py
def miletokm(mile):
return mile*1.609
def kmtomile(km):
return km*0.62
def feettoinches(feet):
return feet*12
def inchestofeet(inch):
return inch*0.0833
#Constants
MILES= '1.609344 KM'
View Chap4Q1.py
def kgtotonne(Kg):
return Kg*0.001
def tonnetoKg(tonne):
return tonne**1000
def kgtopound(Kg):
return Kg*2.20462
def poundtokg(pound):
return pound*0.453592
#Constants
View Chap2Q12.py
def bubble_sort(k):
for i in range(1,len(k)):
for j in range (len(k)-1):
if k[j]>k[j+1]:
k[j],k[j+1]=k[j+1],k[j]
return k
dict = {'c':1,'f':22,'a':13,'d':4,'e':5,'h':8}
k=list(dict.values())
print(bubble_sort(k))
View Chap2Q11.py
def bubble_sort(k):
for i in range(1,len(k)):
for j in range (len(k)-1):
if k[j]>k[j+1]:
k[j],k[j+1]=k[j+1],k[j]
return k
dict = {'c':1,'f':22,'a':13,'d':4,'e':5,'h':8}
k=list(dict.keys())
print(bubble_sort(k))
View Chap2Q10.py
def add_dict(dict1, dict2):
return dict(list(dict1.items()) + list(dict2.items()))
dic1 = {'a':1, 'b':2, 'c':3}
dic2 = {'c':4, 'd':5, 'e':6}
print(add_dict(dic1, dic2))
View Chap2Q9.py
mo={'January':31,'February':28,'March':31,'April':30,'May':31,'June':30,
'July':31,'August':31,'September':30,'October':30,'November':30,
'December':31}
m=input("\nEnter a month:")
print('\nNumber of days in', m,'is',mo.get(m.strip(),'Enter again:'))
print("\nAll months in sorted order are as follows: ",sorted(mo))
for i in mo:
if mo[i]==31:
print ('\nMonth having 31 day is:',i, end=' ')
print()
View Chap2Q8.py
def make_fibonacci(n):
fib = []
a, b = 0, 1
for i in range(n):
a, b = b, a+b
fib.append(a)
return tuple(fib)
make_fibonacci(8)
View Chap2Q7.py
first = 'Jimmy'
second = 'Johny'
print('Before Swapping',first, second) # Jimmy Johny
first, second = second, first # swaps the variables
print("After swapping:", first, second) # Johny Jimmy