No history yet

Python Basics

Setting Up Your Workspace

Before you can write Python code, you need to install the Python interpreter on your computer. This program reads your code and carries out its instructions. The best place to get it is the official website, python.org. Download the latest version for your operating system.

When you install Python, it comes with a built-in development environment called IDLE (Integrated Development and Learning Environment). It's a simple tool that lets you write and run Python code right away, which is perfect for getting started.

Lesson image

Once installed, you can open IDLE and see a window called the Python Shell. This is where you can type commands and see immediate results. Try typing print("Hello, World!") and hitting Enter. You've just run your first piece of Python code.

The Rules of the Road

Every programming language has its own set of rules, called syntax. In Python, the syntax is designed to be clean and readable. One of its most famous rules is the use of indentation.

Unlike many other languages that use brackets or keywords to group code, Python uses whitespace. Lines of code that are part of the same block must be indented by the same amount. You'll see why this is important as we get into more complex topics, but for now, just remember that spacing matters.

Another key part of writing good code is leaving notes for yourself and others. These are called comments. Python ignores anything on a line that follows a hash symbol (#). Comments don't change how your program runs, but they make it much easier to understand.

# This is a comment. It explains what the next line does.
print("This line will run.")

# print("This line will NOT run.")

Storing and Using Data

Programs work with information. To keep track of it, we use variables. Think of a variable as a labeled box where you can store a piece of data. You give it a name and put a value inside it.

# Assign the string "Alice" to a variable named 'user_name'
user_name = "Alice"

# Assign the integer 30 to a variable named 'user_age'
user_age = 30

# Now we can use the variables
print(user_name)
print(user_age)

The kind of data you store determines its data type. Python has several built-in types, but let's start with the four most common ones.

Data TypeDescriptionExample
String (str)A sequence of characters, like text. Always enclosed in quotes."Hello there!"
Integer (int)A whole number, without a fractional part.42
Float (float)A number with a decimal point.3.14159
Boolean (bool)Represents one of two values: True or False.True

Once you have data in variables, you can perform operations on it using operators.

Making Things Happen

Operators are special symbols that perform computations. The most familiar are the arithmetic operators. You can use them with numbers to do calculations.

a = 15
b = 4

print(a + b)  # Addition -> 19
print(a - b)  # Subtraction -> 11
print(a * b)  # Multiplication -> 60
print(a / b)  # Division -> 3.75

There are also comparison operators, which compare two values and result in a Boolean (True or False).

x = 10
y = 5

# Is x greater than y?
print(x > y)   # -> True

# Is x equal to y?
print(x == y)  # -> False

# Is x not equal to y?
print(x != y)  # -> True

Notice the double equals sign (==) for checking equality. A single equals sign (=) is used for assigning a value to a variable.

Let's review what we've covered.

Ready to test your knowledge?

Quiz Questions 1/5

What is the best place to download the official Python interpreter?

Quiz Questions 2/5

In Python, what symbol is used to indicate that the rest of the line is a comment?

You've now taken the first steps in Python. You know how to set up your environment, write basic commands, store information, and perform simple operations. These are the fundamental building blocks for everything that comes next.