Lesson 1: Introduction to Python#
Welcome to your first programming lesson! Python is a beginner-friendly programming language thatโs:
Easy to read: Looks almost like English
Powerful: Used by Google, Netflix, NASA, and more
Versatile: Web development, data science, AI, automation, games
What Youโll Learn#
How to run Python code
Printing messages to the screen
Comments and documentation
Your first programs
1. Hello, World!#
The traditional first program in any language is to print โHello, World!โ to the screen.
Click on the cell below and press Shift+Enter to run it:
print("Hello, World!")
๐ Congratulations! You just ran your first Python program!
How it works:#
print()is a function that displays textThe text inside quotes (
"Hello, World!") is called a stringStrings must be wrapped in quotes (single
'or double")
2. Printing Different Messages#
You can print anything you want:
print("Python is awesome!")
print("I am learning to code")
print("2024")
โ๏ธ Try It Yourself#
Modify the code below to print your own message:
# Type your code here
print("Your message here")
4. Printing Multiple Lines#
You can print multiple lines in different ways:
# Method 1: Multiple print statements
print("First line")
print("Second line")
print("Third line")
# Method 2: Using \n (newline character)
print("First line\nSecond line\nThird line")
# Method 3: Triple quotes for multiple lines
print("""
First line
Second line
Third line
""")
5. Printing with Separators#
The print() function can print multiple items:
# Print multiple items (separated by space by default)
print("Hello", "World")
print("Python", "is", "fun")
# Custom separator
print("Python", "Java", "JavaScript", sep=" | ")
print("2024", "10", "30", sep="-")
6. Printing Numbers#
You can print numbers without quotes:
print(42)
print(3.14)
print(2 + 2) # Python evaluates math first
print("The answer is:", 42) # Mix text and numbers
โ ๏ธ Common Mistake:#
print("2 + 2") # This prints the text "2 + 2"
print(2 + 2) # This calculates and prints 4
7. Simple ASCII Art#
You can create simple drawings using print statements:
print(" * ")
print(" *** ")
print("*****")
print(" ||| ")
# A simple face
print(" O O ")
print(" ^ ")
print(" \_/ ")
๐ Exercises#
Exercise 1: Personal Introduction#
Create a program that prints:
Your name
Your age (or any number)
Your favorite hobby
Each should be on a separate line.
# Your code here
Exercise 2: Math Display#
Print the following calculations (let Python do the math):
15 + 27
100 - 42
8 * 7
Make it look nice with labels, like: "15 + 27 =" followed by the answer.
# Your code here
Exercise 3: Create ASCII Art#
Create your own simple ASCII art! Ideas:
A house
A car
Your initials
An animal
Get creative!
# Your ASCII art here
Exercise 4: Story Printer#
Write a short 5-line story using print statements. Make it interesting!
# Your story here
Exercise 5: Pattern Challenge#
Print this pattern:
*
**
***
****
*****
# Your code here
โ Self-Check Quiz#
Before moving on, make sure you can answer these:
What function do we use to display text in Python?
What are strings, and how do you write them?
How do you create a comment in Python?
Whatโs the difference between
print(2 + 2)andprint("2 + 2")?How do you print multiple items on one line?
๐ฏ Key Takeaways#
print()displays text and numbersStrings are text wrapped in quotes:
"like this"or'like this'Comments start with
#and are ignored by PythonYou can print multiple items separated by commas
Python can do math inside
print()statements
๐ Next Steps#
Great job! Youโve learned the basics of Python output. Next, youโll learn about variables - how to store and reuse information.
Continue to: 02_variables_and_data_types.ipynb
๐ฌ Did You Know?#
Python was created by Guido van Rossum in 1991 and named after the comedy group โMonty Pythonโ - not the snake! ๐
3. Comments#
Comments are notes in your code that Python ignores. They help you and others understand your code.
Use
#to create a comment:๐ก Pro Tip:#
Good programmers use comments to explain why code does something, not just what it does.