No history yet

Python Basics

Your First Lines of Code

Python is famous for its clean and readable syntax. It looks a lot like plain English, which makes it a great language for beginners. One of the first things to know is that Python takes whitespace seriously. Unlike many programming languages that use curly braces {} to group code, Python uses indentation. How you space your code is part of the code itself.

Indentation isn't just for style in Python—it's a rule the computer follows.

Before we write any real instructions, let's look at how to leave notes in our code. Comments are lines that the computer completely ignores. They’re for you and other programmers to read. To write a comment, you just start the line with a hash symbol (#).

# This is a comment. Python will ignore it.
# You can use comments to explain what your code does.

Now for an actual instruction. The most basic way to see the result of your code is to print a message to the screen. In Python, we do this with the print() function. A function is a reusable block of code that performs a specific action. The print() function's action is to display whatever you put inside its parentheses.

print("Hello, World!")

Run that code, and you'll see the text Hello, World! appear. Congratulations, you've just written your first Python program.

Storing Information

Programs need to work with data, and we need a way to store that data while the program is running. We do this using variables. Think of a variable as a labeled box where you can store a piece of information. You give the box a name, and you can put something inside it. The = symbol is used to assign a value to a variable.

# Assign the text "Welcome to Python!" to a variable named 'greeting'
greeting = "Welcome to Python!"

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

Here, greeting is the name of our variable. The computer now knows that greeting holds the text "Welcome to Python!". When we ask it to print greeting, it looks inside that box and shows us the contents. Notice we don't use quotes when printing the variable name.

The information we store comes in different forms, or data types. Let's look at the most common ones.

Core Data Types

Every value in Python has a type, which tells the computer what kind of data it is and what can be done with it.

Common data types include numbers for calculations, text for display, and true/false values for making decisions.

Integers and Floats are Python's number types. Integers are whole numbers, like 10, -5, or 0. Floating-point numbers, or floats, are numbers with a decimal point, like 3.14 or -0.001.

year = 2024         # An integer
pi_approx = 3.14159  # A float

print(year)
print(pi_approx)

Next up is the string, which is used for text. You create a string by putting text inside single (') or double (") quotes.

string

noun

A sequence of characters used to represent text.

Finally, we have the boolean data type. This one is simple: it can only be one of two values, True or False. Booleans are the foundation of logic in programming. They are essential for making decisions, which we'll explore later. Note that True and False must be capitalized.

is_learning = True
is_difficult = False

print(is_learning)

Interacting with Users

Besides printing output, you can also get input from a user. The input() function pauses your program and waits for the user to type something and press Enter. Whatever the user types is then returned as a string.

# Ask the user for their name and store it in the 'name' variable
name = input("What is your name? ")

# Greet the user by name
print("Hello, " + name + "!")

In this example, the text inside input() is a prompt shown to the user. The program waits, captures the user's entry in the name variable, and then uses it to print a personalized greeting. The + operator here joins the strings together.