Now that you've learned about printing to the console and the fundamental data types in Python (integers, floats, strings, and booleans), it's time to put your knowledge to the test with some simple exercises. These exercises will help solidify your understanding and build your confidence as you take your first steps into the world of Python programming.
Exercise 1: Personalized Greeting
Your task is to create a program that asks the user for their name and then prints a personalized greeting. This exercise will reinforce your understanding of the input() function and string concatenation.
name = input("Enter your name: ")
print("Hello, " + name + "! Welcome to Python.")Exercise 2: Basic Calculator (Addition)
For this exercise, you'll write a program that takes two numbers as input from the user, adds them together, and then prints the result. Remember that input() returns a string, so you'll need to convert your inputs to integers or floats before performing the addition. This will give you practice with type conversion.
num1_str = input("Enter the first number: ")
num2_str = input("Enter the second number: ")
num1 = int(num1_str) # Convert string to integer
num2 = int(num2_str) # Convert string to integer
result = num1 + num2
print("The sum is: " + str(result)) # Convert result back to string for printingExercise 3: Information Display
Create a program that stores your name, age, and favorite color in variables. Then, print this information in a nicely formatted sentence. This exercise will help you practice variable assignment and printing multiple pieces of information.
my_name = "Alice"
my_age = 25
favorite_color = "blue"
print("My name is " + my_name + ", I am " + str(my_age) + " years old, and my favorite color is " + favorite_color + ".")Exercise 4: Boolean Logic Check
Write a program that defines two boolean variables, for example, is_raining = True and is_sunny = False. Then, print the results of logical operations like and, or, and not applied to these variables. This will familiarize you with boolean operators.