[go: up one dir, main page]

0% found this document useful (0 votes)
23 views43 pages

Debdeep Sanyal - Roll No-326 - Python Practical File

Uploaded by

Bharti Rani
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views43 pages

Debdeep Sanyal - Roll No-326 - Python Practical File

Uploaded by

Bharti Rani
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 43

MALOUT INSTITUTE OF MANAGEMENT AND

INFORMATION TECHNOLOGY , MALOUT


PROGRAMMING IN PYTHON LAB
PRACTICAL FILE
SUBMISSION DATE- 30 /12 /2021

SUBMITTED BY :
NAME –DEBDEEP SANYAL
ROLL NO. – 326
UNIV. ROLL NO. – 1906931
BRANCH – B.TECH CSE-a
SEM / YEAR – 5TH / 3 rd

SUBMITTED T0
Harjasdeep Singh Sir
INDEX
Task 1:
Write a program to demonstrate different number data types in Python.
Task 2:
Write a program to perform different Arithmetic Operations on numbers in
Python.
Task 3:
Write a program to create, concatenate and print a string and accessing sub-
string
from a given string.
Task 4:
Write a python script to print the current date in the following format “Sun May
29 02:26:23 IST 2017”
Task 5:
Write a program to create, append, and remove lists in python.
Task 6:
Write a program to demonstrate working with tuples in python.
Task 7:
Write a program to demonstrate working with dictionaries in python.
Task 8:
Write a python program to find largest of three numbers.
Task 9:
Write a Python program to convert temperatures to and from Celsius,
Fahrenheit.
[ Formula: c/5 = f-32/9]
Task 10:
Write a Python program to construct the following pattern, using a nested for
loop *
*
**
***
****
***
**
*
*
Task 11:
Write a Python script that prints prime numbers less than 20.
Task 12:
Write a python program to find factorial of a number using Recursion.
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).
Task 14:
Write a python program to define a module to find Fibonacci Numbers and
import the module to another program.
Task 15:
Write a python program to define a module and import a specific function in
that
module to another program.
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.
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.
Task 18:
Write a Python class to convert an integer to a roman numeral.
Task 19:
Write a Python class to implement pow(x, n)
Task 20:
Write a Python class to reverse a string word by word.
20/11/2021, 15:28 P1-DEBDEEP SANYAL-ROLL NO-326 - Jupyter Notebook

Pr 1. Write a program to demonstrate different number


data types in Python.
In [1]:

x = 10
print(x)
print(type(x))

10
<class 'int'>

In [2]:

x = 1.5
print(x)
print(type(x))

1.5
<class 'float'>

In [4]:

x = 1j
print(x)
print(type(x))

1j
<class 'complex'>

In [5]:

x = "My name is Debdeep"


print(x)
print(type(x))

My name is Debdeep
<class 'str'>

In [6]:

x = ["apple", "kela", "anar"]


print(x)
print(type(x))

['apple', 'kela', 'anar']


<class 'list'>

localhost:8888/notebooks/P1-DEBDEEP SANYAL-ROLL NO-326.ipynb 1/3


20/11/2021, 15:28 P1-DEBDEEP SANYAL-ROLL NO-326 - Jupyter Notebook

In [7]:

x = ("apple", "kela", "anar")


print(x)
print(type(x))

('apple', 'kela', 'anar')


<class 'tuple'>

In [8]:

x = {"name" : "Debdeep", "age" : 21}


print(x)
print(type(x))

{'name': 'Debdeep', 'age': 21}


<class 'dict'>

In [9]:

x = True
print(x)
print(type(x))

True
<class 'bool'>

In [10]:

x = b"Hello"
print(x)
print(type(x))

b'Hello'
<class 'bytes'>

In [14]:

x = {"1", "2", "3"}


print(x)
print(type(x))

{'1', '2', '3'}


<class 'set'>

In [15]:

x = frozenset({"a", "b", "c"})


print(x)
print(type(x))

frozenset({'b', 'c', 'a'})


<class 'frozenset'>

In [ ]:

localhost:8888/notebooks/P1-DEBDEEP SANYAL-ROLL NO-326.ipynb 2/3


20/11/2021, 15:28 P1-DEBDEEP SANYAL-ROLL NO-326 - Jupyter Notebook

In [ ]:

In [ ]:

In [ ]:

localhost:8888/notebooks/P1-DEBDEEP SANYAL-ROLL NO-326.ipynb 3/3


