Your First Python Program
Welcome to your first Python programming lesson! In this tutorial, you'll learn how to write and run your very first Python program. Don't worry if you're completely new to programming - we'll start simple and build from there.
The Classic "Hello, World!"
Every programming journey begins with the traditional "Hello, World!" program. It's a simple program that displays a message on the screen.
Writing the Code
Open your text editor or Python IDE and type the following:
print("Hello, World!")
That's it! You've written your first Python program. Let's break it down:
print()is a built-in Python function that displays text on the screen- The text inside the parentheses and quotes is called a "string"
- The quotes (either single
'or double") tell Python that this is text, not code
Running Your Program
Method 1: Using the Command Line
- Save your file as
hello.py(the.pyextension tells your computer it's a Python file) - Open your terminal or command prompt
- Navigate to where you saved the file
- Type:
python hello.pyand press Enter
Method 2: Using Python's Interactive Mode
- Open your terminal
- Type
python(orpython3on some systems) and press Enter - You'll see the Python prompt:
>>> - Type your code directly:
print("Hello, World!") - Press Enter to see the result
Understanding the Output
When you run the program, you should see:
Hello, World!
Congratulations! You've successfully run your first Python program.
Let's Make It More Interesting
Now let's create a more interactive program that asks for your name:
# This is a comment - Python ignores this line
name = input("What's your name? ")
print("Hello, " + name + "!")
print("Welcome to Python programming!")
New Concepts Explained
- Comments: Lines starting with
#are comments. They help explain your code but don't run - Variables:
nameis a variable that stores the user's input - input(): This function asks the user to type something
- String concatenation: The
+operator joins strings together
Running the Interactive Program
Save this as greet.py and run it. You'll see something like:
What's your name? Alice
Hello, Alice!
Welcome to Python programming!
A Slightly More Complex Example
Let's create a simple calculator:
# Simple Calculator
print("=== Simple Calculator ===")
# Get two numbers from the user
num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: "))
# Perform calculations
addition = num1 + num2
subtraction = num1 - num2
multiplication = num1 * num2
division = num1 / num2
# Display results
print("\nResults:")
print(f"Addition: {num1} + {num2} = {addition}")
print(f"Subtraction: {num1} - {num2} = {subtraction}")
print(f"Multiplication: {num1} * {num2} = {multiplication}")
print(f"Division: {num1} / {num2} = {division}")
New Concepts
- float(): Converts text input to a decimal number
- Mathematical operations:
+,-,*,/for basic math - f-strings: The
f""syntax is a modern way to insert variables into strings - \n: Creates a new line in the output
Understanding Python Syntax
Python has some unique characteristics:
Indentation Matters
Unlike many programming languages that use curly braces {}, Python uses indentation to group code:
# This will work
if True:
print("This is indented")
print("This is also indented")
# This will cause an error
if True:
print("This is not indented correctly")
Case Sensitivity
Python is case-sensitive, meaning Print and print are different:
print("This works") # Correct
Print("This doesn't") # Error - capital P
Common Beginner Mistakes
1. Forgetting Quotes
# Wrong
print(Hello, World!)
# Correct
print("Hello, World!")
2. Mixing Quote Types
# Wrong
print("Hello, World!')
# Correct
print("Hello, World!") # or print('Hello, World!')
3. Forgetting Parentheses
# Wrong
print "Hello, World!"
# Correct
print("Hello, World!")
Practice Exercises
Try these exercises to reinforce what you've learned:
Exercise 1: Personal Information
Write a program that asks for your name, age, and favorite color, then displays them in a formatted message.
Exercise 2: Simple Math
Create a program that asks for a number and displays its square (the number multiplied by itself).
Exercise 3: Temperature Converter
Write a program that converts Celsius to Fahrenheit using the formula: F = (C × 9/5) + 32
Sample Solutions
Exercise 1 Solution:
name = input("Enter your name: ")
age = input("Enter your age: ")
color = input("Enter your favorite color: ")
print(f"Hi {name}! You are {age} years old and your favorite color is {color}.")
Exercise 2 Solution:
number = float(input("Enter a number: "))
square = number * number
print(f"The square of {number} is {square}")
Exercise 3 Solution:
celsius = float(input("Enter temperature in Celsius: "))
fahrenheit = (celsius * 9/5) + 32
print(f"{celsius}°C is equal to {fahrenheit}°F")
Debugging Tips
When your program doesn't work as expected:
- Read error messages carefully - they often tell you exactly what's wrong
- Check your spelling -
prnitinstead ofprintwon't work - Check your quotes and parentheses - make sure they match
- Use print statements to see what's happening in your code
What's Next?
You've successfully written and run your first Python programs! You've learned about:
- The
print()function - Variables and user input
- Basic string operations
- Comments
- Simple mathematical operations
Next Steps:
Key Takeaways
- Python is designed to be readable and simple
- The
print()function displays output - Variables store information
- Indentation is important in Python
- Practice makes perfect!
Remember: Every expert programmer started with "Hello, World!" Don't worry if things seem confusing at first - programming is a skill that improves with practice.
