Python Tutorials

First Python Program : print(“Hello, World!”)

Every great developer, from the engineers at Google to the creators of massive video games, started their journey with one simple tradition: making the computer say “Hello, World!”.

In Python, we achieve this using the print() function. This function is arguably the most fundamental and frequently used tool in the entire language. Its sole job is to take information from your code and display it on the screen (the console) so that you, or the user of your application, can read it.

Whether you are debugging a complex data algorithm or simply welcoming a user to your app, print() is your primary way of communicating with the outside world.

Syntax & Basic Usage

The syntax for print() is incredibly straightforward. You type the word print, followed by opening and closing parentheses (). Inside those parentheses, you place the text you want to display.

Because text is considered a String in programming, you must wrap your text in quotation marks so Python knows it is reading words, not computer commands.

# Our very first line of Python code!
# We tell the computer to print the exact phrase inside the quotes.
print("Hello, World!")

# Expected Output:
# Hello, World!
Code language: PHP (php)

Deep Dive & Variations

While the basic print() command is simple, there are a few important variations and hidden powers under the hood that you will use constantly.

1. Single vs. Double Quotes

Python is very flexible. You can use either double quotes (" ") or single quotes (' ') to define your text strings. Both work exactly the same way, but you must be consistent. If you start with a single quote, you must end with a single quote.

# Using double quotes
print("Learning Python is fun.")

# Using single quotes
print('I am writing code like a pro.')

# Expected Output:
# Learning Python is fun.
# I am writing code like a pro.
Code language: PHP (php)

2. Printing Multiple Items

You are not limited to printing just one piece of text. You can print multiple items inside a single print() function by separating them with a comma ,. Python will automatically insert a space between the items when it displays them.

# Printing multiple separate strings in one go
print("Python", "is", "awesome!")

# Expected Output:
# Python is awesome!
Code language: PHP (php)

3. The sep and end Parameters (Advanced)

By default, Python separates multiple items with a space. You can change this behavior using the sep (separator) parameter. Furthermore, every print() statement naturally ends by hitting the “Enter” key (creating a new line). You can alter this using the end parameter.

# Changing the default space to a dash using 'sep'
print("Apples", "Bananas", "Cherries", sep="---")

# Preventing Python from creating a new line at the end using 'end'
print("Downloading file...", end=" ")
print("Download complete!")

# Expected Output:
# Apples---Bananas---Cherries
# Downloading file... Download complete!
Code language: PHP (php)

Real-World Practical Examples

How does print() look in an actual software application? Here are two practical scenarios.

Scenario 1: A Video Game Welcome Screen

When a user launches an application or game, the terminal often displays a welcome sequence. We can use print() to simulate loading a player profile.

# Defining descriptive variables for our player's data
player_alias = "DragonSlayer99"
current_level = 42

# Displaying a formatted welcome sequence
print("--- SYSTEM BOOT ---")
print("Welcome back,", player_alias)
print("Loading Level", current_level, "...")

# Expected Output:
# --- SYSTEM BOOT ---
# Welcome back, DragonSlayer99
# Loading Level 42 ...
Code language: PHP (php)

Scenario 2: E-Commerce Order Receipt

Imagine you are writing the backend code for an online store. Once a customer checks out, you need to output their final billing status.

# Customer billing data
customer_full_name = "Jane Doe"
total_checkout_price = 150.75

# Displaying the final receipt
print("Thank you for your order, ", customer_full_name, "!", sep="")
print("Your credit card has been charged: $", total_checkout_price, sep="")

# Expected Output:
# Thank you for your order, Jane Doe!
# Your credit card has been charged: $150.75
Code language: PHP (php)

(Notice how we used sep="" to remove the default spaces, so the $ connects perfectly to the number!)

Best Practices & Common Pitfalls

As a beginner, you will inevitably make mistakes. Don’t worry—errors are just the computer’s way of asking for clarification. Here are the most common pitfalls to avoid:

  • Missing Parentheses: In older versions of Python (Python 2), you didn’t need parentheses. In modern Python 3, print "Hello" will instantly crash your program and throw a SyntaxError. Always use ().
  • Missing Quotation Marks: If you write print(Hello), Python thinks Hello is a variable, not text. Because you haven’t defined what Hello is, it will crash with a NameError. Always wrap text in quotes!
  • Case Sensitivity: Python is strictly case-sensitive. Print(), PRINT(), and print() are three entirely different things to the computer. The command must always be lowercase print().

Summary

  • The print() function is used to display output to the screen or console.
  • Text data must be wrapped inside single or double quotes so Python recognizes it as a String.
  • You can print multiple items on the same line by separating them with commas.
  • Advanced parameters like sep and end allow you to customize how your output is formatted.
  • Watch out for common syntax errors, like forgetting your parentheses or typing a capital “P”.

Congratulations! You have officially written your first Python code and commanded the computer to speak. Next, we will dive deeper into how to store and manipulate different types of data using Variables.

Leave a Comment