20/11/2021, 15:42 P2-DEBDEEP SANYAL-ROLL NO-326 - Jupyter Notebook

Pr 2. Write a program to perform different Arithmetic


Operations on numbers in Python
In [1]:

a=10
b=20

In [2]:

a+b

Out[2]:

30

In [3]:

a-b

Out[3]:

-10

In [4]:

a*b

Out[4]:

200

In [5]:

a/b

Out[5]:

0.5

In [6]:

a//b

Out[6]:

In [7]:

a%b

Out[7]:

10

localhost:8888/notebooks/P2-DEBDEEP SANYAL-ROLL NO-326.ipynb 1/3


20/11/2021, 15:42 P2-DEBDEEP SANYAL-ROLL NO-326 - Jupyter Notebook

In [8]:

2**3

Out[8]:

In [9]:

a="Hello whats your name"


b="How are you"

In [10]:

a+b

Out[10]:

'Hello whats your nameHow are you'

In [11]:

a+" "+b

Out[11]:

'Hello whats your name How are you'

In [12]:

a-b

---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-12-5ae0619f8fe1> in <module>
----> 1 a-b

TypeError: unsupported operand type(s) for -: 'str' and 'str'

In [15]:

"hi"*10

Out[15]:

'hihihihihihihihihihi'

In [16]:

a[0]

Out[16]:

'H'

localhost:8888/notebooks/P2-DEBDEEP SANYAL-ROLL NO-326.ipynb 2/3


20/11/2021, 15:42 P2-DEBDEEP SANYAL-ROLL NO-326 - Jupyter Notebook

In [17]:

a[0:4]

Out[17]:

'Hell'

In [18]:

a[-1]

Out[18]:

'e'

In [19]:

len(a)

Out[19]:

21

In [ ]:

In [ ]:

localhost:8888/notebooks/P2-DEBDEEP SANYAL-ROLL NO-326.ipynb 3/3


20/11/2021, 15:52 P3-DEBDEEP SANYAL-ROLL N0-326 - Jupyter Notebook

P3- Write a program to create, concatenate and print a


string and accessing sub- string from a given
string
In [1]:

str1 = 'DEBDEEP'
str2 ='SANYAL'

In [2]:

print('str1 + str2 = ', str1 + str2)

str1 + str2 = DEBDEEPSANYAL

In [3]:

print('str1 * 2 =', str1 * 2)

str1 * 2 = DEBDEEPDEBDEEP

In [9]:

str = 'DEBDEEP'
print('str = ', str)
print('str[0] = ', str[0])

str = DEBDEEP
str[0] = D

In [10]:

print('str[-1] = ', str[-1])

str[-1] = P

In [11]:

print('str[1:5] = ', str[1:5])

str[1:5] = EBDE

In [12]:

print('str[5:-2] = ', str[5:-2])

str[5:-2] =

localhost:8888/notebooks/P3-DEBDEEP SANYAL-ROLL N0-326.ipynb 1/1


29/12/2021, 21:05 P4-Debdeep Sanyal,Roll no-326 - Jupyter Notebook

P4 Write a python script to print the current date in the


following format “Sun May 29 02:26:23 IST 2017”
In [2]:

import time;
ltime=time.localtime();
print(time.strftime("%a %b %d %H:%M:%S %Z %Y",ltime));

Wed Dec 29 21:03:55 India Standard Time 2021

localhost:8888/notebooks/P4-Debdeep Sanyal%2CRoll no-326.ipynb 1/1


29/12/2021, 20:41 P5-edited-Debdeep Sanyal-Roll no-326 - Jupyter Notebook

P5 : Write a program to create, append, and remove


lists in python
Example of an empty list

In [1]:

list1=[]

Create list

In [1]:

my_list = [1, 2, 3]

Here we have used append function to add the element at the end
of the list
In [2]:

list1=[5,6,8,9,10]
list1.append(5)
print(list1)

[5, 6, 8, 9, 10, 5]

insert function

In [9]:

list2=[]
list2.insert(5,10)
print(list2)

[10]

remove function

In [2]:

list1=[5,6,8,9,10]=
list1.remove(5)
print(list1)

[6, 8, 9, 10]

pop method

localhost:8888/notebooks/P5-edited-Debdeep Sanyal-Roll no-326.ipynb 1/3


