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

PythonLabSemester2_merged (2) (2)

The document is a practical file for a Python Programming Lab submitted by a student at Madhav Institute of Technology & Science. It includes various programming exercises covering data types, arithmetic operations, Boolean and logical operations, and string manipulations. Each program is accompanied by code snippets and expected outputs.

Uploaded by

sm5909488
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

PythonLabSemester2_merged (2) (2)

The document is a practical file for a Python Programming Lab submitted by a student at Madhav Institute of Technology & Science. It includes various programming exercises covering data types, arithmetic operations, Boolean and logical operations, and string manipulations. Each program is accompanied by code snippets and expected outputs.

Uploaded by

sm5909488
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

MADHAV INSTITUTE OF TECHNOLOGY &

SCIENCE, GWALIOR
(Deemed University)
(Declared Under Distinct Category by Ministry of
Education, Government of India)
NAAC Accredited with A++ Grade

Department of Electronics Engineering

A
Practical File
On
Python Programming Lab
(20241207)

SUBMITTED BY
SATYAM MISHRA
BTET24O1119
ET B (Batch B1)
Semester II
Electronics and Telecommunication Engineering

SUBMITTED TO
Dr. Himanshu Singh

Session: Jan – June 2025


Table of Contents

S.No. Name of the Program Date Remarks

1. Write python programming to declare various data type and


display its data type.
2. Write python programming to declare sequential data types
and display its data type.
3. Write python programming to perform addition and
subtraction and display the result.
4. Write python programming to perform multiplication and
division and display the result.
5. Write a python programming to perform Boolean operation
and display the result.
6. Write a python programming to perform logical operations
and display the result.
7. Write a python programming to declare a string, display its
different index position and also change the letter of string
with some other letter.
8. Write python programming to declare array and display its
different index position.
9. Write python programming to declare a string then:
(a) Capitalize it
(b) Convert into title format
(c) Swap the case of string.
10. Write a python programming to declare a string use slice
object to slice the given sequence to perform addition,
subtraction, multiplication and division of integer and
floating values.
PROGRAM 1: Write python programming to declare various data type
and display its data type.

CODE:
integer_var = 10
float_var = 10.5
string_var = "Hello, World!"
boolean_var = True
list_var = [1, 2, 3, 4, 5]
tuple_var = (1, 2, 3, 4, 5)
dictionary_var = {"name": "Alice", "age": 25}
set_var = {1, 2, 3, 4, 5}
none_var = None

print(f"The type of integer_var is {type(integer_var)}")


print(f"The type of float_var is {type(float_var)}")
print(f"The type of string_var is {type(string_var)}")
print(f"The type of boolean_var is {type(boolean_var)}")
print(f"The type of list_var is {type(list_var)}")
print(f"The type of tuple_var is {type(tuple_var)}")
print(f"The type of dictionary_var is {type(dictionary_var)}")
print(f"The type of set_var is {type(set_var)}")
print(f"The type of none_var is {type(none_var)}")

OUTPUT:
PROGRAM 2: Write python programming to declare sequential data
types and display its data type.

CODE:
# 1. Managing Student Records (List and Tuple)
students = [("Alice", 20, "A"), ("Bob", 22, "B"), ("Charlie", 21, "A")]
print("Student Records:")
for student in students:
name, age, grade = student
print("Name: {name}, Age: {age}, Grade: {grade}")
OUTPUT:

# 2. Generating a List of Even Numbers (Range)


even_numbers = list(range(2, 21, 2)) # Generating even numbers from 2 to 20
print("\nEven Numbers from 2 to 20:", even_numbers
OUTPUT:

# 3. Working with Text (String Operations)


(a) Splitting Strings into Words
text = "Powerful Python Programming"
words = text.split() # Splitting the text into individual words
print("\nWords in the Text:", words)
OUTPUT:

(b) Checking if the word is a palindrome


word = "madam"
is_palindrome = word == word[::-1]
print(f"\nIs '{word}' a palindrome? {is_palindrome}")
OUTPUT:
# 4. Using a Matrix to Represent Data (Nested List)
matrix = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]
print("\nMatrix Representation (2D List):")
for row in matrix:
print(row)
OUTPUT:

# 5. Using Tuples to Store GPS Coordinates


gps_coords = (37.7749, -122.4194) # San Francisco coordinates
print("\nGPS Coordinates (San Francisco):", gps_coords)
OUTPUT:

# 6. Reversing User Input


