Introduction
Python lab programs are one of the most important parts of computer science education. Whether you are a B.Tech student, diploma student, or beginner, practicing lab programs helps you understand programming concepts clearly.
In this article, you will learn Python lab programs with output, explanation, and viva questions. This guide is perfect for exams, assignments, and interviews.
What are Python Lab Programs
Python lab programs are practical coding exercises given in colleges and universities. These programs help students:
- Understand basic programming logic
- Learn syntax and structure
- Solve real-world problems
- Prepare for viva and exams
1. Python Program to Print Hello World
Code
print("Hello World")
Output
Hello World
Explanation
This is the simplest Python program. It uses the print() function to display output.
2. Python Program to Check Even or Odd
Code
num = int(input("Enter a number: "))if num % 2 == 0:
print("Even")
else:
print("Odd")
Output
Enter a number: 4
Even
Explanation
The modulus operator % checks the remainder. If it is 0, the number is even.
3. Python Program to Find Factorial
Code
num = int(input("Enter number: "))
fact = 1for i in range(1, num + 1):
fact *= iprint("Factorial:", fact)
Output
Enter number: 5
Factorial: 120
Explanation
Factorial is the multiplication of all positive integers up to a number.
4. Python Program to Check Prime Number
Code
num = int(input("Enter number: "))
flag = Falseif num > 1:
for i in range(2, num):
if num % i == 0:
flag = True
breakif flag:
print("Not Prime")
else:
print("Prime")
Output
Enter number: 7
Prime
5. Python Program to Find Largest of Three Numbers
Code
a = int(input("Enter first: "))
b = int(input("Enter second: "))
c = int(input("Enter third: "))if a > b and a > c:
print("Largest:", a)
elif b > c:
print("Largest:", b)
else:
print("Largest:", c)
6. Python Program for Fibonacci Series
Code
n = int(input("Enter terms: "))
a, b = 0, 1for i in range(n):
print(a, end=" ")
a, b = b, a + b
Output
0 1 1 2 3 5
7. Python Program to Reverse a String
Code
text = input("Enter string: ")
print("Reversed:", text[::-1])
8. Python Program to Check Palindrome
Code
text = input("Enter string: ")if text == text[::-1]:
print("Palindrome")
else:
print("Not Palindrome")
9. Python Program to Count Vowels
Code
text = input("Enter string: ")
count = 0for char in text:
if char.lower() in "aeiou":
count += 1print("Vowels:", count)
10. Python Program to Find Sum of Numbers
Code
n = int(input("Enter number: "))
total = 0for i in range(1, n + 1):
total += iprint("Sum:", total)
Viva Questions (Important for Exams)
1. What is Python?
Python is a high-level, interpreted programming language.
2. What is a variable?
A variable is used to store data.
3. What is a loop?
A loop is used to execute a block of code repeatedly.
4. Difference between list and tuple?
List is mutable, tuple is immutable.
5. What is indentation in Python?
Indentation defines code blocks.
Tips to Score High in Lab Exams
- Practice all programs regularly
- Understand logic, not just code
- Write clean and readable code
- Prepare viva questions
- Practice dry run of programs
Conclusion
These Python lab programs with output are essential for every student. They cover basic to intermediate concepts and help in exams, viva, and interviews.
If you practice these programs daily, you will build a strong foundation in Python programming.