No history yet

Python Basics

Your First Lines of Code

Let's jump right in. The first program many people write simply prints a message to the screen. In Python, this is incredibly straightforward.

print("Hello, World!")

This line of code uses the print() function to display the text inside the parentheses. The text itself, "Hello, World!", is called a string. A string is just a sequence of characters, and we tell Python it's a string by wrapping it in quotes. You'll notice Python's syntax is clean and reads a bit like plain English, which is one of its biggest strengths.

Variables and Data

Programs need to work with information, like numbers and text. We store this information in variables. Think of a variable as a labeled container where you can keep a piece of data. To create a variable, you just give it a name and use the equals sign (=) to assign it a value.

message = "This is a message."
print(message)

Here, we created a variable named message and stored a string in it. We can then use the variable's name to access its value. Variables can hold different kinds of data, and Python is smart about recognizing them. These different kinds of data are called data types.

The four most common data types are strings for text, integers for whole numbers, floats for decimal numbers, and booleans for true/false values.

Data TypeDescriptionExample Code
StringA sequence of characters (text)name = "Alice"
IntegerA whole numberage = 30
FloatA number with a decimal pointprice = 19.99
BooleanRepresents True or Falseis_active = True

Python's Golden Rule

Many programming languages use curly braces {} or keywords to define blocks of code. Python does things differently. It uses indentation—the spaces at the beginning of a line.

This is a strict rule. If a line of code has incorrect indentation, your program will not run. For everything we've covered so far, this means every line of code should start at the very beginning of the line, with no leading spaces or tabs. As we get into more complex structures, you'll see how indentation creates a clean, readable layout.

Talking to Your Program

Besides displaying information with print(), we can also get information from the user. We do this with the input() function. It pauses the program, waits for the user to type something and press Enter, and then gives us back the text they entered.

# The text inside input() is the prompt shown to the user.
user_name = input("What is your name? ")

# We can combine strings and variables in the print() function.
print("Hello, " + user_name + "!")

When you run this code, it will first ask for your name. If you type "Bob" and press Enter, the program will store "Bob" in the user_name variable and then print Hello, Bob!. Notice that the input() function always gives you the user's entry as a string, even if they type a number.

Time to check your understanding.

Quiz Questions 1/5

What is the primary role of a variable in Python?

Quiz Questions 2/5

Which line of code correctly prompts the user for their name and stores it in a variable called user_name?

You've just taken your first steps into Python, learning how to display text, store information in variables, and interact with a user. These are the fundamental building blocks for everything that comes next.