Skip to content

Latest commit

 

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 

README.md

01 - Hello Python

Welcome to Python! This is where your programming journey begins. Python is a powerful, versatile programming language that's also friendly to beginners. It's used everywhere—from web development and data science to automation and artificial intelligence. Best of all, Python emphasizes code readability, making it an excellent first language to learn.

In this topic, you'll take your first steps: installing Python, running your first program, and understanding the basics of how Python works. By the end, you'll be able to write simple programs that display output to the screen—the foundation for everything else you'll learn.

🎯 Learning Objectives

By the end of this topic, you will be able to:

  1. Install Python and verify it's working correctly on your system
  2. Use the Python REPL (interactive shell) to run Python code
  3. Write and execute Python scripts (.py files)
  4. Use the print() function to display output
  5. Understand the difference between the interactive shell and script files
  6. Write comments to document your code
  7. Identify and fix basic syntax errors

📚 Prerequisites

  • A computer (Windows, Mac, or Linux)
  • Basic computer skills (creating files, using terminal/command prompt)
  • No programming experience required!

📖 Contents

This topic includes:

📝 Core Concepts

Concept 1: Installing Python

Python needs to be installed on your computer before you can use it. Most modern operating systems come with Python preinstalled, but you'll want to ensure you have Python 3.8 or higher.

Check if Python is installed:

Open your terminal (Mac/Linux) or Command Prompt (Windows) and type:

python3 --version

You should see something like: Python 3.10.5

If Python is not installed:

  • Visit python.org/downloads
  • Download the latest version for your operating system
  • Run the installer (on Windows, make sure to check "Add Python to PATH")

Concept 2: The Python REPL (Interactive Shell)

REPL stands for Read-Eval-Print Loop. It's an interactive environment where you can type Python code and see results immediately.

To start the REPL:

python3

You'll see a prompt that looks like:

>>>

This >>> is where you type Python code.

Example:

>>> 5 + 3
8
>>> print("Hello, World!")
Hello, World!
>>> exit()  # This exits the REPL

When to use the REPL:

  • Testing small pieces of code
  • Performing quick calculations
  • Experimenting with new concepts
  • Debugging specific statements

Concept 3: Writing Python Scripts

While the REPL is great for experimentation, you'll usually write your code in files called scripts. Python scripts have a .py extension.

Creating your first script:

  1. Create a new file called hello.py
  2. Add this code:
print("Hello, World!")
  1. Save the file
  2. Run it from terminal:
python3 hello.py

Output:

Hello, World!

Concept 4: The print() Function

The print() function displays output to the screen. It's one of the most commonly used functions in Python.

Basic Syntax:

print(value)

Examples:

print("Hello!")                    # Output: Hello!
print(42)                          # Output: 42
print(3.14)                        # Output: 3.14
print("The answer is:", 42)        # Output: The answer is: 42

You can print multiple values by separating them with commas:

print("My age is", 25, "years")    # Output: My age is 25 years

Concept 5: Comments

Comments are notes in your code that Python ignores. They help you and others understand what your code does.

Single-line comments start with #:

# This is a comment
print("Hello")  # This comment is at the end of a line

Multi-line comments use triple quotes:

"""
This is a multi-line comment.
It can span multiple lines.
Useful for longer explanations.
"""

Why use comments?

  • Explain what your code does
  • Document complex logic
  • Leave notes for your future self
  • Help others understand your code

Concept 6: Understanding Errors

When you make a mistake, Python shows you an error message. Don't panic—errors are helpful! They tell you exactly what went wrong.

Common beginner error:

print("Hello)

Error message:

SyntaxError: unterminated string literal

This tells you:

  • What: SyntaxError (something is written incorrectly)
  • Where: Line number is usually included
  • Why: "unterminated string literal" (forgot closing quote)

Fix:

print("Hello")  # Added closing quote

💡 Important Notes

Common Mistakes

  • Mistake 1: Forgetting quotes around text

    • ❌ Wrong: print(Hello)
    • ✅ Right: print("Hello")
    • Why: Text must be in quotes; without quotes, Python looks for a variable named Hello
  • Mistake 2: Using wrong Python version

    • Some systems have both Python 2 and Python 3
    • Always use python3 command, not python
    • Python 2 is outdated and no longer supported
  • Mistake 3: Forgetting parentheses with print

    • ❌ Wrong: print "Hello" (Python 2 syntax)
    • ✅ Right: print("Hello") (Python 3 syntax)

Best Practices

  1. Use meaningful filenames: hello.py is better than test1.py
  2. Add comments: Explain what your code does
  3. Start simple: Get basic version working before adding complexity
  4. Test frequently: Run your code often to catch errors early

When to Use

  • Use the REPL for quick experiments and testing
  • Use scripts for any code you want to save and run multiple times
  • Use print() to see what your program is doing

🔗 Practice Problems

Test your understanding with practice problems:

🎯 Next Steps

After mastering this topic:

  1. Complete the exercises
  2. Solve the practice problems
  3. Move on to 02-variables-data-types

📚 Additional Resources


Ready to practice? Check out examples.py and run the code!