Python β€” Getting Started

Objectives: Python β€” Getting Started

Python β€” Getting Started (Simple English)

Python β€” Getting Started (Simple English)

These notes teach you how to start with Python. I use simple English and give many examples. For each code example I explain how you run it and which Python program (interpreter) processes it.

1. What is Python?

Python is a high-level programming language. It is interpreted, friendly, and used for websites, data work, automation, and more.

Interpreter vs Compiler β€” simple idea

- A compiler turns source code into machine code (e.g., C >> .exe).
- An interpreter reads your code and runs it line by line (or compiles to bytecode then runs). Python's usual implementation, CPython, compiles Python code to bytecode (.pyc files) and then executes bytecode in a virtual machine. You still run a Python program with the python command (the interpreter).

2. Install Python

Check if Python is already installed

Windows (Command Prompt)

C:\Users\YourName> python --version

Mac / Linux (Terminal)

$ python --version # or sometimes $ python3 --version
Note: Many systems have both Python 2 and Python 3. Use python3 if your machine uses python for Python 2.

Where to download

If not installed, download from python.org. The installer will add python to your PATH if you check that option on Windows.

Alternative interpreters

  • CPython β€” official, most common. Run code with python or python3.
  • PyPy β€” faster JIT-compiled Python for some programs. Run with pypy.
  • Jython / IronPython β€” Python on Java/.NET (rare for beginners).

3. First Python file

Create a file named hello.py in a text editor and write:

# hello.py print("Hello, World!")

How to run the file

Open a command line, go to the folder with the file, and run:

# Windows (Command Prompt) C:\Users\YourName> python hello.py
Mac / Linux (Terminal)

$ python3 hello.py

or

$ python hello.py

What happens: The python program (CPython interpreter) reads hello.py, compiles it to bytecode, and executes it. The output is:

Hello, World!

4. REPL β€” interactive Python

REPL means Read–Eval–Print Loop. It is an interactive mode where you type Python and see results immediately.

$ python3 Python 3.x.x (default, ...) >>> 2 + 3 5 >>> print("hi") hi >>> exit() # or Ctrl+D on Linux/Mac, Ctrl+Z then Enter on Windows

Run command used: python or python3 (starts the interpreter in interactive mode).

5. Running small code from command line

$ python3 -c "print('Hello from -c')"

Meaning: -c tells the interpreter to run the code string immediately.

6. Modules and packages

A module is a file with Python code (e.g., mymodule.py). A package is a folder with an __init__.py file and modules inside.

# mymodule.py def greet(name): return f"Hello, {name}!" # run with $ python3 -c "import mymodule; print(mymodule.greet('Amina'))"

To run as a script directly from a module file, add:

if __name__ == "__main__": # code here runs when file is executed directly print("Script is running")

7. Virtual environments (isolate project packages)

You should use a virtual environment so project libraries do not conflict with each other.

Create and use venv (built-in)

# Create (Windows) C:\path\to\project> python -m venv venv
Create (Mac/Linux)

$ python3 -m venv venv

Activate (Windows Command Prompt)

C:\path\to\project> venv\Scripts\activate

Activate (PowerShell)

PS C:\path\to\project> .\venv\Scripts\Activate.ps1

Activate (Mac/Linux)

$ source venv/bin/activate

When active, install packages:

(venv) $ pip install requests

Save dependencies:

(venv) $ pip freeze > requirements.txt

What runs: python -m venv calls the Python module that creates a small, local Python environment. After activation, pip installs packages into that environment.

8. pip β€” Python package manager

# Install a package pip install requests
Show installed packages

pip list

To install exact versions from file

pip install -r requirements.txt

9. Debugging and errors

When a program fails, Python prints a traceback. Read from bottom up to find where error happened. Common issues:

  • IndentationError β€” wrong spaces/tabs.
  • ModuleNotFoundError β€” missing package, use pip to install.
  • SyntaxError β€” wrong code format.

10. Example snippets with explanation

Function example

