Python 30 Practice Questions with
Answers: A Beginner’s Guide
Muhammad Amir Mushtaq
1. Introduction
This article contains 30 Python practice questions, along with hints, code, and outputs,
designed for beginners who want to develop programming logic through simple tasks.
Many days we try to learn but cannot reach the desired result. I provide you with some
basic but important code for beginners, which they can use to build upon at a more
advanced level. Hard work and practice make us valuable. I run my code in Google
Colab, an online platform provided by Google that supports Python.
Figure 1: Python Practice Coding For You
1
Muhammad Amir Mushtaq Python Practice
Figure 2: Practice makes the man perfect
2. Methodology
The practice problems were divided into logical groups, each focusing on a particular
concept in Python, such as print statements, variables, conditionals, and loops. Each
subsection below contains a hint, Python code, and its expected output. You run this
code in the next article other topics you see for best Python practice coding zero to hero
level.
In our life bigger challenge is to learn anything without proper arrangement. Then we
cannot reach into peak of success. Python is using widely used in Data Science, Machine
Learning, Deep Learning, Web development, and software development, and more.
2.1 Print Statements and Basics
1. Print ’Hello, World!’
In print(””) function anything you write, it will execute with the same input.
2
Muhammad Amir Mushtaq Python Practice
For programming practice first code is the Hello World
For Practical implementation, we ON/OFF an LED. Hint: Prints a greeting message to
the screen.
1 print ( ’ Hello , World ! ’)
2. Print your name
Hint: Prints a hardcoded name.
1 print ( ’ Amir ’)
3. Assign a number to a variable
Hint: Creates a variable and assigns it a number.
1 num = 42
2 print ( num )
4. Assign a string to a variable
Hint: Stores a name in a variable.
1 name = ’ Amir ’
2 print ( name )
5. Print the result of addition
Hint: Performs addition and prints the result.
1 print (10 + 5)
6. Assign and print multiple variables
Hint: Assigns values to two variables and prints them.
1 a, b = 3, 4
2 print (a , b )
7. Swap two variables
Hint: Swaps values between two variables using Python syntax.
3
Muhammad Amir Mushtaq Python Practice
1 x, y = 1, 2
2 x, y = y, x
3 print (x , y )
8. Use type() to check variable type
Hint: Prints the type of a variable using type().
1 x = 10
2 print ( type ( x ) )
9. Use input() to read your name
Hint: Takes user input and prints it.
1 name = input ( ’ Enter your name : ’)
2 print ( ’ Hello ’ , name )
10. Convert input to integer
Hints: Reads input and converts it to an integer.
1 age = int ( input ( ’ Enter your age : ’) )
2 print ( ’ Age : ’ , age )
11. Add two numbers from user input
Hint: Reads two numbers from the user and prints their sum.
1 a = int ( input ( ’ Enter first number : ’) )
2 b = int ( input ( ’ Enter second number : ’) )
3 print ( ’ Sum : ’ , a + b )
12. Subtract two numbers
Hint: Subtracts the second number from the first.
1 a = int ( input ( ’ Enter first number : ’) )
2 b = int ( input ( ’ Enter second number : ’) )
3 print ( ’ Difference : ’ , a - b )
4
Muhammad Amir Mushtaq Python Practice
13. Multiply two numbers
Hint: Multiplies two user-input numbers.
1 a = int ( input ( ’ Enter first number : ’) )
2 b = int ( input ( ’ Enter second number : ’) )
3 print ( ’ Product : ’ , a * b )
14. Divide two numbers
Hint: Divides the first number by the second.
1 a = int ( input ( ’ Enter numerator : ’) )
2 b = int ( input ( ’ Enter denominator : ’) )
3 print ( ’ Quotient : ’ , a / b )
15. Find the remainder
hint: Find the remainder using the modulus operator.
1 a = int ( input ( ’ Enter dividend : ’) )
2 b = int ( input ( ’ Enter divisor : ’) )
3 print ( ’ Remainder : ’ , a % b )
17. Check if the number is negative
Hint: Checks whether a number is negative.
1 x = int ( input ( ’ Enter a number : ’) )
2 print ( ’ Negative ’ if x < 0 else ’ Not negative ’)
18. Check if the number is zero
Hint: Checks whether a number is exactly zero.
1 x = int ( input ( ’ Enter a number : ’) )
2 print ( ’ Zero ’ if x == 0 else ’ Not zero ’)
19. Print numbers from 1 to 5 using a for loop
Hint: Uses a for loop with range() to print numbers.
1 for i in range (1 , 6) :
2 print ( i )
5
Muhammad Amir Mushtaq Python Practice
22. Print odd numbers from 1 to 10
Hint: Uses a loop to print only odd numbers.
1 for i in range (1 , 11) :
2 if i % 2 != 0:
3 print ( i )
23. Sum of numbers from 1 to 100
Hint: Calculates the sum using a loop.
1 total = 0
2 for i in range (1 , 101) :
3 total += i
4 print ( ’ Sum : ’ , total )
24. Calculate the factorial of a number
Hint: Multiplies all integers up to the given number.
1 n = int ( input ( ’ Enter a number : ’) )
2 fact = 1
3 for i in range (1 , n + 1) :
4 fact *= i
5 print ( ’ Factorial : ’ , fact )
25. Check if the number is divisible by 3 and 5
Hint: Checks a number against both conditions.
1 n = int ( input ( ’ Enter a number : ’) )
2 print ( ’ Divisible by 3 and 5 ’ if n % 3 == 0 and n % 5 == 0 else ’
,→ Not divisible ’)
28. Count down from 5 to 1
Hint: Prints numbers in reverse using a loop.
1 for i in range (5 , 0 , -1) :
2 print ( i )
29. Break from the loop on condition
Hint: Use break to exit the loop early.
6
Muhammad Amir Mushtaq Python Practice
1 for i in range (10) :
2 if i == 5:
3 break
4 print ( i )
30. Continue loop on condition
Hint: Skips the current iteration using continue.
1 for i in range (5) :
2 if i == 2:
3 continue
4 print ( i )
3. Results
3.1 Output is in tabular form
See the Output at the second last page
3.2 By practicing the above exercises, beginners can:
• Understand the syntax and semantics of Python.
• Develop problem-solving skills through basic algorithms.
• Build confidence in using core programming constructs like loops, conditionals, and
functions.
4. Conclusion
This collection of Python practice questions is designed to strengthen your foundational
knowledge. Whether you’re new to programming or reviewing basics, these examples
offer hands-on practice incrementally and engagingly. This code looks very easy, but run
it and leave a comment if you like it.
Python is not difficult if we are good learners. Every programmer wants to clear their
whole life to remove the bugs and make their coding lead to a peak level.
Thanks for reading! Your feedback helps improve this content for everyone learning
Python.
Python, why we use it in our field, it’s an interesting question.
7
Muhammad Amir Mushtaq Python Practice
Name Output
Print “Hello World” Hello World
Print your name Amir
Assign a number to a variable 42
Assign a string to a variable Amir
Print result of addition 15
Assign and print multiple variables 34
Swap two variables 21
Use type() to check variable type <class ’int’>
Use input() to read your name Hello Muhammad Amir
Mushtaq
Convert input to integer Age: 20
Add two user inputs Sum: 6
Subtract two numbers Difference: 2
Multiply two numbers Product: 12
Divide two numbers Quotient: 5.0
Find the remainder Remainder: 1
Check if number is positive Positive
Check if number is negative Negative
Check if number is zero Not zero
Print 1 to 5 using for loop 12345
Print 1 to 5 using while loop 12345
Print even numbers from 1 to 10 2 4 6 8 10
Print odd numbers from 1 to 10 13579
Sum of 1 to 100 Sum: 5050
Factorial of a number Factorial: 24
Check divisibility by 3 and 5 Not divisible
Find largest of three numbers Largest: 3
Check leap year Leap year
Countdown from 5 to 1 54321
Break loop on condition 01234
Continue loop on condition (skips 2) 0134
Table 1: Name and Output of Basic Python Tasks
It is an open source programming language. Easy to understand, like our English litera-
ture. For every person not easy to use it.
If you see any error, feel free to tell me I correct it.
8
Muhammad Amir Mushtaq Python Practice
Figure 3: Practice is the biggest challenge
Figure 4: Python Practice Coding For You