Skip to content

You are viewing a free preview of this lesson.

Subscribe to unlock all 10 lessons in this course and every other course on LearningBro.

Getting Started with Python

Getting Started with Python

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.


Why Learn Python?

  • Beginner-friendly — Python reads almost like English, making it one of the easiest languages to learn
  • Versatile — used in web development, data science, machine learning, automation, scripting, and more
  • Huge community — millions of developers, thousands of libraries, and endless tutorials
  • In demand — consistently ranked among the top programming languages for jobs
  • Cross-platform — runs on Windows, macOS, and Linux

Installing Python

Windows

  1. Go to python.org/downloads
  2. Download the latest Python 3 installer
  3. Important: Check the box that says "Add Python to PATH" during installation
  4. Click "Install Now"

macOS

macOS may come with Python 2 pre-installed, but you need Python 3:

# Using Homebrew (recommended)
brew install python3

# Verify installation
python3 --version

Linux (Ubuntu/Debian)

sudo apt update
sudo apt install python3 python3-pip

# Verify
python3 --version

Verifying Your Installation

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 Python REPL (Interactive Mode)

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).


Your First Python Script

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!

Understanding the Code

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

Choosing a Code Editor

You can write Python in any text editor, but these make it easier:

VS Code (Recommended)

  • Free, lightweight, and powerful
  • Install the Python extension for syntax highlighting, linting, and debugging
  • Download from code.visualstudio.com

Other Options

  • PyCharm — a full-featured Python IDE (free Community edition)
  • Thonny — designed specifically for beginners
  • IDLE — comes bundled with Python

Python Syntax Basics

Indentation Matters

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).

Case Sensitivity

Python is case-sensitive:

name = "Alice"
Name = "Bob"
NAME = "Charlie"

# These are three different variables
print(name)   # Alice
print(Name)   # Bob
print(NAME)   # Charlie

Statements and Lines

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

Running Python Programs

There are several ways to run Python code:

1. From the Command Line

python3 my_script.py

2. Interactive REPL

python3
>>> print("Hello!")

3. From VS Code

  • Open the .py file
  • Click the Run button (play icon) in the top right
  • Or press Ctrl+Shift+P and type "Run Python File"

4. Jupyter Notebooks

Jupyter notebooks let you run code in cells — great for learning and data analysis:

pip install jupyter
jupyter notebook

Common Beginner Mistakes

1. Forgetting Parentheses with print

# Wrong (Python 2 syntax)
print "Hello"

# Correct (Python 3)
print("Hello")

2. Using the Wrong Quotes

# All of these are valid strings
print("Hello")
print('Hello')
print("""Hello""")

# But don't mix them
print("Hello')  # SyntaxError!

3. Forgetting the Colon

# Wrong
if True
    print("oops")

# Correct
if True:
    print("correct")

Summary

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.