You are viewing a free preview of this lesson.
Subscribe to unlock all 10 lessons in this course and every other course on LearningBro.
Python is one of the most popular programming languages in the world. Created by Guido van Rossum and first released in 1991, Python is known for its clean, readable syntax and its vast ecosystem of libraries. Whether you want to build websites, analyse data, automate tasks, or create games, Python is an excellent first language.
macOS may come with Python 2 pre-installed, but you need Python 3:
# Using Homebrew (recommended)
brew install python3
# Verify installation
python3 --version
sudo apt update
sudo apt install python3 python3-pip
# Verify
python3 --version
Open a terminal (or Command Prompt on Windows) and type:
python3 --version
You should see something like:
Python 3.12.2
On Windows, you may need to use python instead of python3.
The REPL stands for Read-Eval-Print Loop. It lets you type Python code and see results immediately.
Start the REPL:
python3
You will see a prompt like this:
Python 3.12.2 (main, Feb 6 2024, 20:19:44)
>>>
Now try some code:
>>> 2 + 3
5
>>> "Hello, World!"
'Hello, World!'
>>> 10 * 5
50
>>> type(42)
<class 'int'>
>>> type("hello")
<class 'str'>
The REPL is perfect for experimenting and testing small ideas. To exit, type exit() or press Ctrl+D (macOS/Linux) or Ctrl+Z then Enter (Windows).
Create a file called hello.py using any text editor:
# hello.py — my first Python program
print("Hello, World!")
print("Welcome to Python!")
# Basic arithmetic
result = 7 + 3
print("7 + 3 =", result)
# User input
name = input("What is your name? ")
print("Nice to meet you,", name + "!")
Run it from the terminal:
python3 hello.py
Output:
Hello, World!
Welcome to Python!
7 + 3 = 10
What is your name? Alice
Nice to meet you, Alice!
Let's break down what each line does:
| Code | What It Does |
|---|---|
# comment | A comment — Python ignores everything after # |
print("text") | Displays text on the screen |
result = 7 + 3 | Creates a variable called result and stores 10 in it |
input("prompt") | Waits for the user to type something and returns it as a string |
You can write Python in any text editor, but these make it easier:
Unlike many languages that use curly braces {}, Python uses indentation (whitespace) to define blocks of code:
# Correct — indented with 4 spaces
if True:
print("This is indented correctly")
print("So is this")
# Wrong — inconsistent indentation will cause an error
if True:
print("This is fine")
print("This will cause an IndentationError!")
The convention is to use 4 spaces per indentation level (not tabs).
Python is case-sensitive:
name = "Alice"
Name = "Bob"
NAME = "Charlie"
# These are three different variables
print(name) # Alice
print(Name) # Bob
print(NAME) # Charlie
Each line is typically one statement. You can split long lines with a backslash:
total = 1 + 2 + 3 + \
4 + 5 + 6
print(total) # 21
Or use parentheses (preferred):
total = (1 + 2 + 3 +
4 + 5 + 6)
print(total) # 21
There are several ways to run Python code:
python3 my_script.py
python3
>>> print("Hello!")
.py fileCtrl+Shift+P and type "Run Python File"Jupyter notebooks let you run code in cells — great for learning and data analysis:
pip install jupyter
jupyter notebook
# Wrong (Python 2 syntax)
print "Hello"
# Correct (Python 3)
print("Hello")
# All of these are valid strings
print("Hello")
print('Hello')
print("""Hello""")
# But don't mix them
print("Hello') # SyntaxError!
# Wrong
if True
print("oops")
# Correct
if True:
print("correct")
In this lesson you have installed Python, used the REPL for interactive coding, written and run your first script, and learned the fundamental syntax rules — indentation, case sensitivity, and how to use print() and input(). You are now ready to dive into variables and data types.