0% found this document useful (0 votes)
10 views25 pages

JHDHUSD

The document is a lab manual for Python programming, detailing 20 lab activities designed to teach fundamental programming concepts. Each activity includes objectives, sample source code, input/output examples, and questions for further understanding. Additional activities and references for further learning are also provided.

Uploaded by

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

JHDHUSD

The document is a lab manual for Python programming, detailing 20 lab activities designed to teach fundamental programming concepts. Each activity includes objectives, sample source code, input/output examples, and questions for further understanding. Additional activities and references for further learning are also provided.

Uploaded by

partozamark2005
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

PYTHON PROGRAMMING

LAB MANUAL

COLLEGE
OF
ENGINEERING AND ARCHITECTURE

Prepared by:
Manuel C. Sapinit III
LAB ACTIVITIES
1. Hello World Program
2. Basic Calculator
3. Find the Largest of Three Numbers
4. Palindrome Checker
5. Fibonacci Series Generator
6. Prime Number Checker
7. Factorial Calculator
8. Temperature Conversion (Celsius to Fahrenheit)
9. Multiplication Table Generator
10. Count Vowels and Consonants in a String
11. Sum of Digits in a Number
12. List Reversal
13. Grade Classification
14. Multiplication of Matrices
15. Sorting a List
16. Dictionary Operations
17. Find the Common Elements in Two Lists
18. Text File Reader/Writer
19. Student Information System
20. Tic-Tac-Toe
Lab Activity 1
Hello World Program
Objective:
Write a Python program that outputs "Hello, World!" to the screen. This will help you set up
your environment and learn basic syntax.

Sample Source Code:

Output:

Questions:
1. What is the purpose of the print () function?

2. What happens if you remove the parentheses around the string?


Lab Activity 2
Basic Calculator
Objective:
Create a program that takes two numbers from the user and performs addition, subtraction,
multiplication, and division based on the user's input.

Sample Source Code:

Input and Output:

Questions:
1. How would you modify the program to handle invalid inputs for numbers?

2. How can you add functionality to handle exponentiation (e.g., num1 ^ num2)?
Lab Activity 3
Find the Largest of Three Numbers
Objective:
Write a program that takes three numbers as input and outputs the largest of the three using if-
else statements.

Sample Source Code:

Input and Output:

Questions:
1. How can you simplify the code by using the max () function?

2. What would happen if you entered equal numbers?


Lab Activity 4
Palindrome Checker
Objective:
Create a program that checks whether a given string is a palindrome (a word that reads the
same forward and backward).

Sample Source Code:

Input and Output:

Questions:
1. What does the [::-1] slice do?

2. Can you modify the code to ignore spaces and punctuation?


Lab Activity 5
Fibonacci Series Generator
Objective:
Write a Python program to generate the Fibonacci sequence up to a given number or a
specified number of terms.

Sample Source Code:

Input and Output:

Questions:
1. What happens if you set n = 0?

2. Can you implement this using recursion instead of a loop? Rewrite the code using
recursion.
Lab Activity 6
Prime Number Checker
Objective:
Write a program that takes a number as input and checks whether it's prime or not.

Sample Source Code:

Input and Output:

Questions:
1. What is the purpose of the else block after the for loop?

2. How can you optimize the prime number check to only check up to the square root of
num?
Lab Activity 7
Factorial Calculator
Objective:
Create a program that calculates the factorial of a given number using iterative method.

Sample Source Code:

Input and Output:

Questions:
1. What is the base case in the recursive solution?

2. Rewrite the code using the recursion method.


Lab Activity 8
Temperature Conversion (Celsius to Fahrenheit)
Objective:
Write a Python program that converts temperature from Celsius to Fahrenheit and vice versa
based on user input.

Sample Source Code:

Input and Output:

Questions:
1. How would you extend the program to handle Kelvin conversions?

2. What happens if the user enters an invalid unit?


