Python Lab – Basic Programs and Control Structures
This lab introduces fundamental Python programming concepts through hands-on practice. These experiments cover essential programming constructs including input/output operations, conditional statements, loops, string manipulation, and basic algorithms. Mastering these programs builds a strong foundation for advanced Python programming and data science.
Python's simplicity and readability make it an excellent language for beginners. These experiments demonstrate Python's syntax, data types, control flow, and built-in functions. Each program teaches specific concepts while building practical programming skills.
Complete List of Experiments
- Print "Hello, World" and user name – Introduction to Python syntax, print() function, input() function, and string formatting. Learn basic I/O operations and variable assignment. Understand Python's dynamic typing and simple string operations.
- Read two numbers and perform arithmetic operations – Learn type conversion (int(), float()), arithmetic operators (+, -, *, /, %, **), and formatted output. Practice handling user input and performing calculations. Understand operator precedence and type handling.
- Check whether a number is even or odd – Introduction to conditional statements (if-else), modulus operator (%), and boolean expressions. Learn decision-making in programs and understand how conditions control program flow.
- Find largest of three numbers – Master nested if-else statements, logical operators (and, or, not), and multi-way decision making. Learn to compare multiple values and find maximum/minimum. Can also use built-in max() function.
- Generate Fibonacci series up to N terms – Learn loops (for, while), sequence generation, multiple variable management, and iterative algorithms. Understand how to generate mathematical sequences and handle series. Practice loop control and variable updates.
- Find factorial of a number using loop – Master iterative computation, accumulator pattern, and loop-based calculations. Understand how to compute mathematical functions iteratively. Can be extended to recursive implementation for learning recursion.
- Check whether a string is palindrome – Learn string manipulation, indexing, slicing, string reversal, and string comparison. Understand how to work with strings in Python, including negative indexing and string methods like reverse() or slicing [::-1].
- Sum of digits of a number – Practice number manipulation, digit extraction using modulus and division, loops with numbers, and accumulator patterns. Learn to break down numbers into digits and process them individually.
- Display multiplication table of a given number – Master nested loops (if needed for formatting), formatted output, and table generation. Learn to create structured output and understand loop-based table generation. Practice string formatting for aligned output.
- Count vowels and consonants in a string – Learn character checking, string traversal, conditional logic with characters, and counting patterns. Understand how to iterate through strings, check character properties, and maintain counters. Practice using membership operators (in, not in) with strings.
Experiment Structure
For each experiment, include the following components:
- Aim – Clear objective stating what the program should accomplish
- Algorithm – Step-by-step approach to solve the problem
- Program – Complete, well-commented Python code
- Sample Input and Output – Example test cases with expected results
- Result/Observation – Analysis of program behavior, edge cases, and possible improvements
Key Python Concepts Covered
| Concept | Experiments |
|---|---|
| Input/Output | All experiments |
| Variables & Data Types | All experiments |
| Conditional Statements | Even/odd, largest, palindrome |
| Loops (for, while) | Fibonacci, factorial, multiplication table, digit operations |
| String Operations | Hello World, palindrome, vowel counting |
| Number Operations | Arithmetic, digit sum, even/odd |
Sample Program: Fibonacci Series
# Aim: Generate Fibonacci series up to N terms
# Algorithm:
# 1. Read number of terms N
# 2. Initialize first two terms (0, 1)
# 3. Print first two terms
# 4. Generate next terms by adding previous two
# 5. Print each term
n = int(input("Enter number of terms: "))
a, b = 0, 1
if n <= 0:
print("Please enter a positive number")
elif n == 1:
print(f"Fibonacci series: {a}")
else:
print(f"Fibonacci series: {a}, {b}", end="")
for i in range(2, n):
c = a + b
print(f", {c}", end="")
a, b = b, c
print() # New line
# Sample Input: 8
# Sample Output: Fibonacci series: 0, 1, 1, 2, 3, 5, 8, 13
Learning Outcomes
After completing these experiments, students should be able to:
- Write Python programs using basic syntax and data types
- Handle user input and produce formatted output
- Use conditional statements for decision-making
- Implement loops for repetitive tasks
- Manipulate strings and numbers effectively
- Design algorithms to solve simple problems
- Debug programs and handle edge cases
Frequently Asked Questions
Q1: What is the difference between for and while loops in Python?
For loops iterate over a sequence (list, string, range) and execute a fixed number of times. While loops continue as long as a condition is true and are used when the number of iterations is unknown. Choose for loops when you know the iteration count, while loops for conditional repetition.
Q2: How do I handle invalid input in Python programs?
Use try-except blocks to catch exceptions. For example, wrap int(input()) in try-except ValueError to handle non-numeric input. Always validate user input before processing to prevent program crashes.
Q3: What is the difference between == and = in Python?
= is the assignment operator (assigns value to variable). == is the equality comparison operator (checks if two values are equal). Using = in conditions is a common error that assigns instead of comparing.