No history yet

Python Basics

Setting Up Your Workspace

Before you can write any Python code, you need to install the Python interpreter on your computer. Think of the interpreter as a translator that converts your Python code into instructions the computer can understand. The best place to get it is from the official Python website, python.org. The installation is straightforward for Windows, macOS, and Linux.

Once installed, you'll have access to a program called IDLE (Integrated Development and Learning Environment). It’s a simple tool that lets you write and run Python code right away. It's perfect for beginners. As you advance, you might switch to more powerful code editors like VS Code or PyCharm, but for now, IDLE has everything you need.

Lesson image

Syntax and Spacing

Python was designed to be easy to read, almost like plain English. Its rules, or syntax, are simpler than many other programming languages. The first command nearly everyone learns is print(), which simply displays text or the value of a variable on the screen.

print("Hello, World!")

One of the most unique and important rules in Python is its use of indentation. In many languages, whitespace like spaces and tabs are just for making code look neat. In Python, indentation is part of the syntax. It's used to define blocks of code, such as what's inside a loop or a function. You don't need to worry about those yet, but it's crucial to get into the habit of indenting correctly from the start. A new block of code is indented, and the end of the indentation signals the end of that block. Most programmers use four spaces for each level of indentation.

Storing Information

In programming, we need a way to store and manage information. We do this using variables. You can 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 data inside it, take it out, or replace it with something new.

To create a variable in Python, you just choose a name, use the equals sign (=), and provide a value.

# Assign the text "Hello, Python learner!" to a variable named 'message'
message = "Hello, Python learner!"

# Print the value stored in the 'message' variable
print(message)

The data you store in variables comes in different forms, or data types. Python automatically detects the type of data you're using. For now, we'll focus on four basic types.

Data TypeDescriptionExample Code
IntegerWhole numbers, positive or negative.age = 30
FloatNumbers with a decimal point.price = 19.99
StringA sequence of characters, like text.name = "Ada Lovelace"
BooleanRepresents one of two values: True or False.is_active = True

Basic Operations

Operators are special symbols that perform operations on variables and values. Python has several types, but we'll start with the three most common ones: arithmetic, comparison, and logical.

Arithmetic operators are used for mathematical calculations. They work just like they do in regular math.

a = 15
b = 4

print(a + b)  # Addition: 19
print(a - b)  # Subtraction: 11
print(a * b)  # Multiplication: 60
print(a / b)  # Division: 3.75

Comparison operators are used to compare two values. The result of a comparison is always a Boolean value: either True or False.

x = 10
y = 12

# The '==' symbol checks for equality
print(x == y)  # False, because 10 is not equal to 12

print(x < y)   # True, because 10 is less than 12

print(x != y)  # True, because 10 is not equal to 12

Finally, logical operators (and, or, not) are used to combine conditional statements. They're useful for checking multiple conditions at once.

  • and returns True only if both conditions are true.
  • or returns True if at least one of the conditions is true.
  • not reverses the result, returning False if the result is true and vice-versa.
temp = 75
is_sunny = True

# Is it warm AND sunny?
print(temp > 70 and is_sunny == True) # True

# Is it cold OR sunny?
print(temp < 60 or is_sunny == True) # True

# Is it NOT sunny?
print(not is_sunny) # False

Now let's review these foundational concepts.

Quiz Questions 1/5

What is the primary role of the Python interpreter?

Quiz Questions 2/5

In Python, indentation is used only for making the code more readable and has no effect on how the program runs.