No history yet

Python Basics

What is Python?

Python is a programming language, a way for us to give instructions to a computer. It was created in the late 1980s by Guido van Rossum. He named it after the British comedy troupe Monty Python, not the snake. The name hints at the language's philosophy: it aims to be fun and easy to use.

What makes Python so popular? First, its syntax is clean and readable. Writing Python code often feels like writing in plain English, which makes it a great choice for beginners. Second, it's incredibly versatile. Developers use Python for building websites, analyzing data, creating artificial intelligence, automating repetitive tasks, and much more.

It also has a massive and supportive community. This means there are countless libraries (collections of pre-written code) available to help you accomplish complex tasks without having to start from scratch.

Lesson image

Getting Set Up

To start writing Python, you need a program called an interpreter. The interpreter reads your Python code and translates it into instructions the computer can understand and execute. You can download the official Python interpreter directly from python.org.

The installation is straightforward. During the process, make sure to check the box that says "Add Python to PATH." This allows you to run Python from your computer's command line, a text-based interface for interacting with your system.

Lesson image

While you can write code in any plain text editor, most developers use an Integrated Development Environment, or IDE. An IDE is a specialized editor that includes helpful tools like syntax highlighting (coloring your code to make it more readable) and debugging aids. When you install Python, it comes with a simple IDE called IDLE.

For beginners, online editors are another great option. Websites like Replit allow you to write and run Python code directly in your web browser, with no installation required.

The Rules of Spacing

Most programming languages use brackets or keywords to group lines of code together. Python does things differently. It uses indentation, the spaces at the beginning of a line.

Think of it like an outline for an essay. Major points are at the margin, and sub-points are indented underneath them. This structure shows which ideas belong together. In Python, indentation isn't just for style; it's a strict rule. Code that is indented is part of a block, and the interpreter needs this visual structure to understand your program's logic.

In Python, indentation defines structure. The amount of space is up to you, but it must be consistent. The community standard is to use four spaces for each level of indentation.

If you get the indentation wrong, your program will produce an error. This might seem strict at first, but it forces you to write clean, organized code that is easy for anyone to read.

Your First Conversation

Let's write a program that communicates with the user. The most basic way to display information is with the print() function. A function is a named block of code that performs a specific task. The print() function's task is to show a message on the screen.

To use it, you place the text you want to display inside the parentheses, enclosed in quotes.

print("Hello, world!")

To get information from a user, we can use the input() function. This function displays a prompt on the screen and waits for the user to type something and press Enter. Whatever the user types is then returned by the function, which we can store in a variable. A variable is simply a name that we use to hold a value.

# The message inside input() is the prompt shown to the user.
# The user's response is stored in the 'name' variable.
name = input("What is your name? ")

# Now we can use the 'name' variable in our print statement.
print("Nice to meet you,")
print(name)

Try running that code. It will first ask for your name, wait for your response, and then greet you. You've just written your first interactive Python program.