"Enter A Number:": Def If Return Else Return
"Enter A Number:": Def If Return Else Return
def fact(n):
if (n == 0 or n == 1):
return 1
else:
return n * fact(n-1)
N = int(input("Enter a number:"))
print(fact(N))
Enter a number:6
720
result = fib(n)
In [7]: # read from file and print unique words in sorted words
file = open("data.txt",'r')
rtext = file.read()
rtext = rtext.lower()
rwords = rtext.split()
unique = []
unique.append(rwords)
unique.sort()
print(unique)
poly = []
poly.append (coefficient)
result = 0
sum = poly[i]
sum = sum * x
print (result)
'Enter element 1 : -2
'Enter element 2 : 6
'Enter element 3 : 2
'Enter element 4 : 1
13
a = []
a.append(b)
a.sort(key = len)
print(a)
Enter element 1 : hi
a = []
a.append(b)
max = len(a[0])
for x in a:
if len(x)>max :
max = len(x)
temp = x
print(temp)
Enter element 1 : hi
hello
sum = 0
while (n != 0):
n = n/10
return sum
a = []
print(a)
[81, 100]
In [17]: #dictionary
test = input("Enter string:")
l = []
l = test.split()
print(dict(zip(l,words)))
In [4]: #dictionary with key as 1st character and value as words starting with that character
The course structure is very well defined and explained in a systematic way.'''
words = string_input.split()
dictionary = {}
dictionary[word[0]] = []
dictionary[word[0]].append(word)
else:
dictionary[word[0]].append(word)
print(dictionary)
{'B': ['BMSCE'], 'i': ['is', 'in'], 'a': ['a', 'and'], 'g': ['good'], 'u': ['universit
y'], 'f': ['for'], 'c': ['computer', 'course'], 'e': ['enthusiasts.', 'explained'], 'T':
['The'], 's': ['structure', 'systematic'], 'v': ['very'], 'w': ['well', 'way.'], 'd':
['defined']}
n = int(input("Enter a number:"))
a = []
while(n>0):
d = n%2
a.append(d)
n = n//2
a.reverse()
for i in a:
print(i,end="")
Enter a number:23
10111
for line in f:
l=line.title()
print(l)
dict1 = {}
for i in str1:
keys = dict1.keys()
if i in keys:
dict1[i]+=1
else:
dict1[i]=1
print (dict1)
if datatype =='int':
answer = 0
if datatype =='str':
answer =''
for x in args:
answer = answer + x
print(answer)
add('int', 8, 9)
17
Hello World
class Animal:
def walk(self):
class Dog(Animal):
def walk(self):
r = Dog()
r.walk()
r = Animal()
r.walk()
In [19]: #Multithreading
import threading
def print_cube(num):
def print_square(num):
if __name__ == "__main__":
t1 = threading.Thread(target=print_square, args=(6,))
t2 = threading.Thread(target=print_cube, args=(6,))
t1.start()
t2.start()
t1.join()
t2.join()
print("Done!")
Square: 36
Cube: 216
Done!
In [21]: #Inheritance
class Person(object):
self.name = name
self.id = id
print(self.name, self.id)
Kapilesh 1234
def counter(fname):
num_words = 0
num_lines = 0
num_charc = 0
num_spaces = 0
for line in f:
num_lines += 1
word = 'Y'
num_words += 1
word = 'N'
num_spaces += 1
word = 'Y'
for i in letter:
num_charc += 1
counter(fname)
Enter a file:data.txt
f = input("Enter filename:")
file1 = open(f,"rt")
print(rev,end = "")
Enter filename:sample.txt
hselipaK ma I
In [ ]: