Courses/Python Programming/Python Basics - Print and Variables

Python Basics - Print and Variables

Your first Python program

Beginner⏱️ 10 minutes

Lesson Content

Welcome to Python!

What is Python?

Python is one of the world's most popular programming languages. It's powerful yet beginner-friendly, with clear, readable syntax that looks almost like English!

Why Learn Python?

  • Versatile: Web development, data science, AI, automation, games
  • In-Demand: Top language for jobs in tech
  • Beginner-Friendly: Clean syntax, less code than other languages
  • Huge Community: Millions of developers, tons of resources
  • Industry Standard: Used by Google, Netflix, NASA, Instagram

How Python Works

You write code, Python interpreter reads it line-by-line, executes instructions, and shows output.

The Print Function - Your First Output!

What is print()?

Displays text or data to the screen (console/terminal).

print("Hello, World!")           # Displays text
print("I love Python!")          # Another message
print(42)                        # Can print numbers
print(3.14, "is pi")            # Multiple values

Why print() Matters

  • Debugging: See what your code is doing
  • User Feedback: Show messages to users
  • Testing: Check if values are correct
  • Learning: Understand how code executes

Variables - Storing Information

What Are Variables?

Named containers that store data. Unlike math, = means "assign" not "equals".

name = "Alice"        # String (text)
age = 25              # Integer (whole number)
height = 5.6          # Float (decimal)
is_student = True     # Boolean (True/False)

Python is Dynamically Typed

You don't declare types—Python figures it out!

x = 10        # x is a number
x = "Hello"   # Now x is a string - Python doesn't care!

F-Strings - Modern String Formatting

What Are F-Strings?

The best way to insert variables into text. Put f before the quote and use {variable}.

name = "Bob"
age = 30
print(f"My name is {name} and I am {age} years old")
# Output: My name is Bob and I am 30 years old

# Can do math inside {}
price = 19.99
quantity = 3
print(f"Total: {price * quantity}")
# Output: Total: 59.97

Old Ways (Don't Use)

# String concatenation (messy!)
print("Name: " + name + " Age: " + str(age))

# .format() (outdated)
print("Name: {} Age: {}".format(name, age))

# F-strings are cleaner! ✅
print(f"Name: {name} Age: {age}")

Real-World Example: User Profile

username = "techguru42"
email = "guru@email.com"
follower_count = 1523
is_verified = True

print(f"Profile: @{username}")
print(f"Email: {email}")
print(f"Followers: {follower_count}")
print(f"Verified: {is_verified}")

Your Turn: Experiment!

Task: Create variables for your name and age, then print a formatted message.

Experiments to Try:

  1. Print Different Types: Try printing strings, numbers, booleans
  2. Multiple Variables: Create 5 variables and print them all in one f-string
  3. Math in F-Strings: print(f"10 + 5 = {10 + 5}")—the math executes!
  4. Change Variable Values: Assign a new value to a variable, print before and after
  5. No F-String: Try without the f—see {name} prints literally!
  6. Quotes: Try single quotes 'text' vs double quotes "text"—both work!
python
Loading...
Expected Output: Hello
Next (Complete this lesson first)
Data Types and Type Conversion