No history yet

Python Basics

Your First Lines of Code

Before writing Python, you need two things: the Python interpreter and a place to write your code. The interpreter is what reads your code and runs it. A code editor or an Integrated Development Environment (IDE) is a program designed to help you write code. For now, any simple text editor will work, but IDEs offer helpful features you'll appreciate later.

Lesson image

Let's write a program. The most traditional first program is one that simply displays the message "Hello, World!". In Python, this is incredibly straightforward. We use a built-in function called print().

print("Hello, World!")

When the interpreter runs this line, it executes the print() function, which displays whatever is inside the parentheses on the screen. The text inside the quotes is called a string, which is just a sequence of characters.

The Importance of Indentation

Many programming languages use curly braces {} or keywords to define blocks of code. Python is different. It uses whitespace, specifically indentation, to structure code. Lines of code that are part of the same block must be indented at the same level.

For now, just remember that the indentation of your code is meaningful. You'll see why this is so important when we get to more complex structures. Sticking to the left margin is the default.

In Python, how you space your code matters. Consistent indentation is not just for looks, it's a rule the language enforces.

Variables and Data Types

Programs need to store and manage information. We do this using variables. Think of a variable as a labeled box where you can put a piece of data. You give the box a name, and you can change what's inside it later.

To create a variable, you choose a name and use the equals sign = to assign it a value.

# Assigning the string "Hello again!" to a variable named 'greeting'
greeting = "Hello again!"

# Now we can print the variable's content
print(greeting)

The data we store in variables comes in different forms, or data types. Python automatically detects the type of data you're using. Here are the essential ones to start:

Data TypeDescriptionExample
String (str)A sequence of text characters."Python is fun"
Integer (int)A whole number, without a decimal.42
Float (float)A number with a decimal point.3.14159
Boolean (bool)Represents one of two values: True or False.True

You can assign each of these to variables.

# A string for a name
student_name = "Alex"

# An integer for an age
age = 21

# A float for a grade point average
gpa = 3.8

# A boolean to check enrollment status
is_enrolled = True

Interacting with the User

Good programs often need to get information from the person using them. We've used print() to send information out. To get information in, we use the input() function. It pauses the program, waits for the user to type something and press Enter, and then returns whatever they typed as a string.

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

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

In that example, we prompt the user with a question. Their answer is stored in the user_name variable. Then, we use the + operator to combine, or concatenate, our strings to create a personalized greeting. It's important to know that input() always gives you back a string, even if the user types a number.

Leaving Notes with Comments

As your programs grow, you'll want to leave notes for yourself or other programmers to explain what your code does. These notes are called comments. The Python interpreter completely ignores them.

To write a comment, just start the line with a hash symbol (#). Everything after the # on that line is a comment.

# This is a comment. It doesn't do anything.

# Set the value for pi. This variable will be used in a calculation later.
pi = 3.14159

radius = 5 # You can also put comments at the end of a line.

# The next line calculates the area of a circle
area = pi * (radius ** 2)

print(area) # Display the final result

Writing clear, readable code is a skill in itself. Good variable names and comments make your code much easier to understand when you come back to it weeks or months later.

Quiz Questions 1/7

What is the primary role of the Python interpreter?

Quiz Questions 2/7

Which line of code correctly prints the exact message Hello, World! to the screen?

You've just taken your first steps with Python. You've learned how to display output, store information in variables, and write comments to keep your code clear.