No history yet

Python Basics

Your First Python Code

Before you can write Python, you need to have it installed on your computer. You can download it for free from the official Python website. Once installed, you have a couple of ways to run code. You can use Python's interactive shell for quick tests or write your code in a file (usually ending with .py) and run it from your terminal.

Lesson image

A classic first program for any language is one that just prints "Hello, World!" to the screen. In Python, this is incredibly simple. It's just one line.

print("Hello, World!")

That's it. The print() function is a built-in command that tells the computer to display whatever you put inside the parentheses.

One of the most important rules in Python is indentation. Unlike many other languages that use brackets or keywords to group code, Python uses whitespace. Lines of code that are part of the same block must be indented at the same level. You'll see this in action more when we get to loops and functions, but for now, just know that incorrect indentation will cause errors.

In Python, indentation isn't just for style, it's a rule the language enforces.

Variables and Data Types

Imagine you need to store a piece of information that you'll use later, like a person's age or a username. You can store it in a variable. A variable is like a labeled box where you can keep a value.

variable

noun

A symbolic name that is a reference or pointer to an object. Once an object is assigned to a variable, you can refer to the object by that name.

Assigning a value to a variable is done with the equals sign (=).

# Assigning a number to the variable 'age'
age = 30

# Assigning text to the variable 'name'
name = "Alice"

The data you store in variables comes in different types. Python is smart and usually figures out the type on its own. The most common basic types are:

  • Integers: Whole numbers, like -5, 0, or 100.
  • Floats: Numbers with a decimal point, like 3.14 or -0.5.
  • Strings: Text, enclosed in single (') or double (") quotes.
  • Booleans: Can only be one of two values: True or False.
user_count = 150        # This is an integer
pi_approx = 3.14159    # This is a float
greeting = "Welcome!"   # This is a string
is_active = True         # This is a boolean

Working with Operators

Operators are special symbols that perform operations on variables and values. You're already familiar with many of them from math class.

Python has three main types of operators for basic tasks: arithmetic, comparison, and logical.

Arithmetic operators perform mathematical calculations.

OperatorNameExampleResult
+Addition5 + 27
-Subtraction5 - 23
*Multiplication5 * 210
/Division5 / 22.5
//Floor Division5 // 22
%Modulus5 % 21
**Exponentiation5 ** 225

Comparison operators compare two values and result in a boolean (True or False).

OperatorNameExampleResult
==Equal to5 == 2False
!=Not equal to5 != 2True
>Greater than5 > 2True
<Less than5 < 2False
>=Greater than or equal to5 >= 5True
<=Less than or equal to5 <= 2False

Logical operators (and, or, not) are used to combine boolean values.

# Returns True because both conditions are true
(5 > 2) and (3 < 4)

# Returns True because at least one condition is true
(5 > 2) or (3 > 4)

# Returns False because it inverts the True result of (5 > 2)
not (5 > 2)

Now let's review these core concepts.

Time to test your understanding.

Quiz Questions 1/5

Which line of Python code will correctly print the text "Hello, Python!" to the screen?

Quiz Questions 2/5

In Python, code blocks (like those in loops or functions) are defined by their indentation.

With these building blocks, you're ready to start writing simple programs and explore more of what Python can do.