0% found this document useful (0 votes)
2 views50 pages

Python Lab Record

Uploaded by

rameshkumar.m
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views50 pages

Python Lab Record

Uploaded by

rameshkumar.m
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

EX. No.

:
Date:
Variables and Basic Data Types

Aim
To demonstrate the use of variables and basic data types in Python.

Procedure
1. Start the Python program.
2. Declare variables with different data types.
3. Assign integer, float, string, and boolean values.
4. Store values in variables.
5. Use type() function to identify data type.
6. Display values and their data types.
7. Execute the program.
8. Observe the output.

Program
a = 10
b = 3.5
c = "Python"
d = True

print(a, type(a))
print(b, type(b))
print(c, type(c))
print(d, type(d))
Output
10 <class 'int'>
3.5 <class 'float'>
Python <class 'str'>
True <class 'bool'>
Result
Thus, the program to demonstrate variables and basic data types was executed successfully.
EX. No.:
Date:
Positive or Negative Number (if–else)

Aim
To check whether a given number is positive or negative using if–else statement.

Procedure
1. Start the program.
2. Read an integer value.
3. Store the value in a variable.
4. Compare the number with zero.
5. If number ≥ 0, it is positive.
6. Else, it is negative.
7. Display the result.
8. End the program.

Program
num = int(input("Enter a number: "))

if num >= 0:
print("Positive number")
else:
print("Negative number")
Output
Enter a number: -4
Negative number
Result
Thus, the program to check whether a number is positive or negative was executed
successfully.
EX. No.:
Date:
Grade Calculation (else–if ladder)

Aim
To calculate the grade of a student using else–if ladder.

Procedure
1. Start the program.
2. Read marks obtained by the student.
3. Store marks in a variable.
4. Compare marks with grading conditions.
5. If marks ≥ 90, assign Grade A.
6. Else if marks ≥ 70, assign Grade B.
7. Else if marks ≥ 50, assign Grade C.
8. Otherwise, declare Fail.
9. Display the grade.

Program
marks = int(input("Enter marks: "))

if marks >= 90:


print("Grade A")
elif marks >= 70:
print("Grade B")
elif marks >= 50:
print("Grade C")
else:
print("Fail")
Output
Enter marks: 72
Grade B
Result
Thus, the program to calculate grade using else–if ladder was executed successfully.
EX. No.:
Date:
Largest of Three Numbers (Nested if)

Aim
To find the largest of three numbers using nested if statement.

Procedure
1. Start the program.
2. Read three integer values.
3. Store values in variables.
4. Compare first and second numbers.
5. If first is greater, compare with third.
6. Otherwise, compare second with third.
7. Identify the largest value.
8. Display the result.

Program
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
c = int(input("Enter third number: "))
if a > b:
if a > c:
print("Largest =", a)
else:
print("Largest =", c)
else:
if b > c:
print("Largest =", b)
else:
print("Largest =", c)
Output
Enter first number: 10
Enter second number: 25
Enter third number: 15
Largest = 25

Result
Thus, the program to find the largest of three numbers using nested if was executed
successfully.
EX. No.:
Date:

Factorial with Validation Using FOR Loop

Aim

To calculate the factorial of a number using for loop with input validation in Python.

Procedure

1. Start the Python program.


2. Read an integer value from the user.
3. Check whether the number is negative.
4. If negative, display an error message.
5. Otherwise initialize factorial as 1.
6. Use for loop from 1 to n.
7. Multiply factorial with loop variable.
8. Formula: Factorial = 1 × 2 × 3 × … × n
9. Display the factorial result.
10. Stop the program.

Program

n = int(input("Enter a number: "))

if n < 0:
print("Factorial not defined for negative numbers")
else:
fact = 1
for i in range(1, n + 1):
fact = fact * i
print("Factorial of", n, "=", fact)
Output

Enter a number: 6
Factorial of 6 = 720
Result

Thus, the program to calculate factorial using for loop with validation was executed
successfully.
EX. No.:
Date:
Sum of Digits and Count of Digits Using WHILE Loop

Aim

To find the sum of digits and count the number of digits using while loop in Python.

Procedure