user_input = input("\nEnter a phrase to reverse: ")
reversed_input = user_input[::-1]
print("Reversed Input:", reversed_input)
OUTPUT:

# 7. Storing Immutable Sensor Data (Tuple)


sensor_data = (25.4, 26.1, 24.8, 25.9) # Example temperature readings
print("\nSensor Data Readings:", sensor_data)
OUTPUT:
# 8. Managing a To-Do List (List Operations)
to_do_list = ["Complete project", "Buy groceries", "Call Mom"]
print("Initial To-Do List:", to_do_list)

to_do_list.append("Workout") # Adding a new task


print("Updated To-Do List:", to_do_list)

to_do_list.remove("Buy groceries") # Removing a task that was completed


print("Final To-Do List:", to_do_list)
OUTPUT:

# 9. Log Data Handling Using Bytearray


log_message = bytearray("Error: Disk Full", "utf-8") # Encoding error message
print("\nInitial Log Data:", log_message)
log_message.extend(b" - Action Required")
print("Updated Log Data:", log_message)
OUTPUT:

# 10. Data Representation Using a Nested List (Dataset)


dataset = [
["ID", "Name", "Score"],
[1, "Alice", 95],
[2, "Bob", 88],
[3, "Charlie", 92]
]
print("Dataset Representation:")
for row in dataset:
print(row)
OUTPUT:
PROGRAM 3: Write python programming to perform addition and
subtraction and display the result.

CODE:
# 1. Addition Cases
print("Int + Int:", 5 + 10)
print("Float + Float:", 5.5 + 2.3)
print("Complex + Complex:", (2+3j) + (1+4j))
print("Int + Float:", 5 + 2.5)
print("Int + Complex:", 5 + (3+2j))
print("Float + Complex:", 2.5 + (1+3j))

# for Sequential data types


print("String + String:", "Hello " + "World")
print("List + List:", [1, 2, 3] + [4, 5, 6])
print("Tuple + Tuple:", (1, 2) + (3, 4))
OUTPUT:

# 2. Subtraction Cases
print("Int - Int:", 10 - 5)
print("Float - Float:", 5.5 - 2.3)
print("Complex - Complex:", (2+3j) - (1+4j))
print("Int - Float:", 5 - 2.5)
print("Int - Complex:", 5 - (3+2j))
print("Float - Complex:", 2.5 - (1+3j))
OUTPUT:
# 3. Subtraction is not supported for Strings, Lists, Tuples, and Sets
try:
print("String - String:", "Hello" - "World")
except TypeError as e:
print("Error:", e)

try:
print("List - List:", [1, 2, 3] - [4, 5, 6])
except TypeError as e:
print("Error:", e)

try:
print("Tuple - Tuple:", (1, 2) - (3, 4))
except TypeError as e:
print("Error:", e)

try:
print("Set - Set:", {1, 2, 3} - {2, 3})
except TypeError as e:
print("Error:", e)
OUTPUT:
PROGRAM 4: Write python programming to perform multiplication
and division and display the result.

CODE:
# 1. Integer Multiplication & Division
a = 20
b=7

print("Multiplication:",f"{a} * {b} = {a * b}")


print("Division:",f"{a} / {b} = {a / b}")
print("Floor division:",f"{a} // {b} = {a // b}")
print("Modulus (remainder):",f"{a} % {b} = {a % b}")
OUTPUT:

# 2. Negative integer multiplication & division


neg_a = -10
neg_b = -4

print("Multiplication:",f"{neg_a} * {neg_b} = {neg_a * neg_b}")


print("Division:",f"{neg_a} / {neg_b} = {neg_a / neg_b}")
OUTPUT:

# 3. Float Multiplication & Division


x = 10.2
y = 2.5

print("Multiplication:",f"{x} * {y} = {x * y}")


print("Division:",f"{x} / {y} = {x / y}")
OUTPUT:
# 4. Negative float multiplication & division
neg_x = -7.16
neg_y = -4.5

print("Multiplication:",f"{neg_x} * {neg_y} = {neg_x * neg_y}")


print("Division:",f"{neg_x} / {neg_y} = {neg_x / neg_y}")
OUTPUT:

# 5. Complex Number Operations


c1 = complex(2, 3) # 2 + 3j
c2 = complex(1, -1) # 1 - 1j
print("Multiplication:",f"{c1} * {c2} = {c1 * c2}")
print("Division:",f"{c1} / {c2} = {c1 / c2}")
OUTPUT:

# 6. Multiplication with Zero


