No history yet

Python Basics

The Grammar of Python

Every language has rules. In English, we use punctuation and arrange words in a specific order to form sentences. Programming languages are no different. The set of rules that defines how a program is written and interpreted is called its syntax.

Python is known for its clean and readable syntax. It's designed to be less cluttered than many other languages. The goal is to let you focus on what you want to do, not how you have to write it. Let's look at the most basic instruction you can give.

print("Hello, World!")

This line is a statement, a complete instruction that the Python interpreter can execute. In this case, it tells the computer to display the text "Hello, World!" on the screen. The text inside the quotes is a piece of data.

Storing Information

To do anything useful, we need a way to store and label data. Think of it like putting items in labeled boxes. In programming, these boxes are called variables.

A variable is just a name that refers to a value. You create one using an assignment statement, which uses a single equals sign (=).

variable

noun

A name that represents or refers to a value stored in the computer's memory.

Let's assign the text "Hello, Python!" to a variable named greeting.

# The variable 'greeting' now holds the text.
greeting = "Hello, Python!"

# We can print the variable's value.
print(greeting)

The name greeting now points to the piece of text. We can use that name anywhere we would have used the original text. You can name variables almost anything you want, but it's best to choose names that describe the data they hold. Variable names can't have spaces and typically use lowercase letters and underscores, like first_name.

Types of Data

Variables can hold different kinds of information. These different kinds are called data types. Python automatically figures out the type of data you're storing. The most common types are numbers, text, and booleans.

  • Integers (int): Whole numbers, like -5, 0, or 100.
  • Floating-Point Numbers (float): Numbers with a decimal point, like 3.14 or -0.01.
  • Strings (str): Sequences of characters, like text. You create them by wrapping text in single (') or double (") quotes.
  • Booleans (bool): Can only be one of two values: True or False. They are essential for making decisions in code.
Data TypePython NameExample
Integerint42
Floatfloat98.6
Stringstr"Zoe"
BooleanboolTrue

Operators and Expressions

Now that we have data, we can start working with it. Operators are special symbols that perform operations on values and variables. The values that an operator acts on are called operands.

A combination of values, variables, and operators that Python can evaluate to produce a result is called an expression.

Think of it like math. In 2 + 3, the + is the operator, and 2 and 3 are the operands. The whole thing, 2 + 3, is an expression.

Python has several types of operators. Arithmetic operators are the most familiar.

# Arithmetic Operators
addition = 10 + 5       # Result is 15
subtraction = 20 - 8    # Result is 12
multiplication = 4 * 7  # Result is 28
division = 30 / 5       # Result is 6.0

Notice that division always produces a float, even if the result is a whole number. There are also comparison operators, which compare two values and result in a boolean (True or False).

# Comparison Operators
is_equal = (5 == 5)        # Result is True
is_not_equal = (5 != 6)    # Result is True
is_greater = (10 > 20)     # Result is False
is_less_or_equal = (3 <= 3) # Result is True

Each line above contains an expression on the right side of the = sign. Python evaluates that expression first, gets a result (True or False), and then assigns that result to the variable on the left. The entire line is a complete instruction—a statement.

Quiz Questions 1/5

What is the term for the set of rules that defines how a program is written and interpreted?

Quiz Questions 2/5

Which of the following lines of code correctly assigns the text "Python" to a variable named language?

These are the fundamental pieces of Python. With variables to store data, data types to classify it, and operators to manipulate it, you have the basic tools to start building programs.