You are on page 1of 23

CT Group of Institutions, Maqsudan

Experiment Title:Write a program to demonstrate different number d


types in Python.
Laboratory: Python Lab (BTCS 513-18)

Experiment No: 1 Department: B.TECH CSE Semester: 5TH

Write a program to demonstrate different number data types in Python.

Code:

num1=10
num2=45.56
num3=complex(a,b)
print("Type of num1 is:", type(num1))
print("Type of num2 is:", type(num2))
print("Type of num3 is:", type(num3))

Output:

Roll no:2123668 Page|1


CT Group of Institutions, Maqsudan
Experiment Title:Write a program to perform different Arithmetic
Operations on numbers in Python.
Laboratory: Python Lab (BTCS 513-18)

Experiment No: 2 Department: B.TECH CSE Semester: 5TH

Aim: Write a program to perform different Arithmetic Operations on numbers in


Python.

Code:

num1=int(input("Enter num1 :"))


num2=int(input("Enter num2 :"))
a=num1+num2
b=num1-num2
c=num1*num2
d=num1/num2
e=num1%num2
print("Addition of two numbers is:",a)
print("Subtraction of two numbers is:",b)
print("Multiplication of two numbers is:",c)
print("Division of two numbers is:",d)
print(" Remainder is:",e)

Output:

Roll no:2123668 Page|2


CT Group of Institutions, Maqsudan
Experiment Title:Write a program to create, concatenate and print a string
and accessing sub-string from a given string.
Laboratory: Python Lab (BTCS 513-18)

Experiment No: 3 Department: B.TECH CSE Semester: 5TH

Aim: Write a program to create, concatenate and print a string and accessing sub-
string from a given string.

Code:

Str1=Str(input(“Enter First String:”))

Str1=Str(input(“Enter First String:”))

Concatenate=Str1+ “” +Str2

Print(“The String is:Concatenate)

Print(“The Substring is:”, Concatenate[1:6])

Output:

Roll no:2123668 Page|3


CT Group of Institutions, Maqsudan
Experiment Title:Write a python script to print the current date in the
following format “Sun May 29 02:26:23 IST 2017”
Laboratory: Python Lab (BTCS 513-18)

Experiment No: 4 Department: B.TECH CSE Semester: 5TH

Aim: Write a python script to print the current date in the following format “Sun
May 29 02:26:23 IST 2017”

Code:

import time

ltime=time.localtime();

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

print(ltime)

Output:

Roll no:2123668 Page|4


CT Group of Institutions, Maqsudan
Experiment Title:Write a program to create, append, and remove lists in
python
Laboratory: Python Lab (BTCS 513-18)

Experiment No: 5 Department: B.TECH CSE Semester: 5TH

Aim: Write a program to create, append, and remove lists in python.

Code:

a=[1,78,20,"Python"]

print("length of list is:", len(a)+1)

a.append(98)

print("New list A is:",list(a))

a.remove(78)

print("updated list is:",list(a))

list=[1,"asia",3.2,"cup"]

list.append (2023)

print(list)

list.remove(3.2)

print(list)

Output:

Roll no:2123668 Page|5


Roll no:2123668 Page|6
EXPERIMENT :- 6

Aim: Write a program to demonstrate working with tuples in python.


Code:

a=input("enter your tuple :").split(",")#first tuple


print('first Tuple is:-',a)
print('value of tuple at index 6 is:-',a[6])
print("length of a tuple is:-",len(a))
print("repetition of tuple: ",a*2)
print(a.count(9))
b=(40,9,32.5,12.98,65,98) #second tuple
print("second tuple is:-",b)
print("maximum value in tuple: ",max(b))
print("minimum value in tuple: ",min(b))
print("sum of tuple is:",sum(b))
print("tuple after sorting",sorted(b))
c=('Raju')
print(tuple(c))
Output:

Roll no:2123668 Page|7


EXPERIMENT :- 7

Aim: Write a program to demonstrate working with dictionaries in python.


Code:

dic1 = {1: "Hello, ", 2: "welcome ", 3: "to ", 4: "python"}


dic2 = dic1.copy()
print(dic2)
dic1.clear()
print(dic1)
print(dic2.get(1))
print(dic2.items())
print(dic2.keys())
dic2.pop(4)
print(dic2)
dic2.popitem()
print(dic2)
dic2.update({3: "To"})
print(dic2)
print(dic2.values())
Output:

Roll no:2123668 Page|8


EXPERIMENT :- 8

Aim: Write a python program to find largest of three numbers.


Code:

a=int(input("Enter number 1:"))


b=int(input("Enter number 2:"))
c=int(input("Enter number 3:"))
if(a>b)and(a>c):
print("number 1 is largest")
if(b>a) and (b>c):
print("number 2 is largest")
else:
print("number 3 is largest")

Output:

Roll no:2123668 Page|9


EXPERIMENT :- 9

Aim: Write a Python program to convert temperatures to and from Celsius,


