A Practical File of
"Pyth on Programming Lab "
Submitted In Practical Fulfillment of the requirements for the award of degree of
Bechlor of Technology
In
Computer Science & Engineering
From
Government Engineering College, Bikaner
Affiliated to
Bikaner Technical University, Bikaner
(Session: 2025-26)
Submitted By: - Submitted To: -
________________________________
Dr. Ranu Lal Chou h an
Assistant Professor
University Roll Number: - Department of Computer
Science & Engineering (ECB)
________________________________
Python Programming Lab (4CS4-23)
Contents
1 Python Programming Theory 2
1.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.2 Features of Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.3 Variables and Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.4 Control Flow . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.5 Functions and Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
2 Lab Programs and Outputs 4
2.1 Program 1: Hello World and Basic Arithmetic . . . . . . . . . . . . . . . 4
2.2 Program 2: Data Type Conversion (Casting) . . . . . . . . . . . . . . . . 5
2.3 Program 3: String Methods and Slicing . . . . . . . . . . . . . . . . . . . 6
2.4 Program 4: List Operations . . . . . . . . . . . . . . . . . . . . . . . . . 7
2.5 Program 5: Tuple and Set Operations . . . . . . . . . . . . . . . . . . . . 8
2.6 Program 6: Dictionary Operations . . . . . . . . . . . . . . . . . . . . . . 9
2.7 Program 7: Largest of Three Numbers . . . . . . . . . . . . . . . . . . . 10
2.8 Program 8: Prime Numbers in Range . . . . . . . . . . . . . . . . . . . . 11
2.9 Program 9: Fibonacci Series . . . . . . . . . . . . . . . . . . . . . . . . . 12
2.10 Program 10: Math and Random Modules . . . . . . . . . . . . . . . . . . 13
2.11 Program 11: Functions with Arguments . . . . . . . . . . . . . . . . . . . 14
2.12 Program 12: Lambda, Map, and Filter . . . . . . . . . . . . . . . . . . . 15
2.13 Program 13: File Writing . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
2.14 Program 14: File Word Count . . . . . . . . . . . . . . . . . . . . . . . . 17
2.15 Program 15: File Copy . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
2.16 Program 16: Matrix Addition . . . . . . . . . . . . . . . . . . . . . . . . 19
2.17 Program 17: Matrix Transpose . . . . . . . . . . . . . . . . . . . . . . . 20
2.18 Program 18: NumPy Basics . . . . . . . . . . . . . . . . . . . . . . . . . 21
2.19 Program 19: NumPy Concatenate . . . . . . . . . . . . . . . . . . . . . . 22
2.20 Program 20: Pandas Basics . . . . . . . . . . . . . . . . . . . . . . . . . 23
Page 1
Python Programming Lab (4CS4-23)
1 Python Programming Theory
1.1 Introduction
Python is a high-level, interpreted, general-purpose programming language. Created by
Guido van Rossum and rst released in 1991, Python's design philosophy emphasizes
code readability with its notable use of signicant whitespace. Its language constructs
and object-oriented approach aim to help programmers write clear, logical code for small
and large-scale projects.
1.2 Features of Python
Simple and Easy to Learn: Python has a very simple and elegant syntax. It is
much easier to read and write Python programs compared to other languages like
C++, Java, or C#.
Interpreted Language: Python code is executed line by line. This makes debug-
ging easy and thus suitable for beginners.
Portable: Python programs can run on any platform (Windows, macOS, Linux)
without needing any changes.
Extensive Libraries: Python has a large standard library that provides modules
and functions for various tasks like web development, data analysis, and machine
learning.
Free and Open Source: Python is freely available and its source code can be
modied and distributed.
1.3 Variables and Data Types
In Python, variables are created when you assign a value to them. Python is dynamically
typed, so you don't need to declare the type of a variable.
Common Data Types:
1. Numeric: int (integers), oat (decimal numbers), complex.
2. Sequence: list (mutable), tuple (immutable), range.
3. Text: str (strings).
4. Mapping: dict (key-value pairs).
5. Set: set (unique elements).
6. Boolean: bool (True/False).
1.4 Control Flow
Python uses standard control ow statements:
Conditional Statements: if, elif, and else are used for decision making.
Loops: for loops are used for iterating over a sequence, and while loops are
used to repeat a block of code as long as a condition is true.
Page 2
Python Programming Lab (4CS4-23)
1.5 Functions and Modules
Functions are blocks of code that run only when called. They help in code reusability.
Modules are les containing Python code (functions, variables) that can be imported and
used in other programs.
Page 3
Python Programming Lab (4CS4-23)
2 Lab Programs and Outputs
2.1 Program 1: Hello World and Basic Arithmetic
Problem Statement: Write a program to print "Hello World" and perform basic arith-
metic operations.
1 # Program 1: Hello World and Basic Math
2 # Just printing hello and doing some math
3
4 print("Hello World")
5
6 num1 = 20
7 num2 = 10
8
9 print("First number:", num1)
10 print("Second number:", num2)
11
12 # Doing arithmetic
13 print("Addition:", num1 + num2)
14 print("Subtraction:", num1 - num2)
15 print("Multiplication:", num1 * num2)
16 print("Division:", num1 / num2)
17 print("Remainder:", num1 % num2)
18 print("Power:", num1 ** num2)
19 print("Floor Division:", num1 // num2)
Output:
Hello World
First number: 20
Second number: 10
Addition: 30
Subtraction: 10
Multiplication: 200
Division: 2.0
Remainder: 0
Power: 10240000000000
Floor Division: 2
Page 4
Python Programming Lab (4CS4-23)
2.2 Program 2: Data Type Conversion (Casting)
Problem Statement: Write a program to demonstrate data type conversion between
int, oat, and string.
1 # Program 2: Changing Data Types
2 # Using int, float, and str to change types
3
4 # Integer to float and string
5 a = 10
6 print("Original number:", a)
7 print(type(a))
8
9 b = float(a)
10 print("Changed to float:", b)
11 print(type(b))
12
13 c = str(a)
14 print("Changed to string:", c)
15 print(type(c))
16
17 # Float to integer
18 d = 3.14
19 print("\nFloat value:", d)
20 e = int(d)
21 print("Changed to int:", e)
22
23 # String to float
24 f = "50"
25 print("\nString value:", f)
26 g = float(f)
27 print("Changed to float:", g)
Output:
Original number: 10
<class ’int’>
Changed to float: 10.0
<class ’float’>
Changed to string: 10
<class ’str’>
Float value: 3.14
Changed to int: 3
String value: 50
Changed to float: 50.0
Page 5
Python Programming Lab (4CS4-23)
2.3 Program 3: String Methods and Slicing
Problem Statement: Write a program to demonstrate String indexing, slicing, and
built-in methods (strip, lower, upper, replace).
1 # Program 3: String Operations
2 # Demonstrating slicing, indexing, and strip, lower, upper
methods
3
4 text = " HELLO PYTHON "
5 print("Original text:", text)
6
7 # Using strip to remove spaces
8 print("After strip:", [Link]())
9
10 # Using lower to make it small letters
11 print("Lower case:", [Link]())
12
13 # Using upper to make it big letters
14 print("Upper case:", [Link]())
15
16 # Using replace to change some letters
17 print("Replace HELLO with HI:", [Link]("HELLO", "HI"))
18
19 # Using indexing and slicing
20 print("\nFirst character:", text[2])
21 print("Characters from 2 to 7:", text[2:7])
22 print("Characters from 8 to 14:", text[8:14])
Output:
Original text: HELLO PYTHON
After strip: HELLO PYTHON
Lower case: hello python
Upper case: HELLO PYTHON
Replace HELLO with HI: HI PYTHON
First character: H
Characters from 2 to 7: HELLO
Characters from 8 to 14: PYTHON
Page 6
Python Programming Lab (4CS4-23)
2.4 Program 4: List Operations
Problem Statement: Write a program to perform List operations: appending, insert-
ing, removing, and sorting.
1 # Program 4: List Operations
2 # Making a list and doing things to it
3
4 my_list = [10, 20, 30, 40]
5 print("Original list:", my_list)
6
7 # Adding to list
8 my_list.append(50)
9 print("After append 50:", my_list)
10
11 # Inserting at a spot
12 my_list.insert(1, 15)
13 print("After inserting 15 at index 1:", my_list)
14
15 # Removing from list
16 my_list.remove(30)
17 print("After removing 30:", my_list)
18
19 # Sorting the list
20 my_list.sort(reverse=True)
21 print("After sorting reverse:", my_list)
Output:
Original list: [10, 20, 30, 40]
After append 50: [10, 20, 30, 40, 50]
After inserting 15 at index 1: [10, 15, 20, 30, 40, 50]
After removing 30: [10, 15, 20, 40, 50]
After sorting reverse: [50, 40, 20, 15, 10]
Page 7
Python Programming Lab (4CS4-23)
2.5 Program 5: Tuple and Set Operations
Problem Statement: Write a program to demonstrate Tuple and Set operations (union,
intersection, dierence).
1 # Program 5: Tuples and Sets
2 # Showing how tuples and sets work
3
4 # Tuples are like lists but you can’t change them
5 my_tuple = (1, 2, 3, 4, 5)
6 print("My Tuple:", my_tuple)
7
8 # Sets don’t allow duplicates
9 set1 = {1, 2, 3, 4}
10 set2 = {3, 4, 5, 6}
11 print("\nSet 1:", set1)
12 print("Set 2:", set2)
13
14 # Set operations
15 print("Union:", [Link](set2))
16 print("Intersection:", [Link](set2))
17 print("Difference (Set1 - Set2):", [Link](set2))
Output:
My Tuple: (1, 2, 3, 4, 5)
Set 1: {1, 2, 3, 4}
Set 2: {3, 4, 5, 6}
Union: {1, 2, 3, 4, 5, 6}
Intersection: {3, 4}
Difference (Set1 - Set2): {1, 2}
Page 8
Python Programming Lab (4CS4-23)
2.6 Program 6: Dictionary Operations
Problem Statement: Write a program to demonstrate Dictionary operations: access-
ing, adding, and removing key-value pairs.
1 # Program 6: Dictionary Operations
2 # Creating and changing dictionaries
3
4 # My dictionary with name and age
5 my_dict = {"name": "Aman", "age": 20, "city": "Bikaner"}
6 print("Full Dictionary:", my_dict)
7
8 # Getting one value
9 print("Name from dict:", my_dict["name"])
10
11 # Adding a new key and value
12 my_dict["roll_no"] = 101
13 print("After adding roll_no:", my_dict)
14
15 # Deleting a key
16 del my_dict["city"]
17 print("After deleting city:", my_dict)
Output:
Full Dictionary: {’name’: ’Aman’, ’age’: 20, ’city’: ’Bikaner’}
Name from dict: Aman
After adding roll_no: {’name’: ’Aman’, ’age’: 20, ’city’: ’Bikaner’, ’rol
After deleting city: {’name’: ’Aman’, ’age’: 20, ’roll_no’: 101}
Page 9
Python Programming Lab (4CS4-23)
2.7 Program 7: Largest of Three Numbers
Problem Statement: Write a program to nd the largest of three numbers using if-else.
1 # Program 7: Largest of 3 Numbers
2 # Checking which number is bigger using if-else
3
4 num1 = 15
5 num2 = 25
6 num3 = 10
7
8 print("Number 1:", num1)
9 print("Number 2:", num2)
10 print("Number 3:", num3)
11
12 if num1 >= num2 and num1 >= num3:
13 print("\nNumber 1 is the biggest")
14 elif num2 >= num1 and num2 >= num3:
15 print("\nNumber 2 is the biggest")
16 else:
17 print("\nNumber 3 is the biggest")
Output:
Number 1: 15
Number 2: 25
Number 3: 10
Number 2 is the biggest
Page 10
Python Programming Lab (4CS4-23)
2.8 Program 8: Prime Numbers in Range
Problem Statement: Write a program to print all prime numbers in a given range
using a for loop.
1 # Program 8: Prime Numbers in a Range
2 # Printing all prime numbers from 1 to 20
3
4 start = 1
5 end = 20
6 print("Prime numbers between 1 and 20 are:")
7
8 for num in range(start, end + 1):
9 if num > 1:
10 # Check for factors
11 is_prime = True
12 for i in range(2, num):
13 if (num % i) == 0:
14 is_prime = False
15 break
16 if is_prime:
17 print(num)
Output:
Prime numbers between 1 and 20 are:
2
3
5
7
11
13
17
19
Page 11
Python Programming Lab (4CS4-23)
2.9 Program 9: Fibonacci Series
Problem Statement: Write a program to generate the Fibonacci series using a while
loop.
1 # Program 9: Fibonacci Series
2 # Printing numbers in fibonacci sequence up to 50
3
4 # First two numbers
5 num1 = 0
6 num2 = 1
7
8 print("Fibonacci series up to 50:")
9 print(num1)
10 print(num2)
11
12 # Next numbers
13 next_num = num1 + num2
14 while next_num <= 50:
15 print(next_num)
16 num1 = num2
17 num2 = next_num
18 next_num = num1 + num2
Output:
Fibonacci series up to 50:
0
1
1
2
3
5
8
13
21
34
Page 12
Python Programming Lab (4CS4-23)
2.10 Program 10: Math and Random Modules
Problem Statement: Write a program to demonstrate the use of math and random
modules.
1 # Program 10: Using math and random modules
2 # Just showing how to import and use them
3
4 import math
5 import random
6
7 # Math module
8 print("Square root of 25 is:", [Link](25))
9 print("Ceil of 3.4 is:", [Link](3.4))
10 print("Floor of 3.4 is:", [Link](3.4))
11
12 # Random module
13 print("\nRandom number between 1 and 100:", [Link](1,
100))
14 fruits = ["apple", "banana", "cherry"]
15 print("Random choice from fruits:", [Link](fruits))
Output:
Square root of 25 is: 5.0
Ceil of 3.4 is: 4
Floor of 3.4 is: 3
Random number between 1 and 100: 69
Random choice from fruits: cherry
Page 13
Python Programming Lab (4CS4-23)
2.11 Program 11: Functions with Arguments
Problem Statement: Write a program to create functions with positional, keyword,
and default arguments.
1 # Program 11: Simple Functions
2 # Creating functions with different arguments
3
4 # Simple function with default value
5 def welcome(name="User"):
6 print("Hello", name)
7
8 # Function with two arguments
9 def add_nums(a, b):
10 print("Addition of", a, "and", b, "is:", a + b)
11
12 # Calling functions
13 welcome("Aman")
14 welcome()
15 add_nums(10, 20)
16 add_nums(b=30, a=5) # Keyword arguments
Output:
Hello Aman
Hello User
Addition of 10 and 20 is: 30
Addition of 5 and 30 is: 35
Page 14
Python Programming Lab (4CS4-23)
2.12 Program 12: Lambda, Map, and Filter
Problem Statement: Write a program to demonstrate the use of Lambda functions
with map() and lter().
1 # Program 12: Lambda functions
2 # Using lambda with map and filter
3
4 # A simple list
5 nums = [1, 2, 3, 4, 5, 6]
6 print("Original numbers:", nums)
7
8 # map: double all numbers
9 doubled = list(map(lambda x: x * 2, nums))
10 print("Doubled numbers:", doubled)
11
12 # filter: only even numbers
13 evens = list(filter(lambda x: x % 2 == 0, nums))
14 print("Even numbers:", evens)
Output:
Original numbers: [1, 2, 3, 4, 5, 6]
Doubled numbers: [2, 4, 6, 8, 10, 12]
Even numbers: [2, 4, 6]
Page 15
Python Programming Lab (4CS4-23)
2.13 Program 13: File Writing
Problem Statement: Write a program to create a new text le and write multiple lines
into it.
1 # Program 13: Writing to a File
2 # Creating a file and writing some lines in it
3
4 # Creating file
5 f = open("[Link]", "w")
6 [Link]("Hello, this is my first file.\n")
7 [Link]("I am writing multiple lines.\n")
8 [Link]("Python is very easy to learn.\n")
9 [Link]()
10
11 print("File ’[Link]’ created successfully!")
Output:
File ’[Link]’ created successfully!
Page 16
Python Programming Lab (4CS4-23)
2.14 Program 14: File Word Count
Problem Statement: Write a program to read a le and count the number of lines,
words, and characters.
1 # Program 14: Count lines, words, and characters
2 # Reading from a file and counting things
3
4 # Open file for reading
5 f = open("[Link]", "r")
6
7 lines = 0
8 words = 0
9 chars = 0
10
11 for line in f:
12 lines = lines + 1
13 words = words + len([Link]())
14 chars = chars + len(line)
15
16 print("Total lines:", lines)
17 print("Total words:", words)
18 print("Total characters:", chars)
19
20 [Link]()
Output:
Total lines: 3
Total words: 17
Total characters: 89
Page 17
Python Programming Lab (4CS4-23)
2.15 Program 15: File Copy
Problem Statement: Write a program to copy the contents of one le to another using
le handling.
1 # Program 15: Copy File
2 # Reading from one file and writing to another
3
4 # Source file
5 f1 = open("[Link]", "r")
6
7 # Destination file
8 f2 = open("[Link]", "w")
9
10 # Reading and writing
11 for line in f1:
12 [Link](line)
13
14 [Link]()
15 [Link]()
16
17 print("File copied successfully!")
Output:
File copied successfully!
Page 18
Python Programming Lab (4CS4-23)
2.16 Program 16: Matrix Addition
Problem Statement: Write a program to perform Matrix Addition using nested lists.
1 # Program 16: Matrix Addition
2 # Adding two matrices using nested lists
3
4 # Matrix 1 (2x2)
5 X = [[1, 2],
6 [3, 4]]
7
8 # Matrix 2 (2x2)
9 Y = [[5, 6],
10 [7, 8]]
11
12 # Result matrix (initially all 0)
13 result = [[0, 0],
14 [0, 0]]
15
16 # Addition
17 for i in range(2):
18 for j in range(2):
19 result[i][j] = X[i][j] + Y[i][j]
20
21 print("Addition of two matrices:")
22 for row in result:
23 print(row)
Output:
Addition of two matrices:
[6, 8]
[10, 12]
Page 19
Python Programming Lab (4CS4-23)
2.17 Program 17: Matrix Transpose
Problem Statement: Write a program to calculate the Transpose of a matrix.
1 # Program 17: Transpose of a Matrix
2 # Flipping a matrix rows to columns
3
4 # A 2x3 matrix
5 matrix = [[1, 2, 3],
6 [4, 5, 6]]
7
8 # Result matrix (initially all 0)
9 # It should be 3x2
10 result = [[0, 0],
11 [0, 0],
12 [0, 0]]
13
14 # Transpose
15 for i in range(2):
16 for j in range(3):
17 result[j][i] = matrix[i][j]
18
19 print("Original matrix:")
20 for row in matrix:
21 print(row)
22
23 print("\nTranspose of matrix:")
24 for row in result:
25 print(row)
Output:
Original matrix:
[1, 2, 3]
[4, 5, 6]
Transpose of matrix:
[1, 4]
[2, 5]
[3, 6]
Page 20
Python Programming Lab (4CS4-23)
2.18 Program 18: NumPy Basics
Problem Statement: Write a program to demonstrate NumPy array creation, slicing,
and reshaping.
1 # Program 18: NumPy Basics
2 # Creating arrays and reshaping
3
4 import numpy as np
5
6 # Create 1D array
7 arr = [Link]([1, 2, 3, 4, 5, 6])
8 print("Original Array:", arr)
9
10 # Reshape to 2D
11 arr_2d = [Link](2, 3)
12 print("\nReshaped Array (2x3):")
13 print(arr_2d)
14
15 # Slicing
16 print("\nSlicing (first 3 elements):", arr[:3])
Output:
Original Array: [1 2 3 4 5 6]
Reshaped Array (2x3):
[[1 2 3]
[4 5 6]]
Slicing (first 3 elements): [1 2 3]
Page 21
Python Programming Lab (4CS4-23)
2.19 Program 19: NumPy Concatenate
Problem Statement: Write a program to demonstrate NumPy array joining (concate-
nate) operations.
1 # Program 19: NumPy Concatenate
2 # Joining two arrays together
3
4 import numpy as np
5
6 arr1 = [Link]([1, 2, 3])
7 arr2 = [Link]([4, 5, 6])
8
9 # Join them
10 new_arr = [Link]((arr1, arr2))
11
12 print("Array 1:", arr1)
13 print("Array 2:", arr2)
14 print("Joined Array:", new_arr)
Output:
Array 1: [1 2 3]
Array 2: [4 5 6]
Joined Array: [1 2 3 4 5 6]
Page 22
Python Programming Lab (4CS4-23)
2.20 Program 20: Pandas Basics
Problem Statement: Write a program to load data and perform basic statistical op-
erations (Mean, Median, Mode).
1 # Program 20: Pandas Statistics
2 # Loading data and finding mean, median, mode
3
4 import pandas as pd
5 import numpy as np
6
7 # Creating a small data for example
8 data = {
9 ’marks’: [85, 90, 85, 70, 95, 80]
10 }
11
12 # Create DataFrame
13 df = [Link](data)
14
15 # Show statistics
16 print("Data Marks:\n", df[’marks’].values)
17 print("\nMean of marks:", df[’marks’].mean())
18 print("Median of marks:", df[’marks’].median())
19 print("Mode of marks:", df[’marks’].mode()[0])
Output:
Data Marks:
[85 90 85 70 95 80]
Mean of marks: 84.16666666666667
Median of marks: 85.0
Mode of marks: 85
Page 23