Python is one of the most widely used programming languages which are used by students, amateurs, engineers and developers. This is a good programming fundamentals language because it has a simple syntax to understand.
Learning Python is a learning process for students, they need not just theory So, why do you need to write good practical programs regularly and run them? because writing programs helps you understand the logic of programming.
The 50+ Python practical programs with output for the students are given in this article. These programs include some fundamental Python concepts like variable, operators, conditional statements, loops, number string list tuple dictionary function patterns searching and sorting.
These examples can be useful for Python lab practice, B.Tech practical exams, assignments, viva preparation, and beginner coding practice.
1. Python Hello World Program
The Hello World program is usually the first program beginners learn.
print("Hello World")
Output
Hello World
The print() function is used to display text on the screen.
2. Add Two Numbers
a = 10
b = 20
sum = a + b
print("Sum =", sum)
Output
Sum = 30
This program uses the + operator to add two numbers.
3. Subtract Two Numbers
a = 50
b = 20
difference = a - b
print("Difference =", difference)
Output
Difference = 30
4. Multiply Two Numbers
a = 10
b = 5
result = a * b
print("Multiplication =", result)
Output
Multiplication = 50
5. Divide Two Numbers
a = 20
b = 5
result = a / b
print("Division =", result)
Output
Division = 4.0
6. Find Square of a Number
number = 8
square = number ** 2
print("Square =", square)
Output
Square = 64
7. Find Cube of a Number
number = 5
cube = number ** 3
print("Cube =", cube)
Output
Cube = 125
8. Check Even or Odd
This is one of the most common Python practical programs.
number = 12
if number % 2 == 0:
print("Even")
else:
print("Odd")
Output
Even
The % operator gives the remainder after division.
9. Check Positive or Negative
number = -5
if number > 0:
print("Positive")
elif number < 0:
print("Negative")
else:
print("Zero")
Output
Negative
10. Find Largest of Two Numbers
a = 25
b = 40
if a > b:
print("Largest =", a)
else:
print("Largest =", b)
Output
Largest = 40
11. Find Largest of Three Numbers
a = 20
b = 50
c = 30
if a > b and a > c:
largest = a
elif b > c:
largest = b
else:
largest = c
print("Largest =", largest)
Output
Largest = 50
12. Check Leap Year
year = 2024
if year % 400 == 0 or (year % 4 == 0 and year % 100 != 0):
print("Leap Year")
else:
print("Not a Leap Year")
Output
Leap Year
13. Check Voting Eligibility
age = 20
if age >= 18:
print("Eligible to vote")
else:
print("Not eligible to vote")
Output
Eligible to vote
14. Simple Calculator
a = 20
b = 10
operator = "+"
if operator == "+":
print("Result =", a + b)
elif operator == "-":
print("Result =", a - b)
elif operator == "*":
print("Result =", a * b)
elif operator == "/":
print("Result =", a / b)
else:
print("Invalid operator")
Output
Result = 30
15. Print Numbers from 1 to 10
for i in range(1, 11):
print(i, end=" ")
Output
1 2 3 4 5 6 7 8 9 10
16. Print Even Numbers
for i in range(2, 21, 2):
print(i, end=" ")
Output
2 4 6 8 10 12 14 16 18 20
17. Print Odd Numbers
for i in range(1, 20, 2):
print(i, end=" ")
Output
1 3 5 7 9 11 13 15 17 19
18. Multiplication Table
number = 5
for i in range(1, 11):
print(number, "x", i, "=", number * i)
Output
5 x 1 = 5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45
5 x 10 = 50
19. Sum of Natural Numbers
total = 0
for i in range(1, 11):
total += i
print("Sum =", total)
Output
Sum = 55
20. Factorial of a Number
number = 5
factorial = 1
for i in range(1, number + 1):
factorial *= i
print("Factorial =", factorial)
Output
Factorial = 120
21. Fibonacci Series
a = 0
b = 1
for i in range(10):
print(a, end=" ")
a, b = b, a + b
Output
0 1 1 2 3 5 8 13 21 34
22. Reverse a Number
number = 12345
reverse = 0
while number > 0:
digit = number % 10
reverse = reverse * 10 + digit
number //= 10
print("Reverse =", reverse)
Output
Reverse = 54321
23. Sum of Digits
number = 12345
total = 0
while number > 0:
total += number % 10
number //= 10
print("Sum =", total)
Output
Sum = 15
24. Count Digits
number = 123456
count = 0
while number > 0:
count += 1
number //= 10
print("Number of digits =", count)
Output
Number of digits = 6
25. Check Prime Number
A prime number is a number greater than 1 that has only two factors: 1 and itself.
number = 17
is_prime = True
if number < 2:
is_prime = False
for i in range(2, int(number ** 0.5) + 1):
if number % i == 0:
is_prime = False
break
if is_prime:
print("Prime Number")
else:
print("Not a Prime Number")
Output
Prime Number
26. Print Prime Numbers
for number in range(2, 51):
is_prime = True
for i in range(2, int(number ** 0.5) + 1):
if number % i == 0:
is_prime = False
break
if is_prime:
print(number, end=" ")
Output
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47
27. Check Palindrome Number
number = 121
if str(number) == str(number)[::-1]:
print("Palindrome")
else:
print("Not Palindrome")
Output
Palindrome
28. Armstrong Number
number = 153
digits = str(number)
total = sum(int(digit) ** len(digits) for digit in digits)
if total == number:
print("Armstrong Number")
else:
print("Not Armstrong Number")
Output
Armstrong Number
29. Find GCD
GCD stands for Greatest Common Divisor.
import math
a = 24
b = 36
print("GCD =", math.gcd(a, b))
Output
GCD = 12
30. Find LCM
import math
a = 12
b = 18
lcm = abs(a * b) // math.gcd(a, b)
print("LCM =", lcm)
Output
LCM = 36
31. Reverse a String
text = "Python"
print(text[::-1])
Output
nohtyP
32. Check String Palindrome
text = "madam"
if text == text[::-1]:
print("Palindrome")
else:
print("Not Palindrome")
Output
Palindrome
33. Count Vowels
text = "Python Programming"
count = 0
for char in text.lower():
if char in "aeiou":
count += 1
print("Vowels =", count)
Output
Vowels = 4
34. Convert String to Uppercase
text = "python programming"
print(text.upper())
Output
PYTHON PROGRAMMING
35. Convert String to Lowercase
text = "PYTHON PROGRAMMING"
print(text.lower())
Output
python programming
36. Find Length of a String
text = "Python"
print("Length =", len(text))
Output
Length = 6
37. Find Largest Element in a List
numbers = [10, 45, 23, 67, 12]
print("Largest =", max(numbers))
Output
Largest = 67
38. Find Smallest Element in a List
numbers = [10, 45, 23, 67, 12]
print("Smallest =", min(numbers))
Output
Smallest = 10
39. Find Sum of List
numbers = [10, 20, 30, 40, 50]
print("Sum =", sum(numbers))
Output
Sum = 150
40. Sort a List
numbers = [50, 20, 40, 10, 30]
numbers.sort()
print(numbers)
Output
[10, 20, 30, 40, 50]
41. Remove Duplicate Elements
numbers = [1, 2, 2, 3, 3, 4]
unique = list(set(numbers))
print(unique)
Output
[1, 2, 3, 4]
The order may vary because sets are unordered collections.
42. Search an Element in a List
numbers = [10, 20, 30, 40, 50]
target = 30
if target in numbers:
print("Element Found")
else:
print("Element Not Found")
Output
Element Found
43. Find Second Largest Number
numbers = [10, 20, 50, 40, 30]
numbers = list(set(numbers))
numbers.sort()
print("Second Largest =", numbers[-2])
Output
Second Largest = 40
44. Tuple Example
numbers = (10, 20, 30, 40)
print(numbers)
print("Second element =", numbers[1])
Output
(10, 20, 30, 40)
Second element = 20
45. Dictionary Example
student = {
"name": "Rahul",
"age": 20,
"course": "B.Tech"
}
print("Name:", student["name"])
print("Course:", student["course"])
Output
Name: Rahul
Course: B.Tech
46. Set Union
a = {1, 2, 3}
b = {3, 4, 5}
print(a | b)
Output
{1, 2, 3, 4, 5}
47. Set Intersection
a = {1, 2, 3}
b = {2, 3, 4}
print(a & b)
Output
{2, 3}
48. Function to Add Numbers
def add(a, b):
return a + b
print("Sum =", add(10, 20))
Output
Sum = 30
49. Function to Check Even Number
def is_even(number):
return number % 2 == 0
print(is_even(10))
Output
True
50. Factorial Using Recursion
def factorial(n):
if n == 0:
return 1
return n * factorial(n - 1)
print(factorial(5))
Output
120
51. Star Pattern
for i in range(1, 6):
print("*" * i)
Output
*
**
***
****
*****
52. Number Pattern
for i in range(1, 6):
for j in range(1, i + 1):
print(j, end=" ")
print()
Output
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
53. Bubble Sort
Bubble Sort is a simple sorting algorithm that repeatedly compares neighboring elements.
numbers = [5, 3, 8, 1, 2]
for i in range(len(numbers)):
for j in range(0, len(numbers) - i - 1):
if numbers[j] > numbers[j + 1]:
numbers[j], numbers[j + 1] = numbers[j + 1], numbers[j]
print(numbers)
Output
[1, 2, 3, 5, 8]
54. Linear Search
Linear Search checks elements one by one.
numbers = [10, 20, 30, 40, 50]
target = 40
found = False
for number in numbers:
if number == target:
found = True
break
if found:
print("Element Found")
else:
print("Element Not Found")
Output
Element Found
55. Celsius to Fahrenheit
celsius = 30
fahrenheit = (celsius * 9 / 5) + 32
print("Fahrenheit =", fahrenheit)
Output
Fahrenheit = 86.0
56. Generate a Random Number
import random
number = random.randint(1, 10)
print("Random Number =", number)
Sample Output
Random Number = 7
The output can change every time because the number is randomly generated.
Why Python Practical Programs Are Important for Students
Practical programming helps students understand concepts that may be difficult to understand from theory alone.
For example, a student may understand what a for loop is from a textbook, but writing a multiplication table program gives practical experience with how the loop actually works.
Python practical programs help students improve:
- Programming fundamentals
- Logical thinking
- Problem-solving
- Debugging skills
- Coding speed
- Understanding of Python syntax
- Knowledge of data structures
- Exam preparation
Python Practical Programs for B.Tech Students
B.Tech students often need to practice programming examples for laboratory examinations and assignments.
Important areas to practice include:
Basic Programs
- Addition
- Subtraction
- Multiplication
- Division
- Area calculations
- Temperature conversion
Conditional Programs
- Even or odd
- Largest number
- Leap year
- Grade calculation
- Voting eligibility
Loop Programs
- Factorial
- Fibonacci series
- Prime numbers
- Number reversal
- Multiplication tables
- Sum of numbers
String Programs
- Reverse string
- Palindrome
- Vowel counting
- Character counting
- Case conversion
Data Structure Programs
- Lists
- Tuples
- Sets
- Dictionaries
- Searching
- Sorting
Function Programs
- Functions with parameters
- Functions with return values
- Recursive functions
How to Prepare for a Python Practical Exam
If you have a Python practical exam, don’t try to memorize every program. Instead, understand the basic logic behind common problems.
Start by practicing input/output programs.
Next, learn if, elif, and else.
After that, practice for and while loops.
Then move to strings, lists, tuples, sets, dictionaries, and functions.
Finally, practice searching, sorting, recursion, and object-oriented programming.
Before your practical exam, try writing programs without looking at the answer.
Common Python Errors Students Face
Beginners commonly make small mistakes while writing Python programs.
SyntaxError
This occurs when Python syntax is incorrect.
IndentationError
Python uses indentation to define code blocks, so incorrect indentation can cause an error.
NameError
This can occur when a variable is used before it has been defined.
TypeError
This happens when an operation is performed on incompatible data types.
ValueError
This can occur when a function receives a value of the correct type but an invalid value.
Understanding these errors is an important part of learning Python.
Tips for Practicing Python
1. Write Code Yourself
Don’t only copy programs. Type them yourself and run them.
2. Change the Values
If a program uses:
number = 10
try:
number = 25
and test the result.
3. Try Different Inputs
For input-based programs, test positive numbers, negative numbers, zero, and larger values.
4. Modify Existing Programs
Take a program and make a small change.
For example, change an even-number program into an odd-number program.
5. Understand the Logic
The most important part of programming is understanding how the solution works.
Frequently Asked Questions
What are Python practical programs?
Python practical programs are small programs designed to help students learn and practice Python programming concepts.
Are these programs useful for Python lab exams?
Yes. Basic programs involving conditions, loops, strings, lists, functions, numbers, searching, and sorting are useful for Python laboratory practice.
Can beginners practice these programs?
Yes. The programs in this article start with basic examples and gradually introduce more concepts.
Where can I run Python programs?
You can run Python code using Python IDLE, VS Code, PyCharm, Jupyter Notebook, Google Colab, or other Python-supported environments.
Should students memorize these programs?
It is better to understand the logic and practice writing the programs yourself instead of only memorizing the code.
Conclusion
These 50+ Python practical programs with output provide a simple starting point for students who want to improve their Python programming skills.
The examples cover important topics such as basic operations, conditions, loops, numbers, strings, lists, tuples, sets, dictionaries, functions, recursion, patterns, searching, and sorting.
If you are preparing for a Python lab exam, start with the basic programs and then gradually practice the more difficult examples.
The best way to learn Python is to write, run, test, modify, and understand the code.
Keep practicing different Python programs and try creating your own solutions for new problems. This will gradually improve your coding confidence and problem-solving skills.





