No history yet

Python Basics

Your First Steps in Python

Before you can write Python code, you need a place to run it. The easiest way to start is by installing Python from the official website, python.org. This installation typically includes an Integrated Development and Learning Environment (IDLE), which is a simple editor that lets you write and run code immediately. For larger projects, many developers use more advanced code editors like Visual Studio Code or PyCharm, but for now, IDLE is all you need.

Lesson image

Once you have Python set up, you can write your first program. It's a tradition to start with a program that just prints "Hello, World!" to the screen. In Python, this is incredibly straightforward.

print("Hello, World!")

Type that into your editor or interactive shell and run it. You've just executed your first piece of Python code. Notice there are no semicolons at the end of the line or curly braces. Python's syntax is designed to be clean and readable.

Syntax and Indentation

One of the most distinctive features of Python is its use of indentation. Where other languages might use brackets or keywords to define blocks of code, Python uses whitespace. This isn't just for style; it's a rule of the language. Code that is part of the same block must be indented at the same level.

Consistent indentation is not optional in Python. It's how the interpreter understands the structure of your program.

For example, when we get to conditional statements, you'll see how indentation defines what code runs when a condition is met. The standard is to use four spaces for each level of indentation. Most code editors can be configured to insert four spaces when you press the Tab key.

# This is a comment. It's ignored by Python.

# This code is NOT indented, so it's in the main block.
print("This is the first line.") 

if True:  # We'll cover 'if' statements soon.
    # This line IS indented, so it belongs to the 'if' block.
    print("This line is inside the if statement.")

# This line is not indented, so we're back to the main block.
print("This is the last line.")

Variables and Data

Programming is all about working with data. We use variables to store data so we can refer to it and manipulate it later. Think of a variable as a labeled box where you can keep a piece of information. Creating a variable in Python is as simple as choosing a name and using the equals sign (=) to assign it a value.

message = "This is a string of text."
user_age = 30
pi_approx = 3.14
is_learning = True

Python automatically detects the type of data you assign. In the example above, we have four of the most common basic data types:

  • String (str): A sequence of characters, like text. Always enclosed in single or double quotes.
  • Integer (int): A whole number, without a decimal point.
  • Float (float): A number that has a decimal point.
  • Boolean (bool): Represents one of two values: True or False. Booleans are the foundation of decision-making in code.

Once a value is stored in a variable, you can use the variable name to access it. For instance, print(user_age) would display 30 on the screen.

Making Things Happen

Variables are just storage. The real work happens when we use operators to create expressions. Expressions combine variables and values to produce a new value.

You're already familiar with many operators from math class. Python uses them in a similar way.

Operator TypeOperatorsExampleResult
Arithmetic+, -, *, /, %10 + 515
Comparison==, !=, <, >, <=user_age == 30True
Logicaland, or, notis_learning and user_age > 18True

A key part of programming is controlling the flow of execution, which means deciding which lines of code should run and when. This is handled by control structures.

The if statement is the most basic control structure. It runs a block of code only if a certain condition is true.

temperature = 75

if temperature > 70:
    print("It's a warm day!")

Loops allow you to repeat a block of code multiple times. A for loop is great for iterating over a sequence of items, while a while loop continues as long as a condition remains true.

# A 'for' loop that prints numbers 0 through 4
for i in range(5):
    print(i)

# A 'while' loop that counts down from 3
count = 3
while count > 0:
    print(count)
    count = count - 1 # Update the counter
print("Liftoff!")

These building blocks, variables, operators, and control structures, are the foundation of every Python program.

Quiz Questions 1/5

What is the primary purpose of indentation in Python?

Quiz Questions 2/5

Which line of code will correctly print the text 'Hello, World!' to the screen?