No history yet

Introduction to Python

What is Python?

Python is a popular programming language known for being powerful, versatile, and surprisingly easy to read. It was created in the late 1980s by Guido van Rossum, who named it after the British comedy troupe Monty Python's Flying Circus.

So, what makes it special? For starters, its syntax is clean and emphasizes readability. This means you spend less time wrestling with complex symbols and more time bringing your ideas to life. It’s often said that Python code looks a lot like plain English.

Python also comes with a huge standard library, which is like a toolbox packed with pre-written code for common tasks. Need to work with text, connect to the internet, or read files? There's a good chance Python has a ready-made tool for you.

Lesson image

Because it’s so versatile, Python is used by companies like Google, Netflix, and NASA to power web apps, analyze data, and even control spacecraft.

Getting Python Installed

Before you can start coding, you need to install the Python interpreter on your computer. This is the program that reads your Python code and runs it. While some operating systems like macOS and Linux come with a version of Python pre-installed, it's often an older version. It's always best to install the latest stable release from the official source, python.org.

Lesson image

For Windows, you'll download an installer from the website. During installation, make sure to check the box that says "Add Python to PATH." This allows you to run Python from your command prompt easily. On a Mac, you can also use the installer from the website. Linux users can typically install Python using their distribution's package manager, like apt for Debian/Ubuntu or yum for Fedora/CentOS.

Once installed, you can verify everything is working by opening your terminal (on macOS or Linux) or Command Prompt (on Windows) and typing the following command:

python --version
# On some systems, you might need to use python3 instead
# python3 --version

If the command returns a version number, like Python 3.12.0, you're all set.

Writing Your First Script

With Python installed, you're ready to write your first piece of code. There are two main ways to run Python: interactively in a "shell" or by writing a script in a file.

The interactive shell is great for testing small snippets of code. To start it, just type python or python3 in your terminal and press Enter. You'll see a prompt that looks like >>>.

Let's try telling the computer to say hello:

>>> print("Hello, world!")
Hello, world!

The shell immediately executes your command and prints the result. While useful, the real power comes from writing scripts. A Python script is just a plain text file that contains your code. By convention, these files end with a .py extension.

To write a script, you'll need a code editor. You can start with a simple text editor that comes with your OS, but it's better to use an editor designed for programming. A good option that comes bundled with Python is IDLE (Integrated Development and Learning Environment). It provides features like syntax highlighting, which colors your code to make it easier to read.

Lesson image

Open IDLE (or your chosen editor), create a new file, and type the same line of code:

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

Save the file as hello.py. To run it, navigate to the directory where you saved the file in your terminal and run:

python hello.py

You should see Hello, world! printed to your screen. Congratulations, you've just written and executed your first Python script!

Quiz Questions 1/6

The Python programming language was named after which of the following?

Quiz Questions 2/6

What is the primary benefit of Python's large standard library?

You've taken the first steps into the world of Python, from understanding its origins to running your own code. This foundation is the starting point for building much more complex and interesting programs.