0% found this document useful (0 votes)
1 views11 pages

Python Practical Unit 1

The document contains a series of practical Python programming exercises prepared by Assi. Prof. Manesh Patel for BCA Semester 5 students. It includes tasks such as swapping numbers, summing complex numbers, manipulating byte arrays, and implementing various mathematical calculations through user input and menu-driven programs.

Uploaded by

Manesh Patel
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)
1 views11 pages

Python Practical Unit 1

The document contains a series of practical Python programming exercises prepared by Assi. Prof. Manesh Patel for BCA Semester 5 students. It includes tasks such as swapping numbers, summing complex numbers, manipulating byte arrays, and implementing various mathematical calculations through user input and menu-driven programs.

Uploaded by

Manesh Patel
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/ 11

Assi. Prof.

MANESH PATEL
PRESIDENT INSTITUTE OF COMPUTER APPLICAION COLLEGE, SHAYONA CAMPUS, A’BAD.

B C A SEM: 5 Practical Python– Unit 1 Python

1. Write a program to swap two numbers without


taking a temporary variable.

a=5
b = 10

a=a+b
b=a-b
a=a-b

print("After swapping = ")


print("a =", a)
print("b =", b)

2. Write a program to display sum of two complex


numbers.

# Input complex numbers Comment Line

num1 = complex (input ("Enter the first complex number (e.g., 2+3j) = "))
num2 = complex (input ("Enter the second complex number (e.g., 4+5j)= "))

sum = num1 + num2

# Display the result

print("The sum is = ", sum)

Enter the first complex number (e.g., 2+3j) = 2+3j


Enter the second complex number (e.g., 4+5j) = 4+5j

The sum is = (6+8j)

PREPARED BY: PATEL MANESH - M.Sc(CA & IT) Contact: 90165 17796 1
PATEL MANESH

3. Write a program to create a byte type array, read,


modify, and display the elements of the array.

import array

# byte-type array (type code 'B' is for unsigned bytes: 0 to 255)

byte_array = array.array('B', [10, 20, 30, 40, 50])

# Display original array elements

print ("Original array= ")

for i in byte_array:
print(i, end=" ")
print()

# Modify an element (e.g., change the 2nd element to 200)

byte_array[1] = 200

# Read and print individual elements

print ("\n Reading elements one by one= ")


for index in range (len (byte_array)):
print (f"Element at index {index} = { byte_array[index] }")

# Display modified array

print ("\n Modified array= ")


for i in byte_array:
print (i, end=" ")
print()

s
PREPARED BY: PATEL MANESH - M.Sc(CA & IT), B.ED Contact: 90165 17796 2
PATEL MANESH

Output:

Original array=
10 20 30 40 50

Reading elements one by one=


Element at index 0 = 10
Element at index 1 = 200
Element at index 2 = 30
Element at index 3 = 40
Element at index 4 = 50

Modified array=
10 200 30 40 50

4. Create a sequence of numbers using range datatype


to display 1 to 30, with an increment of 2

# Create a sequence from 1 to 30 with step 2

sequence = range(1, 30, 2)

print ("Numbers from 1 to 30 with an increment of 2:")


for number in sequence:
print (number, end=" ")

Output:

Numbers from 1 to 30 with an increment of 2:


1 3 5 7 9 11 13 15 17 19 21 23 25 27 29

s
PREPARED BY: PATEL MANESH - M.Sc(CA & IT), B.ED Contact: 90165 17796 3
PATEL MANESH

5. Write a program to find out and display the common


and the non common elements in the list using
membership operators

list1 = [1, 2, 3, 4, 5]
list2 = [4, 5, 6, 7, 8]

# Find common elements using 'in'

common_elements = []

for item in list1:


if item in list2:
common_elements.append(item)

# Find non-common elements using 'not in'

non_common_elements = []

for item in list1:


if item not in list2:
non_common_elements.append(item)

for item in list2:


List 1: [1, 2, 3, 4, 5]
if item not in list1:
List 2: [4, 5, 6, 7, 8]
non_common_elements.append(item)
Common elements: [4, 5]
Non-common elements: [1, 2, 3, 6, 7, 8]
print("List 1:", list1)
print("List 2:", list2)
print("Common elements:", common_elements)
print("Non-common elements:", non_common_elements)

s
PREPARED BY: PATEL MANESH - M.Sc(CA & IT), B.ED Contact: 90165 17796 4
PATEL MANESH

6. Create a program to display memory locations of two


variables using id() function, and then use identity
operators two compare whether two objects are same or
not in python

a = 10
b = 10

# Display memory locations using id()

print("Memory location of a:", id(a))


print("Memory location of b:", id(b))

# Use identity operators to compare

if a is b:
print("a and b are same object ")
else:
print("a and b are not same object ")

# Create two different objects with same value

x = [1, 2, 3]
y = [1, 2, 3]

# Display memory locations

print("Memory location of x:", id(x)) Memory location of a: 11754184


