Loading...

Section

Putting It All Together: Simple Exercises

Part of The Prince Academy's AI & DX engineering stack.

Follow The Prince Academy Inc.

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 printing

Exercise 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.

is_raining = True
is_sunny = False

print("Is it raining AND sunny?", is_raining and is_sunny)
print("Is it raining OR sunny?", is_raining or is_sunny)
print("Is it NOT raining?", not is_raining)

These exercises are designed to be a gentle introduction to writing Python code. Don't worry if you make mistakes; that's a crucial part of the learning process! As you become more comfortable, you can explore more complex exercises that involve combining these concepts.

graph TD;
    A[Start]
    B{Get User Input for Name}
    C[Print Greeting]
    D[End]

    A --> B
    B --> C
    C --> D