# greet.py def greet(name): return "Hello, " + name

if name == "main":
print(greet("Mwanafunzi"))

Run: python greet.py. This runs the file directly. The interpreter executes the top-level code and prints the returned string.

Loop and list

# numbers.py numbers = [1, 2, 3, 4] for n in numbers: print(n * n)

Run: python numbers.py. The interpreter reads the file, compiles to bytecode, runs the loop, and prints square numbers.

Class example

# person.py class Person: def __init__(self, name): self.name = name
def say(self):
    print("My name is", self.name)


if name == "main":
p = Person("Asha")
p.say()

Run: python person.py. The class is defined and then used in the script's main block.

11. Shebang and making script executable (Linux / Mac)

#!/usr/bin/env python3 # script.py print("Run as executable")

Then:

$ chmod +x script.py $ ./script.py

What this does: The #!/usr/bin/env python3 line tells the system which interpreter to use when executing the file directly. The OS runs the interpreter and passes the file to it.

12. Running modules as scripts

$ python -m http.server 8000 # This runs the http.server module as a script and starts a simple web server.

13. Useful commands summary

CommandWhat it does
python hello.pyRun a Python file with CPython interpreter (Windows uses this).
python3 hello.pyRun Python 3 explicitly (common on Mac/Linux).
python -m venv venvCreate a virtual environment.
source venv/bin/activateActivate virtual environment (Mac/Linux).
venv\Scripts\activateActivate virtual environment (Windows).
pip install packageInstall a package into active environment.
python -m pip install packageAlternative that ensures you use pip for the current python.
python -m moduleRun a library/module as a script (e.g., python -m http.server).
python -c "code"Run small commands directly from shell.

14. Editors and IDEs

Choose one:

  • VS Code β€” lightweight, many extensions.
  • PyCharm β€” full-featured IDE (Community edition is free).
  • Sublime Text / Atom β€” simple editors.
  • Thonny β€” very simple, good for beginners.

15. Tips for learning

  1. Write small programs every day.
  2. Use print() to see values when debugging.
  3. Use virtual environments per project.
  4. Read error tracebacks from the bottom up.
  5. Learn to use pip and requirements.txt.

16. Troubleshooting common setup problems

python command not found: add Python to PATH (Windows) or use full path like /usr/bin/python3.

Wrong Python version: use python3 or configure your IDE to point to the correct python executable (path shown by which python or where python).

17. Short reference β€” what runs and why

  • python file.py β€” CPython interpreter reads file.py, compiles to bytecode, runs the Python virtual machine (this is how your code executes).
  • python -m module β€” interpreter runs the given module as a script.
  • python -c "code" β€” interpreter runs the small code string immediately.
  • chmod +x script.py with shebang β€” OS uses interpreter declared by the shebang to run the file directly.

18. Extra small examples you can copy & run

# file: add.py def add(a, b): return a + b if __name__ == "__main__": print(add(5, 7)) # Run: python add.py # Output: 12
# file: server_example.py # Quick HTTP server for development import http.server import socketserver PORT = 8000 Handler = http.server.SimpleHTTPRequestHandler with socketserver.TCPServer(("", PORT), Handler) as httpd: print("Serving at port", PORT) httpd.serve_forever() # Run: python server_example.py # Or run: python -m http.server 8000

19. Where to learn more

  • Official Python tutorial
  • Practice small projects: calculator, file reader, web scraper (requests + BeautifulSoup), small web app with Flask.
Final tip: Always say which python command you used ( python vs python3 ), and whether you activated a virtual environment. This makes it easier to fix problems.

If you want, I can convert this into a printable PDF or give a smaller "one-page cheat sheet" version. Tell me which you prefer and I will produce it in HTML too.

Reference Book: N/A

Author name: MWALA_LEARN Work email: biasharabora12@gmail.com
#MWALA_LEARN Powered by MwalaJS #https://mwalajs.biasharabora.com
#https://educenter.biasharabora.com

:: 1::

β¬… ➑