Debdeep Sanyal - Roll No-326 - Python Practical File
Debdeep Sanyal - Roll No-326 - Python Practical File
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
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]:
My name is Debdeep
<class 'str'>
In [6]:
In [7]:
In [8]:
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]:
In [15]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
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
In [8]:
2**3
Out[8]:
In [9]:
In [10]:
a+b
Out[10]:
In [11]:
a+" "+b
Out[11]:
In [12]:
a-b
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-12-5ae0619f8fe1> in <module>
----> 1 a-b
In [15]:
"hi"*10
Out[15]:
'hihihihihihihihihihi'
In [16]:
a[0]
Out[16]:
'H'
In [17]:
a[0:4]
Out[17]:
'Hell'
In [18]:
a[-1]
Out[18]:
'e'
In [19]:
len(a)
Out[19]:
21
In [ ]:
In [ ]:
str1 = 'DEBDEEP'
str2 ='SANYAL'
In [2]:
In [3]:
str1 * 2 = DEBDEEPDEBDEEP
In [9]:
str = 'DEBDEEP'
print('str = ', str)
print('str[0] = ', str[0])
str = DEBDEEP
str[0] = D
In [10]:
str[-1] = P
In [11]:
str[1:5] = EBDE
In [12]:
str[5:-2] =
import time;
ltime=time.localtime();
print(time.strftime("%a %b %d %H:%M:%S %Z %Y",ltime));
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
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)
list5=[9,8,7,6,5,2]
print(list5[0:5])
In [11]:
list5[:5]
Out[11]:
[9, 8, 7, 6, 5]
In [12]:
list5[:]
print(list5)
[9, 8, 7, 6, 5, 2]
In [14]:
list5=[9,8,7,6,5,2]
print(len(list5))
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 [ ]:
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]:
In [5]:
print(s1)
Tuple Length
In [6]:
In [7]:
print(len(s1))
In [8]:
s1=("apple",)
In [9]:
print(type(s1))
<class 'tuple'>
In [10]:
s1=("apple")
In [11]:
print(type(s1))
<class 'str'>
In [12]:
In [13]:
print(tuple1)
In [14]:
tuple2 = (1, 5, 7, 9, 3)
In [15]:
print(tuple2)
(1, 5, 7, 9, 3)
In [17]:
In [18]:
print(tuple3)
In [19]:
In [20]:
print(tuple1)
In [22]:
print(thistuple)
In [24]:
print(s1[1])
banana
Negative Indexing
In [25]:
In [26]:
print(s1[-1])
cherry
Range of Indexes
In [27]:
In [28]:
print(s1[2:5])
In [30]:
print(s1[:4])
In [31]:
print(s1[2:])
In [32]:
print(s1[1::2])
In [34]:
print(s1[-4:-1])
In [39]:
if "apple" in s1:
In [38]:
Update Tuples
print(s1)
Add Items
In [45]:
In [46]:
print(thistuple)
Remove Items
In [49]:
['banana', 'cherry']
In [51]:
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-51-5ec1ce39e8e0> in <module>
1 s1 = ("apple", "banana", "cherry")
2 del s1
----> 3 print(s1)
Unpack Tuples
In [1]:
print(green)
print(yellow)
print(red)
apple
banana
cherry
Using Asterisk*
In [2]:
print(green)
print(yellow)
print(red)
apple
banana
['cherry', 'strawberry', 'raspberry']
Multiply Tuples
In [4]:
print(mytuple)
In [ ]:
In [ ]:
In [3]:
thisdict = {
"brand": "Honda",
"model": "120",
"year": 2015
}
print(thisdict)
Dictionary Items
In [4]:
thisdict = {
"brand": "Honda",
"model": "120",
"year": 2015
}
print(thisdict["brand"])
Honda
thisdict = {
"brand": "Honda",
"model": "120",
"year": 2015,
"year": 2018
}
print(thisdict)
Dictionary Length
In [7]:
print(len(thisdict))
In [10]:
In [9]:
thisdict = {
"brand": "Honda",
"model": "120",
"year": 2015,
"colors": ["red", "white", "blue"]
}
print(thisdict)
type()
In [11]:
thisdict = {
"brand": "Honda",
"model": "120",
"year": 2015
}
print(type(thisdict))
<class 'dict'>
Accessing Items
In [12]:
thisdict = {
"brand": "Honda",
"model": "120",
"year": 2015
}
a=thisdict["model"]
print(a)
120
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)
Get Values
In [15]:
thisdict = {
"brand": "Honda",
"model": "120",
"year": 2015
}
a= thisdict.values()
print(a)
Get Items
In [16]:
thisdict = {
"brand": "Honda",
"model": "120",
"year": 2015
}
a= thisdict.items()
print(a)
thisdict = {
"brand": "Honda",
"model": "120",
"year": 2015
}
if "model" in thisdict:
print("Yes, 'model' is one of the keys in the thisdict dictionary")
Change Values
In [18]:
thisdict = {
"brand": "Honda",
"model": "120",
"year": 2015
}
thisdict["year"] = 2021
print(thisdict)
Update Dictionary
In [19]:
thisdict = {
"brand": "Honda",
"model": "120",
"year": 2015
}
thisdict.update({"year": 2020})
print(thisdict)
Adding Items
In [20]:
thisdict = {
"brand": "Honda",
"model": "120",
"year": 2015
}
thisdict["color"] = "black"
print(thisdict)
Update Dictionary
In [21]:
thisdict = {
"brand": "Honda",
"model": "120",
"year": 2015
}
thisdict.update({"color": "black"})
print(thisdict)
Removing Items
pop()
In [22]:
thisdict = {
"brand": "Honda",
"model": "120",
"year": 2015
}
thisdict.pop("model")
print(thisdict)
popitem()
In [23]:
thisdict = {
"brand": "Honda",
"model": "120",
"year": 2015
}
thisdict.popitem()
print(thisdict)
del
In [24]:
thisdict = {
"brand": "Honda",
"model": "120",
"year": 2015
}
del thisdict["model"]
print(thisdict)
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)
clear()
In [26]:
thisdict = {
"brand": "Honda",
"model": "120",
"year": 2015
}
thisdict.clear()
print(thisdict)
{}
Loop Dictionaries
In [30]:
thisdict = {
"brand": "Honda",
"model": "120",
"year": 2015
}
for x in thisdict:
print(x)
brand
model
year
In [31]:
thisdict = {
"brand": "Honda",
"model": "120",
"year": 2015
}
for x in thisdict:
print(thisdict[x])
Honda
120
2015
In [32]:
thisdict = {
"brand": "Honda",
"model": "120",
"year": 2015
}
for x in thisdict.values():
print(x)
Honda
120
2015
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:
In [44]:
thisdict = {
"brand": "Honda",
"model": "120",
"year": 2015
}
mydict = thisdict.copy()
print(mydict)
dict() function
In [45]:
thisdict = {
"brand": "Honda",
"model": "120",
"year": 2015
}
mydict = dict(thisdict)
print(mydict)
Nested Dictionaries
In [47]:
myfamily = {
"child1" : {
"name" : "Ram",
"year" : 2004
},
"child2" : {
"name" : "Shyam",
"year" : 2007
},
"child3" : {
"name" : "Vikas",
"year" : 2011
}
}
print(myfamily)
celsius = int(input("Enter the temperature in Degrees Celsius that you would like to conver
fahrenheit = (celsius * 9/5) + 32
Enter the temperature in Degrees Celsius that you would like to convert: 15
The converted temperature is 59.0 Degrees Fahrenheit.
**
***
****
***
**
*
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('')
*
* *
* * *
* * * *
* * *
* *
*
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
Enter side a: 3
Enter side b: 4
Enter side c: 5
The triangle is a right triangle.
import fibonacci
num=int(input("Enter any number to print Fibonacci series "))
fibonacci.fib(num)
---------------------------------------------------------------------------
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))
In [ ]:
In [ ]:
In [ ]:
In [ ]:
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 [ ]:
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)
In [ ]:
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: "))