Python Lab PDF
Python Lab PDF
UNIT 1
UNIT 2
UNIT-III
1. Write a program to sort words in a file and put them in another file.
The output file should have
only lower-case words, so any upper-case words from source must be
lowered.
2. Python program to print each line of a file in reverse order.
3. Python program to compute the number of characters, words and lines in
a file.
4. Write a program to create, display, append, insert and reverse the
order of the items in the array.
5. Write a program to add, transpose and multiply two matrices.
6. Write a Python program to create a class that represents a shape.
Include methods to calculate its
area and perimeter. Implement subclasses for different shapes like
circle, triangle, and square.
UNIT V
UNIT -1
Exponentiation: 1000.0
Floor Division: 3.0
ii) Relational Operators
Enter first number for relational operations: 10
Enter second number for relational operations: 3
a == b: False
a != b: True
a > b: True
a < b: False
a >= b: True
a <= b: False
iii) Assignment Operators
Enter a number for assignment operations: 10
Enter another number for assignment operations: 3
a += b: 13.0
a -= b: 10.0
a *= b: 30.0
a /= b: 10.0
a %= b: 1.0
a **= b: 1.0
a //= b: 0.0
iv) Logical Operators
Enter 1 for True or 0 for False for logical operations (a): 1
Enter 1 for True or 0 for False for logical operations (b): 0
a and b: False
a or b: True
not a: False
v) Bitwise Operators
Enter an integer for bitwise operations (a): 10
Enter another integer for bitwise operations (b): 3
a & b: 2
a | b: 11
a ^ b: 9
~a: -11
a << 2: 40
a >> 2: 2
vi) Ternary Operator
Enter first number for ternary operation: 10
Enter second number for ternary operation: 20
b is greater
vii) Membership Operators
Enter a list of numbers separated by space: 1 2 3 4 5
Enter a number to check membership in the list: 3
Enter another number to check membership in the list: 10
b in a: True
c not in a: True
viii) Identity Operators
a is b: True
a is c: False
a == c: True
5. Add and multiply complex numbers
Output
Enter the real part of first complex number: 2
Enter the imaginary part of first complex number: 3
Enter the real part of second complex number: 4
Enter the imaginary part of second complex number: 5
Addition of (2+3j) and (4+5j) is (6+8j)
Multiplication of (2+3j) and (4+5j) is (-7+22j)
6. Print multiplication table of a given number
Output
Enter a number to print its multiplication table: 5
5x1=5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45
5 x 10 = 50
UNIT-2
1. Define a function with multiple return values
Output
Enter first number: 10
Enter second number: 2
Sum: 12.0, Difference: 8.0, Product: 20.0, Quotient: 5.0
2. Define a function using default arguments
Output
Enter your name: Alice
Enter a greeting (optional): Hi
Hi, Alice!
Enter your name: Bob
Enter a greeting (optional):
Hello, Bob!
3. Find the length of the string without using any library functions
Output
Enter a string: OpenAI
The length of the string 'OpenAI' is 6.
4. Check if the substring is present in a given string or not
Output
Enter the main string: Hello, world!
Enter the substring: world
The substring 'world' is present in 'Hello, world!'.
Enter the main string: Hello, world!
Enter the substring: OpenAI
The substring 'OpenAI' is not present in 'Hello, world!'.
5. Perform operations on a list: addition, insertion, slicing
Output
Enter a list of numbers separated by space: 1 2 3 4 5
Updated List: [1, 20, 2, 3, 4, 5, 10]
Sliced List: [20, 2, 3]
6. Perform any 5 built-in functions by taking any list
Output
Enter a list of numbers separated by space: 3 1 4 1 5 9
Length: 6
Max Value: 9
Min Value: 1
Sum: 23
Sorted List: [1, 1, 3, 4, 5, 9]
UNIT-3
1. Create and concatenate tuples
# Output
Concatenated Tuple: ('Alice', 21, '123 Maple Street', 'XYZ College',
'Bob', 22, '456 Oak
Avenue', 'ABC University')
2. Count the number of vowels in a string (No control flow allowed)
# Output
Enter a string: OpenAI
Number of vowels: 3
3. Check if a given key exists in a dictionary or not
# Output
Enter a key to check: age
The key 'age' exists in the dictionary.
Enter a key to check: college
The key 'college' does not exist in the dictionary.
4. Add a new key-value pair to an existing dictionary
# Output
Enter a key to add: college
Enter a value to add: XYZ College
Updated Dictionary: {'name': 'Alice', 'age': 21, 'address': '123 Maple
Street', 'college': 'XYZ
College'}
5. Sum all the items in a given dictionary
# Output
Sum of all items: 600
UNIT -4
1: Sort words in a file and put them in another file
Input (`input.txt`):
Hello World
apple Banana
Orange grape
Output (`output.txt`):
apple
banana
grape
hello
orange
world
2: Print each line of a file in reverse order
Input (`input.txt`):
First line
Second line
Third line
Output:
Third line
Second line
First line
3: Compute number of characters, words, and lines in a file
Input (`input.txt`):
This is line 1.
This is line 2 with more words.
And this is line 3.
Output:
Number of lines: 3
Number of words: 16
Number of characters: 63
4: Operations on an array (create, display, append, insert, reverse)
Output:
Array: [1, 2, 3, 4, 5]
Array: [1, 2, 3, 4, 5, 6]
Array: [1, 2, 10, 3, 4, 5, 6]
Array: [6, 5, 4, 3, 10, 2, 1]
5: Add, transpose, and multiply two matrices
Output:
Matrix Addition:
[[ 6 8]
[10 12]]
Matrix Transpose:
Matrix 1:
[[1 3]
[2 4]]
Matrix 2:
[[5 7]
[6 8]]
Matrix Multiplication:
[[19 22]
[43 50]]
6: Class representing shapes with area and perimeter methods
Output:
Circle Area: 78.53981633974483
Circle Perimeter: 31.41592653589793
Triangle Area: 6.0
Triangle Perimeter: 13
Square Area: 16
Square Perimeter: 16