No history yet

Python Basics

What is Python?

Python is a programming language known for its clear, readable syntax. Think of it as a set of instructions a computer can understand. It was created in the late 1980s by Guido van Rossum, who named it after the British comedy group Monty Python's Flying Circus.

Lesson image

One of Python's core philosophies is that code should be easy to read and write. This makes it a popular choice for beginners and a powerful tool for experts. It's used in web development, data science, artificial intelligence, and many other fields. Its versatility is one of its greatest strengths.

Getting Set Up

Before you can write Python code, you need to install it on your computer. You can download the official installer from the Python website, python.org. Be sure to grab the latest stable version for your operating system, whether it's Windows, macOS, or Linux.

During installation on Windows, you'll see a checkbox that says "Add Python to PATH." It's very important to check this box. It makes it easier for your computer to find and run Python from anywhere.

Lesson image

Once installed, Python includes a simple program called IDLE (Integrated Development and Learning Environment). This is a great place to start writing your first lines of code. It includes a text editor for writing scripts and a shell for running them.

Your First Program

It's a tradition in programming to start with a program that displays "Hello, World!" on the screen. Let's do that now.

First, open IDLE. Go to File > New File to open a new editor window. In this window, type the following line of code:

print("Hello, World!")

The print() part is a function. It tells the computer to display whatever is inside the parentheses. The text "Hello, World!" is called a string, which is just a sequence of characters. Strings always need to be enclosed in quotes.

Now, save your file. Go to File > Save and name it something like hello.py. The .py extension is crucial; it tells the computer this is a Python file. To run your program, press F5 or go to Run > Run Module. You should see "Hello, World!" appear in the IDLE shell window.

Basic Syntax and Structure

Python code is made up of statements. The print("Hello, World!") line is a complete statement. Let's look at a few other basic building blocks.

Variables are like containers for storing data. You give a variable a name and assign it a value using the equals sign (=).

For example, we can store our greeting in a variable:

message = "Welcome to Python!"
print(message)

Here, message is the variable. The code first stores the string "Welcome to Python!" in the message variable, and then it prints the value stored in that variable. The output will be Welcome to Python!.

You can also add comments to your code. Comments are notes for humans that the computer ignores. They start with a hash symbol (#). Good comments explain why you did something, not just what you did.

# This is a comment. The computer will ignore it.

# Assign a greeting to a variable
message = "This is my first variable."

# Print the contents of the variable to the screen
print(message)

Finally, whitespace matters in Python. Things like indentation (how far a line is pushed to the right) have special meaning. We'll see why this is so important when we start writing more complex programs.

Time to check what you've learned.

Quiz Questions 1/5

Who is the creator of the Python programming language?

Quiz Questions 2/5

During a standard Python installation on Windows, it is recommended to leave the 'Add Python to PATH' option unchecked.

You've taken your first steps with Python, from installation to writing and running a simple script. These fundamentals are the base you'll build on.