a=5
x=7.4
print(f"{a} * 0 = {a * 0}")
print(f"{x} * 0 = {x * 0}")
OUTPUT:

# 7. String Multiplication (Repetition)


str_val = "Hello"
num_times = 3
print(f'"{str_val}" * {num_times} = "{str_val * num_times}"')
OUTPUT:
# 8. Boolean Multiplication & Division
bool_true = True
bool_false = False

print(f"{bool_true} * {bool_false} = {bool_true * bool_false}")


print(f"{bool_true} * 5 = {bool_true * 5}")
print(f"{bool_false} * 5 = {bool_false * 5}")

# Division with boolean


try:
print(f"{bool_true} / {bool_false} = {bool_true / bool_false}") # 1 / 0 raises error
except ZeroDivisionError:
print("Error: Cannot divide True (1) by False (0).")
OUTPUT:

# 9. Division with strings is invalid


str_val = "Python"
num_times = 3

try:
result = str_val / num_times # This will raise a TypeError
print(f'{str_val} / {num_times} = {result}')
except TypeError:
print("Error: Division is not supported for string data type.")
OUTPUT:
PROGRAM 5: Write a python programming to perform Boolean
operation and display the result.

CODE:
# 1. Boolean Operations in Python
bool1 = True
bool2 = False

print(f"{bool1} AND {bool2} = {bool1 and bool2}")


print(f"{bool1} OR {bool2} = {bool1 or bool2}")
print(f"NOT {bool1} = {not bool1}")
print(f"NOT {bool2} = {not bool2}")
OUTPUT:

# 2. Boolean Operations with Integers


int1 = 5
int2 = 0

print(f"{int1} AND {int2} = {int1 and int2}")


print(f"{int1} OR {int2} = {int1 or int2}")
print(f"NOT {int1} = {not int1}")
print(f"NOT {int2} = {not int2}")
OUTPUT:
# 3. Boolean Operations with Strings
str1 = "Python"
str2 = ""

print(f'"{str1}" AND "{str2}" = {str1 and str2}')


print(f'"{str1}" OR "{str2}" = {str1 or str2}')
print(f'NOT "{str1}" = {not str1}')
print(f'NOT "{str2}" = {not str2}')
OUTPUT:

# 4. Boolean Operations with Lists


list1 = [1, 2, 3]
list2 = []

print(f"{list1} AND {list2} = {list1 and list2}")


print(f"{list1} OR {list2} = {list1 or list2}")
print(f"NOT {list1} = {not list1}")
print(f"NOT {list2} = {not list2}")
OUTPUT:
PROGRAM 6: Write a python programming to perform logical
operations and display the result.

CODE:
# 1. Logical Operations with Boolean Values
bool1 = True
bool2 = False

print(f"{bool1} AND {bool2} = {bool1 and bool2}")


print(f"{bool1} OR {bool2} = {bool1 or bool2}")
print(f"NOT {bool1} = {not bool1}")
print(f"NOT {bool2} = {not bool2}")
OUTPUT:

# 2. Logical Operations with Integers


int1 = 7
int2 = 0

print(f"{int1} AND {int2} = {int1 and int2}")


print(f"{int1} OR {int2} = {int1 or int2}")
print(f"NOT {int1} = {not int1}")
print(f"NOT {int2} = {not int2}")
OUTPUT:
# 3. Logical Operations with Strings
str1 = "Hello"
str2 = ""

print(f'"{str1}" AND "{str2}" = {str1 and str2}')


print(f'"{str1}" OR "{str2}" = {str1 or str2}')
print(f'NOT "{str1}" = {not str1}')
print(f'NOT "{str2}" = {not str2}')
OUTPUT:

# 4. Logical Operations with Lists


list1 = [1, 2, 3]
list2 = []

print(f"{list1} AND {list2} = {list1 and list2}")


print(f"{list1} OR {list2} = {list1 or list2}")
print(f"NOT {list1} = {not list1}")
print(f"NOT {list2} = {not list2}")
OUTPUT:
# 5. Logical Operations with None Type
none_value = None
bool1 = True

print(f"{none_value} AND {bool1} = {none_value and bool1}")


print(f"{none_value} OR {bool1} = {none_value or bool1}")
print(f"NOT {none_value} = {not none_value}")
OUTPUT:

# 6. Logical Operations with Mixed Data Types


print(f"7 AND 'Hello' = {5 and 'Hello'}")
print(f"0 OR 'World' = {0 or 'World'}")
print(f"NOT [] = {not []}")
OUTPUT:

You might also like