0% found this document useful (0 votes)
13 views

Paper-2 Lab-1 (1)

The document outlines a series of Python programming exercises aimed at introducing various programming concepts such as arithmetic operations, temperature conversions, string manipulations, and data structures like lists, tuples, sets, and dictionaries. Each exercise includes a specific aim, program code, and expected output, allowing learners to practice and understand fundamental programming skills. The exercises also cover advanced topics like exception handling, file operations, and object-oriented programming.

Uploaded by

kedarnathdas33
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)
13 views

Paper-2 Lab-1 (1)

The document outlines a series of Python programming exercises aimed at introducing various programming concepts such as arithmetic operations, temperature conversions, string manipulations, and data structures like lists, tuples, sets, and dictionaries. Each exercise includes a specific aim, program code, and expected output, allowing learners to practice and understand fundamental programming skills. The exercises also cover advanced topics like exception handling, file operations, and object-oriented programming.

Uploaded by

kedarnathdas33
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/ 18

BCA 2.

1 Lab: Introduction to Python Programming

1. Write a program to demonstrate the usage of various arithmetic operators.


2. Write a program that will convert various temperatures.
a. Fahrenheit to Centigrade
b. Centigrade to Fahrenheit
3. Write a program that will find the roots of a quadratic equation: ax² + bx + c = 0
4. Write a program that demonstrates the usage of various String functions.
5. Write a program that will ask you to enter your name, through keyboard, and perform
following operations
a. Find the middle name
b. Find the last name (using string slicing)
c. Re-write the name with surname first.
6. Write a program to find out whether the integer entered by the user, through the
keyboard, is even or odd number.
7. Find out the youngest among Shyam, Dugu and Ishan whose ages are entered by the
user through keyboard.
8. Given three points (x1, y1), (x2, y2), (x3, y3), write a program to check all the three
points fall on one straight line.
9. Write a program to demonstrate basic operations on the list.
10. Write a program to demonstrate stack and queue operations using a list of numbers.
11. Write a program to ask the data of five students that contain name, roll number, age.
Sort the list based on roll number of the Student. [Note: Use list of lists].
12. Write a program to demonstrate basic operations on the tuple.
13. Store the data about the shares held by the user as tuples containing the following
information about shares: share name, cost price, number of shares, selling price.
Write a program to determine:
a. total cost of the portfolio
b. total amount gained or lost
14. Write a program to demonstrate basic operations on the set.
15. Write a program to demonstrate basic operations on the dictionary.
16. Create a dictionary to store data (name, roll number) of N students. The key will be
the roll number of the student and the value contains the data of the student (in a list).
Write a program that asks the user to enter a name of a Student, search it in the
dictionary and print the data of the Student if it is available otherwise display an
appropriate message.
17. Write a program to demonstrate basic comprehensions on list, set and dictionary.
18. Write a program to find the factorial value of a number entered by the user using
function.
19. Write a program to find the factorial of a number using recursion.
20. Write a program to showcase use of Lambda functions, map, filter, reduce function.
21. Create a Python class called "Student" that encapsulates various attributes of a
student. Implement methods within the class to perform operations utilizing these
attributes.
22. Write a program to demonstrate both Static and Dynamic Polymorphism in Python.
23. Write a program to demonstrate exception handling mechanisms for various types of
exceptions.
24. Write a program to read texts from a file and write them into another file.
***
EXPERIMENT-1

AIM: Write a python program to demonstrate the usage of various arithmetic operators.

PROGRAM CODE:

num1 = int(input('Enter First number: '))


num2 = int(input('Enter Second number '))

add = num1 + num2


dif = num1 - num2
mul = num1 * num2
div = num1 / num2
floor_div = num1 // num2
power = num1 ** num2
modulus = num1 % num2

print('Sum of ',num1 ,'and' ,num2 ,'is :',add)


print('Difference of ',num1 ,'and' ,num2 ,'is :',dif)
print('Product of' ,num1 ,'and' ,num2 ,'is :',mul)
print('Division of ',num1 ,'and' ,num2 ,'is :',div)
print('Floor Division of ',num1 ,'and' ,num2 ,'is :',floor_div)
print('Exponent of ',num1 ,'and' ,num2 ,'is :',power)
print('Modulus of ',num1 ,'and' ,num2 ,'is :',modulus)

