🚀 Your First Program
Time to write your first Python program! Every programmer remembers their first program, and Python makes it easy and fun. You'll learn the basics of displaying messages, getting user input, and working with variables—all with code that reads almost like English.
Let's start with the classic first program that every programmer writes:
# Your first Python program!
print("Hello, World!")
print("Welcome to Python programming!")
# Make it personal
name = "Future Programmer"
print(f"Hello, {name}!")
👋 The Classic "Hello, World!"
"Hello, World!" is the traditional first program that most programmers write when learning any language. It's a simple program that displays text on the screen, but it's an important milestone!
🎯 Why "Hello, World!" Matters:
- Tests your setup: Proves Python is working correctly
- Programming tradition: Used across all programming languages since 1972
- Immediate success: You see results right away
- Foundation: Introduces basic output functionality
# The classic Hello World program
print("Hello, World!")
# You can use single or double quotes
print('Hello, World!')
# Print multiple messages
print("Python is fun!")
print("I'm learning to code!")
📄 The print() Function
The print() function is your tool for displaying information on the screen. It's one of the most important functions you'll use in Python.
# Different ways to use print()
print("Hello, World!")
print('Single quotes work too!')
# Print numbers
print(42)
print(3.14)
# Print multiple things at once
print("My name is", "Alice", "and I am", 25, "years old")
# Print with calculations
print("2 + 3 =", 2 + 3)
💬 Getting User Input with input()
The input() function lets your program ask questions and get answers from users. This makes your programs interactive!
📋 Key Facts About input():
- Pauses the program until user types something
- Always returns a string (text), even if user types numbers
- Shows a prompt to tell users what to type
# Getting user input
name = input("What's your name? ")
print(f"Nice to meet you, {name}!")
age = input("How old are you? ")
print(f"Wow, {age} is a great age!")
# Ask multiple questions
city = input("What city are you from? ")
hobby = input("What's your favorite hobby? ")
print(f"So {name}, you're {age} years old, from {city}, and you love {hobby}!")
📦 Working with Variables
Variables are like containers that store information. They let you save data and use it later in your program.
📝 Variable Rules in Python:
- Valid names:
user_name
,age
,favorite_color
- Invalid names:
2name
(can't start with number),user-age
(no hyphens),class
(reserved word) - Case sensitive:
age
andAge
are different - Use descriptive names:
name
is better thanx
# Creating and using variables
name = "Alice"
age = 25
favorite_color = "blue"
print(f"Name: {name}")
print(f"Age: {age}")
print(f"Favorite color: {favorite_color}")
# Variables can change
age = 26
print(f"Next year I'll be {age}")
# Variables can be used in calculations
birth_year = 2024 - age
print(f"I was born in {birth_year}")
🔤 String Formatting with f-strings
f-strings are Python's modern way to insert variable values into text. They make your output clean and readable.
🔧 How f-strings Work:
- Start with
f
before the quotes:f"Hello {name}"
- Put variables inside curly braces:
{variable_name}
- Much cleaner than older methods
# f-string examples
name = "Bob"
age = 30
height = 5.8
# Basic f-string
print(f"Hello, {name}!")
# Multiple variables
print(f"{name} is {age} years old and {height} feet tall")
# With calculations
print(f"In 10 years, {name} will be {age + 10} years old")
# With formatting
price = 19.99
print(f"The price is ${price:.2f}")
🔢 Simple Math in Python
Python makes math easy! You can use variables and numbers together to create useful calculations.
➕ Basic Math Operators:
- Addition:
+
(2 + 3 = 5) - Subtraction:
-
(5 - 2 = 3) - Multiplication:
*
(3 * 4 = 12) - Division:
/
(10 / 2 = 5.0)
# Simple calculator program
print("=== SIMPLE CALCULATOR ===")
# Get numbers from user
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
# Do the math
addition = num1 + num2
subtraction = num1 - num2
multiplication = num1 * num2
division = num1 / num2
# Show results
print(f"{num1} + {num2} = {addition}")
print(f"{num1} - {num2} = {subtraction}")
print(f"{num1} * {num2} = {multiplication}")
print(f"{num1} / {num2} = {division:.2f}")
📁 Creating Your First Python File
Let's create a real Python program file that you can save and run!
📄 File Requirements:
- File extension: Must end with
.py
- Good names:
hello.py
,my_first_program.py
,calculator.py
- Avoid: Spaces, special characters, starting with numbers
# Save this as "my_first_program.py"
print("=== MY FIRST PYTHON PROGRAM ===")
print("Created by: [Your Name]")
print()
# Get user information
name = input("What's your name? ")
age = int(input("What's your age? "))
city = input("What city are you from? ")
print(f"\nHello {name}!")
print(f"You're {age} years old and from {city}.")
# Fun calculation
birth_year = 2024 - age
next_birthday = age + 1
print(f"You were probably born in {birth_year}")
print(f"Next year you'll be {next_birthday} years old!")
print("\nCongratulations on your first Python program! 🎉")
🔄 Common Programming Patterns
As you write more programs, you'll see these patterns over and over:
Input-Process-Output Pattern:
- Input: Get data from user
- Process: Do calculations or logic
- Output: Show results
This is the foundation of most interactive programs!
📚 Key Concepts Summary
You've learned the fundamental building blocks of Python programming:
Concept | What It Does | Example |
---|---|---|
print() | Displays text on screen | print("Hello!") |
input() | Gets user input (returns string) | name = input("Name? ") |
Variables | Store data for later use | age = 25 |
f-strings | Insert variables into text | f"Hello {name}!" |
Math operators | +, -, *, / for calculations | total = 5 + 3 |
.py files | Save Python programs | my_program.py |
🧠 Test Your Knowledge
Ready to test what you've learned about your first Python program?
🚀 What's Next?
Congratulations! You've written your first Python program and learned the basic building blocks. Next, we'll explore how Python code is organized and structured.
Continue to: Python Code Structure
You're doing great! Keep coding! 🚀
Was this helpful?
Track Your Learning Progress
Sign in to bookmark tutorials and keep track of your learning journey.
Your progress is saved automatically as you read.