29/12/2021, 20:41 P5-edited-Debdeep Sanyal-Roll no-326 - Jupyter Notebook

In [3]:

list1=[5,6,8,9,10]
list1.pop() # In this method list will act as stack and it will pop the top of the element

Out[3]:

10

copy method

In [4]:

list3=[5,4,5,6,8]
list4=[6,5,4,7,3]
list3=list4

In [5]:

print(list3)

[6, 5, 4, 7, 3]

join method

In [8]:

list5=[9,8,7,6,5,2]
list6=[7,9,10,11,0]
list7=list5+list6
print(list7)

[9, 8, 7, 6, 5, 2, 7, 9, 10, 11, 0]

slicing method in list

list5=[9,8,7,6,5,2]

print(list5[0:5])

In [11]:

list5[:5]

Out[11]:

[9, 8, 7, 6, 5]

localhost:8888/notebooks/P5-edited-Debdeep Sanyal-Roll no-326.ipynb 2/3


29/12/2021, 20:41 P5-edited-Debdeep Sanyal-Roll no-326 - Jupyter Notebook

In [12]:

list5[:]
print(list5)

[9, 8, 7, 6, 5, 2]

length function of list

In [14]:

list5=[9,8,7,6,5,2]
print(len(list5))

maximum number in a list

In [16]:

list5=[9,8,7,6,5,2]
print(max(list5))

Append list

In [17]:

list5=[9,8,7,6,5,2]
list5.append(10)
print(list5)

[9, 8, 7, 6, 5, 2, 10]

In [ ]:

localhost:8888/notebooks/P5-edited-Debdeep Sanyal-Roll no-326.ipynb 3/3


29/12/2021, 20:45 P6 -edited-Debdeep Sanyal-Roll no-326 - Jupyter Notebook

P6 Write a program to demonstrate working with tuples


in python
In [1]:

t1= (2,5,8,1.5,7)

In [2]:

print(t1)

(2, 5, 8, 1.5, 7)

Data types

In [3]:

print(type(t1))

<class 'tuple'>

Allow Duplicates
In [4]:

s1= ("apple", "banana", "cherry", "apple", "cherry")

In [5]:

print(s1)

('apple', 'banana', 'cherry', 'apple', 'cherry')

Tuple Length
In [6]:

s1= ("apple", "banana", "cherry", "apple", "cherry")

In [7]:

print(len(s1))

Create Tuple With One Item

localhost:8888/notebooks/P6 -edited-Debdeep Sanyal-Roll no-326.ipynb 1/7


29/12/2021, 20:45 P6 -edited-Debdeep Sanyal-Roll no-326 - Jupyter Notebook

In [8]:

s1=("apple",)

In [9]:

print(type(s1))

<class 'tuple'>

In [10]:

s1=("apple")

In [11]:

print(type(s1))

<class 'str'>

Tuple Items - Data Types


In [ ]:

##Tuple items can be of any data type

In [12]:

tuple1 = ("apple", "banana", "cherry")

In [13]:

print(tuple1)

('apple', 'banana', 'cherry')

In [14]:

tuple2 = (1, 5, 7, 9, 3)

In [15]:

print(tuple2)

(1, 5, 7, 9, 3)

In [17]:

tuple3 = (True, False, False)

localhost:8888/notebooks/P6 -edited-Debdeep Sanyal-Roll no-326.ipynb 2/7


29/12/2021, 20:45 P6 -edited-Debdeep Sanyal-Roll no-326 - Jupyter Notebook

In [18]:

print(tuple3)

(True, False, False)

In [19]:

tuple1 = ("abc", 34, True, 40, "male")

In [20]:

print(tuple1)

('abc', 34, True, 40, 'male')

The tuple() Constructor


In [21]:

thistuple = tuple(("apple", "banana", "cherry"))

In [22]:

print(thistuple)

('apple', 'banana', 'cherry')

Access Tuple Items


In [23]:

s1 = ("apple", "banana", "cherry")

In [24]:

print(s1[1])

banana

Negative Indexing
In [25]:

s1 = ("apple", "banana", "cherry")

In [26]:

print(s1[-1])

cherry

localhost:8888/notebooks/P6 -edited-Debdeep Sanyal-Roll no-326.ipynb 3/7


29/12/2021, 20:45 P6 -edited-Debdeep Sanyal-Roll no-326 - Jupyter Notebook

Range of Indexes
In [27]:

s1 = ("apple", "banana", "cherry", "orange", "kiwi", "melon", "mango")

In [28]:

print(s1[2:5])

('cherry', 'orange', 'kiwi')

In [30]:

print(s1[:4])

('apple', 'banana', 'cherry', 'orange')

In [31]:

print(s1[2:])

('cherry', 'orange', 'kiwi', 'melon', 'mango')

In [32]:

print(s1[1::2])

('banana', 'orange', 'melon')

Range of Negative Indexes


In [33]:

s1 = ("apple", "banana", "cherry", "orange", "kiwi", "melon", "mango")

In [34]:

print(s1[-4:-1])

('orange', 'kiwi', 'melon')

Check if Item Exists


In [36]:

s1 = ("apple", "banana", "cherry")

localhost:8888/notebooks/P6 -edited-Debdeep Sanyal-Roll no-326.ipynb 4/7


29/12/2021, 20:45 P6 -edited-Debdeep Sanyal-Roll no-326 - Jupyter Notebook

In [39]:

if "apple" in s1:

File "<ipython-input-39-fa3226a9fa3f>", line 1


if "apple" in s1:
^
SyntaxError: unexpected EOF while parsing

In [38]:

print("Yes, 'apple' is in the fruits tuple")

Yes, 'apple' is in the fruits tuple

Update Tuples

Change Tuple Values


In [42]:

s1 = ("apple", "banana", "cherry")


t1 = list(s1)
t1[1] = "cucumber"
s1 = tuple(t1)

print(s1)

('apple', 'cucumber', 'cherry')

Add Items

1. Convert into a list

In [45]:

thistuple = ("apple", "banana", "cherry")


y = list(thistuple)
y.append("orange")
thistuple = tuple(y)
print(thistuple)

('apple', 'banana', 'cherry', 'orange')

2. Add tuple to a tuple

localhost:8888/notebooks/P6 -edited-Debdeep Sanyal-Roll no-326.ipynb 5/7


29/12/2021, 20:45 P6 -edited-Debdeep Sanyal-Roll no-326 - Jupyter Notebook

In [46]:

thistuple = ("apple", "banana", "cherry")


y = ("orange",)
thistuple += y

print(thistuple)

('apple', 'banana', 'cherry', 'orange')

Remove Items
In [49]:

s1 = ("apple", "banana", "cherry")


t1 = list(s1)
t1.remove("apple")
thistuple = tuple(t1)
print(t1)

['banana', 'cherry']

In [51]:

s1 = ("apple", "banana", "cherry")


del s1
print(s1)

---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-51-5ec1ce39e8e0> in <module>
1 s1 = ("apple", "banana", "cherry")
2 del s1
----> 3 print(s1)

NameError: name 's1' is not defined

Unpack Tuples
In [1]:

fruits = ("apple", "banana", "cherry")

(green, yellow, red) = fruits

print(green)
print(yellow)
print(red)

apple
banana
cherry

localhost:8888/notebooks/P6 -edited-Debdeep Sanyal-Roll no-326.ipynb 6/7


29/12/2021, 20:45 P6 -edited-Debdeep Sanyal-Roll no-326 - Jupyter Notebook

Using Asterisk*
In [2]:

fruits = ("apple", "banana", "cherry", "strawberry", "raspberry")

(green, yellow, *red) = fruits

print(green)
print(yellow)
print(red)

apple
banana
['cherry', 'strawberry', 'raspberry']

Join Two Tuples


In [3]:

tuple1 = ("debdeep", "aditya" , "ramij")


tuple2 = (1, 2, 3)

tuple3 = tuple1 + tuple2


print(tuple3)

('debdeep', 'aditya', 'ramij', 1, 2, 3)

Multiply Tuples
In [4]:

names = ("debdeep", "aditya" , "ramij")


mytuple = names * 2

print(mytuple)

('debdeep', 'aditya', 'ramij', 'debdeep', 'aditya', 'ramij')

In [ ]:

In [ ]:

localhost:8888/notebooks/P6 -edited-Debdeep Sanyal-Roll no-326.ipynb 7/7


17/09/2021, 04:15 Debdeep Sanyal-Roll no-326-Experiment-1-Dictionary - Jupyter Notebook

In [3]:

thisdict = {
"brand": "Honda",
"model": "120",
"year": 2015
}
print(thisdict)

{'brand': 'Honda', 'model': '120', 'year': 2015}

