0% found this document useful (0 votes)
3 views13 pages

Python Lab Manual

The document contains multiple Python programs demonstrating various programming concepts such as arithmetic operations, string manipulation, date formatting, list operations, tuple usage, dictionary handling, finding the largest of three numbers, temperature conversion, pattern printing, prime number generation, and calculating factorial using recursion. Each program includes source code and sample input/output for clarity. The examples serve as practical applications of fundamental Python programming techniques.

Uploaded by

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

Python Lab Manual

The document contains multiple Python programs demonstrating various programming concepts such as arithmetic operations, string manipulation, date formatting, list operations, tuple usage, dictionary handling, finding the largest of three numbers, temperature conversion, pattern printing, prime number generation, and calculating factorial using recursion. Each program includes source code and sample input/output for clarity. The examples serve as practical applications of fundamental Python programming techniques.

Uploaded by

br5532435
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

1).Write a program to perform different Arithmetic Operations on numbers in Python.

SOURCECODE:

x = 15

y=4

# Output: x + y = 19

print('x + y =',x+y)

# Output: x - y = 11

print('x - y =',x-y)

# Output: x * y = 60

print('x * y =',x*y)

# Output: x / y = 3.75

print('x / y =',x/y)

# Output: x // y = 3

print('x // y =',x//y)

# Output: x ** y = 50625

print('x ** y =',x**y)

INPUT ANDOUTPUT:

x + y = 19

x - y = 11

x * y = 60
2. Write a program to create, concatenate and print a string and accessing sub-string
from a given string.26
SOURCECODE:

# all of the following are equivalent

my_string = 'Hello'
print(my_string)
my_string = "Hello"
print(my_string)
my_string = '''Hello'''
print(my_string)
# triple quotes string can extend multiple lines
my_string = """Hello, welcome to
the world of Python"""
print(my_string)
c=" mlritm"
print(my_string+c)
# substring function
print(my_string[5:11])

INPUT AND OUTPUT:

Hello
Hello
Hello
Hello, welcome to
the world of Python
Hello, welcome to
the world of Python mlritm
, welc
3. Write a python script to print the current date in the following format “Sun May 29
02:26:23 IST 2017”

SOURCECODE:

from datetime import date

today =[Link]()

# dd/mm/YY

d1 =[Link]("%d/%m/%Y")

print("d1 =", d1)

# Textual month, day and year

d2 =[Link]("%B %d, %Y")

print("d2 =", d2)

# mm/dd/y

d3 =[Link]("%m/%d/%y")

print("d3 =", d3)

# Month abbreviation, day and year

d4 =[Link]("%b-%d-%Y")

print("d4 =", d3)

INPUT ANDOUTPUT:

d1 = 25/12/2018

d2 = December 25, 2018

d3 = 12/25/18

d4 = 12/25/18
4. Write a program to create, append, and remove lists in python.

SOURCECODE:
my_list = ['p','r','o','b','l','e','m']
my_list.remove('p')
# Output: ['r', 'o', 'b', 'l', 'e', 'm']
print(my_list)
# Output: 'o'
print(my_list.pop(1))
# Output: ['r', 'b', 'l', 'e', 'm']
print(my_list)
# Output: 'm'
print(my_list.pop())
# Output: ['r', 'b', 'l', 'e']
print(my_list)
my_list.clear()
# Output: []
print(my_list)

INPUT ANDOUTPUT:

my_list=['p','r','o','b','l','e','m']

>>>my_list[2:3]=[]

>>>my_list

['p','r','b','l','e','m']

>>>my_list[2:5]=[]

>>>my_list

['p','r','m']
5. Write a program to demonstrate working with tuples in python.

SOURCECODE:

# empty tuple

# Output: ()

my_tuple = ()

print(my_tuple)

# tuple having integers

# Output: (1, 2, 3)

my_tuple = (1, 2, 3)

print(my_tuple)

# tuple with mixed datatypes

# Output: (1, "Hello", 3.4)

my_tuple = (1, "Hello", 3.4)

print(my_tuple)

# nested tuple

# Output: ("mouse", [8, 4, 6], (1, 2, 3))

my_tuple = ("mouse", [8, 4, 6], (1, 2, 3))

print(my_tuple)

# tuple can be created without parentheses

# also called tuple packing

# Output: 3, 4.6, "dog"

my_tuple = 3, 4.6, "dog"

print(my_tuple)

# tuple unpacking is also possible

# Output:

#3
# 4.6

# dog

a, b, c = my_tuple

print(a)

print(b)

print(c)

INPUT ANDOUTPUT:

38

()

(1, 2, 3)

(1, 'Hello', 3.4)

('mouse', [8, 4, 6], (1, 2, 3))

(3, 4.6, 'dog')

4.6

Dog
6. Write a program to demonstrate working with dictionaries in python.

SOURCECODE:

my_dict = {'name':'Jack', 'age': 26}

# Output: Jack

print(my_dict['name'])

# Output: 26

print(my_dict.get('age'))

# Trying to access keys which doesn't exist throws error

# my_dict.get('address')

# my_dict['address']

INPUT ANDOUTPUT:

Jack

26
7. Write a python program to find largest of three numbers.

SOURCECODE:

# Python program to find the largest number among the three input numbers

# change the values of num1, num2 and num3

# for a different result

num1 = 10

num2 = 14

num3 = 12

# uncomment following lines to take three numbers from user

#num1 = float(input("Enter first number: "))

#num2 = float(input("Enter second number: "))

#num3 = float(input("Enter third number: "))

if (num1 >= num2) and (num1 >= num3):

largest = num1

elif (num2 >= num1) and (num2 >= num3):

largest = num2

else:

largest = num3

print("The largest number between",num1,",",num2,"and",num3,"is",largest)

INPUT ANDOUTPUT:

The largest number between 10, 14 and 12 is 14.0


8. Write a Python program to convert temperatures to and from Celsius, Fahrenheit.

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

SOURCECODE:

# Python Program to convert temperature in celsius to fahrenheit

# change this value for a different result

celsius = 37.5

# calculate fahrenheit

fahrenheit = (celsius * 1.8) + 32

print('%0.1f degree Celsius is equal to %0.1f degree Fahrenheit' %(celsius,fahrenheit))

INPUT AND OUTPUT:

37.5 degree Celsius is equal to 99.5 degree Fahrenheit


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

**

***

****

*****

****

***

**

SOURCECODE:

n=5;

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('')
INPUT AND OUTPUT:

**

***

****

*****

****

***

**

*
[Link] a Python script that prints prime numbers less than 20.

SOURCECODE:

r=int(input("Enter upper limit: "))

for a inrange(2,r+1):

k=0

foriinrange(2,a//2+1):

if(a%i==0):

k=k+1

if(k<=0):

print(a)

INPUT ANDOUTPUT:

Enter upper limit: 15

11

13
[Link] a python program to find factorial of a number using Recursion.

SOURCECODE:

# Python program to find the factorial of a number provided by the user.

# change the value for a different result

num = 7

# uncomment to take input from the user

#num = int(input("Enter a number: "))

factorial = 1

# check if the number is negative, positive or zero

if num < 0:

print("Sorry, factorial does not exist for negative numbers")

elif num == 0:

print("The factorial of 0 is 1")

else:

for i in range(1,num + 1):

factorial = factorial*i

print("The factorial of",num,"is",factorial)

INPUT ANDOUTPUT:

The factorial of 7 is 5040

You might also like