No history yet

Python Basics

Setting Up Python

Before you can start programming, you need to install Python on your computer. Think of it as getting the right tools before starting a project. Python is a popular, powerful, and surprisingly readable programming language. Its design philosophy emphasizes code readability with its notable use of significant whitespace.

You can download the official installer from the Python website at python.org. The installation process is typically straightforward. Just run the installer and follow the on-screen instructions. Make sure to check the box that says "Add Python to PATH" during installation. This small step makes it much easier to run your programs from the command line.

Lesson image

Once installed, Python comes with a simple built-in editor called IDLE. It's a great place to start writing and running your first lines of code.

Your First Program

A long-standing tradition in programming is to make your first program display the text "Hello, World!". It's a simple way to confirm that your setup is working correctly. In Python, this is incredibly easy.

Open IDLE or any text editor, type the following line, and save the file with a .py extension, like hello.py.

print("Hello, World!")

When you run this file, you'll see Hello, World! printed to the screen. The print() part is a function. Functions are reusable blocks of code that perform a specific action. The print() function's job is to display whatever you put inside its parentheses.

Making Notes with Comments

As your programs get more complex, you'll want to leave notes for yourself and for others who might read your code. These notes are called comments. The computer completely ignores them; they're just for humans.

In Python, any line that starts with a hash symbol (#) is a comment.

# This line is a comment. Python won't run it.
print("This line will run.")

# You can also use comments to temporarily disable code.
# print("This line is commented out and won't run.")

Comments are essential for explaining why you wrote the code a certain way. Good code explains how it works, but good comments explain why it's necessary.

Storing and Managing Information

Programs need to work with information, like numbers, text, and other types of data. To keep track of this information, we use variables.

variable

noun

A named storage location in memory that holds a value. You can change the value stored in a variable.

Think of a variable as a labeled box where you can store something. You give the box a name, and you can put data inside it. To create a variable in Python, you just choose a name, use the equals sign (=), and provide a value.

# Storing text (a string)
user_name = "Alice"

# Storing a whole number (an integer)
user_age = 25

# Storing a number with a decimal (a float)
account_balance = 150.75

# Storing a true or false value (a boolean)
is_active = True

The type of data a variable holds is called its data type. In the example above, "Alice" is a string (text), 25 is an integer, 150.75 is a float, and True is a boolean. Python is smart enough to figure out the data type automatically.

Variable names can't start with a number and can't be the same as a Python keyword. Keywords are reserved words that have special meaning in the language, like if, for, and while.

Working with Operators

Once you have data stored in variables, you can perform operations on it. Operators are special symbols that perform computations, like addition or subtraction.

Arithmetic operators work just like they do in math.

OperatorNameExampleResult
+Addition5 + 38
-Subtraction5 - 32
*Multiplication5 * 315
/Division6 / 32.0
**Exponent5 ** 225
%Modulo5 % 32

Comparison operators are used to compare two values. They always result in a boolean value: either True or False.

x = 10
y = 5

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

# Is x equal to y? (Note the double equals sign)
print(x == y)  # Output: False

# Is x not equal to y?
print(x != y)  # Output: True

These basic building blocks—variables, data types, and operators—are the foundation of every Python program you'll ever write.

Quiz Questions 1/6

When installing Python, which option is crucial for making it easier to run programs from the command line?

Quiz Questions 2/6

What is the primary purpose of a comment in a Python script?