No history yet

Python Basics

Getting Started with Python

Before you can write Python code, you need a way to run it. Python code is just plain text saved in a file, usually with a .py extension. To bring that code to life, you need a program called the Python interpreter. The interpreter reads your code line by line and executes the instructions.

The best way to get the interpreter is to download it directly from the official Python website, python.org. You'll find installers for Windows, macOS, and instructions for Linux. Running the installer will set up everything you need.

While you can write code in any plain text editor, most developers use an Integrated Development Environment, or IDE. An IDE is a special text editor that understands Python. It helps you by highlighting your code in different colors, pointing out simple errors, and making it easy to run your programs. When you install Python, it comes with a simple IDE called IDLE, which is great for beginners.

Lesson image

Your First Program

A long-standing tradition in programming is to make your first program display the message "Hello, World!" on the screen. In Python, this is incredibly simple. It takes just one line of code.

print("Hello, World!")

Let's break this down. print() is a built-in Python command, called a function, that tells the computer to display output. Whatever you put inside the parentheses is what gets displayed. Because we want to display text, we have to wrap it in quotation marks. This tells Python that it's a piece of text, also known as a string.

The print() function is your primary tool for seeing the output of your code and understanding what it's doing.

Storing Information

Writing code often involves working with data. To keep track of this data, we use variables. Think of a variable as a labeled box where you can store a piece of information. You give the box a name, and you can put something inside it. Later, you can refer to the box by its name to see what's inside or to change its contents.

# Assigning a string to a variable
message = "Hello, Python learner!"

# Printing the value stored in the variable
print(message)

In this example, message is the name of our variable. We use the equals sign (=), which is the assignment operator, to store the text string "Hello, Python learner!" inside it. When we use print(message), Python looks up what's stored in the message variable and prints that value.

Variables can hold many different types of data, not just text. Here are a few fundamental data types:

Data TypeDescriptionExample
String (str)A sequence of characters. Used for text."Hello" or 'Python'
Integer (int)A whole number, without a fractional part.10, -5, 0
Float (float)A number with a decimal point.3.14, -0.5, 2.0
Boolean (bool)Represents one of two values: True or False.True, False

Python is smart about data types. You don't have to tell it what kind of data a variable will hold. It figures it out automatically based on the value you assign.

name = "Alice"       # This is a string
age = 30            # This is an integer
height = 5.5        # This is a float
is_student = True  # This is a boolean

print(name)
print(age)

Interacting with the User

So far, we've only been displaying information. We can also write programs that ask the user for information. To do this, we use another built-in function called input(). This function pauses your program, waits for the user to type something and press Enter, and then gives you back what they typed.

# The text inside input() is the prompt shown to the user
user_name = input("What is your name? ")

print("Hello, " + user_name)

When this code runs, it will first display "What is your name? ". Then, it will wait. If you type "Bob" and press Enter, the program will store "Bob" in the user_name variable. Finally, it will print "Hello, Bob".

Notice the + sign. When used with strings, it doesn't add numbers. Instead, it joins the strings together. This is called concatenation.

One important thing to know is that input() always gives you back a string, even if the user types a number. If you need to treat the input as a number (for example, to do math), you have to convert it first.

age_string = input("How old are you? ")

# Convert the string to an integer
age_number = int(age_string)

year_of_birth = 2024 - age_number

print("You were born in", year_of_birth)

Here, we use the int() function to convert the string from the user into an integer. Now we can perform subtraction with it. There are similar functions, like float(), to convert to other data types.

Let's check your understanding of these basic building blocks.

Quiz Questions 1/5

What is the primary role of the Python interpreter?

Quiz Questions 2/5

Which line of Python code correctly stores the number 42 in a variable named age?

You've taken your first steps into Python. You know how to display output, store information in variables, and get input from a user. These are the fundamental skills you'll build on as you learn more.