No history yet

Introduction to Python

Your First Program

Let's start by writing a single line of code. This simple program will print a message to the screen. In programming, this is often called a "Hello, World!" program.

print("Hello, World!")

Here's what's happening:

  • print() is a function, a pre-written piece of code that performs a specific action. In this case, it displays text on the screen.
  • "Hello, World!" is the argument we give to the function. It's the data we want the function to work with. The quotation marks tell Python that this is a piece of text, also known as a string.

Python's syntax is designed to be clean and readable. You'll notice there are no semicolons or other symbols at the end of the line. For the most part, what you write looks a lot like plain English.

Lesson image

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.

# We create a variable named 'message' and store text in it.
message = "This is a stored message."

# Now we can use the variable's name to access the data.
print(message)

Python works with several basic data types. You don't usually have to tell Python what type a variable is; it figures it out automatically.

Strings (str): Used for text. You create them with single or double quotes. greeting = "Hello there"

Integers (int): Used for whole numbers. user_age = 25

Floats (float): Used for numbers with decimal points. price = 19.99

Booleans (bool): Used for True or False values, which are essential for making decisions in your code. is_active = True

You can also get data from the user with the input() function. This function prompts the user to type something and then returns their input as a string.

# Ask the user for their name and store it
name = input("What is your name? ")
print("Hello, " + name)

# input() always gives back a string, so we must convert it for math
age_text = input("How old are you? ")
age_number = int(age_text) # Convert the string to an integer

years_until_100 = 100 - age_number
print("You will be 100 in", years_until_100, "years.")

Making Decisions and Repeating Actions

Programs often need to perform different actions based on certain conditions. We use if statements to control this flow. The code inside an if statement only runs if its condition is True.

Notice the colon at the end of the if line and the indented code below it. In Python, indentation is not just for looks—it's how you group lines of code together. This is a core part of Python's syntax.

temperature = 75

if temperature > 80:
    print("It's a hot day!")
elif temperature < 60: # 'elif' is short for 'else if'
    print("Better bring a jacket.")
else:
    print("The weather is perfect.")

Sometimes you need to repeat an action multiple times. This is done with loops.

A for loop is great for when you want to repeat a task a specific number of times or for each item in a list.

# The range(5) function generates numbers from 0 up to (but not including) 5.
for number in range(5):
    print("Repeating action, lap", number)

A while loop is used when you want to keep repeating an action as long as a certain condition remains True.

count = 0
while count < 3:
    print("Count is:", count)
    count = count + 1 # This increases the count by 1 each time

Be careful with while loops! If the condition never becomes false, you'll create an infinite loop that runs forever.

Creating Reusable Code Blocks

As your programs get more complex, you'll find yourself writing the same bits of code over and over. Functions let you package a block of code, give it a name, and reuse it whenever you need it. This makes your code more organized and easier to read.

We define a function using the def keyword.

# Define a function that takes one piece of data (a 'name')
def greet(name):
    # This is the code inside the function
    greeting_message = "Hello, " + name + "!"
    return greeting_message

# Call the function with different arguments and print the result
print(greet("Alice"))
print(greet("Bob"))

In this example, name is a parameter—a placeholder for the data the function will receive. When we call greet("Alice"), the string "Alice" is the argument passed to the function. The return keyword sends a value back from the function, which we can then print or store in another variable.

Python also comes with many pre-written collections of functions, called modules. You can bring these into your program using the import statement. For example, the random module has functions for generating random numbers.

import random # Gives us access to the random module

# Generate a random integer between 1 and 10 (inclusive)
random_number = random.randint(1, 10)
print("Your lucky number is:", random_number)

You've now covered the core building blocks of Python. Let's review these concepts before you test your knowledge.

Ready to see what you've learned? Give these questions a try.

Quiz Questions 1/6

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

Quiz Questions 2/6

Which of the following lines of code will correctly print the message Welcome! to the screen?

With these fundamentals—variables, control structures, and functions—you have the tools to start writing your own simple but powerful Python scripts.