0% found this document useful (0 votes)
0 views9 pages

20_Small_Python_Programs_Practice

The document provides a collection of 20 small Python programs designed for beginners to practice fundamental programming concepts. Each program includes a brief explanation, the code, and suggested modifications for further practice. The programs cover topics such as basic syntax, arithmetic operations, loops, conditionals, and data structures.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views9 pages

20_Small_Python_Programs_Practice

The document provides a collection of 20 small Python programs designed for beginners to practice fundamental programming concepts. Each program includes a brief explanation, the code, and suggested modifications for further practice. The programs cover topics such as basic syntax, arithmetic operations, loops, conditionals, and data structures.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

20 Small Python Programs for Practice

Beginner-friendly practice set with concepts, programs, explanations, and exercises.

1. Print Hello World


Concept: Basic syntax and print().

print("Hello World")

Explanation: print() displays output.

Practice: Change the message to your name.

2. Print Your Name and Age


Concept: Printing multiple values.

print("Name: Parthiban")

print("Age: 36")

Explanation: Each print() displays a line.

Practice: Print your name, age, department and college.

3. Add Two Numbers


Concept: Variables, input and arithmetic.

a = int(input("Enter first number: "))

b = int(input("Enter second number: "))

total = a + b

print("Sum =", total)

Explanation: input() reads text and int() converts it to an integer.

Practice: Modify it for subtraction, multiplication and division.

4. Calculate Area of a Circle


Concept: Floating-point arithmetic.

radius = float(input("Enter radius: "))


area = 3.14159 * radius * radius

print(f"Area = {area:.2f}")

Explanation: float() accepts decimal input; :.2f displays two decimal places.

Practice: Calculate circumference too.

5. Swap Two Numbers


Concept: Python multiple assignment.

a = int(input("Enter first number: "))

b = int(input("Enter second number: "))

a, b = b, a

print("a =", a)

print("b =", b)

Explanation: Python can swap values without a temporary variable.

Practice: Try swapping with a temporary variable.

6. Check Even or Odd


Concept: if-else and modulus.

n = int(input("Enter a number: "))

if n % 2 == 0:

print("Even")

else:

print("Odd")

Explanation: % gives the remainder. A remainder of 0 when divided by 2 means even.

Practice: Check whether a number is divisible by 5.

7. Largest of Two Numbers


Concept: Comparison with if-elif-else.

a = int(input("Enter first number: "))

b = int(input("Enter second number: "))


if a > b:

print(a, "is larger")

elif b > a:

print(b, "is larger")

else:

print("Both are equal")

Explanation: The program also handles equality.

Practice: Find the largest of three numbers.

8. Positive, Negative or Zero


Concept: Multiple conditions.

n = int(input("Enter a number: "))

if n > 0:

print("Positive")

elif n < 0:

print("Negative")

else:

print("Zero")

Explanation: Python uses indentation to define blocks.

Practice: Also identify whether a non-zero number is even or odd.

9. Calculate Student Grade


Concept: elif ladder.

mark = int(input("Enter mark: "))

if mark >= 90:

print("Grade A")

elif mark >= 80:

print("Grade B")

elif mark >= 70:


print("Grade C")

elif mark >= 60:

print("Grade D")

elif mark >= 50:

print("Grade E")

else:

print("Fail")

Explanation: Conditions are checked from top to bottom.

Practice: Add validation for marks from 0 to 100.

10. Simple Calculator


Concept: Operators and if-elif.

a = float(input("Enter first number: "))

op = input("Enter operator (+, -, *, /): ")

b = float(input("Enter second number: "))

if op == "+":

print("Result =", a + b)

elif op == "-":

print("Result =", a - b)

elif op == "*":

print("Result =", a * b)

elif op == "/":

if b != 0:

print("Result =", a / b)

else:

print("Cannot divide by zero")

else:

print("Invalid operator")

Explanation: Operators can be compared as strings; division by zero is checked.


Practice: Add % and **.

11. Print Numbers 1 to 10


Concept: for loop and range().

for i in range(1, 11):

print(i)

Explanation: range(1, 11) produces 1 through 10.

Practice: Print 10 down to 1.

12. Multiplication Table


Concept: Loop and arithmetic.

n = int(input("Enter a number: "))

for i in range(1, 11):

print(n, "x", i, "=", n * i)

Explanation: The loop runs ten times.

Practice: Print tables from 1 to 5.

13. Sum of 1 to N
Concept: Accumulator variable.

n = int(input("Enter n: "))

total = 0

for i in range(1, n + 1):

total += i

print("Sum =", total)

Explanation: total accumulates the running sum.

Practice: Find the sum of even numbers from 1 to N.

14. Factorial
Concept: Multiplication inside a loop.
n = int(input("Enter a number: "))

factorial = 1

for i in range(1, n + 1):

factorial *= i

print("Factorial =", factorial)

Explanation: 5! = 120. factorial starts at 1.

Practice: Handle negative input.

15. Count Digits


Concept: while loop and integer division.

n = int(input("Enter a number: "))

n = abs(n)

if n == 0:

count = 1

else:

count = 0

while n != 0:

n //= 10

count += 1

print("Number of digits =", count)

Explanation: // 10 removes the last digit; abs() handles negative input.

Practice: Test several numbers.

16. Reverse a Number


Concept: Extracting digits.

n = int(input("Enter a number: "))

reverse = 0

while n != 0:

digit = n % 10
reverse = reverse * 10 + digit

n //= 10

print("Reverse =", reverse)

Explanation: % 10 extracts the last digit and // 10 removes it.

Practice: Find the sum of digits.

17. Palindrome Number


Concept: Reversal and comparison.

n = int(input("Enter a number: "))

original = n

reverse = 0

while n != 0:

digit = n % 10

reverse = reverse * 10 + digit

n //= 10

if original == reverse:

print("Palindrome")

else:

print("Not Palindrome")

Explanation: A palindrome reads the same forward and backward, such as 121.

Practice: Test 121, 1331 and 123.

18. Prime Number


Concept: Loop and Boolean flag.

n = int(input("Enter a number: "))

is_prime = True

if n < 2:

is_prime = False

else:
for i in range(2, n):

if n % i == 0:

is_prime = False

break

if is_prime:

print("Prime")

else:

print("Not Prime")

Explanation: is_prime is a Boolean flag. Finding a divisor makes it False.

Practice: Later optimize by checking only up to sqrt(n).

19. Largest Element in a List


Concept: Lists and traversal.

numbers = []

for i in range(5):

[Link](int(input("Enter number: ")))

largest = numbers[0]

for i in range(1, len(numbers)):

if numbers[i] > largest:

largest = numbers[i]

print("Largest =", largest)

Explanation: A list stores multiple values; append() adds an item.

Practice: Find the smallest element and average.

20. Count Vowels in a String


Concept: Strings, loops and membership.

text = input("Enter a string: ")

count = 0

for ch in [Link]():
if ch in "aeiou":

count += 1

print("Number of vowels =", count)

Explanation: lower() simplifies comparison and in checks membership.

Practice: Count vowels and consonants separately.

Practice Progression
Level Programs Main Focus

Beginner 1–5 Syntax, variables,


input/output

Beginner 6–10 if, elif, else and operators

Basic Logic 11–15 for, while and loops

Logic Building 16–18 Number manipulation

Intermediate 19–20 Lists and strings

Recommended Practice Cycle


1. Read the problem.
2. Write the algorithm in plain English.
3. Try coding it yourself.
4. Run the program and fix errors.
5. Compare with the reference solution.
6. Modify the program.
7. Solve a similar problem without looking.

You might also like