No history yet

Python Basics

The Building Blocks of Code

Every language has rules. In English, we use grammar and punctuation to make our sentences understandable. Programming languages have similar rules, which we call syntax. Python's syntax is famous for being clean and readable, which makes it a great language for beginners.

One of the most fundamental rules is how to give the computer a command. Let's look at the classic first program everyone writes. It simply tells the computer to display the words "Hello, World!" on the screen.

# This is a comment. Python ignores lines starting with #.
# The print() function displays text to the screen.
print("Hello, World!")

That's it. The command is print(), and the text we want to display goes inside the parentheses, wrapped in quotes. The quotes tell Python that this is a piece of text, not another command.

Storing Information

Programs need to remember information while they're running. To do this, we use variables. Think of a variable as a labeled box where you can store a piece of data. You give the box a name, and you can put things in it, take them out, or change what's inside.

For example, if we're making a game, we might need to store the player's score. We can create a variable named score and assign it a value.

score = 100

Here, score is the variable's name, and 100 is the value we've stored in it. The equals sign (=) is the assignment operator; it assigns the value on the right to the variable on the left.

Information comes in different forms, or data types. Python automatically recognizes the type of data you store. Here are the most common ones:

Data TypeDescriptionExample
IntegerA whole numberplayer_level = 15
FloatA number with a decimal pointplayer_health = 95.5
StringText, wrapped in quotesplayer_name = "Gandalf"
BooleanRepresents True or Falseis_alive = True

This feature, where Python figures out the data type on its own, is called dynamic typing. It keeps the syntax simple because you don't have to manually declare what kind of data a variable will hold.

Talking to Your Program

A program isn't very useful if it can't communicate. We need ways to get information from the user (input) and display information to the user (output). We've already seen the primary output function: print().

You can print the value of a variable directly. Even better, you can combine text and variables into a single, clean message using something called an f-string. Just put an f before the opening quote and place your variable names inside curly braces {}.

player_name = "Aragorn"
player_level = 15

# Using an f-string to combine text and variables
print(f"Welcome, {player_name}! You are level {player_level}.")

To get input from a user, we use the input() function. This function will pause the program, display a message (called a prompt), and wait for the user to type something and press Enter.

# The text inside the parentheses is the prompt shown to the user.
user_name = input("Please enter your name: ")

print(f"Hello, {user_name}!")

One important detail: the input() function always returns the user's entry as a string. If you ask for a number, you'll need to convert it from a string to an integer or float before you can do math with it.

For example, to get a user's age and calculate their age next year, you need to convert the input to an integer using int().

age_input = input("How old are you? ")
age = int(age_input)  # Convert the string to an integer
age_next_year = age + 1

print(f"Next year, you will be {age_next_year}!")

With just these simple tools—syntax, variables, and input/output—you have the core components needed to start writing your own Python programs.