No history yet

Python Basics

The Building Blocks of Python

Python is known for its clean and readable syntax, which makes it a great language for beginners. Think of syntax as the grammar of a programming language. It's the set of rules that define how programs are written and interpreted. Getting the syntax right is the first step to telling the computer what you want it to do.

Let's start with the most traditional first program: printing "Hello, World!" to the screen.

# This line of code will display a message.
print("Hello, World!")

That’s it. The print() function is a built-in command that outputs text to the console. The text you want to print, called a string, goes inside the parentheses and is wrapped in quotes.

Storing Information

To do anything useful, we need a way to store and manage information. In programming, we use variables for this. A variable is like 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 with something new.

Creating a variable in Python is simple. You just choose a name, use the equals sign (=), and provide the value you want to store.

current_year = 2024
company_name = "Global Finance Inc."
interest_rate = 0.05

The data we store comes in different forms, known as data types. Python automatically figures out the type of data you're storing. The most common basic types are:

Data TypeDescriptionExample
intInteger, a whole number.42
floatA number with a decimal point.3.14
strString, a sequence of characters (text)."Hello"
boolBoolean, represents True or False.True

Making Things Happen

Once you have data stored in variables, you can perform operations on it. Operators are special symbols that carry out computations. The most familiar are arithmetic operators.

x = 10
y = 3

# Basic arithmetic
print(x + y)  # Addition: 13
print(x - y)  # Subtraction: 7
print(x * y)  # Multiplication: 30
print(x / y)  # Division: 3.333...
print(x ** y) # Exponent: 10 to the power of 3 is 1000
print(x % y)  # Modulo: Remainder of 10 / 3 is 1

We can also compare values using comparison operators. These are crucial for making decisions in our code. The result of a comparison is always a Boolean value: True or False.

price = 50

print(price > 25)   # Is price greater than 25? -> True
print(price == 50)  # Is price exactly equal to 50? -> True
print(price != 100) # Is price not equal to 100? -> True

Notice the double equals sign == for checking equality. A single equals sign = is used only for assigning a value to a variable.

Controlling the Flow

Programs rarely run straight from top to bottom. Often, you need to execute some code only if a certain condition is met. This is where control structures come in. The most fundamental is the if statement.

account_balance = 500
withdrawal_amount = 100

if account_balance >= withdrawal_amount:
    print("Withdrawal successful.")
    # Note the indentation. This is important!
    account_balance = account_balance - withdrawal_amount

print("Your new balance is:", account_balance)

In Python, indentation (the space at the beginning of a line) is not just for looks; it’s part of the syntax. It tells Python which lines of code belong inside the if block.

What if the condition is false? You can use else to provide an alternative block of code to run.

age = 17

if age >= 18:
    print("You are eligible to vote.")
else:
    print("You are not yet eligible to vote.")

Sometimes you need to perform an action over and over. That's what loops are for. A for loop is great for iterating over a sequence of items, like a range of numbers.

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

The Zen of Python

Python has a guiding philosophy that shapes the language and how developers use it. These principles, known as the Zen of Python, were written by longtime contributor Tim Peters. You can actually see them by typing import this into a Python interpreter.

Lesson image

Here are a few of the core ideas:

  • Beautiful is better than ugly.
  • Explicit is better than implicit.
  • Simple is better than complex.
  • Readability counts.

These principles encourage writing code that is not only functional but also clean, understandable, and maintainable. As you continue your journey, keeping this philosophy in mind will help you write code that other programmers (and your future self) will appreciate.

Let's check your understanding of these foundational concepts.

Quiz Questions 1/6

What is the correct way to print the text 'Welcome!' to the console in Python?

Quiz Questions 2/6

Consider the following Python code snippet. What will be printed to the console?

score = 85

if score >= 90:
    print("A")
elif score >= 80:
    print("B")
else:
    print("C")