Dictionary Items
In [4]:

thisdict = {
"brand": "Honda",
"model": "120",
"year": 2015
}
print(thisdict["brand"])

Honda

Duplicates Not Allowed


In [6]:

thisdict = {
"brand": "Honda",
"model": "120",
"year": 2015,
"year": 2018

}
print(thisdict)

{'brand': 'Honda', 'model': '120', 'year': 2018}

Dictionary Length
In [7]:

print(len(thisdict))

Dictionary Items - Data Types

localhost:8888/notebooks/Debdeep Sanyal-Roll no-326-Experiment-1-Dictionary.ipynb 1/9


17/09/2021, 04:15 Debdeep Sanyal-Roll no-326-Experiment-1-Dictionary - Jupyter Notebook

In [10]:

## The values in dictionary items can be of any data type:

In [9]:

thisdict = {
"brand": "Honda",
"model": "120",
"year": 2015,
"colors": ["red", "white", "blue"]
}
print(thisdict)

{'brand': 'Honda', 'model': '120', 'year': 2015, 'colors': ['red', 'white',


'blue']}

type()
In [11]:

thisdict = {
"brand": "Honda",
"model": "120",
"year": 2015
}
print(type(thisdict))

<class 'dict'>

Access Dictionary Items

Accessing Items
In [12]:

thisdict = {
"brand": "Honda",
"model": "120",
"year": 2015
}
a=thisdict["model"]
print(a)

120

localhost:8888/notebooks/Debdeep Sanyal-Roll no-326-Experiment-1-Dictionary.ipynb 2/9


17/09/2021, 04:15 Debdeep Sanyal-Roll no-326-Experiment-1-Dictionary - Jupyter Notebook

In [13]:

thisdict = {
"brand": "Honda",
"model": "120",
"year": 2015
}
a=thisdict.get("model")
print(a)

120

Get Keys
In [14]:

thisdict = {
"brand": "Honda",
"model": "120",
"year": 2015
}
a=thisdict.keys()
print(a)

dict_keys(['brand', 'model', 'year'])

Get Values
In [15]:

thisdict = {
"brand": "Honda",
"model": "120",
"year": 2015
}
a= thisdict.values()
print(a)

dict_values(['Honda', '120', 2015])

Get Items
In [16]:

thisdict = {
"brand": "Honda",
"model": "120",
"year": 2015
}
a= thisdict.items()
print(a)

dict_items([('brand', 'Honda'), ('model', '120'), ('year', 2015)])

localhost:8888/notebooks/Debdeep Sanyal-Roll no-326-Experiment-1-Dictionary.ipynb 3/9


17/09/2021, 04:15 Debdeep Sanyal-Roll no-326-Experiment-1-Dictionary - Jupyter Notebook

Check if Key Exists


In [17]:

thisdict = {
"brand": "Honda",
"model": "120",
"year": 2015
}
if "model" in thisdict:
print("Yes, 'model' is one of the keys in the thisdict dictionary")

Yes, 'model' is one of the keys in the thisdict dictionary

Change Dictionary Items

Change Values
In [18]:

thisdict = {
"brand": "Honda",
"model": "120",
"year": 2015
}
thisdict["year"] = 2021
print(thisdict)

{'brand': 'Honda', 'model': '120', 'year': 2021}

Update Dictionary
In [19]:

thisdict = {
"brand": "Honda",
"model": "120",
"year": 2015
}
thisdict.update({"year": 2020})
print(thisdict)

{'brand': 'Honda', 'model': '120', 'year': 2020}

Add Dictionary Items

Adding Items

localhost:8888/notebooks/Debdeep Sanyal-Roll no-326-Experiment-1-Dictionary.ipynb 4/9


17/09/2021, 04:15 Debdeep Sanyal-Roll no-326-Experiment-1-Dictionary - Jupyter Notebook

In [20]:

thisdict = {
"brand": "Honda",
"model": "120",
"year": 2015
}
thisdict["color"] = "black"
print(thisdict)

{'brand': 'Honda', 'model': '120', 'year': 2015, 'color': 'black'}

Update Dictionary
In [21]:

thisdict = {
"brand": "Honda",
"model": "120",
"year": 2015
}
thisdict.update({"color": "black"})
print(thisdict)

{'brand': 'Honda', 'model': '120', 'year': 2015, 'color': 'black'}