1. Start the Python program.


2. Read an integer number from the user.
3. Initialize sum and count variables as zero.
4. Extract the last digit using modulo operation.
5. Add the digit to sum.
6. Increase the digit count by 1.
7. Remove last digit using integer division.
8. Formula:
o digit = n % 10
o sum = sum + digit
o n = n // 10
9. Repeat until number becomes zero.
10. Display sum and count.

Program

n = int(input("Enter a number: "))


total = 0
count = 0

while n > 0:
digit = n % 10
total = total + digit
count = count + 1
n = n // 10
print("Sum of digits =", total)
print("Number of digits =", count)
Output

Enter a number: 2456


Sum of digits = 17
Number of digits = 4
Result

Thus, the program to find sum and count of digits using while loop was executed
successfully.
EX. No.:
Date:

Non-Fruitful Function with Conditional Logic

Aim

To implement a non-fruitful function using conditional statements in Python.

Procedure

1. Start the Python program.


2. Define a function using def.
3. Read marks inside the function.
4. Use conditional statements to determine result.
5. Do not return any value.
6. Print result directly inside the function.
7. Call the function from main program.
8. Execute and observe output.

Program

def result_check():
marks = int(input("Enter marks: "))

if marks >= 50:


print("Result: Pass")
else:
print("Result: Fail")

result_check()
Output

Enter marks: 65
Result: Pass
Result

Thus, the program to implement a non-fruitful function with conditional logic was executed
successfully.
EX. No.:
Date:

Fruitful Function for Area Calculation

Aim

To calculate the area of a rectangle using a fruitful function in Python.

Procedure

1. Start the program.


2. Define a function with parameters.
3. Accept length and breadth values.
4. Calculate area inside function.
5. Formula: Area = length × breadth
6. Return the calculated value.
7. Call the function from main program.
8. Display returned result.

Program

def area_rectangle(length, breadth):


area = length * breadth
return area

l = int(input("Enter length: "))


b = int(input("Enter breadth: "))

result = area_rectangle(l, b)
print("Area =", result)
Output

Enter length: 10
Enter breadth: 5
Area = 50
Result

Thus, the program to calculate area using a fruitful function was executed successfully.
EX. No.:
Date:

Function with Positional Arguments – Average Marks

Aim

To calculate the average marks using a function with positional arguments.

Procedure

1. Start the Python program.


2. Define function with three positional parameters.
3. Pass marks in correct order while calling.
4. Calculate total marks.
5. Compute average value.
6. Formula: Average = Total / Number of subjects
7. Return average value.
8. Display the result.

Program

def average_marks(m1, m2, m3):


total = m1 + m2 + m3
avg = total / 3
return avg

result = average_marks(75, 80, 85)


print("Average marks =", result)
Output

Average marks = 80.0


Result

Thus, the program to calculate average marks using positional arguments was executed
successfully.
EX. No.:
Date:

LIST (Basic Operations)

Aim
To create a list, access, update elements, and perform basic list operations.

Procedure:
1. Start the program.
2. Create a list with different data types.
3. Display the original list.
4. Access elements using index (positive and negative).
5. Append a new element to the list.
6. Extend the list with multiple elements.
7. Insert an element at a specific position.
8. Update an element using index.
9. Remove a specific element using remove().
10. Remove an element using pop().
11. Clear all elements from the list.
12. Display results after each operation.
13. Stop the program.

Program
list=[2,4,5, "ABC", 25.7, 5, 4, "EFG"]
print(list)
print(list[0])
print(list[3])
print(list[-1])

[Link](56.87)
print(list)

[Link]([10,"RAMESH", 78.95])
Output
[2, 4, 5, 'ABC', 25.7, 5, 4, 'EFG']
2
ABC
EFG
[2, 4, 5, 'ABC', 25.7, 5, 4, 'EFG', 56.87]
[2, 4, 5, 'ABC', 25.7, 5, 4, 'EFG', 56.87, 10, 'RAMESH', 78.95]
[2, 4, 5, 15.876, 'ABC', 25.7, 5, 4, 'EFG', 56.87, 10, 'RAMESH', 78.95]
[2, 4, 5, 'KUMAR', 'ABC', 25.7, 5, 4, 'EFG', 56.87, 10, 'RAMESH', 78.95]
[2, 4, 5, 'KUMAR', 25.7, 5, 4, 'EFG', 56.87, 10, 'RAMESH', 78.95]
After Pop Operation : [2, 4, 5, 'KUMAR', 5, 4, 'EFG', 56.87, 10, 'RAMESH', 78.95]
Cleared : []
print(list)

