Practice Exercises: Python Basics
Welcome to your first set of python basics exercises! If you’re just starting out with Python and want to build a solid foundation, you’re in the right…
Welcome to your first set of python basics exercises! If you’re just starting out with Python and want to build a solid foundation, you’re in the right place. Practicing with small, hands-on tasks is one of the best ways to learn Python by doing. Remember, it’s perfectly normal to make mistakes—that’s how you learn and grow as a programmer.
Let’s jump in and start building your Python skills!
Why Practice Python Basics?
You might wonder why practice is so important when learning Python. Here’s why:
- Practice turns knowledge into real skills. Reading about variables or input is helpful, but actually using them makes the ideas stick.
- Repetition makes things easier. The more you work with Python’s building blocks, the more comfortable you’ll become.
- Solving small problems prepares you for bigger projects. Each exercise is a stepping stone toward writing your own programs.
Whether you’re aiming for a programming job or just want to understand how code works, these python basics exercises will help you gain confidence.
How to Use These Exercises
To get the most out of these python beginner practice tasks:
- Try each exercise on your own first. Resist the urge to peek at hints or solutions right away.
- Don’t stress about mistakes. Every error helps you learn something new.
- Use any Python environment you like. You can write code in an online editor (like Replit or Trinket), IDLE, or even a plain text editor.
Ready? Let’s dive into your first exercise!
Exercise 1: Practice with Variables
Variables are like containers that store information for your program. Let’s get comfortable working with them!
Tasks:
- Create a variable called
nameand set it to your first name as a string (text). - Make a variable named
ageand set it to your age as a number. - Change the value of
ageto a new number (maybe your age next year). - Print both variables to the screen.
Bonus: Try creating a variable with an invalid name (like 2cool) and see what error you get. What do you learn about naming rules?
Common beginner questions:
-
What happens if I forget to use quotes around my name when creating the
namevariable?
If you forget the quotes, Python will look for a variable with that name and give an error if it doesn’t exist. Always use quotes for text! -
Why do I get an error when I try to name a variable
2cool?
Variable names can’t start with a number. Try starting with a letter or underscore instead.
Quiz Question 1
Question: Which of the following variable names is valid in Python?
- A) 2nd_place
- B) user-name
- C) userName
- D) print
Exercise 2: Working with Numbers
Python makes math easy! Let’s practice some basic operations.
Tasks:
- Create two variables,
aandb, and assign them any numbers you like. - Add
aandbtogether and store the result in a new variable calledsum. - Subtract
bfromaand store the result indifference. - Multiply
aandband store the result inproduct. - Divide
abyband store the result inquotient. - Print all the results.
Bonus: Try using decimal numbers (like 3.5 or 2.0) instead of just whole numbers.
Common beginner questions:
-
Why does dividing two integers sometimes give me a decimal result?
In Python 3, dividing with/always gives a decimal (float) result, even if both numbers are integers. -
If I enter a decimal number using input(), how do I make sure it stays a decimal and not an integer?
Usefloat()instead ofint()to convert the input.
Quiz Question 2
Question: If you want to get a number from the user and double it, which code will work correctly?
- A) num = input('Enter a number: ')\ndouble = num * 2\nprint(double)
- B) num = int(input('Enter a number: '))\ndouble = num * 2\nprint(double)
- C) num = input('Enter a number: ')\ndouble = int(num) * 2\nprint(double)
Exercise 3: Playing with Strings
Strings are how Python stores text. Let’s practice creating and combining them.
Tasks:
- Make a variable called
greetingand set it to "Hello". - Make another variable called
nameand set it to your name. - Combine
greetingandnameinto a new variable calledmessageso that it says something like "Hello Alice". - Print
messageto the screen.
Bonus: Add an exclamation mark or a smiley face to your message for extra flair!
Common beginner questions:
-
How do I add a space between 'Hello' and my name when combining strings?
You can add a space inside the greeting, or add" "between the two strings when joining them. -
What is the difference between using single quotes and double quotes for strings?
Both work the same in Python. Use whichever you prefer, but be consistent.
Quiz Question 3
Question: How can you join two strings together in Python?
- A) With the
*operator - B) By putting them in a list
- C) With the
+operator - D) By using
printonly
Exercise 4: Getting User Input
Python can ask the user for information using the input() function. Let’s try it out.
Tasks:
- Ask the user for their favorite color and store it in a variable called
color. - Print a message that says "Your favorite color is ___", filling in the blank with what the user typed.
- Ask the user for a number, store it in a variable, and print double that number.
Tip: Remember, input() always gives you a string. If you want to do math, you’ll need to convert it using int() or float().
Common beginner questions:
-
How do I change a string input from the user into a number so I can do math with it?
Useint()for whole numbers, orfloat()for decimals. For example:age = int(input("Enter your age: ")) -
Is there a way to check what type of data a variable holds?
Yes! Use thetype()function, liketype(age).
Quiz Question 4
Question: What does input("Enter your name: ") do in Python?
- A) Prints the user's name
- B) Asks the user to enter their name and stores it as a string
- C) Converts the user's input to an integer
- D) Creates a variable called name
Mini Quiz: Check Your Understanding
Test yourself with these quick questions. Try to answer before checking the answer key at the end!
Quiz Question 5
Question: What happens if you try to use a variable name that starts with a number?
- A) Python will accept it
- B) Python will ignore it
- C) Python will give an error
- D) Python will rename it automatically
Exercise 5: Putting It All Together
Let’s combine what you’ve learned in a mini-project!
Task:
Write a program that:
- Asks the user for their name.
- Asks the user for their age.
- Calculates what year they will turn 100 years old.
- Prints a message like: "Hi Alice! You will turn 100 in the year 2090."
Hints:
- You’ll need to use variables,
input(), math, and string concatenation. - Remember to convert the age input to an integer for calculations.
Feel free to get creative—add more questions or personalize the message!
Quiz Question 6
Question: If a = 5 and b = 2, what is the value of a / b in Python 3?
- A) 2
- B) 2.5
- C) 2.0
- D) 3
Quiz Question 7
Question: How do you print the value of a variable called score?
- A) print(score)
- B) print 'score'
- C) print = score
- D) echo(score)
Next Steps in Your Python Journey
Great job completing these python basics exercises! The more you practice, the more confident you’ll become. Here are some ideas for what to explore next:
- Practice with lists, loops, and functions.
- Try more python beginner practice exercises on learnpyfast.com.
- Build small projects, like a calculator or a quiz game.
Remember: learning to code is a journey. Every bit of practice gets you closer to your goals. Keep experimenting, keep learning, and most importantly—have fun with Python!
Quiz Answer Key
Question 1
Correct answer: C) userName
Explanation: Variable names cannot start with a number, cannot contain dashes, and should not use reserved words like print. userName is valid.
Question 2
Correct answer: B) num = int(input('Enter a number: '))
double = num * 2
print(double)
Explanation: input() returns a string, so you need to convert it to an integer with int() before doing math.
Question 3
Correct answer: C) With the + operator
Explanation: The + operator joins (concatenates) two strings together in Python.
Question 4
Correct answer: B) Asks the user to enter their name and stores it as a string
Explanation: The input() function displays the prompt and waits for the user to type something, returning it as a string.
Question 5
Correct answer: C) Python will give an error
Explanation: Variable names cannot start with a number. Python will show a syntax error if you try.
Question 6
Correct answer: B) 2.5
Explanation: In Python 3, dividing two integers with / gives a float (decimal) result.
Question 7
Correct answer: A) print(score)
Explanation: Use print(score) to display the value of the variable score in Python.