No history yet

Python Basics

The Language of Code

Learning to code in Python is a lot like learning a spoken language. It has vocabulary (keywords) and grammar rules (syntax) that you need to follow for the computer to understand your instructions. One of Python's most famous features is its clean and readable syntax. It's designed to be straightforward, which is why it's a great language for beginners.

The fundamental rule in Python is that whitespace matters. Indentation, the space at the beginning of a line, is used to define blocks of code, something other languages often use curly braces for. This makes the code's structure visually clear.

Let's start with the traditional first program anyone writes in a new language. It simply prints a message to the screen.

# This is a comment. Python ignores lines starting with a #.
# The print() function displays output to the screen.
print("Hello, World!")

This single line of code tells the computer to display the text "Hello, World!". Simple, right? The text inside the quotation marks is called a string, which is one of several fundamental data types we'll explore next.

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 you can put things in it, take them out, or replace them.

Every piece of data in Python has a type. The type tells the computer what kind of data it is and what operations can be performed on it. Let's look at the most common ones.

Data TypeDescriptionExample
IntegerWhole numbers, without decimals.42, -100
FloatNumbers with a decimal point.3.14, -0.5
StringA sequence of characters (text)."hello", 'Python'
BooleanRepresents one of two values: True or False.True, False

You create a variable by choosing a name and using the equals sign (=) to assign it a value.

# Assigning values to variables
student_count = 1500      # An integer
sale_price = 24.99        # A float
user_name = "Alex"        # A string
is_active = True          # A boolean

# You can print the variables to see their values
print(student_count)
print(user_name)

Making Decisions and Repeating Actions

Programs aren't just lists of instructions executed in order. They need to respond to different situations and perform repetitive tasks. This is where control structures come in. They control the flow of the program's execution.

The two main types of control structures are conditionals (if-statements) and loops (for-loops and while-loops).

An if statement checks if a condition is true and runs a block of code only if the condition is met. You can add an else clause to run a different block of code if the condition is false.

temperature = 75

# Note the colon at the end of the line and the indentation for the code inside the if/else blocks.
if temperature > 80:
    print("It's a hot day!")
else:
    print("It's not too hot.")

A for loop is used to iterate over a sequence, like a list of items or a range of numbers. It runs a block of code once for each item in the sequence.

# This loop will run 5 times, for numbers 0 through 4.
for i in range(5):
    print("This is loop number", i)

Interacting with the User

So far, all our data has been hard-coded directly into the program. To make programs interactive, we need a way to get input from the user and display output back to them.

We've already seen the print() function for displaying output. To get input from a user, we can use the input() function.

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

# The input() function always returns a string.
print("Hello, " + name + "!")

When the program runs this code, it will pause and wait for the user to type something and press Enter. Whatever they type is then stored as a string in the name variable.

These building blocks—syntax, data types, control structures, and input/output—are the foundation upon which all Python programs are built. Mastering them is the first step toward tackling any programming challenge.

Quiz Questions 1/5

Which line of code correctly prints the text "Learning Python" to the screen?

Quiz Questions 2/5

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