No history yet

Python Basics

Setting Up Your Workspace

Before you can write Python, you need two things: the Python interpreter and a place to write your code. The interpreter is what reads your code and runs it. You can download it for free from the official Python website, python.org.

Once Python is installed, you need a code editor. While you could use a simple text editor, most programmers use an Integrated Development Environment (IDE). Think of an IDE as a word processor specifically for writing code. It helps by color-coding your text, catching errors, and making it easier to run your programs. Python comes with a simple built-in IDE called IDLE, which is great for starting out.

Lesson image

Many developers also use more powerful editors like Visual Studio Code or PyCharm, but for now, IDLE is all you need.

Your First Program

It's a tradition for a programmer's first program to simply display the text "Hello, World!" on the screen. In Python, this is incredibly straightforward. Open your editor, type the following line, and run it.

print("Hello, World!")

That's it. The print() command is a built-in Python function that tells the computer to display whatever is inside the parentheses. Since we wanted to display text, we wrapped it in double quotes to let Python know it's a string of characters.

The Rules of the Road

Python is known for its clean and readable syntax. One of the most important rules is its use of indentation. Unlike many other programming languages that use brackets or keywords to group code, Python uses whitespace. This isn't just for looks; it's a strict rule.

Indentation in Python is not a suggestion. It is a requirement that defines the structure of your code.

Think of it like an outline for a paper. Main points are at one level, and sub-points are indented underneath them. In Python, blocks of code that belong together, like the actions to take if a certain condition is true, must be indented the same amount. The standard is to use four spaces for each level of indentation.

Here's an example. Don't worry about what the code does yet, just notice the structure.

weather = "sunny"

if weather == "sunny":
    print("It's a nice day for a walk.")
    print("Don't forget your sunglasses!")

print("Enjoy your day.")

The two print() statements under the if line are indented, which tells Python they are part of that block. They will only run if the weather is sunny. The final print() statement is not indented, so it's not part of the if block and will run no matter what.

Storing Information

To do anything useful, programs need to store and manage information. We do this using variables. A variable is like a labeled box where you can keep a piece of data. You give the box a name and put something inside it.

variable

noun

A symbolic name that is a reference or pointer to an object. Once an object is assigned to a variable, you can refer to the object by that name.

You create a variable by choosing a name and using the equals sign (=) to assign it a value.

# An integer (whole number)
age = 30

# A float (number with a decimal)
price = 19.99

# A string (text)
greeting = "Welcome to Python"

Once a variable is created, you can use its name to get the value back. For example, you can print a variable's value.

print(age)
print(greeting)

This would display 30 and Welcome to Python on separate lines. Variables make your code flexible. Instead of writing the same value over and over, you can store it once in a variable and reuse it, making your code easier to read and update.

Now, let's check your understanding of these basic concepts.