No history yet

Introduction to Python

What is Python?

Python is a programming language known for its simple, readable style. Think of it like a set of instructions you give a computer. Unlike many other languages that look cryptic, Python code often reads a lot like plain English. This makes it a fantastic starting point for beginners and a powerful tool for experts.

Its versatility is key. Python is used for everything from building websites and analyzing data to creating artificial intelligence. For our purposes, it’s a perfect tool for automating the repetitive, boring tasks you might do at work every day.

Python is a versatile and beginner-friendly programming language that has become immensely popular for its readability and wide range of applications.

Setting Up Your Workspace

Before you can write code, you need a place to write and run it. We'll use an Integrated Development Environment, or IDE. An IDE is like a word processor specifically for code. It helps you write, test, and fix your programs all in one place.

We recommend Thonny, an IDE designed for beginners. It comes with Python built-in, so the setup is simple. Just head to the official Thonny website, download the installer for your operating system (Windows, Mac, or Linux), and run it. The installation process is straightforward; just follow the on-screen prompts.

Lesson image

Once installed, open Thonny. You'll see a main window for writing code and a smaller window at the bottom called the "Shell." This is where the output of your programs will appear.

Your First Python Program

Let's start with a classic tradition in programming: making the computer say hello. In the main editor window in Thonny, type the following line:

print("Hello, World!")

Now, click the green "Run" button (it looks like a play symbol) in the toolbar. Thonny will ask you to save the file. Name it hello.py and save it. The .py extension tells the computer it's a Python file.

After you save it, you'll see the text Hello, World! appear in the Shell at the bottom. You’ve just written and run your first program! The print() command is a built-in Python function that simply displays whatever you put inside the parentheses.

Building Blocks of Code

Programs work by manipulating data. To keep track of that data, we use variables. Think of a variable as a labeled box where you can store information. You give the box a name and put something inside it.

# A string (text) for a person's name
greeting = "Welcome to Python!"

# An integer (whole number) for a count
file_count = 57

# A float (decimal number) for a price
item_price = 24.95

In this example, greeting, file_count, and item_price are variable names. The information they store has a specific data type.

  • String: Plain text, like "Welcome to Python!". Strings are always wrapped in quotes.
  • Integer: A whole number, like 57.
  • Float: A number with a decimal point, like 24.95.

These basic data types—strings, integers, and floats—are the fundamental ingredients for almost any program you'll write.

Making Decisions and Repeating Actions

Programs would be pretty limited if they could only run instructions in a straight line. They get their power from being able to make decisions and repeat tasks. These are handled by control structures.

To make a decision, we use an if statement. It checks if a condition is true and, if so, runs a specific block of code. Notice the colon at the end of the if line and the indentation of the print line. That indentation is crucial in Python; it tells the program which code belongs to the if statement.

temperature = 80

if temperature > 75:
    print("It's a hot day. Drink some water!")

What if you want to repeat an action multiple times? That's where loops come in. A for loop is perfect for running a block of code a specific number of times. For example, to print the numbers 1 through 4:

for number in range(1, 5):
    print(number)

This code tells Python: "For each number in the sequence from 1 up to (but not including) 5, print that number." The range() function is a handy way to generate a sequence of numbers.

Organizing Code with Functions

As your programs get more complex, you'll want to organize your code into reusable blocks. Functions let you do just that. A function is a named block of code that performs a specific task. You can "call" it by name whenever you need it.

We define a function using the def keyword, followed by the function's name, parentheses, and a colon. Just like with if statements and loops, the code inside the function must be indented.

def greet_user():
    print("Hello, welcome!")

# Now we call the function to run its code
greet_user()
greet_user()

When you run this script, it will print "Hello, welcome!" two times. By defining the logic once in a function, you avoid repeating yourself and make your code much easier to read and maintain.

Python also has many pre-built functions organized into modules. You can use the import statement to bring these modules into your program and use the functions they contain. For instance, the random module has functions for generating random numbers.

import random

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

With these fundamentals—variables, data types, control structures, and functions—you have the core tools needed to start writing simple but powerful Python scripts.

Let's check your understanding of these core concepts.

Quiz Questions 1/6

What is the primary characteristic of the Python programming language highlighted in the introduction?

Quiz Questions 2/6

In the code item_price = 24.95, what is the data type of the value stored in the item_price variable?

You've now taken your first steps into the world of Python. You've set up your environment and learned the basic syntax and structures that form the foundation of all Python programs. Next, we'll start putting these skills to work on practical automation tasks.