Fahrenheit. [ Formula: c/5 = f-32/9]

Code:

celsius = float(input("Enter the temperature in Celsius :"))

f=float(input("Enter the temperature in Faherenheit :"))

fahrenheit = celsius*(9/5)+32

print("The temperature converted in fahrenheit is :",fahrenheit)

celsius=(f-32 )* 5/9

print("The temperature converted in celsius is :",celsius)

Output:

Roll no:2123668 Page|10


EXPERIMENT :- 10

Aim: Write a Python program to construct the following pattern, using a nested for
loop .

Code:

n=4

for i in range(0, n):

for j in range(0, i + 1):

print("*", end=' ')

print(" ")

for i in range(n , 0, -1):

for j in range(0, i - 1):

print("*", end=' ')

print(" ")

print("*")

Output:

Roll no:2123668 Page|11


CT Group of Institutions, Maqsudan

Experiment Title: Write a Python script that prints prime numbers less than
20

Laboratory: Python Lab (BTCS 513-18)

Experiment No:11 Department: B.TECH CSE Semester: 5TH

Aim: Write a Python script that prints prime numbers less than 20.
Code:

print("prime numbers between 1 and 20 are:")


ulmt=20;
for num in range(ulmt):
if num>1:
for i in range(2,num):
if(num%i)==0:
break;
else:
print(num)

Output:

Roll no:2123668 Page|12


CT Group of Institutions, Maqsudan

Experiment Title:Write a python program to find factorial of a number using


Recursion.

Laboratory: Python Lab (BTCS 513-18)

Experiment No:12 Department: B.TECH CSE Semester: 5TH

Aim: Write a python program to find factorial of a number using Recursion.


Code:

def recur_fact(n):
if n==1:
return n
else:
return n*recur_fact(n-1)
num=int (input("enter a number: "))
if num<0:
print("sorry, factorial does not exist for negative numbers")
elif num==0:
print("the factorial of 0 is 1")
else:
print("the factorial of ", num,"is", recur_fact(num))

Output:

Roll no:2123668 Page|13


CT Group of Institutions, Maqsudan
Experiment Title: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).
Laboratory: Python Lab (BTCS 513-18)

Experiment No:13 Department: B.TECH CSE Semester: 5TH

Aim: 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).

Code:

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")

Output:

Roll no:2123668 Page|14


CT Group of Institutions, Maqsudan
Experiment Title:Write a python program to define a module to find
Fibonacci Numbers and import the module to another program.
Laboratory: Python Lab (BTCS 513-18)

Experiment No:14 Department: B.TECH CSE Semester: 5TH

Aim: Write a python program to define a module to find Fibonacci Numbers and
import the module to another program.

Code:

Module file:

def fib(num):

f1= 0

f2=1

print(f1,f2, end=" ")

for i in range(num):

f3=f1+f2

print(f3, end=" ")

f1=f2

f2=f3

Main file:

import fib

n=int(input("Enter number:"))

fib.fib(n)

Output:

Roll no:2123668 Page|15


Roll no:2123668 Page|16
CT Group of Institutions, Maqsudan

Experiment Title:Write a python program to define a module and import a


specific function in that module to another program.

Laboratory: Python Lab (BTCS 513-18)

Experiment No:15 Department: B.TECH CSE Semester: 5TH

Aim: Write a python program to define a module and import a specific function in
that module to another program.

1. Module file sub:

def sub(a,b):

c=a-b

return c

2. Module file add:

def add(a,b):
c=a+b
return c
3. Main file:

import sub
import add
num1= int(input("enter first number"))
num2= int(input("enter second number"))
print("subtraction of two numbers is:",sub.sub(num1,num2))
print("addition of two numbers is:", add.add(num1,num2))
Output:

Roll no:2123668 Page|17


Roll no:2123668 Page|18
CT Group of Institutions, Maqsudan

Experiment Title: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.

Laboratory: Python Lab (BTCS 513-18)

Experiment No:16 Department: B.TECH CSE Semester: 5TH

Aim: 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.

Code:

Output:

Roll no:2123668 Page|19


CT Group of Institutions, Maqsudan

Experiment Title:

Laboratory: Python Lab (BTCS 513-18)

Experiment No:17 Department: B.TECH CSE Semester: 5TH

Roll no:2123668 Page|20


CT Group of Institutions, Maqsudan

Experiment Title:

Laboratory: Python Lab (BTCS 513-18)

Experiment No:18 Department: B.TECH CSE Semester: 5TH

Roll no:2123668 Page|21


CT Group of Institutions, Maqsudan

Experiment Title:

Laboratory: Python Lab (BTCS 513-18)

Experiment No:19 Department: B.TECH CSE Semester: 5TH

Roll no:2123668 Page|22


CT Group of Institutions, Maqsudan

Experiment Title:

Laboratory: Python Lab (BTCS 513-18)

Experiment No:20 Department: B.TECH CSE Semester: 5TH

Roll no:2123668 Page|23

You might also like