0% found this document useful (0 votes)
4 views21 pages

Python Lab - II CS

The document contains a series of Python programming exercises, each with an aim, program code, output, and result verification. Topics covered include mathematical operations, control structures, data structures like arrays, lists, tuples, and dictionaries, as well as recursion and function usage. Each exercise is designed to demonstrate specific programming concepts and their practical applications.

Uploaded by

Mercy Philip
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)
4 views21 pages

Python Lab - II CS

The document contains a series of Python programming exercises, each with an aim, program code, output, and result verification. Topics covered include mathematical operations, control structures, data structures like arrays, lists, tuples, and dictionaries, as well as recursion and function usage. Each exercise is designed to demonstrate specific programming concepts and their practical applications.

Uploaded by

Mercy Philip
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

INDEX

[Link] DATE EXERCISE SIGNATURE

1 Mathematical Operations

2 Positive or Negative numbers

3 Sum of n Numbers

4 Multiplication table

5 Square root of a number

6 Fibonacci Series using Recursion

7 Addition using functions with passing


arguments

8 Factorial of a number using Recursion

9 Arrays

10 String Operations

11 Modules

12 List

13 Tuples

14 Dictionary
Mathematical Operations
ExNo:1

Date:

Aim:

Write a python program to perform mathematical operations.

Program:

a =7

b=2

#Addition

print('Sum:',a+b) #

Subtraction

print('Subtraction:',a-b) #

Multiplication

print('Multiplication:',a*b) #

Division

print('Division:',a/b) #

Floor division