[Link](3, 15.876)
print(list)

list[3]="KUMAR"
print(list)

[Link]("ABC")
print(list)

[Link](4)
print("After Pop Operation :",list)

[Link]()
print("Cleared :", list)

Result
Thus, the list operations were successfully implemented.
EX. No.:
Date:
TUPLE (Packing & Unpacking)

Aim
To demonstrate tuple packing and unpacking in Python.

Procedure
1. Start the program.
2. Create two tuples with elements.
3. Display both tuples.
4. Access elements using indexing.
5. Perform slicing on tuple.
6. Find length, maximum, minimum, and sum of elements.
7. Concatenate two tuples.
8. Display the results.
9. Stop the program.

Program
tup=(12, 15, 75, 23)
tup1=(12, 23, "RAMESH", 15.75, "KUMAR", 78.65)

print(tup)
print(tup1)
print(tup[0])
print(tup1[-1])
print(tup1[1:3]) # Slicing

print(len(tup1))
print(max(tup))
print(min(tup))
print(sum(tup))

print(tup+tup1)
Output
(12, 15, 75, 23)
(12, 23, 'RAMESH', 15.75, 'KUMAR', 78.65)
12
78.65
(23, 'RAMESH')
6
75
12
125
(12, 15, 75, 23, 12, 23, 'RAMESH', 15.75, 'KUMAR', 78.65)
Result
Thus, tuple packing and unpacking were successfully performed.
EX. No.:
Date:
SET - Operations

Aim
To perform basic set operations in Python.

Procedure
1. Start the program.
2. Create a set with elements.
3. Display the set.
4. Add an element using add().
5. Add multiple elements using update().
6. Remove an element using remove().
7. Remove a random element using pop().
8. Discard an element using discard().
9. Copy the set using copy().
10. Clear all elements using clear().
11. Display results after each operation.
12. Stop the program.

Program
s = {10, 20, 30, 40}
print(s)
[Link](70)
print(s)
[Link]([50,60])
print(s)
[Link](30)
print(s)
[Link]()
print(s)
Output
{40, 10, 20, 30}
{70, 40, 10, 20, 30}
{70, 40, 10, 50, 20, 60, 30}
{70, 40, 10, 50, 20, 60}
{40, 10, 50, 20, 60}
{40, 10, 50, 20, 60}
Copy : {40, 10, 50, 20, 60}
set()
[Link](100)
print(s)

print("Copy :",[Link]())

[Link]()
print(s)

Result
Thus, the program to perform set operations in Python was executed successfully.
EX. No.:
Date:
DICTIONARY (Nested & Looping)

Aim
To create a dictionary and perform nested dictionary and traversal operations.

Procedure
1. Start the program.
2. Create a dictionary with key-value pairs.
3. Add a nested dictionary.
4. Display the dictionary.
5. Access nested values.
6. Traverse dictionary using loop.
7. Update a value.
8. Display final dictionary and stop the program.

Program
d={
"name": "Ramesh",
"marks": {"math": 90, "science": 85}
}

print("Dictionary:", d)

print("Science Marks:", d["marks"]["science"])

print("Traversing Dictionary:")
for key, value in [Link]():
print(key, ":", value)

d["name"] = "Kumar"

print("Updated Dictionary:", d)
Output
Dictionary: {'name': 'Ramesh', 'marks': {'math': 90, 'science': 85}}
Science Marks: 85
Traversing Dictionary:
name : Ramesh
marks : {'math': 90, 'science': 85}
Updated Dictionary: {'name': 'Kumar', 'marks': {'math': 90, 'science': 85}}
Result
Thus, nested dictionary and traversal operations were successfully implemented.

You might also like