OUTPUT:

>>>
Enter First number: 12
Enter Second number 3
Sum of 12 and 3 is : 15
Difference of 12 and 3 is : 9
Product of 12 and 3 is : 36
Division of 12 and 3 is : 4.0
Floor Division of 12 and 3 is : 4
Exponent of 12 and 3 is : 1728
Modulus of 12 and 3 is : 0
>>>
EXPERIMENT-2(a)

AIM: Write a python program that will convert various temperatures.


a. Fahrenheit to Centigrade

PROGRAM CODE:
fahrenheit = float(input("Enter temperature in fahrenheit: "))

celsius = (fahrenheit - 32)/1.8

print(str(fahrenheit )+ " degree Fahrenheit is equal\


to " + str(celsius ) + " degree Celsius." )

OUTPUT:
>>>
Enter temperature in Fahrenheit: 140
140.0 degree Fahrenheit is equal to 60.0 degree Celsius.
>>>

EXPERIMENT-2(b)

AIM: Write a python program that will convert various temperatures.


b. Centigrade to Fahrenheit

PROGRAM CODE:

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

fahrenheit = (celsius * 1.8) + 32

print(str(celsius )+ " degree Celsius is equal to " + str(fahrenheit )+


" degree Fahrenheit.")

OUTPUT:

>>>
=== RESTART: C:/Users/HP7/AppData/Local/Programs/Python/Python39/SKM/NEP2b.py ==
Enter temperature in celsius: 60
60.0 degree Celsius is equal to 140.0 degree Fahrenheit.
>>>
EXPERIENT-3

AIM: Write a python program that will find the roots of a quadratic equation: ax² + bx + c =
0

PROGRAM CODE:

# import complex math module


import cmath
a = float(input('Enter a: '))
b = float(input('Enter b: '))
c = float(input('Enter c: '))

# calculate the discriminant


d = (b**2) - (4*a*c)

# find two solutions


sol1 = (-b-cmath.sqrt(d))/(2*a)
sol2 = (-b+cmath.sqrt(d))/(2*a)
print('The solution are {0} and {1}'.format(sol1,sol2))

OUTPUT
>>>
=== RESTART: C:\Users\HP7\AppData\Local\Programs\Python\Python39\SKM\NEP31.py
==
Enter a: 3
Enter b: 4
Enter c: 5
The solution are (-0.6666666666666666-1.1055415967851332j) and (-
0.6666666666666666+1.1055415967851332j)
>>>
EXPERIMENT-4

AIM: Write a python program that demonstrates the usage of various String functions.
.
PROGRAM CODE:
# Python3 program to show the

# working of upper() function


text = 'souMENdra KuMAr moHAnty'

# upper() function to convert


# string to upper case
print("\nConverted String:")
print(text.upper())

# lower() function to convert


# string to lower case
print("\nConverted String:")
print(text.lower())

# converts the first character to


# upper case and rest to lower case
print("\nConverted String:")
print(text.title())

# swaps the case of all characters in the string


# upper case character to lowercase and viceversa
print("\nConverted String:")
print(text.swapcase())

# convert the first character of a string to uppercase


print("\nConverted String:")
print(text.capitalize())

# original string never changes


print("\nOriginal String")
print(text)

OUTPUT:
>>>
=== RESTART: C:/Users/HP7/AppData/Local/Programs/Python/Python39/SKM/NEP4.py ===

Converted String:
SOUMENDRA KUMAR MOHANTY

Converted String:
soumendra kumar mohanty
Converted String:
Soumendra Kumar Mohanty

Converted String:
SOUmenDRA kUmaR MOhaNTY

Converted String:
Soumendra kumar mohanty

Original String
souMENdra KuMAr moHAnty
>>>
EXPERIMENT-5

AIM: Write a python program that will ask you to enter your name, through keyboard, and
perform following operations
a. Find the middle name
b. Find the last name (using string slicing)
c. Re-write the name with surname first.

PROGRAM CODE:

name = input("What is your name? ")


print('My name is: ', name)

# TO DISPLAY THE NAME


names =name.split()
if len(names) == 3:
first_name = names[0]
middle_name = names[1]
last_name = names[2]
print(f" first name - {first_name}\n middle name - {middle_name} \n last name -
{last_name}")

# (a) TO FIND MIDDLE NAME


print("The middle name is: ")
print(f" Middle name - {middle_name}")

# (b) TO FIND LAST NAME


print("The last name is: ")
print(f" Last name - {last_name}")

# (c) Re-write the name with surname first.


print("The modified name with Surname First")
print(f" {last_name} {first_name} {middle_name} ")

OUTPUT:
>>>
=== RESTART: C:\Users\HP7\AppData\Local\Programs\Python\Python39\SKM\NEP5.py ===
What is your name? SOUMENDRA KUMAR MOHANTY
My name is: SOUMENDRA KUMAR MOHANTY
first name - SOUMENDRA
middle name - KUMAR
last name - MOHANTY
The middle name is:
Middle name - KUMAR
The last name is:
Last name - MOHANTY
The modified name with Surname First
MOHANTY SOUMENDRA KUMAR
>>>
EXPERIMENT-6

AIM: Write a python program to find out whether the integer entered by the user, through the
keyboard, is even or odd number.

PROGRAM CODE:

# Prompt the user to enter a number and convert the input to an integer
num = int(input("Enter a number: "))

# Calculate the remainder when the number is divided by 2


mod = num % 2

# Check if the remainder is greater than 0, indicating an odd number


if mod > 0:
# Print a message indicating that the number is odd
print("This is an odd number.")
else:
# Print a message indicating that the number is even
print("This is an even number.")

OUTPUT:
>>>
=== RESTART: C:/Users/HP7/AppData/Local/Programs/Python/Python39/SKM/NEP6.py ===
Enter a number: 12
This is an even number.
>>>
=== RESTART: C:/Users/HP7/AppData/Local/Programs/Python/Python39/SKM/NEP6.py ===
Enter a number: 37
This is an odd number.
>>>
EXPERIMENT-7

AIM: Write a python program to find out the youngest among Shyam, Dugu and Ishan
whose ages are entered by the user through keyboard.
.

PROGRAM CODE:

age1 = int(input("Enter the Age of Shyam :"))


age2 = int(input("Enter the Age of Dugu :"))
age3 = int(input("Enter the Age of Ishan :"))
if(age1<age2 and age1<age3):
print("The Youngest Age is Shyam")
elif(age2<age1 and age2<age3):
print("The Youngest Age is Dugu")
else:
print("The Youngest Age is Ishan")

OUTPUT:

Python 3.9.1 (tags/v3.9.1:1e5d33e, Dec 7 2020, 17:08:21) [MSC v.1927 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license()" for more information.
>>>
=== RESTART: C:/Users/HP7/AppData/Local/Programs/Python/Python39/SKM/NEP7.py ===
Enter the Age of Shyam :45
Enter the Age of Dugu :34
Enter the Age of Ishan :78
The Youngest Age is Dugu
>>>
=== RESTART: C:/Users/HP7/AppData/Local/Programs/Python/Python39/SKM/NEP7.py ===
Enter the Age of Shyam :12
Enter the Age of Dugu :34
Enter the Age of Ishan :56
The Youngest Age is Shyam
>>>
=== RESTART: C:/Users/HP7/AppData/Local/Programs/Python/Python39/SKM/NEP7.py ===
Enter the Age of Shyam :65
Enter the Age of Dugu :45
Enter the Age of Ishan :23
The Youngest Age is Ishan
>>>
EXPERIMENT-9

AIM: Write a python program to demonstrate basic operations on the list.

PROGRAM CODE:

# Python code for various list operation

# declaring a list of integers

iList = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]

# List slicing operations

# printing the complete list

print('iList: ',iList)

# printing first element

print('first element: ',iList[0])

# printing fourth element

print('fourth element: ',iList[3])

# printing list elements from 0th index to 4th index

print('iList elements from 0 to 4 index:',iList[0: 5])

# printing list -7th or 3rd element from the list

print('3rd or -7th element:',iList[-7])

# appending an element to the list

iList.append(111)

print('iList after append():',iList)

# finding index of a specified element

print('index of \'80\': ',iList.index(80))

# sorting the elements of iLIst

iList.sort()

print('after sorting: ', iList);

# popping an element
print('Popped elements is: ',iList.pop())

print('after pop(): ', iList);

# removing specified element

iList.remove(80)

print('after removing \'80\': ',iList)

# inserting an element at specified index

# inserting 100 at 2nd index

iList.insert(2, 100)

print('after insert: ', iList)

# counting occurances of a specified element

print('number of occurences of \'100\': ', iList.count(100))

# extending elements i.e. inserting a list to the list

iList.extend([11, 22, 33])

print('after extending:', iList)

#reversing the list

iList.reverse()

print('after reversing:', iList)

OUTPUT:

Python 3.9.1 (tags/v3.9.1:1e5d33e, Dec 7 2020, 17:08:21) [MSC v.1927 64 bit (AMD64)] on win32

Type "help", "copyright", "credits" or "license()" for more information.

>>>

=== RESTART: C:/Users/HP7/AppData/Local/Programs/Python/Python39/SKM/NEP9.py ===

iList: [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]

first element: 10

fourth element: 40
iList elements from 0 to 4 index: [10, 20, 30, 40, 50]

3rd or -7th element: 40

iList after append(): [10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 111]

index of '80': 7

after sorting: [10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 111]

Popped elements is: 111

after pop(): [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]

after removing '80': [10, 20, 30, 40, 50, 60, 70, 90, 100]

after insert: [10, 20, 100, 30, 40, 50, 60, 70, 90, 100]

number of occurences of '100': 2

after extending: [10, 20, 100, 30, 40, 50, 60, 70, 90, 100, 11, 22, 33]

after reversing: [33, 22, 11, 100, 90, 70, 60, 50, 40, 30, 100, 20, 10]

>>>
EXPERIMENT-10

AIM: Write a python program to demonstrate stack and queue operations using a list of
numbers.

PROGRAM CODE:

# Python code to demonstrate Implementing

# stack using list

print("Given strings in the stack are:")

stack = ["Amar", "Akbar", "Anthony"]

print(stack)

print("The resulting stack elements are after appending Ram in the stack")

stack.append("Ram")

print(stack)

print("The resulting stack elements are after appending Iqbal in the stack")

stack.append("Iqbal")

print(stack)

# Removes the last item

print("The resulting stack elements are after removing Iqbal in the stack")

print(stack.pop())

print(stack)

# Removes the last item

print("The resulting stack elements are after removing Ram in the stack")

print(stack.pop())

print(stack)

# Python code to demonstrate Implementing

# Queue using list

print("Given strings in the queue are:")

queue = ["Amar", "Akbar", "Anthony"]


print("The resulting queue elements are after appending Ram in the REAR END of the queue")

queue.append("Ram")

print(queue)

print("The resulting queue elements are after appending Iqbal in the REAR END of the queue")

queue.append("Iqbal")

print(queue)

# Removes the first item

print("The resulting queue elements are after removing the first element in the queue")

print(queue.pop(0))

print(queue)

# Removes the first item

print("The resulting queue elements are after removing the first element in the queue")

print(queue.pop(0))

print(queue)

OUTPUT:

>>>

=== RESTART: C:/Users/HP7/AppData/Local/Programs/Python/Python39/SKM/NEP10.py ==

Given strings in the stack are:

['Amar', 'Akbar', 'Anthony']

The resulting stack elements are after appending Ram in the stack

['Amar', 'Akbar', 'Anthony', 'Ram']

The resulting stack elements are after appending Iqbal in the stack

['Amar', 'Akbar', 'Anthony', 'Ram', 'Iqbal']

The resulting stack elements are after removing Iqbal in the stack

Iqbal

['Amar', 'Akbar', 'Anthony', 'Ram']


The resulting stack elements are after removing Ram in the stack

Ram

['Amar', 'Akbar', 'Anthony']

Given strings in the queue are:

The resulting queue elements are after appending Ram in the REAR END of the queue

['Amar', 'Akbar', 'Anthony', 'Ram']

The resulting queue elements are after appending Iqbal in the REAR END of the queue

['Amar', 'Akbar', 'Anthony', 'Ram', 'Iqbal']

The resulting queue elements are after removing the first element in the queue

Amar

['Akbar', 'Anthony', 'Ram', 'Iqbal']

The resulting queue elements are after removing the first element in the queue

Akbar

['Anthony', 'Ram', 'Iqbal']

>>>

You might also like