NEHRU ARTS AND SCIENCE COLLEGE
(AUTONOMOUS)
(Affiliated to Bharathiar University Accredited with “A+” Grade by NAAC,
ISO 9001:2015 (QMS) Certified, Recognized by UGC with 2(f) &12(B),
Under Star College Scheme by DBT, Approved by AICTE, Govt. of India)
Nehru Gardens, Thirumalayampalayam, Coimbatore - 641 105, Tamil Nadu, India.
E-mail: nascoffice@[Link]. Ph: 0422-2480007 Web Site: [Link]
PYTHON PROGRAMMING
[24U3DFP202]
LABORATORY MANUAL
I [Link]. Digital and Cyber
Forensic Science
NEHRU ARTS AND SCIENCE COLLEGE
(AUTONOMOUS)
(Affiliated to Bharathiar University Accredited with “A+” Grade by NAAC,
ISO 9001:2015 (QMS) Certified, Recognized by UGC with 2(f) &12(B),
Under Star College Scheme by DBT, Approved by AICTE, Govt. of India)
Nehru Gardens, Thirumalayampalayam, Coimbatore - 641 105, Tamil Nadu, India.
E-mail: nascoffice@[Link]. Ph: 0422-2480007 Web Site: [Link]
1. Lab Objectives:
To write, test, and debug simple Python programs.
To implement Python programs with conditionals and loops.
Use functions for structuring Python programs.
Represent compound data using Python lists, tuples, and dictionaries.
Read and write data from/to files in Python.
2. Lab Outcomes:
Upon completion of the course, students will be able to
Write, test, and debug simple Python programs.
Implement Python programs with conditionals and loops.
Develop Python programs step-wise by defining functions and calling them.
Use Python lists, tuples, dictionaries for representing compound data.
Read and write data from/to files in Python
3. Introduction about lab
Minimum System requirements:
Processors: Intel Atom® processor or Intel® Core™ i3 processor.
Disk space: 1 GB.
Operating systems: Windows* 7 or later, macOS, and Linux.
Python* versions: 2.7.X, 3.6.X.,3.8.X
About lab:
Python is a general purpose, high-level programming language; other high- level languages you
might have heard of C++, PHP, and Java. Virtually all modern programming languages make us ofan
Integrated Development Environment (IDE), which allows the creation, editing, testing, and saving
of programs and modules. In Python, the IDE is called IDLE (like many items in the language, this
is a reference to the British comedy group Monty Python, and in this case, one of its members, Eric
Idle).
Many modern languages use both processes. They are first compiled into a lower level language,
called byte code, and then interpreted by a program called a virtual machine. Python uses both
processes, but because of the way programmers interact with it, it is usually considered an
interpretedlanguage.
Practical aspects are the key to understanding and conceptual visualization Of theoretical aspects
covered in the books. Also, this course is designed to review the concepts of Data Structure using C,
studied in previous semester and implement the various algorithms related to different data
structures.
NEHRU ARTS AND SCIENCE COLLEGE
(AUTONOMOUS)
(Affiliated to Bharathiar University Accredited with “A+” Grade by NAAC,
ISO 9001:2015 (QMS) Certified, Recognized by UGC with 2(f) &12(B),
Under Star College Scheme by DBT, Approved by AICTE, Govt. of India)
Nehru Gardens, Thirumalayampalayam, Coimbatore - 641 105, Tamil Nadu, India.
E-mail: nascoffice@[Link]. Ph: 0422-2480007 Web Site: [Link]
A. General laboratory instructions
1. Students are advised to come to the laboratory at least 5 minutes before (to the starting time),
those who come after 5 minutes will not be allowed into the lab.
2. Plan your task properly much before to the commencement, come prepared to the lab with
the synopsis / program / experiment details.
3. Student should enter into the laboratory with:
a. Laboratory observation notes with all the details (Problem statement, Aim, Algorithm,
Procedure, Program, Expected Output, etc.,) filled in for the lab session.
b. Laboratory Record updated up to the last session experiments and other utensils (if any)
needed in the lab. c. Proper Dress code and Identity card.
4. Sign in the laboratory login register, write the TIME-IN, and occupy the computer system
allotted to you by the faculty.
5. Execute your task in the laboratory, and record the results / output in the lab observation note
book, and get certified by the concerned faculty.
6. All the students should be polite and cooperative with the laboratory staff, must maintain the
discipline and decency in the laboratory.
7. Computer labs are established with sophisticated and high end branded systems, which
should be utilized properly.
8. Students / Faculty must keep their mobile phones in SWITCHED OFF mode during the lab
sessions. Misuse of the equipment, misbehaviors with the staff and systems etc., will attract
severe punishment.
9. Students must take the permission of the faculty in case of any urgency to go out; if anybody
found loitering outside the lab / class without permission during working hours will be treated
seriously and punished appropriately.
10. Students should LOG OFF/ SHUT DOWN the computer system before he/she leaves the lab
after completing the task (experiment) in all aspects. He/she must ensure the system / seat is kept
properly.
NEHRU ARTS AND SCIENCE COLLEGE
(AUTONOMOUS)
(Affiliated to Bharathiar University Accredited with “A+” Grade by NAAC,
ISO 9001:2015 (QMS) Certified, Recognized by UGC with 2(f) &12(B),
Under Star College Scheme by DBT, Approved by AICTE, Govt. of India)
Nehru Gardens, Thirumalayampalayam, Coimbatore - 641 105, Tamil Nadu, India.
E-mail: nascoffice@[Link]. Ph: 0422-2480007 Web Site: [Link]
INDEX
[Link] Date List of Programs [Link] Sign
1. Write a python program to find the square root.
2. Write a python program to find the largest among three
numbers.
3. Write a user-defined function in a python to check whether the
given number is prime or not.
4. Write a python program to check Armstrong number.
5. Write a python program to find the sum of elements in an
array using functions.
6. Write a python program to print the list of numbers using
range and for loop.
7. Write a python program to find the factorial of a number.
8. Write a python program to find the frequency of characters
occurring in a string.
9. Write a python program to let user enter some data in string
and then verify data and print
Write a python program in which a function is defined and
10. calling that function to print Python Programming
11. Write a python program in which a function (with single
string parameter) is defined and calling that function to print
the string parameters given to function.
Write a python program in which a class is define, then
12. create object of that class and call
simple print function define in class.
NEHRU ARTS AND SCIENCE COLLEGE
(AUTONOMOUS)
(Affiliated to Bharathiar University Accredited with “A+” Grade by NAAC,
ISO 9001:2015 (QMS) Certified, Recognized by UGC with 2(f) &12(B),
Under Star College Scheme by DBT, Approved by AICTE, Govt. of India)
Nehru Gardens, Thirumalayampalayam, Coimbatore - 641 105, Tamil Nadu, India.
E-mail: nascoffice@[Link]. Ph: 0422-2480007 Web Site: [Link]
PROGRAM-1 Date:
Aim: To write a Python program to find the square root of a given number.
Algorithm
1. Start the program.
2. Read a number from the user.
3. Calculate the square root of the number.
4. Display the result.
5. Stop the program.
Source Code
# Program to find the square root of a number
num = float(input("Enter a number: "))
sqrt = num ** 0.5
print("Square root of the number is:", sqrt)
Sample Output
Enter a number: 25
Square root of the number is: 5.0
NEHRU ARTS AND SCIENCE COLLEGE
(AUTONOMOUS)
(Affiliated to Bharathiar University Accredited with “A+” Grade by NAAC,
ISO 9001:2015 (QMS) Certified, Recognized by UGC with 2(f) &12(B),
Under Star College Scheme by DBT, Approved by AICTE, Govt. of India)
Nehru Gardens, Thirumalayampalayam, Coimbatore - 641 105, Tamil Nadu, India.
E-mail: nascoffice@[Link]. Ph: 0422-2480007 Web Site: [Link]
PROGRAM-2 Date:
Aim :To write a Python program to find the largest among three given numbers.
Algorithm
1. Start the program.
2. Read three numbers from the user.
3. Compare the three numbers.
4. Find the largest number among them.
5. Display the largest number.
6. Stop the program.
Source Code
# Program to find the largest among three numbers
a = float(input("Enter first number: "))
b = float(input("Enter second number: "))
c = float(input("Enter third number: "))
if a >= b and a >= c:
largest = a
elif b >= a and b >= c:
largest = b
else:
largest = c
print("The largest number is:", largest)
Sample Output
Enter first number: 12
Enter second number: 45
Enter third number: 30
The largest number is: 45
NEHRU ARTS AND SCIENCE COLLEGE
(AUTONOMOUS)
(Affiliated to Bharathiar University Accredited with “A+” Grade by NAAC,
ISO 9001:2015 (QMS) Certified, Recognized by UGC with 2(f) &12(B),
Under Star College Scheme by DBT, Approved by AICTE, Govt. of India)
Nehru Gardens, Thirumalayampalayam, Coimbatore - 641 105, Tamil Nadu, India.
E-mail: nascoffice@[Link]. Ph: 0422-2480007 Web Site: [Link]
PROGRAM-3 Date:
Aim : To write a Python program using a user-defined function to check whether the given number is a prime
number or not.
Algorithm
1. Start the program.
2. Define a function to check whether a number is prime.
3. Read a number from the user.
4. If the number is less than or equal to 1, it is not prime.
5. Check divisibility of the number from 2 to the number minus 1.
6. If divisible by any number, it is not prime.
7. Otherwise, it is a prime number.
8. Display the result.
9. Stop the program.
Source Code
# Program to check prime number using user-defined function
def check_prime(n):
if n <= 1:
return False
for i in range(2, n):
if n % i == 0:
return False
return True
num = int(input("Enter a number: "))
if check_prime(num):
print(num, "is a prime number")
else:
print(num, "is not a prime number")
Sample Output
Enter a number: 7
7 is a prime number
NEHRU ARTS AND SCIENCE COLLEGE
(AUTONOMOUS)
(Affiliated to Bharathiar University Accredited with “A+” Grade by NAAC,
ISO 9001:2015 (QMS) Certified, Recognized by UGC with 2(f) &12(B),
Under Star College Scheme by DBT, Approved by AICTE, Govt. of India)
Nehru Gardens, Thirumalayampalayam, Coimbatore - 641 105, Tamil Nadu, India.
E-mail: nascoffice@[Link]. Ph: 0422-2480007 Web Site: [Link]
PROGRAM-4 Date:
Aim : To write a Python program to check whether a given number is an Armstrong number or not.
Algorithm :
1. Read an integer number from the user.
2. Store the original number in a temporary variable.
3. Find the number of digits in the given number.
4. Initialize a variable sum to 0.
5. Extract each digit of the number.
6. Raise each digit to the power of total digits.
7. Add the result to sum.
8. Repeat steps 5–7 until the number becomes 0.
9. Compare sum with the original number.
10. If both are equal, print Armstrong Number; otherwise, print Not an Armstrong Number.
Coding :
# Program to check Armstrong number
num = int(input("Enter a number: "))
temp = num
sum = 0
digits = len(str(num))
while temp > 0:
digit = temp % 10
sum = sum + digit ** digits
temp = temp // 10
if num == sum:
print("Armstrong Number")
else:
print("Not an Armstrong Number")
NEHRU ARTS AND SCIENCE COLLEGE
(AUTONOMOUS)
(Affiliated to Bharathiar University Accredited with “A+” Grade by NAAC,
ISO 9001:2015 (QMS) Certified, Recognized by UGC with 2(f) &12(B),
Under Star College Scheme by DBT, Approved by AICTE, Govt. of India)
Nehru Gardens, Thirumalayampalayam, Coimbatore - 641 105, Tamil Nadu, India.
E-mail: nascoffice@[Link]. Ph: 0422-2480007 Web Site: [Link]
Sample Output
Enter a number: 153
Output
Armstrong Number
Enter a number: 123
Output
Not an Armstrong Number
NEHRU ARTS AND SCIENCE COLLEGE
(AUTONOMOUS)
(Affiliated to Bharathiar University Accredited with “A+” Grade by NAAC,
ISO 9001:2015 (QMS) Certified, Recognized by UGC with 2(f) &12(B),
Under Star College Scheme by DBT, Approved by AICTE, Govt. of India)
Nehru Gardens, Thirumalayampalayam, Coimbatore - 641 105, Tamil Nadu, India.
E-mail: nascoffice@[Link]. Ph: 0422-2480007 Web Site: [Link]
PROGRAM-5 Date:
Aim : To write a Python program that finds the sum of elements in an array using a
user-defined function.
Algorithm :
1. Start the program.
2. Define a function to calculate the sum of array elements.
3. Read the number of elements from the user.
4. Read the array elements.
5. Pass the array to the function.
6. Initialize a variable total to 0.
7. Add each element of the array to total.
8. Return the sum.
9. Display the result.
10. Stop the program.
Python Program
# Program to find sum of elements in an array using function
def array_sum(arr):
total = 0
for i in arr:
total = total + i
return total
n = int(input("Enter number of elements: "))
arr = []
for i in range(n):
value = int(input("Enter element: "))
[Link](value)
result = array_sum(arr)
print("Sum of array elements:", result)
NEHRU ARTS AND SCIENCE COLLEGE
(AUTONOMOUS)
(Affiliated to Bharathiar University Accredited with “A+” Grade by NAAC,
ISO 9001:2015 (QMS) Certified, Recognized by UGC with 2(f) &12(B),
Under Star College Scheme by DBT, Approved by AICTE, Govt. of India)
Nehru Gardens, Thirumalayampalayam, Coimbatore - 641 105, Tamil Nadu, India.
E-mail: nascoffice@[Link]. Ph: 0422-2480007 Web Site: [Link]
Sample Output
Enter number of elements: 5
Enter element: 10
Enter element: 20
Enter element: 30
Enter element: 40
Enter element: 50
Output
Sum of array elements: 150
NEHRU ARTS AND SCIENCE COLLEGE
(AUTONOMOUS)
(Affiliated to Bharathiar University Accredited with “A+” Grade by NAAC,
ISO 9001:2015 (QMS) Certified, Recognized by UGC with 2(f) &12(B),
Under Star College Scheme by DBT, Approved by AICTE, Govt. of India)
Nehru Gardens, Thirumalayampalayam, Coimbatore - 641 105, Tamil Nadu, India.
E-mail: nascoffice@[Link]. Ph: 0422-2480007 Web Site: [Link]
PROGRAM-6 Date:
Aim : To write a Python program to print a list of numbers using the range() function and a for loop.
Algorithm :
1. Start the program.
2. Read the starting number from the user.
3. Read the ending number from the user.
4. Use the range() function to generate numbers.
5. Use a for loop to iterate through the range.
6. Print each number.
7. Stop the program.
Python Program
# Program to print list of numbers using range and for loop
start = int(input("Enter starting number: "))
end = int(input("Enter ending number: "))
print("List of numbers:")
for i in range(start, end + 1):
print(i)
Sample Output
Enter starting number: 1
Enter ending number: 5
Output
List of numbers:
1
2
3
4
5
NEHRU ARTS AND SCIENCE COLLEGE
(AUTONOMOUS)
(Affiliated to Bharathiar University Accredited with “A+” Grade by NAAC,
ISO 9001:2015 (QMS) Certified, Recognized by UGC with 2(f) &12(B),
Under Star College Scheme by DBT, Approved by AICTE, Govt. of India)
Nehru Gardens, Thirumalayampalayam, Coimbatore - 641 105, Tamil Nadu, India.
E-mail: nascoffice@[Link]. Ph: 0422-2480007 Web Site: [Link]
PROGRAM-7 Date:
Aim : To write a Python program to find the factorial of a given number.
Algorithm :
1. Start the program.
2. Read an integer number from the user.
3. Initialize a variable fact to 1.
4. Use a for loop from 1 to the given number.
5. Multiply fact by each value in the loop.
6. After the loop, fact contains the factorial.
7. Display the result.
8. Stop the program.
Python Program
# Program to find factorial of a number
num = int(input("Enter a number: "))
fact = 1
for i in range(1, num + 1):
fact = fact * i
print("Factorial of", num, "is:", fact)
Sample Output
Enter a number: 5
Output
Factorial of 5 is: 120
NEHRU ARTS AND SCIENCE COLLEGE
(AUTONOMOUS)
(Affiliated to Bharathiar University Accredited with “A+” Grade by NAAC,
ISO 9001:2015 (QMS) Certified, Recognized by UGC with 2(f) &12(B),
Under Star College Scheme by DBT, Approved by AICTE, Govt. of India)
Nehru Gardens, Thirumalayampalayam, Coimbatore - 641 105, Tamil Nadu, India.
E-mail: nascoffice@[Link]. Ph: 0422-2480007 Web Site: [Link]
PROGRAM-8 Date:
Aim : To write a Python program to find the frequency of each character occurring in a given string.
Algorithm
1. Start the program.
2. Read a string from the user.
3. Initialize an empty dictionary to store character frequencies.
4. Traverse each character in the string.
5. If the character already exists in the dictionary, increment its count.
6. If the character does not exist, add it to the dictionary with count 1.
7. Display each character and its frequency.
8. Stop the program.
Python Program
# Program to find frequency of characters in a string
text = input("Enter a string: ")
freq = {}
for char in text:
if char in freq:
freq[char] = freq[char] + 1
else:
freq[char] = 1
print("Character frequency:")
for char in freq:
print(char, ":", freq[char])
Sample Output
Enter a string: hello
Output
Character frequency:
h:1
e:1
l:2
o:1
NEHRU ARTS AND SCIENCE COLLEGE
(AUTONOMOUS)
(Affiliated to Bharathiar University Accredited with “A+” Grade by NAAC,
ISO 9001:2015 (QMS) Certified, Recognized by UGC with 2(f) &12(B),
Under Star College Scheme by DBT, Approved by AICTE, Govt. of India)
Nehru Gardens, Thirumalayampalayam, Coimbatore - 641 105, Tamil Nadu, India.
E-mail: nascoffice@[Link]. Ph: 0422-2480007 Web Site: [Link]
PROGRAM-9 Date:
Aim :To write a Python program that allows the user to enter data as a string, verifies whether the data is
valid, and then prints the verified data.
Algorithm :
1. Start the program.
2. Read a string from the user.
3. Verify whether the entered string contains only alphabetic characters.
4. If the string is valid, print the entered data.
5. If the string is invalid, display an error message.
6. Stop the program.
Simple Python Program
# Program to verify user entered string data
data = input("Enter some data: ")
if [Link]():
print("Valid data entered")
print("Data is:", data)
else:
print("Invalid data! Please enter only alphabets.")
Sample Output
Enter some data: Python
Output
Valid data entered
Data is: Python
Enter some data: Python123
Output
Invalid data! Please enter only alphabets.
NEHRU ARTS AND SCIENCE COLLEGE
(AUTONOMOUS)
(Affiliated to Bharathiar University Accredited with “A+” Grade by NAAC,
ISO 9001:2015 (QMS) Certified, Recognized by UGC with 2(f) &12(B),
Under Star College Scheme by DBT, Approved by AICTE, Govt. of India)
Nehru Gardens, Thirumalayampalayam, Coimbatore - 641 105, Tamil Nadu, India.
E-mail: nascoffice@[Link]. Ph: 0422-2480007 Web Site: [Link]
PROGRAM-10 Date:
Aim :To write a Python program in which a function is defined and then called to print the message “Python
Programming”.
Algorithm :
1. Start the program.
2. Define a function with a suitable name.
3. Inside the function, write a print statement to display “Python Programming”.
4. Call the function.
5. Stop the program.
Python Program
# Program to define and call a function
def display():
print("Python Programming")
display()
Sample Output
Python Programming
NEHRU ARTS AND SCIENCE COLLEGE
(AUTONOMOUS)
(Affiliated to Bharathiar University Accredited with “A+” Grade by NAAC,
ISO 9001:2015 (QMS) Certified, Recognized by UGC with 2(f) &12(B),
Under Star College Scheme by DBT, Approved by AICTE, Govt. of India)
Nehru Gardens, Thirumalayampalayam, Coimbatore - 641 105, Tamil Nadu, India.
E-mail: nascoffice@[Link]. Ph: 0422-2480007 Web Site: [Link]
PROGRAM-11 Date:
Aim : To write a Python program in which a function with a single string parameter is defined and then
called to print the string passed to the function.
Algorithm :
1. Start the program.
2. Define a function that accepts one string parameter.
3. Inside the function, print the received string.
4. Read a string from the user.
5. Call the function by passing the string as an argument.
6. Stop the program.
Python Program
# Program to define and call a function with single string parameter
def display_message(msg):
print("The string is:", msg)
text = input("Enter a string: ")
display_message(text)
Sample Output
Enter a string: Hello Python
Output
The string is: Hello Python
NEHRU ARTS AND SCIENCE COLLEGE
(AUTONOMOUS)
(Affiliated to Bharathiar University Accredited with “A+” Grade by NAAC,
ISO 9001:2015 (QMS) Certified, Recognized by UGC with 2(f) &12(B),
Under Star College Scheme by DBT, Approved by AICTE, Govt. of India)
Nehru Gardens, Thirumalayampalayam, Coimbatore - 641 105, Tamil Nadu, India.
E-mail: nascoffice@[Link]. Ph: 0422-2480007 Web Site: [Link]
PROGRAM-12 Date:
Aim: To write a Python program in which a class is defined, an object of that class is created, and a simple
print function defined inside the class is called.
Algorithm :
1. Start the program.
2. Define a class with a suitable name.
3. Inside the class, define a function that prints a message.
4. Create an object of the class.
5. Call the function using the object.
6. Stop the program.
Python Program
# Program to define a class and call its function
class Message:
def display(self):
print("Welcome to Python Programming")
obj = Message()
[Link]()
Sample Output
Welcome to Python Programming