No history yet

Python Basics

Getting Started with Python

Python is a popular programming language known for its clear and readable syntax. It’s like writing in a simplified version of English, which makes it a great choice for beginners.

First, you need to get Python on your computer. The easiest way is to visit the official Python website, python.org, and download the installer for your operating system. The installation is straightforward and includes a simple program called IDLE, which gives you a place to write and run your first lines of code.

Lesson image

Once it's installed, you can start programming. A long-standing tradition for programmers is to make their first program print the text "Hello, World!". In Python, it's just one simple line.

print("Hello, World!")

That's it! The print() function tells the computer to display whatever you put inside the parentheses.

Storing Information

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

# You can assign the number 28 to a variable named 'age'.
age = 28

# You can assign the text "Alice" to a variable named 'name'.
name = "Alice"

# Now you can use the variable names to access the data.
print(name)
print(age)

The data we store comes in different forms, called data types. Python automatically figures out the type of data you're storing. The most common types are:

Data TypeDescriptionExample Code
Integer (int)Whole numbers, without fractions.score = 100
Float (float)Numbers that have a decimal point.price = 19.99
String (str)A sequence of characters, like text.greeting = "Hi there!"
Boolean (bool)Represents one of two values: True or False.is_active = True

Strings are always wrapped in either single quotes ('...') or double quotes ("..."). Booleans are useful for tracking conditions, like whether a user is logged in or not.

Making Things Happen with Operators

Operators are the symbols that perform operations on your variables and values. You already know many of them from math class. Python uses them in a similar way.

Arithmetic operators perform calculations:

x = 10
y = 3

print(x + y)  # Addition -> 13
print(x - y)  # Subtraction -> 7
print(x * y)  # Multiplication -> 30
print(x / y)  # Division -> 3.333...
print(x % y)  # Modulo (remainder of division) -> 1

Comparison operators compare two values and give you a Boolean (True or False) result. They are essential for making decisions in your code.

a = 5
b = 10

print(a == b)  # Equal to -> False
print(a != b)  # Not equal to -> True
print(a < b)   # Less than -> True
print(a >= 5)  # Greater than or equal to -> True

Finally, logical operators (and, or, not) let you combine Boolean values to check multiple conditions at once.

and is true only if both conditions are true. or is true if at least one condition is true. not flips the value (True becomes False, False becomes True).

Controlling the Flow

So far, our code runs straight from top to bottom. Control flow structures let us change that path, allowing our programs to make decisions or repeat actions.

The if statement runs a block of code only if a certain condition is true.

temperature = 75

if temperature > 70:
    print("It's a warm day!")

Notice the indentation (the space before print). In Python, indentation is crucial. It tells Python which lines of code belong to the if statement.

You can add an else block to run code when the condition is false.

temperature = 60

if temperature > 70:
    print("It's a warm day!")
else:
    print("It's a bit chilly.")

To repeat a block of code multiple times, we use loops. The for loop is great for iterating over a sequence of items, like numbers in a range.

# This loop will run 5 times, for numbers 0 through 4.
for i in range(5):
    print("This is loop number", i)

The while loop keeps running as long as its condition is true. It's useful when you don't know in advance how many times you need to loop.

count = 1

while count <= 3:
    print("Count is:", count)
    count = count + 1  # Increment the count to eventually stop the loop

It's important to make sure the condition for a while loop will eventually become false. Otherwise, you'll create an infinite loop that never ends!

Quiz Questions 1/6

What is the primary function used in Python to display text or the value of a variable on the screen?

Quiz Questions 2/6

In Python, what is the main purpose of indentation (the spaces at the beginning of a line)?

These are the fundamental building blocks of Python. With variables, operators, and control flow, you can start writing simple but powerful programs.