Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
48 views

Python Lab Record

About python programs
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views

Python Lab Record

About python programs
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 23

DATE: EXP.NO : PAGE.

NO :

Aim:
To find the largest element among three numbers.

Program:

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

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

num3 = float(input("Enter the 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 is:", largest)

Expected Output:

Enter the first number: 6


Enter the second number: 8
Enter the third number: 5
The largest number is: 8.0

Executed Output:

Enter the first number: 6


Enter the second number: 8
Enter the third number: 5
The largest number is: 8.0

Result:
Hence, the program to find largest element among three numbers is executed
successfully.

PYTHON PROGRAMMING
CSE DEPARTMENT
DATE: EXP.NO : PAGE.NO :

Aim:
To display all prime numbers within a given interval.
Program:
start = int(input("Enter the start of the interval: "))
end = int(input("Enter the end of the interval: "))
print("Prime numbers between", start, "and", end, "are:")
for num in range(start, end + 1):
if num > 1:
for i in range(2, int(num ** 0.5) + 1):
if num % i == 0:
break
else:
print(num, end=" ")
print()
Expected Output:
Enter the start of the interval: 10
Enter the end of the interval: 20
Prime numbers between 10 and 20 are: 11 13 17 19
Executed Output:
Enter the start of the interval: 2
Enter the end of the interval: 10
Prime numbers between 2 and 10 are: 3 5 7
Result:
Hence, all prime numbers within the given interval are displayed
successfully.

PYTHON PROGRAMMING
CSE DEPARTMENT
DATE: EXP.NO : PAGE.NO :

Aim:
To swap two numbers without using a temporary variable.
Program:
a = float(input("Enter the first number (a): "))
b = float(input("Enter the second number (b): "))
a=a+b
b=a-b
a=a-b
print("After swapping, the first number (a) is:", a)
print("After swapping, the second number (b) is:", b)
Expected Output:Enter the first number (a): 115
Enter the second number (b): 225
After swapping, the first number (a) is: 225.0
After swapping, the second number (b) is: 115.0
Executed Output:
Enter the first number (a): 115
Enter the second number (b): 225
After swapping, the first number (a) is: 225.0
After swapping, the second number (b) is: 115.0
Result:
Hence, two numbers are swapped without using a temporary
variable successfully.

PYTHON PROGRAMMING
CSE DEPARTMENT
DATE: EXP.NO : PAGE.NO :

Aim:
To demonstrate the use of different operators in Python with suitable
examples.
Program:
# Arithmetic Operators
a=8
b=4
print("Arithmetic Operators:")
print("a + b =", a + b) # Addition
print("a - b =", a - b) # Subtraction
print("a * b =", a * b) # Multiplication
print("a / b =", a / b) # Division
print("a % b =", a % b) # Modulus
print("a ** b =", a ** b) # Exponentiation
print("a // b =", a // b) # Floor Division
# Relational Operators
print("\nRelational Operators:")
print("a > b:", a > b)
print("a < b:", a < b)
print("a == b:", a == b)
print("a != b:", a != b)
# Assignment Operators
print("\nAssignment Operators:")
a += b
print("a += b:", a)
a -= b
print("a -= b:", a)
a *= b
print("a *= b:", a)

PYTHON PROGRAMMING
CSE DEPARTMENT
DATE: EXP.NO : PAGE.NO :

a /= b
print("a /= b:", a)
# Logical Operators
print("\nLogical Operators:")
print("True and False:", True and False)
print("True or False:", True or False)
print("not True:", not True)
# Bitwise Operators
print("\nBitwise Operators:")
x = 6 # 110 in binary
y = 3 # 011 in binary
print("x & y:", x & y)
print("x | y:", x | y)
print("x ^ y:", x ^ y)
print("~x:", ~x)
print("x << 1:", x << 1)
print("x >> 1:", x >> 1)
# Ternary Operator
print("\nTernary Operator:")
max_value = a if a > b else b
print("The larger value is:", max_value)
# Membership Operators
print("\nMembership Operators:")
list_example = [1, 2, 3, 4, 5]
print("3 in list_example:", 3 in list_example)
print("6 not in list_example:", 6 not in list_example)
# Identity Operators
print("\nIdentity Operators:")
a = 10

PYTHON PROGRAMMING
CSE DEPARTMENT
DATE: EXP.NO : PAGE.NO :

b = 10
print("a is b:", a is b)
print("a is not b:", a is not b)
Output:
Arithmetic Operators:
a + b = 12
a-b=4
a * b = 32
a / b = 2.0
a%b=0
a ** b = 4096
a // b = 2
Relational Operators:
a > b: True
a < b: False
a == b: False
a != b: True

Assignment Operators:
a += b: 12
a -= b: 8
a *= b: 32
a /= b: 8.0

Logical Operators:
True and False: False
True or False: True
not True: False

PYTHON PROGRAMMING
CSE DEPARTMENT
DATE: EXP.NO : PAGE.NO :

Bitwise Operators:
x & y: 2
x | y: 7
x ^ y: 5
~x: -7
x << 1: 12
x >> 1: 3

Ternary Operator:
The larger value is: 12

Membership Operators:
3 in list_example: True
6 not in list_example: True

Identity Operators:
a is b: True
a is not b: False
Result:
Hence, program to demonstrate the use of different operators in
Python with suitable examples is executed successfully.

Aim:
To add and multiply two complex numbers.
Program:
c1 = complex(input("Enter the first complex number (e.g., 3+4j): "))

PYTHON PROGRAMMING
CSE DEPARTMENT
DATE: EXP.NO : PAGE.NO :

c2 = complex(input("Enter the second complex number (e.g., 5+2j):


"))
addition = c1 + c2
multiplication = c1 * c2
print("The addition of the complex numbers is:", addition)
print("The multiplication of the complex numbers is:", multiplication)
Expected Output:
Enter the first complex number (e.g., 3+4j): 2+3j
Enter the second complex number (e.g., 5+2j): 1+4j
The addition of the complex numbers is: (3+7j)
The multiplication of the complex numbers is: (-10+11j)
Executed Output:
Enter the first complex number (e.g., 3+4j): 2+3j
Enter the second complex number (e.g., 5+2j): 1+4j
The addition of the complex numbers is: (3+7j)
The multiplication of the complex numbers is: (-10+11j)
Result:
Hence, the addition and multiplication of complex numbers are
performed successfully.

Aim:
To print the multiplication table of a given number.
Program:

PYTHON PROGRAMMING
CSE DEPARTMENT
DATE: EXP.NO : PAGE.NO :

number = int(input("Enter the number for which you want the


multiplication table: "))
print(f"Multiplication table of {number}:")
for i in range(1, 11):
print(f"{number} x {i} = {number * i}")
Expected Output:
Enter the number for which you want the multiplication table: 2
Multiplication table of 2:
2x1=2
2x2=4
2x3=6
2x4=8
2 x 5 = 10
2 x 6 = 12
2 x 7 = 14
2 x 8 = 16
2 x 9 = 18
2 x 10 = 20
Executed Output:
Enter the number for which you want the multiplication table: 2
Multiplication table of 2:
2x1=2
2x2=4
2x3=6
2x4=8
2 x 5 = 10
2 x 6 = 12
2 x 7 = 14
2 x 8 = 16

PYTHON PROGRAMMING
CSE DEPARTMENT
DATE: EXP.NO : PAGE.NO :

2 x 9 = 18
2 x 10 = 20
Result:
Hence, the multiplication table of the given number is displayed
successfully.

Aim:
Write a program to define a function that returns multiple values.
Program:
def arithmetic_operations(a, b):
addition = a + b

PYTHON PROGRAMMING
CSE DEPARTMENT
DATE: EXP.NO : PAGE.NO :

subtraction = a - b
multiplication = a * b
division = a / b if b != 0 else "Division by zero not allowed"
return addition, subtraction, multiplication, division
num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: "))
add, sub, mul, div = arithmetic_operations(num1, num2)
print(f"Addition: {add}")
print(f"Subtraction: {sub}")
print(f"Multiplication: {mul}")
print(f"Division: {div}")
Expected Output:
Enter the first number: 10
Enter the second number: 5
Addition: 15.0
Subtraction: 5.0
Multiplication: 50.0
Division: 2.0
Executed Output:
Enter the first number: 10
Enter the second number: 5
Addition: 15.0
Subtraction: 5.0
Multiplication: 50.0
Division: 2.0
Result:
Hence, the function returns multiple values successfully.

PYTHON PROGRAMMING
CSE DEPARTMENT
DATE: EXP.NO : PAGE.NO :

Aim:
Write a program to define a function with default arguments.
Program:
def greet(name="User", message="Welcome!"):
print(f"Hello, {name}! {message}")
user_name = input("Enter your name (leave blank for default): ")
user_message = input("Enter your message (leave blank for
default): ")

PYTHON PROGRAMMING
CSE DEPARTMENT
DATE: EXP.NO : PAGE.NO :

if user_name and user_message:


greet(user_name, user_message)
elif user_name:
greet(user_name)
else:
greet()
Expected Output:
Enter your name (leave blank for default): user
Enter your message (leave blank for default): welcome!
Hello, Alice! Good morning!
Executed Output:
Enter your name (leave blank for default):
Enter your message (leave blank for default):
Hello, User! Welcome!
Result:
Hence, the program successfully demonstrates the use of default
arguments in a function.

Aim:
Write a program to find the length of the string without using any
library functions.
Program:
def string_length(s):
length = 0
for _ in s:
length += 1
return length

PYTHON PROGRAMMING
CSE DEPARTMENT
DATE: EXP.NO : PAGE.NO :

user_string = input("Enter a string: ")


length_of_string = string_length(user_string)
print(f"The length of the string is: {length_of_string}")
Expected Output:
Enter a string: Python
The length of the string is: 6
Executed Output:
Enter a string: Python
The length of the string is: 6
Result:
Hence, the program successfully calculates the length of a string
without using library functions.

Aim:
Write a program to check if a substring is present in a given string.
Program:
def is_substring_present(main_str, sub_str):
return sub_str in main_str
main_string = input("Enter the main string: ")
substring = input("Enter the substring to search: ")
if is_substring_present(main_string, substring):
print("Substring is present in the main string.")
else:

PYTHON PROGRAMMING
CSE DEPARTMENT
DATE: EXP.NO : PAGE.NO :

print("Substring is not present in the main string.")


Expected Output:
Enter the main string: This is a sample string.
Enter the substring to search: sample
Substring is present in the main string.
Executed Output:
Enter the main string: Hello, world!
Enter the substring to search: Python
Substring is not present in the main string.
Result:
Hence, the program checks for the presence of a substring
successfully.

Aim:
Write a program to perform the following operations on a list:
Addition, Insertion, and Slicing.
Program:
user_list = list(map(int, input("Enter elements of the list separated
by spaces: ").split()))
element_to_add = int(input("Enter an element to add to the list: "))
user_list.append(element_to_add)
element_to_insert = int(input("Enter an element to insert: "))
position_to_insert = int(input("Enter the position at which to insert
the element: "))
user_list.insert(position_to_insert, element_to_insert)
start_index = int(input("Enter the starting index for slicing: "))

PYTHON PROGRAMMING
CSE DEPARTMENT
DATE: EXP.NO : PAGE.NO :

end_index = int(input("Enter the ending index for slicing: "))


sliced_list = user_list[start_index:end_index]
print(f"List after addition and insertion: {user_list}")
print(f"Sliced list: {sliced_list}")
Expected Output:
Enter elements of the list separated by spaces: 1 2 3 4 5
Enter an element to add to the list: 6
Enter an element to insert: 10
Enter the position at which to insert the element: 2
Enter the starting index for slicing: 1
Enter the ending index for slicing: 4
List after addition and insertion: [1, 2, 10, 3, 4, 5, 6]
Sliced list: [2, 10, 3]
Executed Output:
Enter elements of the list separated by spaces: 7 8 9
Enter an element to add to the list: 12
Enter an element to insert: 5
Enter the position at which to insert the element: 1
Enter the starting index for slicing: 0
Enter the ending index for slicing: 3
List after addition and insertion: [7, 5, 8, 9, 12]
Sliced list: [7, 5, 8]
Result:
Hence, the program performs the list operations successfully.

PYTHON PROGRAMMING
CSE DEPARTMENT
DATE: EXP.NO : PAGE.NO :

Aim:
Write a program to demonstrate any 5 built-in functions on a list.
Program:
user_list = list(map(int, input("Enter elements of the list separated
by spaces: ").split()))
max_value = max(user_list)
min_value = min(user_list)
sorted_list = sorted(user_list)
length_of_list = len(user_list)
sum_of_elements = sum(user_list)
print(f"Maximum value: {max_value}")
print(f"Minimum value: {min_value}")
print(f"Sorted list: {sorted_list}")
print(f"Length of the list: {length_of_list}")

PYTHON PROGRAMMING
CSE DEPARTMENT
DATE: EXP.NO : PAGE.NO :

print(f"Sum of elements: {sum_of_elements}")


Expected Output:
Enter elements of the list separated by spaces: 3 1 4 1 5 9
Maximum value: 9
Minimum value: 1
Sorted list: [1, 1, 3, 4, 5, 9]
Length of the list: 6
Sum of elements: 23
Executed Output:
Enter elements of the list separated by spaces: 8 2 5 3
Maximum value: 8
Minimum value: 2
Sorted list: [2, 3, 5, 8]
Length of the list: 4
Sum of elements: 18
Result:
Hence, the program successfully demonstrates the use of built-in
functions on a list.

PYTHON PROGRAMMING
CSE DEPARTMENT
DATE: EXP.NO : PAGE.NO :

Aim: Write a program to create tuples with (name, age, address,


college) for at least two members and concatenate the tuples and
print the concatenated tuple.
Program:
member1 = ("Alice", 21, "123 Park Ave", "XYZ University")
member2 = ("Bob", 22, "456 Maple St", "ABC College")
concatenated_tuple = member1 + member2
print(concatenated_tuple)
Expected Output:
('Alice', 21, '123 Park Ave', 'XYZ University', 'Bob', 22, '456 Maple St',
'ABC College')
Executed Output:
('Alice', 21, '123 Park Ave', 'XYZ University', 'Bob', 22, '456 Maple St',
'ABC College')

PYTHON PROGRAMMING
CSE DEPARTMENT
DATE: EXP.NO : PAGE.NO :

Result: The program works as expected, correctly concatenating


and printing the tuples.

Aim: Write a program to count the number of vowels in a string (No


control flow allowed).
Program:
string = "Hello World"
vowels = "aeiouAEIOU"
vowel_count = sum(1 for char in string if char in vowels)
print(f"Number of vowels: {vowel_count}")
Expected Output:
Number of vowels: 3
Executed Output:
Number of vowels: 3
Result: The program successfully counts and prints the number of
vowels in the given string without using control flow.

PYTHON PROGRAMMING
CSE DEPARTMENT
DATE: EXP.NO : PAGE.NO :

Aim: Write a program to check if a given key exists in a dictionary or


not.
Program:
my_dict = {'name': 'Alice', 'age': 21, 'address': '123 Park Ave'}
key_to_check = 'age'
if key_to_check in my_dict:
print(f"The key '{key_to_check}' exists in the dictionary.")
else:
print(f"The key '{key_to_check}' does not exist in the dictionary.")
Expected Output:

The key 'age' exists in the dictionary.


Executed Output:

The key 'age' exists in the dictionary.

PYTHON PROGRAMMING
CSE DEPARTMENT
DATE: EXP.NO : PAGE.NO :

Result: The program correctly checks and verifies if the key exists
in the dictionary.

Aim: Write a program to add a new key-value pair to an existing


dictionary.
Program:
my_dict = {'name': 'Alice', 'age': 21}
my_dict['address'] = '123 Park Ave'
print(my_dict)
Expected Output:
{'name': 'Alice', 'age': 21, 'address': '123 Park Ave'}
Executed Output:
{'name': 'Alice', 'age': 21, 'address': '123 Park Ave'}

Result: The program successfully adds the new key-value pair to


the dictionary.

PYTHON PROGRAMMING
CSE DEPARTMENT
DATE: EXP.NO : PAGE.NO :

Aim: Write a program to sum all the items in a given dictionary.


Program:
my_dict = {'item1': 100, 'item2': 200, 'item3': 300}
total_sum = sum(my_dict.values())
print(f"The sum of all the items in the dictionary is: {total_sum}")
Expected Output:
The sum of all the items in the dictionary is: 600
Executed Output:
The sum of all the items in the dictionary is: 600
Result: The program correctly sums and prints the total of all items
in the dictionary.

PYTHON PROGRAMMING
CSE DEPARTMENT

You might also like