print("Memory location of y:", id(y)) Memory location of b: 11754184
a and b are same object
# Use identity operators to compare
Memory location of x: 22835103451968
if x is y: Memory location of y: 22835103453760
x and y are the same object
print("x and y are the same object ")
else:
print("x and y are not the same object ")

s
PREPARED BY: PATEL MANESH - M.Sc(CA & IT), B.ED Contact: 90165 17796 5
PATEL MANESH

7. Write a program that evaluates an expression given by the


user at run time using eval() function.
Example: Enter and expression: 10+8-9*2-(10*2) Result: -20

expression = input("Enter an expression= ")

result = eval(expression)

print("Result= ", result)

8. Write a python program to find the sum of even numbers


using command line arguments.

maximum = int(input(" Please Enter the Maximum Value: "))


total = 0

for number in range(1, maximum+1):


if(number % 2 == 0):
print("{0}".format(number))
total = total + number

print("The Sum of Even Numbers from 1 to {0} = {1}".format(number, total))

Please Enter the Maximum Value: 15


2
4
6
8
10
12
14
The Sum of Even Numbers from 1 to 15 = 56

s
PREPARED BY: PATEL MANESH - M.Sc(CA & IT), B.ED Contact: 90165 17796 6
PATEL MANESH

9. Write a menu driven python program which perform the


following:
Find area of circle
Find area of triangle
Find area of square and rectangle
Find Simple Interest
Exit.
( Hint: Use infinite while loop for Menu)

import math

while True:
print("\n----- MENU -----")
print("1. Find area of Circle")
print("2. Find area of Triangle")
print("3. Find area of Square and Rectangle")
print("4. Find Simple Interest")
print("5. Exit")

choice = int(input("Enter your choice (1-5): "))

if choice == 1:
radius = float(input("Enter radius of the circle: "))
area = math.pi * radius ** 2
print(f"Area of Circle = {area:.2f}")

elif choice == 2:
base = float(input("Enter base of the triangle: "))
height = float(input("Enter height of the triangle: "))
area = 0.5 * base * height
print(f"Area of Triangle = {area:.2f}")

elif choice == 3:
print("1. Square")
print("2. Rectangle")
shape_choice = int(input("Choose shape (1 for Square, 2 for Rectangle): "))
s
PREPARED BY: PATEL MANESH - M.Sc(CA & IT), B.ED Contact: 90165 17796 7
PATEL MANESH

if shape_choice == 1:
side = float(input("Enter side of the square: "))
area = side ** 2
print(f"Area of Square = {area:.2f}")
elif shape_choice == 2:
length = float(input("Enter length of the rectangle: "))
breadth = float(input("Enter breadth of the rectangle: "))
area = length * breadth
print(f"Area of Rectangle = {area:.2f}")
else:
print("Invalid option for shape!")

elif choice == 4:
principal = float(input("Enter Principal amount: "))
rate = float(input("Enter Rate of interest: "))
time = float(input("Enter Time in years: "))
si = (principal * rate * time) / 100
print(f"Simple Interest = {si:.2f}")

elif choice == 5:
print("Exiting the program. Goodbye!")
break

else:
print("Invalid choice! Please enter a number between 1 and 5.")

s
PREPARED BY: PATEL MANESH - M.Sc(CA & IT), B.ED Contact: 90165 17796 8
PATEL MANESH

10. Write a program to assert the user enters a number


greater than zero.

try:
num = float(input("Enter a number greater than zero: "))
assert num > 0, "Number must be greater than zero!"
print(f"You entered a valid number: {num}")
except AssertionError as e:
print("AssertionError:", e)
except ValueError:
print("Invalid input! Please enter a numeric value.")

s
PREPARED BY: PATEL MANESH - M.Sc(CA & IT), B.ED Contact: 90165 17796 9
PATEL MANESH

11.Write a program to search an element in the list using for


loop and also demonstrate the use of “else” with for loop.

numbers = [10, 20, 30, 40, 50]

# Input element to search


target = int(input("Enter the number to search: "))

# Search using for loop


for num in numbers:
if num == target:
print(f"{target} found in the list.")
break
else:
# This else runs only if the loop completes without a break
print(f"{target} not found in the list.")

s
PREPARED BY: PATEL MANESH - M.Sc(CA & IT), B.ED Contact: 90165 17796 10
PATEL MANESH

12. Write a python program that asks the user to enter a


length in centimeters. If the user enters a negative length, the
program should tell the user that the entry is invalid.
Otherwise, the program should convert the length to inches
and print out the result. (2.54 = 1 inch).

cm = float(input("Enter length in centimeters: "))

# Check if the length is negative


if cm < 0:
print("Invalid entry! Length cannot be negative.")
else:
# Convert centimeters to inches (1 inch = 2.54 cm)
inches = cm / 2.54
print(f"Length in inches = {inches:.2f}")

s
PREPARED BY: PATEL MANESH - M.Sc(CA & IT), B.ED Contact: 90165 17796 11

You might also like