Lab Activity 9
Multiplication Table Generator
Objective:
Write a program that generates a multiplication table for a given number.

Sample Source Code:

Input and Output:

Questions:
1. How would you modify the program to allow the user to specify the range (e.g., 1 to
20)?

2. How could you make the output more readable?


Lab Activity 10
Count Vowels and Consonants in a String
Objective:
Write a program that counts the number of vowels and consonants in a given string.

Sample Source Code:

Input and Output:

Questions:
1. How would you handle punctuation and spaces?

2. Can you modify the program to count digits as well?


Lab Activity 11
Sum of Digits in a Number
Objective:
Create a program that takes an integer input and returns the sum of its digits.

Sample Source Code:

Input and Output:

Questions:
1. How does the modulus operator (%) help in extracting individual digits?

2. Can you modify the program to work with a string input?


Lab Activity 12
List Reversal
Objective:
Write a program to reverse a list using slicing and a loop.

Sample Source Code:

Output:

Questions:
1. What is the difference between reversing a list using slicing and using the reverse()
method?

2. Can you implement the reversal manually using a loop using a list made-up of user
input?
Lab Activity 13
Grade Classification
Objective:
Create a Python program that accepts a student's marks as input and outputs the grade (A, B, C,
etc.) based on a predefined set of criteria.

Sample Source Code:

Output:

Questions:
1. How would you modify the program to classify grades into more categories (e.g., A+, B-)?

2. What happens if the input is a negative number?


Lab Activity 14
Multiplication of Matrices
Objective:
Write a Python program that multiplies two matrices and displays the result.

Sample Source Code:

Output:

Questions:
1. How can you generalize this to multiply matrices of any size?

2. What is the requirement for matrix multiplication in terms of dimensions?


Lab Activity 15
Sorting a List
Objective:
Write a Python program to sort a list in ascending and descending order using Python's built-in
sorted () function and implementation of list comprehension.

Sample Source Code:

Output:

Questions:
1. How would you sort the list in descending order?

2. Can you implement list comprehension to expand the variety of sorting the list?
Lab Activity 16
Dictionary Operations
Objective:
Create a program that performs basic dictionary operations such as adding, updating, deleting,
and displaying key-value pairs.

Sample Source Code:

Output:

Questions:
1. How can you iterate through the dictionary and print each key-value pair?

2. How would you handle a missing key gracefully?


Lab Activity 17
Find the Common Elements in Two Lists
Objective:
Write a program that takes two lists and finds the common elements between them.

Sample Source Code:

Output:

Questions:
1. What is the role of the set () function here?

2. Can you implement this without using sets?


Lab Activity 18
Text File Reader/Writer
Objective:
Create a program to read data from a text file and write user input to another text file.

Sample Source Code:

Output:

Questions:
1. What happens if the file does not exist when trying to read it?

2. How would you append to the file rather than overwrite it?
Lab Activity 19
Student Information System
Objective:
Write a program that stores student information (name, roll number, marks) in a list of
dictionaries and allows operations like viewing, adding, and updating student data.

Sample Source Code:

Output:

Questions:
1. How can you search for a student by their roll number?

2. Can you update a student’s marks?

3. Make the data output more presentable and allow users to input and save data.
Lab Activity 20
Tic-Tac-Toe Game
Objective:
Write a simple two-player Tic-Tac-Toe game using Python where players take turns to mark X
and O on the board.

Sample Source Code:

Output:
Questions:
1. How would you add logic to check if a player has won?

2. Can you implement a function to restart the game after it ends?


Other Activities
1. Write a Python program to count the number of characters (character
frequency) in a string.
2. Write a Python program to convert JSON data to Python object.
3. Write a Python function to find the kth smallest element in a list.
4. Write a Python program to create a function that takes one argument, and
that argument will be multiplied with an unknown given number.
5. Write a Python program to determine whether a given year is a leap year.
References
https://www.w3resource.com/python-exercises
https://www.w3schools.com/python

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy