No history yet

Introduction to Python

What is Python?

Python is a popular programming language known for its clear and readable syntax. Think of syntax as the grammar rules for a language. Just as English has rules for constructing sentences, Python has rules for writing code. The good news is that Python's rules are designed to be straightforward, often resembling plain English.

Lesson image

This simplicity makes it an excellent language for beginners. Let's look at the classic first program everyone writes: printing "Hello, World!" to the screen.

# This line is a comment. Python ignores it.
# The print() function displays text to the screen.
print("Hello, World!")

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 you can put different things inside it.

Python also needs to know what kind of data it's working with. Is it a number? A piece of text? A true/false value? These categories are called data types.

Data TypeDescriptionExample
String (str)A sequence of characters (text)"Alice"
Integer (int)A whole number30
Float (float)A number with a decimal point5.5
Boolean (bool)A value that is either True or FalseTrue

Creating a variable is simple. You just choose a name, use the equals sign (=), and provide the value.

# Assigning values to variables
name = "Alice"
age = 30
height_in_feet = 5.5
is_student = True

# You can print variables to see their values
print(name)
print(age)

Making Decisions

Programs become powerful when they can make decisions and repeat actions. These are called control structures. They control the flow of the program's execution.

The most basic decision-making tool is the if statement. It checks if a condition is true and runs a block of code only if it is. You can add an else block to run code if the condition is false. For more complex choices, elif (short for "else if") lets you check multiple conditions in a row.

temperature = 75

if temperature > 80:
    print("It's a hot day!")
elif temperature > 65:
    print("The weather is nice.")
else:
    print("It's a bit chilly.")

# Output will be: The weather is nice.

Repeating actions is handled by loops. A for loop is perfect for when you want to go through a sequence of items, like a list, and do something with each item.

shopping_list = ["apples", "bread", "milk"]

for item in shopping_list:
    print(f"Don't forget the {item}!")

Creating Reusable Code

As programs grow, you'll find yourself writing the same lines of code over and over. Functions solve this problem by letting you package a block of code, give it a name, and run it whenever you want. This makes your code more organized, readable, and reusable.

function

noun

A named sequence of statements that performs a computation. You define it once and can run it multiple times.

You define a function using the def keyword, followed by the function's name and parentheses. Any code that is part of the function must be indented underneath. Some functions take inputs, called parameters, which you place inside the parentheses.

# Define a function that greets a person
def greet(name):
    # This code runs when the function is called
    message = f"Hello, {name}! Welcome."
    return message

# Call the function with an argument
# and store the result in a variable
welcome_message = greet("Charlie")

print(welcome_message)
# Output: Hello, Charlie! Welcome.

The return keyword sends a value back from the function. In this example, the greet function returns a formatted string, which we then store in the welcome_message variable and print.

Quiz Questions 1/5

In Python, what is the primary purpose of a variable?

Quiz Questions 2/5

Which keyword is used to define a function in Python?