Remove Dictionary Items

Removing Items

pop()

In [22]:

thisdict = {
"brand": "Honda",
"model": "120",
"year": 2015
}
thisdict.pop("model")
print(thisdict)

{'brand': 'Honda', 'year': 2015}

popitem()

localhost:8888/notebooks/Debdeep Sanyal-Roll no-326-Experiment-1-Dictionary.ipynb 5/9


17/09/2021, 04:15 Debdeep Sanyal-Roll no-326-Experiment-1-Dictionary - Jupyter Notebook

In [23]:

thisdict = {
"brand": "Honda",
"model": "120",
"year": 2015
}
thisdict.popitem()
print(thisdict)

{'brand': 'Honda', 'model': '120'}

del

In [24]:

thisdict = {
"brand": "Honda",
"model": "120",
"year": 2015
}
del thisdict["model"]
print(thisdict)

{'brand': 'Honda', 'year': 2015}

In [25]:

thisdict = {
"brand": "Honda",
"model": "120",
"year": 2015
}
del thisdict
print(thisdict)

---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-25-649f44a56a47> in <module>
5 }
6 del thisdict
----> 7 print(thisdict)

NameError: name 'thisdict' is not defined

clear()

localhost:8888/notebooks/Debdeep Sanyal-Roll no-326-Experiment-1-Dictionary.ipynb 6/9


17/09/2021, 04:15 Debdeep Sanyal-Roll no-326-Experiment-1-Dictionary - Jupyter Notebook

In [26]:

thisdict = {
"brand": "Honda",
"model": "120",
"year": 2015
}
thisdict.clear()
print(thisdict)

{}

Loop Dictionaries

Loop Through a Dictionary

Print all key names in the dictionary, one by one:

In [30]:

thisdict = {
"brand": "Honda",
"model": "120",
"year": 2015
}
for x in thisdict:
print(x)

brand
model
year

Print all values in the dictionary, one by one:

In [31]:

thisdict = {
"brand": "Honda",
"model": "120",
"year": 2015
}
for x in thisdict:
print(thisdict[x])

Honda
120
2015

values() method to return values of a dictionary

localhost:8888/notebooks/Debdeep Sanyal-Roll no-326-Experiment-1-Dictionary.ipynb 7/9


17/09/2021, 04:15 Debdeep Sanyal-Roll no-326-Experiment-1-Dictionary - Jupyter Notebook

In [32]:

thisdict = {
"brand": "Honda",
"model": "120",
"year": 2015
}
for x in thisdict.values():
print(x)

Honda
120
2015

keys() method to return the keys of a dictionary

In [33]:

thisdict = {
"brand": "Honda",
"model": "120",
"year": 2015
}
for x in thisdict.keys():
print(x)

brand
model
year

items() method:

In [34]:

thisdict = {
"brand": "Honda",
"model": "120",
"year": 2015
}
for x, y in thisdict.items():
print(x, y)

brand Honda
model 120
year 2015

Copy Dictionaries

copy() method:

localhost:8888/notebooks/Debdeep Sanyal-Roll no-326-Experiment-1-Dictionary.ipynb 8/9


17/09/2021, 04:15 Debdeep Sanyal-Roll no-326-Experiment-1-Dictionary - Jupyter Notebook

In [44]:

thisdict = {
"brand": "Honda",
"model": "120",
"year": 2015
}
mydict = thisdict.copy()
print(mydict)

{'brand': 'Honda', 'model': '120', 'year': 2015}

dict() function
In [45]:

thisdict = {
"brand": "Honda",
"model": "120",
"year": 2015
}
mydict = dict(thisdict)
print(mydict)

{'brand': 'Honda', 'model': '120', 'year': 2015}

Nested Dictionaries
In [47]:

myfamily = {
"child1" : {
"name" : "Ram",
"year" : 2004
},
"child2" : {
"name" : "Shyam",
"year" : 2007
},
"child3" : {
"name" : "Vikas",
"year" : 2011
}
}
print(myfamily)

