No history yet

Python Basics

Setting Up Your Workspace

Before you can write Python, you need to install it on your computer. You can download the latest version for free from the official website, python.org. The installation is straightforward, and it comes with a basic program called IDLE, which lets you write and run code right away.

Many developers prefer to use an Integrated Development Environment, or IDE. Think of an IDE as a powerful text editor designed specifically for coding. It helps by highlighting your code in different colors, spotting errors, and managing your files. Popular choices for Python include Visual Studio Code, PyCharm, and Thonny.

Lesson image

Syntax and Spacing

Python is known for its clean and readable syntax. It's designed to look a lot like plain English, which makes it easier for beginners to pick up. One of the most important rules in Python is indentation.

In Python, the empty space at the beginning of a line is part of the code. It's not just for looks.

Unlike other languages that use brackets or keywords to group code, Python uses whitespace. You create a block of code by indenting a line or a set of lines with four spaces. If you get the indentation wrong, your program will produce an error. This might seem strange at first, but it forces everyone to write clean, organized code.

# Correct indentation
name = "Alice"
age = 30

# Incorrect indentation (this will cause an error)
# The second line is indented for no reason.
location = "New York"
    country = "USA"

Storing Information

In programming, we need a way to store and label information. We do this using variables. A variable is like a container with a name on it, and you can put data inside. You create a variable by giving it a name and using the equals sign (=) to assign it a value.

user_name = "Bard"
user_age = 2

Here, user_name is a variable holding the text "Bard", and user_age is a variable holding the number 2. You can change the value of a variable at any time just by assigning it something new.

Python has a few basic types of data that can be stored in variables.

Data TypeDescriptionExample
StringA sequence of characters, like text."Hello, world!"
IntegerA whole number, without a decimal.42
FloatA number with a decimal point.3.14159
BooleanA value that is either True or False.True

Python automatically figures out the data type when you assign a value, so you don't have to declare it yourself.

Using Operators

Operators are special symbols that perform operations on variables and values. They are the building blocks for doing math and making comparisons.

Arithmetic operators work just like they do in math class. You can add, subtract, multiply, and divide numbers.

sum = 10 + 5       # Result is 15
difference = 10 - 5 # Result is 5
product = 10 * 5    # Result is 50
quotient = 10 / 5   # Result is 2.0

Comparison operators are used to compare two values. The result of a comparison is always a Boolean value: either True or False.

OperatorMeaningExample
==Equal to5 == 5 is True
!=Not equal to5 != 6 is True
>Greater than6 > 5 is True
<Less than5 < 6 is True
>=Greater than or equal to5 >= 5 is True
<=Less than or equal to6 <= 5 is False

Finally, logical operators (and, or, not) are used to combine Boolean values. They help you create more complex conditions. For example, (5 > 3) and (10 < 20) would be True because both of the individual comparisons are true.

Quiz Questions 1/5

What is the primary role of indentation in Python?

Quiz Questions 2/5

Which of the following correctly assigns the string "Player One" to a variable named user?

With these fundamentals, you have the basic tools to start writing simple Python expressions and storing data.