GANDHI INSTITUTE OF
TECHNOLOGY
AND MANAGEMENT (GITAM)
An ISO 9001 Institute
NBA Accredited
LAB RECORD
Name of the Subject: MCPC1207
PROGRAMMING IN PYTHON LAB (0-0-3)
Name of the faculty: MS. ANITA PARIDA
Experiment: Read and Print Values of Variables of Different Data Types in Python
1. Aim of the Experiment
To write a Python program to read and print values of variables of different data types such as
integer, float, string, and boolean.
2. Requirements
Hardware Requirements
Computer System
Minimum 2 GB RAM
Keyboard and Monitor
Software Requirements
Operating System: Windows / Linux / Mac
Python Interpreter (Python 3.x)
Any Text Editor or IDE (IDLE / VS Code / PyCharm)
3. Algorithm
1. Start the program.
2. Read an integer value from the user.
3. Read a float value from the user.
4. Read a string value from the user.
5. Read a boolean value from the user.
6. Display all the entered values along with their data types.
7. Stop the program.
4. Pseudocode
START
INPUT integer value
INPUT float value
INPUT string value
INPUT boolean value
PRINT integer value and its type
PRINT float value and its type
PRINT string value and its type
PRINT boolean value and its type
STOP
5. Python Program
# Program to read and print values of different data types
# Reading values from user
integer_value = int(input("Enter an integer value: "))
float_value = float(input("Enter a float value: "))
string_value = input("Enter a string value: ")
boolean_value = input("Enter a boolean value (True/False): ")
# Converting string input to boolean
boolean_value = boolean_value == "True"
# Printing values and their data types
print("Integer Value:", integer_value, "Type:", type(integer_value))
print("Float Value:", float_value, "Type:", type(float_value))
print("String Value:", string_value, "Type:", type(string_value))
print("Boolean Value:", boolean_value, "Type:", type(boolean_value))
6. Output
Enter an integer value: 10
Enter a float value: 3.5
Enter a string value: Python
Enter a boolean value (True/False): True
Integer Value: 10 Type: <class 'int'>
Float Value: 3.5 Type: <class 'float'>
String Value: Python Type: <class 'str'>
Boolean Value: True Type: <class 'bool'>
Experiment: Arithmetic Operations on Two Integers in Python
1. Aim of the Experiment
To write a Python program to perform addition, subtraction, multiplication, division, and modulo
division on two integers.
2. Requirements
Hardware Requirements
Computer System
Minimum 2 GB RAM
Keyboard and Monitor
Software Requirements
Operating System: Windows / Linux / Mac
Python Interpreter (Python 3.x)
Any Text Editor or IDE (IDLE / VS Code / PyCharm)
3. Algorithm
1. Start the program.
2. Read two integer numbers from the user.
3. Perform addition of the two numbers.
4. Perform subtraction of the two numbers.
5. Perform multiplication of the two numbers.
6. Perform division of the two numbers.
7. Perform modulo division of the two numbers.
8. Display all the results.
9. Stop the program.
4. Pseudocode
START
INPUT number1
INPUT number2
addition = number1 + number2
subtraction = number1 - number2
multiplication = number1 * number2
division = number1 / number2
modulo = number1 % number2
PRINT addition
PRINT subtraction
PRINT multiplication
PRINT division
PRINT modulo
STOP
5. Python Program
# Program to perform arithmetic operations on two integers
# Input two integers
num1 = int(input("Enter first integer: "))
num2 = int(input("Enter second integer: "))
# Performing operations
addition = num1 + num2
subtraction = num1 - num2
multiplication = num1 * num2
division = num1 / num2
modulo = num1 % num2
# Displaying results
print("Addition:", addition)
print("Subtraction:", subtraction)
print("Multiplication:", multiplication)
print("Division:", division)
print("Modulo Division:", modulo)
6. Output
Enter first integer: 10
Enter second integer: 3
Addition: 13
Subtraction: 7
Multiplication: 30
Division: 3.3333333333333335
Modulo Division: 1
Experiment: Check Whether Two Numbers are Equal in Python
1. Aim of the Experiment
To write a Python program to input two numbers and check whether they are equal or not.
2. Requirements
Hardware Requirements
Computer System
Minimum 2 GB RAM
Keyboard and Monitor
Software Requirements
Operating System: Windows / Linux / Mac
Python Interpreter (Python 3.x)
Any Text Editor or IDE (IDLE / VS Code / PyCharm)
3. Algorithm
1. Start the program.
2. Read two numbers from the user.
3. Compare the two numbers using the equality operator (==).
4. If both numbers are equal, display a message stating they are equal.
5. Otherwise, display a message stating they are not equal.
6. Stop the program.
4. Pseudocode
START
INPUT number1
INPUT number2
IF number1 == number2
PRINT "Numbers are equal"
ELSE
PRINT "Numbers are not equal"
END IF
STOP
5. Python Program
# Program to check whether two numbers are equal
# Input two numbers
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
# Checking equality
if num1 == num2:
print("Both numbers are equal.")
else:
print("Numbers are not equal.")
6. Output
Enter first number: 10
Enter second number: 10
Both numbers are equal.
Another Case
Enter first number: 5
Enter second number: 8
Numbers are not equal.
Experiment: Display Grade Description using if-elif-else in Python
1. Aim of the Experiment
To write a Python program to input a grade character (O, A, B, C, F) and display the corresponding
description using if-elif-else construct.
2. Requirements
Hardware Requirements
Computer System
Minimum 2 GB RAM
Keyboard and Monitor
Software Requirements
Operating System: Windows / Linux / Mac
Python Interpreter (Python 3.x)
Any Text Editor or IDE (IDLE / VS Code / PyCharm)
3. Algorithm
1. Start the program.
2. Read a character grade from the user.
3. Check the grade using if-elif-else statements.
4. If grade is O, display Outstanding.
5. If grade is A, display Very Good.
6. If grade is B, display Good.
7. If grade is C, display Average.
8. If grade is F, display Fail.
9. Otherwise display Invalid Grade.
10. Stop the program.
4. Pseudocode
START
INPUT grade
IF grade == 'O'
PRINT "Outstanding"
ELSE IF grade == 'A'
PRINT "Very Good"
ELSE IF grade == 'B'
PRINT "Good"
ELSE IF grade == 'C'
PRINT "Average"
ELSE IF grade == 'F'
PRINT "Fail"
ELSE
PRINT "Invalid Grade"
END IF
STOP
5. Python Program
# Program to display grade description
grade = input("Enter grade (O/A/B/C/F): ")
if grade == 'O':
print("Outstanding")
elif grade == 'A':
print("Very Good")
elif grade == 'B':
print("Good")
elif grade == 'C':
print("Average")
elif grade == 'F':
print("Fail")
else:
print("Invalid Grade")
6. Output
Enter grade (O/A/B/C/F): A
Very Good
Another Case
Enter grade (O/A/B/C/F): F
Fail
Experiment: Fibonacci Series using Recursion in Python
1. Aim of the Experiment
To write a Python program to print the Fibonacci series using recursion.
2. Requirements
Hardware Requirements
Computer System
Minimum 2 GB RAM
Keyboard and Monitor
Software Requirements
Operating System: Windows / Linux / Mac
Python Interpreter (Python 3.x)
Any Text Editor or IDE (IDLE / VS Code / PyCharm)
3. Algorithm
1. Start the program.
2. Define a recursive function fibonacci(n).
3. If n is 0, return 0.
4. If n is 1, return 1.
5. Otherwise return fibonacci(n-1) + fibonacci(n-2).
6. Read the number of terms from the user.
7. Use a loop to print Fibonacci numbers for the given number of terms.
8. Stop the program.
4. Pseudocode
START
FUNCTION fibonacci(n)
IF n == 0
RETURN 0
ELSE IF n == 1
RETURN 1
ELSE
RETURN fibonacci(n-1) + fibonacci(n-2)
END FUNCTION
INPUT number_of_terms
FOR i = 0 TO number_of_terms-1
PRINT fibonacci(i)
END FOR
STOP
5. Python Program
# Program to print Fibonacci series using recursion
def fibonacci(n):
if n == 0:
return 0
elif n == 1:
return 1
else:
return fibonacci(n-1) + fibonacci(n-2)
# Input number of terms
terms = int(input("Enter number of terms: "))
print("Fibonacci Series:")
for i in range(terms):
print(fibonacci(i), end=" ")
6. Output
Enter number of terms: 7
Fibonacci Series:
0112358
Experiment: Find Absolute Value, Square Root and Cube Root of a Number using Math Package in
Python
1. Aim of the Experiment
To write a Python program to print the absolute value, square root, and cube root of a number
using the math package.
2. Requirements
Hardware Requirements
Computer System
Minimum 2 GB RAM
Keyboard and Monitor
Software Requirements
Operating System: Windows / Linux / Mac
Python Interpreter (Python 3.x)
Any Text Editor or IDE (IDLE / VS Code / PyCharm)
3. Algorithm
1. Start the program.
2. Import the math package.
3. Read a number from the user.
4. Find the absolute value using abs() function.
5. Find the square root using [Link]() function.
6. Find the cube root using exponent operator ( ** (1/3) ).
7. Display the results.
8. Stop the program.
4. Pseudocode
START
IMPORT math package
INPUT number
absolute_value = abs(number)
square_root = [Link](number)
cube_root = number^(1/3)
PRINT absolute_value
PRINT square_root
PRINT cube_root
STOP
5. Python Program
# Program to find absolute value, square root and cube root
import math
# Input number
num = float(input("Enter a number: "))
# Calculations
absolute_value = abs(num)
square_root = [Link](num)
cube_root = num ** (1/3)
# Display results
print("Absolute Value:", absolute_value)
print("Square Root:", square_root)
print("Cube Root:", cube_root)
6. Output
Enter a number: 27
Absolute Value: 27.0
Square Root: 5.196152422706632
Cube Root: 3.
Experiment: Find the Greatest of Three Numbers using Functions in Python
1. Aim of the Experiment
To write a Python program to find the greatest of three numbers using a function by passing three
arguments.
2. Requirements
Hardware Requirements
Computer System
Minimum 2 GB RAM
Keyboard and Monitor
Software Requirements
Operating System: Windows / Linux / Mac
Python Interpreter (Python 3.x)
Any Text Editor or IDE (IDLE / VS Code / PyCharm)
3. Algorithm
1. Start the program.
2. Define a function greatest(a, b, c) that takes three arguments.
3. Inside the function compare the three numbers using conditional statements.
4. Determine the greatest number.
5. Return the greatest value.
6. Read three numbers from the user.
7. Call the function by passing the three numbers as arguments.
8. Display the greatest number.
9. Stop the program.
4. Pseudocode
START
FUNCTION greatest(a, b, c)
IF a > b AND a > c
RETURN a
ELSE IF b > c
RETURN b
ELSE
RETURN c
END IF
INPUT num1, num2, num3
result = greatest(num1, num2, num3)
PRINT result
STOP
5. Python Program
# Program to find the greatest of three numbers using functions
def greatest(a, b, c):
if a > b and a > c:
return a
elif b > c:
return b
else:
return c
# Input numbers
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
num3 = float(input("Enter third number: "))
# Function call
result = greatest(num1, num2, num3)
# Display result
print("The greatest number is:", result)
6. Output
Enter first number: 12
Enter second number: 25
Enter third number: 18
The greatest number is: 25
Experiment: Create a New String using First 2 and Last 2 Characters
1. Aim of the Experiment
To write a Python program to create a new string using the first two and last two characters of a
given string. If the string length is less than 2, return an empty string.
2. Requirements
Hardware Requirements
Computer System
Minimum 2 GB RAM
Keyboard and Monitor
Software Requirements
Operating System: Windows / Linux / Mac
Python Interpreter (Python 3.x)
Any Text Editor or IDE (IDLE / VS Code / PyCharm)
3. Algorithm
1. Start the program.
2. Read a string from the user.
3. Check the length of the string.
4. If the length is less than 2, return an empty string.
5. Otherwise extract the first two characters.
6. Extract the last two characters.
7. Concatenate both parts to form a new string.
8. Display the new string.
9. Stop the program.
4. Pseudocode
START
INPUT string
IF length(string) < 2
PRINT empty string
ELSE
new_string = first 2 characters + last 2 characters
PRINT new_string
END IF
STOP
5. Python Program
# Program to create a string from first 2 and last 2 characters
string = input("Enter a string: ")
if len(string) < 2:
result = ""
else:
result = string[:2] + string[-2:]
print("Resulting string:", result)
6. Output
Enter a string: Python
Resulting string: Pyon
Another Case
Enter a string: A
Resulting string:
Experiment: Fetch Data from URL and Write it to a File in Python
1. Aim of the Experiment
To write a Python program to fetch data from a specified URL and store the data in a file.
2. Requirements
Hardware Requirements
Computer System
Minimum 2 GB RAM
Keyboard and Monitor
Internet Connection
Software Requirements
Operating System: Windows / Linux / Mac
Python Interpreter (Python 3.x)
Any Text Editor or IDE (IDLE / VS Code / PyCharm)
3. Algorithm
1. Start the program.
2. Import the [Link] module.
3. Specify the URL from which data needs to be fetched.
4. Open the URL using urlopen() function.
5. Read the data from the URL.
6. Open a file in write mode.
7. Write the fetched data into the file.
8. Close the file.
9. Display a success message.
10. Stop the program.
4. Pseudocode
START
IMPORT [Link]
SET url
OPEN url
READ data
OPEN file in write mode
WRITE data to file
CLOSE file
PRINT "Data written successfully"
STOP
5. Python Program
# Program to fetch data from a URL and write it to a file
import [Link]
url = "[Link]
# Fetch data from URL
response = [Link](url)
data = [Link]()
# Write data to file
file = open("[Link]", "wb")
[Link](data)
[Link]()
print("Data fetched from URL and written to file successfully.")
6. Output
Data fetched from URL and written to file successfully.
File Created:
[Link] will contain the HTML content of the specified webpage.
Check Whether a Number is Even or Odd
1. Aim of the Experiment
To write a Python program to check whether a given number is even or odd.
2. Requirements
Hardware Requirements
Computer System
Minimum 2 GB RAM
Keyboard and Monitor
Software Requirements
Operating System: Windows / Linux / Mac
Python Interpreter (Python 3.x)
Any Text Editor or IDE (IDLE / VS Code / PyCharm)
3. Algorithm
1. Start the program.
2. Read a number from the user.
3. Check if the number is divisible by 2.
4. If divisible, it is even.
5. Otherwise, it is odd.
6. Display the result.
7. Stop the program.
4. Pseudocode
START
INPUT number
IF number % 2 == 0
PRINT "Even"
ELSE
PRINT "Odd"
STOP
5. Python Program
num = int(input("Enter a number: "))
if num % 2 == 0:
print("Number is Even")
else:
print("Number is Odd")
6. Output
Enter a number: 8
Number is Even
Find Factorial of a Number
1. Aim of the Experiment
To write a Python program to find the factorial of a number using a loop.
3. Algorithm
1. Start the program.
2. Read a number from the user.
3. Initialize factorial = 1.
4. Multiply factorial by each number from 1 to n.
5. Display the factorial.
6. Stop the program.
4. Pseudocode
START
INPUT n
fact = 1
FOR i from 1 to n
fact = fact * i
PRINT fact
STOP
5. Python Program
num = int(input("Enter a number: "))
fact = 1
for i in range(1, num + 1):
fact = fact * i
print("Factorial =", fact)
6. Output
Enter a number: 5
Factorial = 120
Reverse a String
1. Aim of the Experiment
To write a Python program to reverse a given string.
3. Algorithm
1. Start the program.
2. Read a string from the user.
3. Reverse the string using slicing.
4. Display the reversed string.
5. Stop the program.
4. Pseudocode
START
INPUT string
reverse = string[::-1]
PRINT reverse
STOP
5. Python Program
text = input("Enter a string: ")
reverse = text[::-1]
print("Reversed string:", reverse)
6. Output
Enter a string: Python
Reversed string: nohtyP
Find Largest Number in a List
1. Aim of the Experiment
To write a Python program to find the largest number in a list.
3. Algorithm
1. Start the program.
2. Read numbers from the user into a list.
3. Use the max() function to find the largest number.
4. Display the largest number.
5. Stop the program.
4. Pseudocode
START
INPUT list of numbers
largest = max(list)
PRINT largest
STOP
5. Python Program
numbers = list(map(int, input("Enter numbers separated by space: ").split()))
largest = max(numbers)
print("Largest number:", largest)
6. Output
Enter numbers separated by space: 5 12 9 25 7
Largest number: 25
Count Vowels in a String
1. Aim of the Experiment
To write a Python program to count the number of vowels in a string.
3. Algorithm
1. Start the program.
2. Read a string from the user.
3. Initialize count = 0.
4. Check each character of the string.
5. If it is a vowel, increase the count.
6. Display the count.
7. Stop the program.
4. Pseudocode
START
INPUT string
count = 0
FOR each character in string
IF character is vowel
count = count + 1
PRINT count
STOP
5. Python Program
text = input("Enter a string: ")
vowels = "aeiouAEIOU"
count = 0
for ch in text:
if ch in vowels:
count += 1
print("Number of vowels:", count)
6. Output
Enter a string: Python Programming
Number of vowels:
Create and Write to a File
1. Aim of the Experiment
To write a Python program to create a file and write data into it.
3. Algorithm
1. Start the program.
2. Open a file in write mode.
3. Write text into the file.
4. Close the file.
5. Display a success message.
6. Stop the program.
4. Pseudocode
START
OPEN file in write mode
WRITE text into file
CLOSE file
PRINT success message
STOP
5. Python Program
file = open("[Link]", "w")
[Link]("Welcome to Python Programming Lab.\n")
[Link]("This file is created using Python.")
[Link]()
print("Data written to file successfully.")
6. Output
Data written to file successfully.
Experiment : Check Whether a Number is Prime
1. Aim of the Experiment
To write a Python program to check whether a given number is a prime number or not.
2. Requirements
Hardware Requirements
Computer System
Minimum 2 GB RAM
Keyboard and Monitor
Software Requirements
Operating System: Windows / Linux / Mac
Python Interpreter (Python 3.x)
Any Text Editor or IDE (IDLE / VS Code / PyCharm)
3. Algorithm
1. Start the program.
2. Read a number from the user.
3. Check divisibility from 2 to n-1.
4. If divisible, it is not prime.
5. Otherwise it is prime.
6. Display the result.
7. Stop the program.
4. Pseudocode
START
INPUT number
FOR i from 2 to number-1
IF number % i == 0
PRINT "Not Prime"
EXIT
PRINT "Prime"
STOP
5. Python Program
num = int(input("Enter a number: "))
if num > 1:
for i in range(2, num):
if num % i == 0:
print("Number is Not Prime")
break
else:
print("Number is Prime")
else:
print("Number is Not Prime")
6. Output
Enter a number: 7
Number is Prime
Experiment : Generate Multiplication Table
1. Aim of the Experiment
To write a Python program to print the multiplication table of a given number.
3. Algorithm
1. Start the program.
2. Read a number from the user.
3. Use a loop from 1 to 10.
4. Multiply the number by loop value.
5. Display the result.
6. Stop the program.
4. Pseudocode
START
INPUT number
FOR i from 1 to 10
PRINT number * i
STOP
5. Python Program
num = int(input("Enter a number: "))
for i in range(1, 11):
print(num, "x", i, "=", num * i)
6. Output
Enter a number: 5
5x1=5
5 x 2 = 10
5 x 3 = 15
...
5 x 10 = 50
Experiment : Sum of Elements in a List
1. Aim of the Experiment
To write a Python program to find the sum of all elements in a list.
3. Algorithm
1. Start the program.
2. Read numbers into a list.
3. Use sum() function to calculate total.
4. Display the result.
5. Stop the program.
4. Pseudocode
START
INPUT list
total = sum(list)
PRINT total
STOP
5. Python Program
numbers = list(map(int, input("Enter numbers: ").split()))
total = sum(numbers)
print("Sum of elements:", total)
6. Output
Enter numbers: 4 5 6 7
Sum of elements: 22
Experiment : Find Length of a String
1. Aim of the Experiment
To write a Python program to find the length of a string.
3. Algorithm
1. Start the program.
2. Read a string from the user.
3. Use len() function.
4. Display the length.
5. Stop the program.
4. Pseudocode
START
INPUT string
length = len(string)
PRINT length
STOP
5. Python Program
text = input("Enter a string: ")
length = len(text)
print("Length of string:", length)
6. Output
Enter a string: Python
Length of string: 6
Experiment : Convert Temperature from Celsius to Fahrenheit
1. Aim of the Experiment
To write a Python program to convert temperature from Celsius to Fahrenheit.
3. Algorithm
1. Start the program.
2. Read temperature in Celsius.
3. Apply conversion formula.
4. Display Fahrenheit value.
5. Stop the program.
4. Pseudocode
START
INPUT celsius
fahrenheit = (celsius * 9/5) + 32
PRINT fahrenheit
STOP
5. Python Program
celsius = float(input("Enter temperature in Celsius: "))
fahrenheit = (celsius * 9/5) + 32
print("Temperature in Fahrenheit:", fahrenheit)
6. Output
Enter temperature in Celsius: 25
Temperature in Fahrenheit: 77.0
Experiment :Check Palindrome Number
1. Aim of the Experiment
To write a Python program to check whether a number is a palindrome or not.
3. Algorithm
1. Start the program.
2. Read a number from the user.
3. Reverse the number.
4. Compare with original number.
5. If equal, it is palindrome.
6. Otherwise not palindrome.
7. Stop the program.
4. Pseudocode
START
INPUT number
reverse = reverse of number
IF number == reverse
PRINT palindrome
ELSE
PRINT not palindrome
STOP
5. Python Program
num = input("Enter a number: ")
if num == num[::-1]:
print("Number is Palindrome")
else:
print("Number is Not Palindrome")
6. Output
Enter a number: 121
Number is Palindrome