{'child1': {'name': 'Ram', 'year': 2004}, 'child2': {'name': 'Shyam', 'yea


r': 2007}, 'child3': {'name': 'Vikas', 'year': 2011}}

localhost:8888/notebooks/Debdeep Sanyal-Roll no-326-Experiment-1-Dictionary.ipynb 9/9


29/12/2021, 20:34 P7-Debdeep Sanyal-Roll no-326 - Jupyter Notebook

Pr 8 : Write a python program to find largest of three


numbers.
In [1]:

a1 = int (input("Enter the 1st number: "))


a2 = int (input("Enter the 2nd number: "))
a3 = int (input("Enter the 3rd number: "))
if (a1 > a2):
if (a1 > a3):
print ("1st number is the largest number")
else:
print ("3rd number is the largest number")
elif (a2 > a3):
print ("2nd number is the largest number")
else:
print ("3rd number is the largest number")

Enter the 1st number: 23


Enter the 2nd number: 25
Enter the 3rd number: 28
3rd number is the largest number

localhost:8888/notebooks/P7-Debdeep Sanyal-Roll no-326.ipynb 1/1


29/12/2021, 20:59 P9-edited- Debdeep Sanyal-Roll no-326 - Jupyter Notebook

P9-Write a Python program to convert temperatures to


and from Celsius, Fahrenheit
In [1]:

celsius = int(input("Enter the temperature in Degrees Celsius that you would like to conver
fahrenheit = (celsius * 9/5) + 32

print("The converted temperature is", fahrenheit, "Degrees Fahrenheit.")

Enter the temperature in Degrees Celsius that you would like to convert: 15
The converted temperature is 59.0 Degrees Fahrenheit.

localhost:8888/notebooks/P9-edited- Debdeep Sanyal-Roll no-326.ipynb 1/1


29/12/2021, 10:48 P-10 Debdeep Sanyal,Roll no-326 - Jupyter Notebook

P-10 Write a Python program to construct the following


pattern, using a nested for loop

**

***

****

***

**

*
In [4]:

n=4;
for i in range(n):
for j in range(i):
print ('* ', end="")
print('')

for i in range(n,0,-1):
for j in range(i):
print('* ', end="")
print('')

*
* *
* * *
* * * *
* * *
* *
*

localhost:8888/notebooks/P-10 Debdeep Sanyal%2CRoll no-326.ipynb 1/1


29/12/2021, 08:55 P11-Debdeep Sanyal-Roll no-326 - Jupyter Notebook

Pr 11 :Write a Python script that prints prime numbers


less than 20
In [3]:

num=int(input("Enter the number "))


primes_num = []
for i in range (2, num+1):
for j in range(2, i):
if i%j == 0:
break
else:
primes_num.append(i)
print(primes_num)

Enter the number 20


[2, 3, 5, 7, 11, 13, 17, 19]

localhost:8956/notebooks/P11-Debdeep Sanyal-Roll no-326.ipynb 1/1


29/12/2021, 09:09 P-12 Debdeep Sanyal ,Roll no-326 - Jupyter Notebook

P-12 Write a python program to find factorial of a


number using Recursion.
In [2]:

def fact(n):
if (n==1 or n==0):
return 1
else:
return (n * fact(n - 1))
num1 = 5;
print("number : ",num1)
print("Factorial : ",fact(num1))

number : 5
Factorial : 120

localhost:8888/notebooks/P-12 Debdeep Sanyal %2CRoll no-326.ipynb 1/1


29/12/2021, 09:20 P-13 Debdeep Sanyal,Roll no-326 - Jupyter Notebook

P-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
In [1]:

a = float(input("Enter side a: "))


b = float(input("Enter side b: "))
c = float(input("Enter side c: "))
if ((a*a + b*b == c*c) or (b*b + c*c == a*a) or (c*c + a*a == b*b)):
print("The triangle is a right triangle.")
else:
print("The triangle is not a right triangle.")

Enter side a: 3
Enter side b: 4
Enter side c: 5
The triangle is a right triangle.

localhost:8888/notebooks/P-13 Debdeep Sanyal%2CRoll no-326.ipynb 1/1


29/12/2021, 09:35 P-14 Debdeep Sanyal,Roll no-326 - Jupyter Notebook

P-14 Write a python program to define a module to find


Fibonacci Numbers and import the module to another
program.
In [1]:

import fibonacci
num=int(input("Enter any number to print Fibonacci series "))
fibonacci.fib(num)

Enter any number to print Fibonacci series 5


1 1 2 3

localhost:8888/notebooks/P-14 Debdeep Sanyal%2CRoll no-326.ipynb 1/1


29/12/2021, 09:48 P-15 Debdeep Sanyal,Roll no-326 - Jupyter Notebook

P-15 Write a python program to define a module and


import a specific function in that module to another
program
In [1]:

from arithmatic import Add


num1=float(input("Enterthe first number: "))
num2=float(input("Enter second Number : "))
print("ADDITION result is : ",Add(num1,num2))
print("SUBTRACTION result is : ",Sub(num1,num2))

Enterthe first number: 6


Enter second Number : 7
ADDITION result is : 13.0

---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-1-712a80b89e01> in <module>
3 num2=float(input("Enter second Number : "))
4 print("ADDITION result is : ",Add(num1,num2))
----> 5 print("SUBTRACTION result is : ",Sub(num1,num2))

NameError: name 'Sub' is not defined

In [ ]:

In [ ]:

localhost:8888/notebooks/P-15 Debdeep Sanyal%2CRoll no-326.ipynb 1/1


29/12/2021, 10:06 P-16 Debdeep Sanyal,Roll no-326 - Jupyter Notebook

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

fi_1=input("Enter the 1st Filename : ")


fi_2=input("Enter the 2nd Filename : ")
fn1 = open(fi_1, 'r')
fn2 = open(fi_2, 'w')
cont = fn1.readlines()

for i in range(0, len(cont)):


fn2.write(cont[i])
fn2.close()
print("copying 1st file content to 2nd file ")
fn2 = open(fi_2, 'r')
cont1 = fn2.read()
print("Content of 2nd file :")
print(cont1)
fn1.close()
fn2.close()

Enter the 1st Filename : debdeep1


Enter the 2nd Filename : debdeep2
copying 1st file content to 2nd file
Content of 2nd file :
he is a good boy

In [ ]:

localhost:8888/notebooks/P-16 Debdeep Sanyal%2CRoll no-326.ipynb 1/1


29/12/2021, 10:21 P-17 Debdeep Sanyal,Roll no-326 - Jupyter Notebook

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

file1 = input("Enter the txt. file name: ")


fh = open(file1)
lst = list()
words=[];
for line in fh:
words += line.split()
words.sort()
print("The unique words in alphabetical order are:")
for word in words:
if word in lst:
continue
else:
lst.append(word)
print(word)

Enter file name: P-17


The unique words in alphabetical order are:
AMIT
BOLO
KALA
QUO
RAVI
SHYAM

In [ ]:

localhost:8888/notebooks/P-17 Debdeep Sanyal%2CRoll no-326.ipynb 1/1


29/12/2021, 10:25 P-18 Debdeep Sanyal,Roll no-326 - Jupyter Notebook

P-18 Write a Python class to convert an integer to a


roman numeral
In [2]:

def int_to_roman(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("2661 = ",int_to_roman(2661))
print("3789 = ",int_to_roman(3789))
print("379 = ",int_to_roman(379))
print("67 = ",int_to_roman(67))
print("39 = ",int_to_roman(39))
print("1 = ",int_to_roman(1))

2661 = MMDCLXI
3789 = MMMDCCLXXXIX
379 = CCCLXXIX
67 = LXVII
39 = XXXIX
1 = I

In [ ]:

localhost:8888/notebooks/P-18 Debdeep Sanyal%2CRoll no-326.ipynb 1/1


29/12/2021, 10:29 P-19 Debdeep Sanyal,Roll no-326 - Jupyter Notebook

P-19 Write a Python class to implement pow(x, n)


In [1]:

class power:
def pow(self,x,n):
print("pow(",x,",",n,") =",x**n)
p=power()
x=int(input("Enter 'x' value : "))
n=int(input("Enter 'n' value : "))
p.pow(x,n)

Enter 'x' value : 2


Enter 'n' value : 3
pow( 2 , 3 ) = 8

In [ ]:

localhost:8888/notebooks/P-19 Debdeep Sanyal%2CRoll no-326.ipynb 1/1


29/12/2021, 10:32 P-20 Debdeep Sanyal,Roll no-326 - Jupyter Notebook

P-20 Write a Python class to reverse a string word by


word
In [3]:

class reverse:
def rev_sentence(self,sentence):
words = sentence.split(' ')
reverse_sentence = ' '.join(reversed(words))
print(reverse_sentence)
c=reverse()
c.rev_sentence(input("Enter the string: "))

Enter the string: i am debdeep


debdeep am i

localhost:8888/notebooks/P-20 Debdeep Sanyal%2CRoll no-326.ipynb 1/1

You might also like