The Print Function - Your First Communication with Python

Welcome to your first Python lesson! The print() function is like a bridge between you and the computer - it's how your program can "speak" and show you information. Think of it as the computer's voice.

In this lesson, you'll learn:

1. Your First Print Statement

Let's start with the most basic use of print(). Every print statement follows this pattern:

print(what_you_want_to_show)

Notice the parentheses - they're required! Let's see it in action:

print("Hello, World!")
print("Welcome to Python programming!")

Notice how each print() statement creates a new line. This is the default behavior - each print starts on a fresh line.

Exercise: Your First Print

Try writing your first print statement. Print the message "Python is awesome!" exactly as shown.

# TODO: Print the message "Python is awesome!"

2. Printing Text (Strings) vs Numbers

Python treats text and numbers differently:

This difference is important! Let's explore what happens with each type:

# Printing strings (text) - notice the quotes
print("This is text")
print('Single quotes work too')
print("The number 42 as text")

Numbers don't need quotes:

# Printing numbers - no quotes needed
print(42)
print(3.14)
print(-17)

Notice the difference between text that looks like numbers and actual numbers:

# What's the difference?
print("42")    # This is text that looks like a number
print(42)      # This is actually the number 42

Exercise: Strings and Numbers

Practice printing both strings and numbers. Print the string "Age:" and then print the number 25 on the next line.

# TODO: Print the string "Age:" # TODO: Print the number 25

3. Printing Multiple Things at Once

One of the most useful features of print() is that you can print multiple things in a single statement by separating them with commas. Python automatically adds spaces between the items.

Pattern: print(item1, item2, item3, ...)

# Printing multiple items with commas
print("My name is", "Alice")
print("I am", 25, "years old")
print("Temperature:", 72, "degrees")

# Mixing strings and numbers
print("The answer is", 42)
print("Pi equals approximately", 3.14159)
print("Score:", 100, "out of", 150)

Notice how Python automatically puts a space between each item when you use commas. This makes it easy to build messages that combine text and numbers.

Exercise: Multiple Items

Print the phrase "I have 3 cats and 2 dogs" using multiple arguments to print(). Break it into separate strings and numbers.

# TODO: Print "I have", the number 3, "cats and", the number 2, "dogs" # Use commas to separate the different parts

4. More Complex Examples

Let's see more examples of how to combine strings and numbers effectively. This is a skill you'll use constantly in Python programming.

# Creating informative messages
print("Today's date:", "January", 15, 2024)
print("Items in cart:", 7)
print("Total cost: $", 23.45)

You can also show calculations in your print statements:

# Multiple calculations shown
print("Length:", 10, "Width:", 5, "Area:", 10 * 5)
print("First number:", 8, "Second number:", 3, "Sum:", 8 + 3)

Here's a fun formatting trick:

# Fun with formatting
print("=" * 20)  # This prints 20 equal signs
print("Welcome to our store!")
print("=" * 20)

Exercise: Restaurant Bill

Create a restaurant bill printout. Print the meal cost ($18.50), tax ($2.22), and total ($20.72) using the format shown in the expected output.

# TODO: Print "Meal cost: $" followed by 18.50 # TODO: Print "Tax: $" followed by 2.22 # TODO: Print "Total: $" followed by 20.72

5. Common Mistakes and How to Avoid Them

Let's look at some common mistakes beginners make with the print function, so you can avoid them:

# Common mistakes (these will cause errors):

# 1. Forgetting parentheses
# print "Hello"  # This would cause an error

# 2. Forgetting quotes around text
# print(Hello)   # This would cause an error

# 3. Mixing quote types incorrectly
# print("Hello')  # This would cause an error

# Correct versions:
print("Hello")           # Correct: quotes and parentheses
print("Mixed quotes work", 'when done right')
print('You can use single quotes throughout')

Remember: numbers don't need quotes, but the behavior is different:

# Numbers don't need quotes
print(42)               # Correct: number without quotes
print("42")             # Also correct: number as text

# The difference:
print(10 + 5)           # This prints 15 (math)
print("10 + 5")         # This prints "10 + 5" (text)

Exercise: Fix the Mistakes

The code below has the right idea but some syntax errors. Fix it so it prints correctly. The goal is to print "Score: 95 out of 100".

# This code has errors - fix them: # print(Score:, 95, out of, 100) # TODO: Add quotes around the text parts and fix any other issues

6. Building Longer Messages

As your programs get more complex, you'll often need to print longer, more detailed messages. Here's how to handle that:

# Creating detailed output
print("Welcome to the Grade Calculator!")
print("Student:", "Sarah Johnson")
print("Course:", "Introduction to Python")
print("Assignments completed:", 8, "out of", 10)
print("Average score:", 87.5, "percent")
print("Letter grade:", "B+")
print("Congratulations on your progress!")

Notice how we use multiple print statements to create organized, readable output. Each print creates a new line, making the information easy to read.

Exercise: Personal Introduction

Create a personal introduction that prints information about a fictional character. Include their name, age, favorite number, and a hobby. Use multiple print statements with mixed strings and numbers.

# TODO: Print "Name: Alex Rodriguez" # TODO: Print "Age:" followed by the number 22 # TODO: Print "Favorite number:" followed by the number 7 # TODO: Print "Hobby: photography" # TODO: Print "Fun fact: I have visited" followed by 15 followed by "countries!"

🎯 Bring It All Together: A Complete Report

Let's combine everything you've learned to create a comprehensive example that shows off all the print function capabilities.

# Complete Weather Report Example
print("=" * 30)
print("DAILY WEATHER REPORT")
print("=" * 30)
print("Date:", "March", 15, 2024)
print("Location:", "Denver, Colorado")
print()  # Empty print creates a blank line

print("CURRENT CONDITIONS:")
print("Temperature:", 68, "degrees Fahrenheit")
print("Humidity:", 45, "percent")
print("Wind speed:", 12, "mph")
print("Sky condition:", "Partly cloudy")

print()  # Another blank line

print("FORECAST:")
print("High temperature:", 75, "degrees")
print("Low temperature:", 52, "degrees")
print("Chance of rain:", 20, "percent")

print()
print("Have a great day!")

Final Exercise: Create Your Own Report

Create a book report using everything you've learned. Include the title, author, page count, rating (out of 5), and a recommendation message.

# TODO: Print "BOOK REPORT" # TODO: Print "Title: The Python Guide" # TODO: Print "Author: Jane Smith" # TODO: Print "Pages:" followed by the number 342 # TODO: Print "My rating:" followed by 4, "out of", 5, "stars" # TODO: Print "Recommendation: Great for beginners!"

📝 What You've Learned

Congratulations! You now know how to use Python's print function effectively:

The print function is your primary tool for getting information out of your programs. Master it, and you'll be able to see what your code is doing every step of the way!

Next Steps

Now that you can display information, the next step is learning how to store information in variables. That way, you can save data and use it in your print statements!