๐ฆ Lesson 2: Python Variables & Data Types
โฑ๏ธ Estimated time: 20 minutes | Difficulty: Beginner
Dynamic Suffix
Python is dynamically typed, meaning you don't need to specify the type of data when you create a variable. Python handles it for you!
๐ฏ Your Goal: Write "Hello, World!"
Every programmer writes this program as their first one. It's a rite of passage! ๐
Step 1: Open Your Text Editor
- Open VS Code (or Notepad if you don't have VS Code)
- Create a new file
- Go to File โ New File
Step 2: Write Your First Program
Type (don't copy-paste!) the following code:
print("Hello, World!")
That's it! Just one line of code. Here's what it means:
- print() = a command that displays text
- "Hello, World!" = the text you want to display
- ( ) = parentheses tell Python this is a function
Step 3: Save Your File
- Press Ctrl + S (or File โ Save)
- Name it:
hello.py(the .py is important!) - Save it somewhere you'll remember (like your Desktop)
Step 4: Run Your Program!
- Open Command Prompt (CMD) or PowerShell
- Navigate to your file's folder (type:
cd Desktop) - Type:
python hello.py - Press Enter
๐ You should see: Hello, World!
Let's Get Fancier!
Now try this code:
print("Hello, World!")
print("My name is Python!")
print("I'm awesome!")
print("")
print("Welcome to programming!")
Run this program and you'll see multiple lines of output! Each print() creates a new line.
More Examples to Try:
Example 1: Using Numbers
print(5 + 3)
print(10 * 4)
print(20 / 5)
Output: 8 40 4.0
Example 2: Combining Text and Numbers
print("I have", 5, "apples")
Output: I have 5 apples
Example 3: Getting Input from Users
name = input("What is your name? ")
print("Hello,", name)
When you run this, it asks for your name, then says hello!
Common Beginner Mistakes:
โ Mistake 1: Forgetting parentheses
print "Hello" (WRONG)
print("Hello") (CORRECT)
โ Mistake 2: Forgetting quotes around text
print(Hello World) (WRONG)
print("Hello World") (CORRECT)
โ Mistake 3: Wrong file extension
hello.txt (WRONG)
hello.py (CORRECT)
๐ Key Points
- print() shows text or values on the screen.
- Strings (text) must be wrapped in quotes:
"hello"or'hello'. - Numbers can be used directly:
5 + 3returns8. - Use
input()to read user input from the console. - Save your scripts with the
.pyextension and run withpython file.py. - Common beginner errors: missing parentheses, wrong quotes, wrong file extension.
๐งช Try It Online
Quick online editors to experiment with code (safe and instant):
๐ Practice Exercises
- Create a program that asks for the user's age and prints "You are X years old".
- Write a small calculator that asks for two numbers and prints their sum, difference, product, and quotient.
Show sample answers
# Exercise 1
age = input("How old are you? ")
print("You are", age, "years old")
# Exercise 2
x = float(input("Enter number 1: "))
y = float(input("Enter number 2: "))
print("Sum:", x + y)
print("Diff:", x - y)
print("Product:", x * y)
print("Quotient:", x / y if y != 0 else "undefined")
๐ Further Reading
Learn more reference-style material and examples from the originals:
- W3Schools Python Tutorial โ clear, example-driven lessons.
- GeeksforGeeks Python โ deeper articles and problem-focused explanations.
โ Quick Quiz - Test Your Knowledge!
โ Question 1: What command displays text in Python?
โ Question 2: What is the correct file extension for Python programs?
โ Question 3: What does this code do? print(5 + 3)
๐ฏ Challenge
Try writing a program that:
- Asks the user for their favorite color
- Displays "Your favorite color is [color]"
- Save as
favorite_color.pyand run it
Hint: Use input() to get the
user's input and print() to display it.