INDEX
Sr.No Title Page no. Teacher
remarks
1). Write a program to demonstrate different 1-2
number data types in Python.
2). Write a program to perform different 3-4
Arithmetic Operations on numbers in Python.
3). Write a program to create, concatenate and 5-6
print a string and accessing sub-string from a
given string.
4). Write a python script to print the current date 7
in the following format “Sun May 29 02:26:23
IST 2017”
5). Write a program to create, append, and 8-16
remove lists in python.
6). Write a program to demonstrate working with 17-21
tuples in python.
7). Write a program to demonstrate working with 22-28
dictionaries in python.
8). Write a python program to find largest of three 29
numbers.
9). Write a Python program to convert 30
temperatures to and from Celsius, Fahrenheit.
[ Formula: c/5 = f-32/9].
10). Write a Python program to construct the 31
following pattern, using a nested for loop.
11). Write a Python script that prints prime 32
numbers less than 20.
12). Write a python program to find factorial of a 33
number using Recursion.
13). Write a program that accepts the lengths of 34
three sides of a triangle as inputs. The
program output should indicate whether or
not the triangle is a right triangle (Recall from
the Pythagorean Theorem that in a right
triangle, the square of one side equals the
sum of the squares of the other two sides).
14). Write a python program to define a module to 35
find Fibonacci Numbers and import the
module to another program.
INDEX
15). Write a python program to define a module 36
and import a specific function in that module
to another program.
16). Write a script named copyfile.py. This script 37
should prompt the user for the names of two
text files. The contents of the first file should
be input and written to the second file.
17). Write a program that inputs a text file. The 38
program should print all of the unique words
in the file in alphabetical order.
18). Write a Python class to convert an integer to a 39
roman numeral.
19). Write a Python class to implement pow(x, n). 40
20). Write a Python class to reverse a string word 41
by word.
12/31/21, 3:37 PM TASK 1 - Jupyter Notebook
TASK 1. Write a program to demonstrate different number data
types in Python.
THERE ARE VARIOUS NUMBER DATA TYPE IN THE PYTHON ARE LIKE INTEGER , FLOAT ,
COMPLEX,THESE ALL ARE NUMERIC DATA TYPES , ALSO OTHER DATA TYPES ARE DICTIONARY,
LIST, STRINGS , TUPLES ETC.
In [11]:
x=313
y=522.33
z="ansh sharma"
m= 3+19j
n=[12,34,56,78,44]
In [2]:
print(x)
313
In [4]:
print(type(x))
<class 'int'>
In [5]:
print(y)
522.33
In [6]:
print(type(y))
<class 'float'>
In [7]:
print(z)
ansh sharma
In [8]:
print(type(z))
<class 'str'>
localhost:8888/notebooks/TASK 1.ipynb 1/2
12/31/21, 3:37 PM TASK 1 - Jupyter Notebook
In [9]:
print(m)
(3+19j)
In [10]:
print(type(m))
<class 'complex'>
In [12]:
print(n)
[12, 34, 56, 78, 44]
In [13]:
print(type(n))
<class 'list'>
In [ ]:
localhost:8888/notebooks/TASK 1.ipynb 2/2
12/31/21, 3:38 PM TASK 2 - Jupyter Notebook
TASK 2. Write a program to perform different Arithmetic
Operations on numbers in Python.
THERE ARE VARIOUS ARTHEMATIC OPERATION IN PYTHON ARE LIKE DIVISON, MULTIPLICATION,
ADDITION, SUBTRACTION, MODULOUS,AND POWER
In [2]:
x=9
y=7
ADDITION
In [5]:
print("ADDITION IS:",x+y)
ADDITION IS: 16
SUBTRACTION
In [6]:
print("subtraction is:", x-y)
subtraction is: 2
multiplication
In [7]:
print("multiplication is:", x*y)
multiplication is: 63
division
In [8]:
print("Division is:", x/y)
Division is: 1.2857142857142858
Modulous
localhost:8888/notebooks/TASK 2.ipynb 1/2
12/31/21, 3:38 PM TASK 2 - Jupyter Notebook
In [9]:
print("modulous is:", x%y)
#here it will givwe the remainder of the values
modulous is: 2
Power
In [11]:
x=8
y=5
In [12]:
print("power is ", x**y)
power of x is 32768
localhost:8888/notebooks/TASK 2.ipynb 2/2
12/31/21, 3:38 PM TASK 3 - Jupyter Notebook
TASK 3. Write a program to create, concatenate and print a string
and accessing sub- string from a given string.
In [1]:
str1= "PYTHON IS VERY"
str2="EASY TO LEARN"
PRINTING THE STRING
In [2]:
print(str1)
print(str2)
PYTHON IS VERY
EASY TO LEARN
CONCATENATE THE STRING
In [5]:
result= str1 +" "+ str2 #by using + we can concatenate
print(result)
PYTHON IS VERY EASY TO LEARN
ACCESSING THE SUB STRING FROM THE GIVEN STRING
In [6]:
result
Out[6]:
'PYTHON IS VERY EASY TO LEARN'
In [7]:
print(result[0:2]) #by using slicing
PY
In [8]:
print(result[0:10])
PYTHON IS
localhost:8888/notebooks/TASK 3.ipynb 1/2
12/31/21, 3:38 PM TASK 3 - Jupyter Notebook
In [14]:
print(result[0::2]) #sub string
PTO SVR AYT ER
In [ ]:
localhost:8888/notebooks/TASK 3.ipynb 2/2
12/31/21, 3:39 PM Task4 - Jupyter Notebook
TASK4 :Write a python script to print the current date in the
following format “Sun May 29 02:26:23 IST 2017”
In [ ]:
In [5]:
import time;
currenttime=time.localtime();
print(time.strftime("The current time is :-""%a %b %d %H:%M:%S %Z %Y",currenttime));
# %a : Abbreviated weekday name.
# %b : Abbreviated month name.
# %d : Day of the month as a decimal number [01,31].
# %H : Hour (24-hour clock) as a decimal number [00,23].
# %M : Minute as a decimal number [00,59].
# %S : Second as a decimal number [00,61].
# %Z : Time zone name (no characters if no time zone exists).
# %Y : Year with century as a decimal number.
The current time is :-Wed Dec 29 11:50:18 India Standard Time 2021
localhost:8888/notebooks/Task4.ipynb 1/1
12/31/21, 3:39 PM task 5(LISTS) - Jupyter Notebook
TASK 5 : demonstrate working with lists in python.
List are mutable. List are ordered also.
In [1]:
list1 = ["apple","mango","banana"]
print(list1)
['apple', 'mango', 'banana']
In [2]:
list1
Out[2]:
['apple', 'mango', 'banana']
APPEND FUNCTION
In [3]:
list1.append("hrishab")
print(list1)
['apple', 'mango', 'banana', 'hrishab']
In [8]:
list1.append("kiwi")
In [9]:
list1 # List can contain different type of data type.
Out[9]:
['apple', 'mango', 'banana', 'hrishab', 23, 23, 'kiwi', 'kiwi', 'kiwi']
INDEXING OF LIST
In [10]:
list1
Out[10]:
['apple', 'mango', 'banana', 'hrishab', 23, 23, 'kiwi', 'kiwi', 'kiwi']
Loading [MathJax]/extensions/Safe.js
localhost:8888/notebooks/task 5(LISTS).ipynb 1/9
12/31/21, 3:39 PM task 5(LISTS) - Jupyter Notebook
In [11]:
list1[0]
Out[11]:
'apple'
In [12]:
list1[2]
Out[12]:
'banana'
In [13]:
list1[-1]
Out[13]:
'kiwi'
In [14]:
list1[-2]
Out[14]:
'kiwi'
REVERSE OF A LIST
In [15]:
list1[::-1]
Out[15]:
['kiwi', 'kiwi', 'kiwi', 23, 23, 'hrishab', 'banana', 'mango', 'apple']
LENGTH OF LIST
In [17]:
print(len(list1))
SLICING OF A LIST
Loading [MathJax]/extensions/Safe.js
localhost:8888/notebooks/task 5(LISTS).ipynb 2/9
12/31/21, 3:39 PM task 5(LISTS) - Jupyter Notebook
In [18]:
list1
Out[18]:
['apple', 'mango', 'banana', 'hrishab', 23, 23, 'kiwi', 'kiwi', 'kiwi']
In [19]:
list1[0:3]
Out[19]:
['apple', 'mango', 'banana']
In [20]:
list1[0:3]
Out[20]:
['apple', 'mango', 'banana']
In [22]:
list2=[12,34,56,78,90]
list2
Out[22]:
[12, 34, 56, 78, 90]
In [23]:
list2[0:2:6]
Out[23]:
[12]
In [25]:
list2[0::]
Out[25]:
[12, 34, 56, 78, 90]
In [ ]:
CONCATINATION OF A LIST
Loading [MathJax]/extensions/Safe.js
localhost:8888/notebooks/task 5(LISTS).ipynb 3/9
12/31/21, 3:39 PM task 5(LISTS) - Jupyter Notebook
In [27]:
list3=[13,32,45,65,78]
list4=[313,345,367,321,567]
In [29]:
list3+list4
Out[29]:
[13, 32, 45, 65, 78, 313, 345, 367, 321, 567]
In [30]:
print(list3)
[13, 32, 45, 65, 78]
In [32]:
list12=['name',234,12332]
list13=['ansh','m']
newlist=list12+list13
print(newlist)
['name', 234, 12332, 'ansh', 'm']
REMOVE FUNCTION
In [34]:
list1.remove("mango")
list1
Out[34]:
['apple', 'banana', 'hrishab', 23, 23, 'kiwi', 'kiwi', 'kiwi']
In [ ]:
COMPARE THE LIST
Loading [MathJax]/extensions/Safe.js
localhost:8888/notebooks/task 5(LISTS).ipynb 4/9
12/31/21, 3:39 PM task 5(LISTS) - Jupyter Notebook
In [35]:
list1>list2
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-35-e40886078308> in <module>
----> 1 list1>list2
TypeError: '>' not supported between instances of 'str' and 'int'
In [36]:
list12<list13
Out[36]:
False
INSERT FUNCTION
In [37]:
list1.insert(12,89)
print(list1)
['apple', 'banana', 'hrishab', 23, 23, 'kiwi', 'kiwi', 'kiwi', 89]
In [38]:
list1
Out[38]:
['apple', 'banana', 'hrishab', 23, 23, 'kiwi', 'kiwi', 'kiwi', 89]
In [43]:
list1.insert(70)
print(list1)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-43-88e670591b46> in <module>
----> 1 list1.insert(70)
2 print(list1)
TypeError: insert expected 2 arguments, got 1
Loading [MathJax]/extensions/Safe.js
localhost:8888/notebooks/task 5(LISTS).ipynb 5/9
12/31/21, 3:39 PM task 5(LISTS) - Jupyter Notebook
In [40]:
list1
Out[40]:
['apple', 'banana', 'hrishab', 23, 23, 'kiwi', 'kiwi', 'kiwi', 89]
DEL FUNCTION
In [44]:
del list1
list1
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-44-60be933dfb00> in <module>
1 del list1
----> 2 list1
NameError: name 'list1' is not defined
In [45]:
list1
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-45-32e3a55f6bef> in <module>
----> 1 list1
NameError: name 'list1' is not defined
POP METHOD
In [46]:
list2
Out[46]:
[12, 34, 56, 78, 90]
In [47]:
list2.pop()
Out[47]:
90
Loading [MathJax]/extensions/Safe.js
localhost:8888/notebooks/task 5(LISTS).ipynb 6/9
12/31/21, 3:39 PM task 5(LISTS) - Jupyter Notebook
In [48]:
list2
Out[48]:
[12, 34, 56, 78]
REMOVE METHOD
In [49]:
list2.remove(34)
In [50]:
list2
Out[50]:
[12, 56, 78]
CLEAR FUNCTION
In [53]:
list2.clear()
list2
Out[53]:
[]
SORTING OF LIST
In [54]:
list2
Out[54]:
[]
In [55]:
list5=[9,8,65,31,23]
list5.sort()
Loading [MathJax]/extensions/Safe.js
localhost:8888/notebooks/task 5(LISTS).ipynb 7/9
12/31/21, 3:39 PM task 5(LISTS) - Jupyter Notebook
In [56]:
list5
Out[56]:
[8, 9, 23, 31, 65]
SUM FUNCTION
In [57]:
list23=[22,42,66,180]
total =sum(list23)
print(total)
310
In [ ]:
EXTEND FUNCTION
In [58]:
list23.extend(list5)
print(list23)
[22, 42, 66, 180, 8, 9, 23, 31, 65]
In [60]:
list23
Out[60]:
[22, 42, 66, 180, 8, 9, 23, 31, 65]
In [61]:
list5
Out[61]:
[8, 9, 23, 31, 65]
In [ ]:
COUNT METHOD
Loading [MathJax]/extensions/Safe.js
localhost:8888/notebooks/task 5(LISTS).ipynb 8/9
12/31/21, 3:39 PM task 5(LISTS) - Jupyter Notebook
In [62]:
list5.count(32)
Out[62]:
In [63]:
list5.count(9)
Out[63]:
In [64]:
list23.count(31)
Out[64]:
Loading [MathJax]/extensions/Safe.js
localhost:8888/notebooks/task 5(LISTS).ipynb 9/9
12/31/21, 3:40 PM TASK6 (TUPLES) - Jupyter Notebook
TASK 6 : Write a program to demonstrate working with tuples in
python.
Tuple is immutable , it means we can not chnge their value after the
declaration.
In [3]:
tuple1=(122,243,1233)
In [4]:
tuple1
Out[4]:
(122, 243, 1233)
In [5]:
tuple1=('ansh',8,0,'work',23.45)
In [6]:
tuple1
Out[6]:
('ansh', 8, 0, 'work', 23.45)
INDEXING OF A TUPLES
In [7]:
tuple1[1]
Out[7]:
In [9]:
tuple1[-1]
Out[9]:
23.45
SLICING OF A TUPLE
localhost:8888/notebooks/TASK6 (TUPLES).ipynb 1/5
12/31/21, 3:40 PM TASK6 (TUPLES) - Jupyter Notebook
In [11]:
tuple1
Out[11]:
('ansh', 8, 0, 'work', 23.45)
In [13]:
tuple1[0:4]
Out[13]:
('ansh', 8, 0, 'work')
In [14]:
tuple1[0:2:30]
Out[14]:
('ansh',)
In [ ]:
In [15]:
tuple1[0:2:2]
Out[15]:
('ansh',)
LENGTH FUNCTION
In [16]:
tuple1
Out[16]:
('ansh', 8, 0, 'work', 23.45)
In [17]:
len(tuple1)
Out[17]:
TYPE FUNCTION
localhost:8888/notebooks/TASK6 (TUPLES).ipynb 2/5
12/31/21, 3:40 PM TASK6 (TUPLES) - Jupyter Notebook
In [18]:
type(tuple1)
Out[18]:
tuple
In [19]:
print(type(tuple1))
<class 'tuple'>
JOIN METHOD
In [22]:
tuples2 =("ansh","sharma","aditya")
x=" ,".join(tuples2)
print(x)
ansh ,sharma ,aditya
In [23]:
tuples2
Out[23]:
('ansh', 'sharma', 'aditya')
INDEXING METHOD
In [25]:
tuples2.index("ansh")
Out[25]:
In [28]:
tuples2.index("aditya")
Out[28]:
COUNT METHOD
localhost:8888/notebooks/TASK6 (TUPLES).ipynb 3/5
12/31/21, 3:40 PM TASK6 (TUPLES) - Jupyter Notebook
In [30]:
tuples2.count("aditya")
Out[30]:
In [35]:
tuples3=(12,12,33,313,344,349)
tuples3.count(12)
Out[35]:
In [36]:
tuples3.count(313)
Out[36]:
WE CANNOT ADD THE ELEMNTS again ONCE IT WILL DECLARE IN
TUPLES
In [39]:
tuples3.insert(32)
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-39-994d4e131066> in <module>
----> 1 tuples3.insert(32)
AttributeError: 'tuple' object has no attribute 'insert'
POP METHOD
In [40]:
tuples3
Out[40]:
(12, 12, 33, 313, 344, 349)
localhost:8888/notebooks/TASK6 (TUPLES).ipynb 4/5
12/31/21, 3:40 PM TASK6 (TUPLES) - Jupyter Notebook
In [41]:
tuples3.pop()
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-41-8acbaf2271b1> in <module>
----> 1 tuples3.pop()
AttributeError: 'tuple' object has no attribute 'pop'
In [44]:
tuples3.clear() # it doesnot take clear, remove,add,insert ALSO
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-44-6f1655a0e141> in <module>
----> 1 tuples3.clear()
AttributeError: 'tuple' object has no attribute 'clear'
In [ ]:
localhost:8888/notebooks/TASK6 (TUPLES).ipynb 5/5
12/31/21, 3:41 PM TASK7(DICT) - Jupyter Notebook
TASK 7: Write a program to demonstrate working with
dictionaries in python.
Dictionaries are unordered and we can not sort. dictionaries uses the key :value pair
In [3]:
dict1={"Name":"Ansh","Branch":"CSE","Roll_no":313}
print(dict1)
{'Name': 'Ansh', 'Branch': 'CSE', 'Roll_no': 313}
dict can also print the indivual key and value pairs.
In [4]:
dict1['Name']
Out[4]:
'Ansh'
In [6]:
dict1["Branch"]
Out[6]:
'CSE'
DICTIONARY INSIDE A DICTIONARY
In [8]:
dict2={"arpit":12,"hrishab":34,"aditya":304,
"LOVISH":{"roll":349,"work":"NOTHING","salary":0.0000}
}
print (dict2)
{'arpit': 12, 'hrishab': 34, 'aditya': 304, 'LOVISH': {'roll': 349, 'work':
'NOTHING', 'salary': 0.0}}
In [ ]:
COPY A DICTIONARY
localhost:8888/notebooks/TASK7(DICT).ipynb 1/7
12/31/21, 3:41 PM TASK7(DICT) - Jupyter Notebook
In [10]:
d2={"ansh":"sharma","hrishab":"raj"}
d3={"fruit":"mango","veg":"paneer"}
d3=d2 # copy function.
print(d3)
d3=dict(d2)
{'ansh': 'sharma', 'hrishab': 'raj'}
CHANGE METHOD
In [11]:
d2["ansh"]="shshsha"
d2
Out[11]:
{'ansh': 'shshsha', 'hrishab': 'raj'}
In [13]:
d3["fruit"]="BANANA"
d3
Out[13]:
{'ansh': 'sharma', 'hrishab': 'raj', 'fruit': 'BANANA'}
ADD THE ELEMENTS IN DICT.
In [14]:
d3["LOVISH"]=349 # adding element in dictionaries. Dictionary is muttable.
In [15]:
d3
Out[15]:
{'ansh': 'sharma', 'hrishab': 'raj', 'fruit': 'BANANA', 'LOVISH': 349}
LENGTH OF THE DICTIONARIES
In [16]:
print(len(d3))
localhost:8888/notebooks/TASK7(DICT).ipynb 2/7
12/31/21, 3:41 PM TASK7(DICT) - Jupyter Notebook
In [17]:
print(len(d2))
PRINITING THE KEY OF DICT
In [18]:
d3.keys() # it will show the keys of a dictionaries
Out[18]:
dict_keys(['ansh', 'hrishab', 'fruit', 'LOVISH'])
PRINTING THE VALUES OF DICT.
In [19]:
d2.values() # it will show the values of the dictionaries.
Out[19]:
dict_values(['shshsha', 'raj'])
GET METHOD
In [21]:
d2.get("hrishab") # show the dictionaries items.
Out[21]:
'raj'
In [22]:
d3.get("fruit")
Out[22]:
'BANANA'
In [23]:
d2.get("ansh")
Out[23]:
'shshsha'
localhost:8888/notebooks/TASK7(DICT).ipynb 3/7
12/31/21, 3:41 PM TASK7(DICT) - Jupyter Notebook
In [24]:
d3
Out[24]:
{'ansh': 'sharma', 'hrishab': 'raj', 'fruit': 'BANANA', 'LOVISH': 349}
UPDATE FUNCTION
In [26]:
d2.update({"keshav":"garg"})
print(d2)
{'ansh': 'shshsha', 'hrishab': 'raj', 'keshav': 'garg'}
In [27]:
d2
Out[27]:
{'ansh': 'shshsha', 'hrishab': 'raj', 'keshav': 'garg'}
TYPE FUNCTION
In [28]:
type(d2)
Out[28]:
dict
In [29]:
type(d3)
Out[29]:
dict
REPEATION IS NOT ALLOWED IN DICT.
In [31]:
d2
Out[31]:
{'ansh': 'shshsha', 'hrishab': 'raj', 'keshav': 'garg'}
localhost:8888/notebooks/TASK7(DICT).ipynb 4/7
12/31/21, 3:41 PM TASK7(DICT) - Jupyter Notebook
In [32]:
d2["keshav"]="garg"
d2
Out[32]:
{'ansh': 'shshsha', 'hrishab': 'raj', 'keshav': 'garg'}
DEL FUNCTION
In [33]:
del(d2["keshav"]) #THIS IS USED TO DELETE THE ITEMS IN THE DICT.
In [34]:
d2
Out[34]:
{'ansh': 'shshsha', 'hrishab': 'raj'}
POP METHOD
In [36]:
d3.pop("hrishab")
Out[36]:
'raj'
In [37]:
d3
Out[37]:
{'ansh': 'sharma', 'fruit': 'BANANA', 'LOVISH': 349}
In [38]:
d2.pop("ansh")
Out[38]:
'shshsha'
localhost:8888/notebooks/TASK7(DICT).ipynb 5/7
12/31/21, 3:41 PM TASK7(DICT) - Jupyter Notebook
In [39]:
d2
Out[39]:
{'hrishab': 'raj'}
In [40]:
d2.popitem()
d2
Out[40]:
{}
In [41]:
d2
Out[41]:
{}
In [42]:
d3.popitem()
Out[42]:
('LOVISH', 349)
In [43]:
d3
Out[43]:
{'ansh': 'sharma', 'fruit': 'BANANA'}
In [45]:
d3.popitem()
Out[45]:
('fruit', 'BANANA')
In [46]:
d3
Out[46]:
{'ansh': 'sharma'}
localhost:8888/notebooks/TASK7(DICT).ipynb 6/7
12/31/21, 3:41 PM TASK7(DICT) - Jupyter Notebook
In [ ]:
localhost:8888/notebooks/TASK7(DICT).ipynb 7/7
12/31/21, 3:42 PM TASK 8 - Jupyter Notebook
TASK 8 : Write a python program to find largest of three
numbers.
In [5]:
def greater(a,b,c):
list1=[a,b,c]
greaterno=max(list1)
print("the greater of three number is: ",greaterno)
a=int(input("enter the no. A: "))
b=int(input("enter the no. B: "))
c=int(input("enter the no. C: "))
greater(a,b,c)
enter the no. A: 34
enter the no. B: 21
enter the no. C: 55
the greater of three number is: 55
In [ ]:
In [ ]:
In [ ]:
localhost:8888/notebooks/TASK 8.ipynb 1/1
12/31/21, 3:42 PM TASK 9 - Jupyter Notebook
TASK 9 : Write a Python program to convert temperatures to and
from Celsius, Fahrenheit.
WE WILL FIRSTLY CONVERT FAHRENHEIT TO CELCUIS
In [3]:
f=float(input("ENTER THE TEMPERATRURE IN FAHERNHEIT :"))
celcuis = (f-32)* (5/9) #THIS IS FORMULA
print(f"the fahernheit tempertaure is equivalent to {celcuis} degree celcuis")
ENTER THE TEMPERATRURE IN FAHERNHEIT :100
the fahernheit tempertaure is equivalent to 37.77777777777778 degree celcui
s
CELCUIS TO FAHGERNHEIT CONVERSION
In [4]:
c=float(input("ENTER THE TEMPERATURE IN CELCUIS : "))
f= c*(9/5)+32
print(f" THE TEMPERATURE OF CELCUIS IS EQUIVALENT TO {f} farhenheit")
ENTER THE TEMPERATURE IN CELCUIS : 37.77
THE TEMPERATURE OF CELCUIS IS EQUIVALENT TO 99.986 farhenheit
In [ ]:
In [ ]:
localhost:8888/notebooks/TASK 9.ipynb 1/1
12/31/21, 1:52 PM task 10 - Jupyter Notebook
Task 10: Write a Python program to construct the following
pattern, using a nested for loop
*
*
* *
* * *
* * * *
* * *
* *
*
*
In [4]:
n=int(input("enter the no. of pattern printing :"))
print("the pattern using nested loop is :")
for i in range(1):
print("\n*")
for i in range(0,n):
print (" " + " *" * i)
i=i+1
for i in range(n,0,-1):
print(" " + " *" *i)
i=i-1
print(" " + " *")
enter the no. of pattern printing :4
the pattern using nested loop is :
*
* *
* * *
* * * *
* * *
* *
*
*
In [ ]:
localhost:8888/notebooks/task 10.ipynb#Task-10:-Write-a-Python-program-to-construct-the-following-pattern,-using-a-nested-for-loop 1/1
12/31/21, 3:42 PM task 11 - Jupyter Notebook
TASK 11 :Write a Python script that prints prime numbers less than 20
In [5]:
l = int(input("Enter lower range of prime number: "))
u= int(input("Enter upper range of prime number : "))
for number in range(l,u+1):
if number > 1:
for i in range(2,number):
if (number % i) == 0:
break
else:
print(f"the prime number from {l}to {u} is :",number)
Enter lower range of prime number: 1
Enter upper range of prime number : 20
the prime number from 1to 20 is : 2
the prime number from 1to 20 is : 3
the prime number from 1to 20 is : 5
the prime number from 1to 20 is : 7
the prime number from 1to 20 is : 11
the prime number from 1to 20 is : 13
the prime number from 1to 20 is : 17
the prime number from 1to 20 is : 19
In [ ]:
In [ ]:
localhost:8888/notebooks/task 11.ipynb 1/1
12/31/21, 3:43 PM TASK 12 - Jupyter Notebook
TASK12 : Write a python program to find factorial of a number
using Recursion.
In [2]:
def factorial(n):
if n==0:
return 1 #this method is used to find factorial using the recursion.
else:
return n*factorial(n-1)
print("the factorial of the no. is :",factorial(7))
the factorial of the no. is : 5040
In [ ]:
localhost:8888/notebooks/TASK 12.ipynb 1/1
12/31/21, 3:43 PM task13 - Jupyter Notebook
Task 13 .Write a program that accepts the lengths of three sides
of a triangle as inputs.
The program output should indicate whether or not the triangle is a right
triangle
(Recall from the Pythagorean Theorem that in a right triangle, the square of
one
side equals the sum of the squares of the other two sides).
In [ ]:
In [2]:
base=float(input("Enter length of Base : "))
perp=float(input("Enter length of Perpendicular : "))
hypo=float(input("Enter length of Hypotenuse : "))
if hypo**2==((base**2)+(perp**2)):
print("It's a right triangle")
else:
print("It's not a right triangle")
Enter length of Base : 3
Enter length of Perpendicular : 4
Enter length of Hypotenuse : 5
It's a right triangle
localhost:8888/notebooks/task13.ipynb 1/1
12/31/21, 3:43 PM task 14 - Jupyter Notebook
TASK 14 : Write a python program to define a module to find
Fibonacci Numbers and import the module to another
program.
In [3]:
file = open("fibonacci.py","r") # defining a module
print(file.read())
file.close()
a = int(input("enter the no. upto which u want to print the fibonacci series
"))
fibonsrs = [0, 1]
for i in range(2, a):
n = fibonsrs[i - 2] + fibonsrs[i - 1]
fibonsrs.append(n)
print(f"The Fibonnaci series upto {a} is : {fibonsrs} ")
In [4]:
import fibonacci
#by importing
enter the no. upto which u want to print the fibonacci series 5
The Fibonnaci series upto 5 is : [0, 1, 1, 2, 3]
In [ ]:
localhost:8888/notebooks/task 14.ipynb 1/1
12/31/21, 3:43 PM task15 - Jupyter Notebook
Task 15 : Write a python program to define a module and import a
specific function in that module to another program.
In [4]:
file = open("test.py","r") # defining a module
print(file.read())
file.close()
a=10
b=25
def h(a,b):
return a+b
In [5]:
import test
print("here we will add the two numbers by use of importing is: ->")
test.h(2,4) # by importing
here we will add the two numbers by use of importing is: ->
Out[5]:
localhost:8888/notebooks/task15.ipynb 1/1
12/30/21, 7:56 PM task16 - Jupyter Notebook
TASK 16:Write a script named copyfile.py. This script should
prompt the user for the names of two text files. The contents of
the first file should be input and written to the second file.
In [ ]:
In [1]:
file1=input("Enter First Filename : ")
file2=input("Enter Second Filename : ")
# open file in read mode
fn1 = open(file1, 'r')
# open other file in write mode
fn2 = open(file2, 'w')
# read the content of the file line by line
cont = fn1.readlines()
for i in range(0, len(cont)):
fn2.write(cont[i])
fn2.close()
print("Content of first file copied to second file ")
# open file in read mode
fn2 = open(file2, 'r')
# read the content of the file
cont1 = fn2.read()
# print the content of the file
print("Content of Second file :")
print(cont1)
fn1.close()
fn2.close()
Enter First Filename : python.txt
Enter Second Filename : python1.txt
Content of first file copied to second file
Content of Second file :
hello!
ansh here!
localhost:8888/notebooks/task16.ipynb 1/2
12/30/21, 7:57 PM task17 - Jupyter Notebook
Task 17: Write a program that inputs a text file. The program
should print all of the unique words in the file in alphabetical
order.
In [ ]:
In [2]:
fname = input("Enter file name: ")
fh = open(fname)
lst = list() # list for the desired output
words=[];
for line in fh: # to read every line of file python2.txt
words += line.split()
words.sort()
print("The unique words in alphabetical order are:")
for word in words:
if word in lst: # if element is repeated
continue # do nothing
else: # else if element is not in the list
lst.append(word)
print(word)
print(lst)
Enter file name: python2.txt
The unique words in alphabetical order are:
are
bye
bye!!
exams
online
['are', 'bye', 'bye!!', 'exams', 'online']
localhost:8888/notebooks/task17.ipynb 1/1
12/30/21, 7:57 PM task18 - Jupyter Notebook
TASK18 :Write a Python class to convert an integer to a roman
numeral.
In [ ]:
In [9]:
class introman:
def int_to_Roman(self, num):
val = [
1000, 900, 500, 400,
100, 90, 50, 40,
10, 9, 5, 4,
1
]
syb = [
"M", "CM", "D", "CD",
"C", "XC", "L", "XL",
"X", "IX", "V", "IV",
"I"
]
roman_num = ''
i = 0
while num > 0:
for _ in range(num // val[i]):
roman_num += syb[i]
num -= val[i]
i += 1
return roman_num
print("THE INTEGER TO ROMAN IS :",introman().int_to_Roman(101))
print("THE INTEGER TO ROMAN IS :",introman().int_to_Roman(4))
THE INTEGER TO ROMAN IS : CI
THE INTEGER TO ROMAN IS : IV
localhost:8888/notebooks/task18.ipynb 1/1
12/30/21, 7:58 PM task19 - Jupyter Notebook
TASK 19 : Write a Python class to implement pow(x, n)
In [ ]:
In [4]:
class py_pow:
def power(self, x, n):
if x==0 or x==1 or n==1:
return x
if x==-1:
if n%2 ==0:
return 1
else:
return -1
if n==0:
return 1
if n<0:
return 1/self.power(x,-n)
val = self.power(x,n//2)
if n%2 ==0:
return val*val
return val*val*x
x=int(input("Enter x value :"))
n=int(input("Enter n value :"))
print("pow(x,n) value is :",py_pow().power(x,n));
Enter x value :3
Enter n value :3
pow(x,n) value is : 27
localhost:8888/notebooks/task19.ipynb 1/1
12/30/21, 7:58 PM task 20 - Jupyter Notebook
TASK20 : Write a Python class to reverse a string word by
word.
In [ ]:
In [4]:
class revstrwbw:
def reverse_words(self, s):
return ' '.join(reversed(s.split()))
print("THE REVERSE OF STRING WORD_BY_WORD IS :"
,revstrwbw().reverse_words('MST2 OF PYTHON WAS VERY EASY!!'))
THE REVERSE OF STRING WORD_BY_WORD IS : EASY!! VERY WAS PYTHON OF MST2
localhost:8888/notebooks/task 20.ipynb 1/1