No history yet

Introduction to Python

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 an excellent choice for beginners. You can use Python for everything from building websites and analyzing data to creating games.

To write and run Python code, you'll need an Integrated Development Environment, or IDE. An IDE is like a word processor for code, with special tools that help you write, test, and run your programs. We'll use Thonny, a simple IDE designed specifically for learning.

You can download and install Thonny for free from its official website, thonny.org. It works on Windows, Mac, and Linux.

Once you have Thonny open, you'll see a main window for writing code and a smaller window at the bottom called the 'Shell'. The Shell is where you'll see the output of your programs.

Let's write your first program. In the main editor window, type the following line:

print("Hello, Python!")

Now, click the green 'Run' button (it looks like a play symbol). Thonny will ask you to save the file. Name it hello.py and save it. After you save it, you should see the text Hello, Python! appear in the Shell. You've just executed your first program!

The print() command is a built-in function that tells the computer to display whatever you put inside the parentheses on the screen.

The Building Blocks

Programs work by handling information. In Python, we store this information in 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.

# Create a variable named 'greeting'
# and store the text "Welcome!" in it.
greeting = "Welcome!" 

# Now, print the contents of the variable.
print(greeting)

The data we store comes in different forms, known as data types. The most common ones are text and numbers.

string

noun

A sequence of characters, like a word or sentence. In Python, strings are enclosed in either single quotes (') or double quotes (").

integer

noun

A whole number, without any decimal points. It can be positive, negative, or zero.

float

noun

A number that has a decimal point. The name comes from "floating-point number."

Here's how you'd create variables for each of these types:

# A string
user_name = "Alex"

# An integer
user_age = 30

# A float
bank_balance = 150.75

Python automatically figures out the data type based on the value you assign.

Controlling the Flow

So far, our code runs straight from top to bottom. But what if we want the program to make decisions or repeat actions? That's where control structures come in. They let you control the path your program takes.

The most common decision-making tool is the if statement. It checks if a certain condition is true and runs a block of code only if it is. Let's look at an example.

age = 19

if age >= 18:
    print("You are old enough to vote.")

Notice the line print(...) is indented. In Python, indentation is not just for looks; it's how you group statements together. The indented code only runs if the if condition (age >= 18) is true.

We can expand on this with else to provide an alternative path.

age = 15

if age >= 18:
    print("You are old enough to vote.")
else:
    print("You are not old enough to vote yet.")

Another essential control structure is a loop, which repeats a block of code multiple times. The for loop is perfect for repeating an action a specific number of times.

# This loop will run 5 times
# range(5) generates numbers 0, 1, 2, 3, 4
for number in range(5):
    print("This is loop number", number)

This simple loop prints a message five times, each time updating the value of the number variable. These building blocks—variables, data types, and control structures—are the foundation of almost every program you'll write.

Now let's test your understanding of these core concepts.

Quiz Questions 1/5

What is the primary role of an Integrated Development Environment (IDE) like Thonny?

Quiz Questions 2/5

What would be the output of the following Python code in the Shell?

print("Hello, Python!")

With these fundamentals, you have a solid starting point for exploring the world of programming with Python.