print('FloorDivision:',a//b) #

Modulo

print('Modulo:',a%b) #

a to the power b

print('Power:',a** b)
Output:

>>>('Sum:

',9)

('Subtraction:',5)

('Multiplication:',14)

('Division: ', 3)

('FloorDivision:',3)

('Modulo: ', 1)

('Power:',49)

>>>:

Result:

Thus the above program was successfully executed and output verified.
Positive or Negative

ExNo:2

Date:

Aim:

To write a program to find whether the given number is positive or negative.

Program:

num=float(input("Enteranumber:")) if

num > 0:

print("Positivenumber")

elif num == 0:

print("Zero")

else:

print("Negativenumber")

Output:

>>>

Enter a number: -154724

Negative number

>>>

Result:

Thus the above program was successfully executed and output verified.
Sum of n Numbers using While loop

ExNo:3

Date:

Aim:

To write a program in python using while loop to calculate the sum of first n numbers.

Program:

print("Sum of Numbers")

i=0

s=0

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

while(i<=n):

s=s+i

i=i+1

print('Sum of',n,'number:',s)

Output:

>>>

Sum of Numbers

Enter the number :10

Sum of 10 number:55

>>>

Result:

Thus the above program was successfully executed and output verified.
Multiplication table using for loop

ExNo:4

Date:

Aim:

To write a program in python using for loop in multiplication table.

Program:

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


print("multiplication table of",n)
for i in range(1,10):
print(n,'x',i,'=',n*i)

Output:

MultiplicationTable:
Enter any number:2
Multiplicationtable of 2:
2x1=2
2x2=4
2x3=6
2x4=8
2x5=10
2x6=12
2x7=14
2x8=16
2x9=18

Result:
Thus the above program was successfully executed and output verified.
Square root of a number using jump statement

Ex no:5

Date:

Aim:

To write a program using to calculate square root of a [Link] the use of


jump statements.

Program:
import math

while True:
num = int(input("Enter no: "))

if num == 999:
break

elif num < 0:


print("Square root of negative numbers cannot be calculated")
continue

else:
print("Square root of", num, "=", [Link](num))
Output:

>>>

Enterno:12

('Squarerootof',12,'=', 3.4641016151377544)

Enterno:34

('Squarerootof',34,'=', 5.830951894845301)

Enterno:-1

Squarerootofnegativenumberscannotbecalculated Enter

no:999

>>>

Result:

Thus the above program was successfully executed and output verified.
Fibonacci Series using Function

Exno:6

Date:

Aim:

To write a program to print the Fibonacc iseries using the function.

Program:

def fibo(n):

if n <= 1:

return n

return fibo(n - 1) + fibo(n - 2)

n=int(input("Enter the number of terms:"))

for i in range(n):

print(fibo(i), end=" ")

Output:
>>>
Enter the number of terms:8

0 1 1 2 3 5 8 13

>>>

Result:

Thus the above program was successfully executed and output verified.
Addition using functions with passing arguments

ExNo:7

Date:

Aim:

To write a program for addition using function with passing arguments.

Program:

# Function definition with two parameters

def add_two_numbers(num1, num2):

return num1 + num2

# Calling the function and passing two arguments

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

num2=int(input("Enter the number1 :"))

result = add_two_numbers(num1, num2)

print(f"The sum is: {result}")


Output:

>>>
Enter the number1 :20
Enter the number1 :40
The sum is: 60
>>>

Result:

Thus the above program was successfully executed and output verified.
Factorial of a number using Recursion

Ex no:8

Date:

Aim:

To write a program to calculate the factorial of a number recursively.

Program:
# Factorial of a number using recursion

def recur_factorial(n):
if n == 1:
return n
else:
return n*recur_factorial(n-1)

num=int(input("Enter the 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_factorial(num))

Output:

>>>

Enter the number :5

The factorial of 5 is 120

>>>
Result:

Thus the above program was successfully executed and output verified.
Create and Print numbers using Array

ExNo:9

Date:

Aim:

To write a program to create and print numbers using arrays.

Program:
import array as arr #access array elts
a = [Link]('i',[2, 5, 62, 5, 42, 52, 48, 5])
print(a)

print("First element:", a[0])


print("Second element:", a[1])
print("Last element:", a[-1])

print(a[2:5]) # 3rd to 5th


print(a[5:]) # 6th to end
print(a[:]) # beginning to end

# changing first element


a[0] = 0
print(a)

# changing 3rd to 5th element


a[2:5] = [Link]('i',[4, 6, 8])
print(a)
Output:

>>>

array('i', [2, 5, 62, 5, 42, 52, 48, 5])

First element: 2

Second element: 5

Last element: 5

array('i', [62, 5, 42])

array('i', [52, 48, 5])

array('i', [2, 5, 62, 5, 42, 52, 48, 5])

array('i', [0, 5, 62, 5, 42, 52, 48, 5])

array('i', [0, 5, 4, 6, 8, 52, 48, 5])

>>>

Result:

Thus the above program was successfully executed ando utput verified.
String Operations

ExNo:10

Date:

Aim:

To write a program to display the strings operations.

Program:
greet = "Hello, "
name = "Jack"

result = greet + name # using + operator


print(result)

str1 = "Hello, world!"


str2 = "I love Swift."
str3 = "Hello, world!"
print(str1 == str2) # compare str1 and str2
print(str1 == str3) # compare str1 and str3

greet = 'Hello'

for letter in greet: # iterating through greet string


print(letter)

greet = 'Hello' # count length of greet string


print(len(greet))

Result:

Thus the above program was successfully executed ando utput verified.
Output:

>>>

Hello, Jack

False

True

>>>

Result:

Thus the above program was successfully executed ando utput verified.
Modules

ExNo:11

Date:

Aim:

To write a program in python using modules.

Program:

import time

local time=[Link]([Link]([Link]()))

print("Local current time : ", local time)

Output:

>>>

Local current time : Mon Jul 6 10:15:28 2026

>>>

Result:

Thus the above program was successfully executed ando utput verified.
Lists

Ex No:12

Date:

Aim:

To write a program in python using lists.

Program:

list=['a','bc',78,1.23]
list1=['d',78]
print(list)
print(list[0])
print(list[1:3])
print(list[2:])
print(list *2)
print(list+list1)

Output:

>>>

['a', 'bc', 78, 1.23]

['bc', 78]

[78, 1.23]

['a', 'bc', 78, 1.23, 'a', 'bc', 78, 1.23]

['a', 'bc', 78, 1.23, 'd', 78]

>>>
Result:

Thus the above program was successfully executed ando utput verified.
Tuples

Ex No:13

Date:

Aim:

To write a program to using tuples.

Program:

Tup=('a','bc','78','23')
Tup2=('d',78)
print(Tup)
print(Tup[0])
print(Tup[1:3])
print(Tup[2:])
print(Tup *2)
print(Tup+Tup2)

Output:
('a', 'bc', '78', '23')
a
('bc', '78')
('78', '23')
('a', 'bc', '78', '23', 'a', 'bc', '78', '23')
('a', 'bc', '78', '23', 'd', 78)

Result:

Thus the above program was successfully executed and output verified.
Dictionary

ExNo:14

Date :

Aim:

To write a program in a python creates a Dicitionary and its key value access.

Program:
# creating a dictionary
country_capitals = {"Germany": "Berlin", "Canada": "Ottawa", "England": "London"}

# printing the dictionary


print(country_capitals)

# access the value of keys


print(country_capitals["Germany"])
print(country_capitals["England"])

# add an item with "Italy" as key and "Rome" as its value


country_capitals["Italy"] = "Rome"
print(country_capitals)

# delete item having "Germany" key


del country_capitals["Germany"]

print(country_capitals)

# change the value of "Italy" key to "Naples"


country_capitals["Italy"] = "Naples"

print(country_capitals)
Output:

>>>

{'Germany': 'Berlin', 'Canada': 'Ottawa', 'England': 'London'}

Berlin

London

{'Germany': 'Berlin', 'Canada': 'Ottawa', 'England': 'London', 'Italy': 'Rome'}

{'Canada': 'Ottawa', 'England': 'London', 'Italy': 'Rome'}

{'Canada': 'Ottawa', 'England': 'London', 'Italy': 'Naples'}

>>>

Result:

Thus the above program was successfully executed and output verified.

You might also like