No history yet

Introduction to Python

Meet Python

Python is a popular programming language known for its clear, readable syntax. It was created in the late 1980s by Guido van Rossum, who wanted a language that was easy to read and write. The name comes from his appreciation for the British comedy group Monty Python, not the snake.

Lesson image

One of Python's core philosophies is that code should be easy to understand. This makes it a great choice for beginners. It's also incredibly versatile, used for everything from web development and data analysis to artificial intelligence and scientific computing.

Getting Set Up

Before you can start coding, you need to install Python on your computer. The official source for Python is the website python.org. You can download the latest version for your operating system, whether it's Windows, macOS, or Linux.

The installer includes the Python interpreter, which is the program that understands and runs your code. It also comes with a basic development environment called IDLE (Integrated Development and Learning Environment), which we'll use to get started.

Lesson image

Two Ways to Code

There are two main ways to interact with Python: through the interactive shell or by running a script file. Think of the interactive shell as a direct conversation with Python. You type a command, press Enter, and Python responds immediately. It's great for testing small snippets of code.

You can open the interactive shell by launching IDLE or typing python (or python3 on some systems) in your computer's command prompt or terminal.

When you open the shell, you'll see a prompt, usually three greater-than signs (>>>). This is where you type your code. Let's try a simple calculation.

>>> 2 + 2
4
>>> 5 * 10
50

The second way to code is by writing a script. A script is a text file containing multiple lines of Python code, saved with a .py extension. This is how you'll write most of your programs. It's like writing down a full recipe before you start cooking.

Let's write the traditional first program, "Hello, World!" Open IDLE and from the File menu, select New File. This will open a blank text editor.

# This is my first Python script: hello.py

print("Hello, World!")

In this script, the first line is a comment. Comments start with a # and are ignored by Python; they're notes for humans reading the code. The second line uses the print() function to display the text "Hello, World!" on the screen.

Save this file as hello.py. To run it in IDLE, you can simply press the F5 key or choose Run Module from the Run menu. The output will appear back in the interactive shell window.

Basic Program Structure

Python programs are made of statements. The line print("Hello, World!") is a statement. Python executes statements one by one, from the top of the file to the bottom. Unlike many other languages, you don't need to use semicolons to end statements.

Whitespace, particularly indentation, is very important in Python. While it doesn't matter for a simple one-line script, you'll soon see how indentation is used to group blocks of code together. For now, just remember to start each new line of code without any leading spaces.

Let's test your understanding of these initial concepts.

Quiz Questions 1/5

What is the primary philosophy behind Python's design?

Quiz Questions 2/5

What is the standard file extension for a Python script?