No history yet

Python Basics

Getting Started with Python

Before you can write Python code, you need a Python interpreter. This is the program that reads your code and runs it. The best place to get it is from the official website, python.org. The installation is straightforward and includes a simple programming environment called IDLE.

Lesson image

IDLE gives you two main things: a shell for running single lines of code and an editor for writing longer scripts. For now, we'll focus on writing simple commands that you can type directly into the shell.

Writing Your First Code

Let's start with a classic tradition in programming: making the computer say hello. In Python, you use the print() function to display text or other values to the screen.

print("Hello, World!")

When you run this line, the text Hello, World! appears. You've just given the computer an instruction, and it followed it. The text inside the quotation marks is called a string, which is just a sequence of characters.

One of the most important rules in Python is indentation. Unlike many languages that use brackets or keywords to group code, Python uses whitespace. How far a line is indented determines its role in the program's structure. Getting this right is crucial, even for simple programs.

In Python, indentation isn't just for style—it's the syntax. Incorrect indentation will cause your code to break.

Storing and Using Data

Programs need to work with information, like numbers, text, or other kinds of data. To keep track of this information, we use variables. Think of a variable as a labeled box where you can store a value. You give the box a name and put something inside it.

# Assign the text "Ada Lovelace" to the variable named 'pioneer'
pioneer = "Ada Lovelace"

# Print the value stored in the variable
print(pioneer)

Here, we created a variable named pioneer and stored the string "Ada Lovelace" in it. We can then use the variable's name to access its value.

Python has several fundamental data types to handle different kinds of information:

Data TypeDescriptionExample
intInteger, for whole numbers.42
floatFloating-point number, for numbers with decimals.3.14159
strString, for text."Hello, Python!"
boolBoolean, for logical values True or False.True

Python automatically figures out the data type when you assign a value to a variable. You don't have to declare it beforehand.

Basic Operations

Once you have data, you can perform operations on it. Python supports standard arithmetic operators you'd expect from a calculator.

a = 15
b = 4

print(a + b)  # Addition: 19
print(a - b)  # Subtraction: 11
print(a * b)  # Multiplication: 60
print(a / b)  # Division: 3.75

Python also has a few other useful arithmetic operators.

OperatorNameExampleResult
//Floor Division15 // 43
%Modulo (Remainder)15 % 43
**Exponentiation2 ** 38

Floor division rounds the result down to the nearest whole number. The modulo operator gives you the remainder of a division. These are surprisingly handy for solving a wide range of problems.

Interacting with Users

So far, our programs have been self-contained. To make them interactive, we need a way to get input from a user. The input() function does exactly that. It pauses the program, waits for the user to type something and press Enter, and then returns whatever they typed as a string.

name = input("What is your name? ")
print("Hello, " + name + "!")

The text inside the parentheses of input() is a prompt that gets displayed to the user. Notice how we used the + operator to combine, or concatenate, several strings into one before printing it.

A crucial point: input() always returns a string. If you need to treat the input as a number, you have to convert it first.

age_string = input("How old are you? ")
age_number = int(age_string)

# Now you can do math with the age
years_until_100 = 100 - age_number
print("You will be 100 in", years_until_100, "years.")

We use the int() function to convert the string from input() into an integer. If the user enters something that isn't a whole number, the program will crash. There are ways to handle that, but for now, this simple conversion works for getting started.

With these building blocks—printing output, storing data in variables, performing operations, and getting user input—you can start writing simple but useful programs.

Ready to check your understanding? Let's work through a few questions.

Quiz Questions 1/5

Which line of Python code will correctly display the message "Hello, Python!" on the screen?

Quiz Questions 2/5

In Python, whitespace indentation is used to group blocks of code, rather than brackets or keywords.

You've now taken your first steps into the world of Python. These fundamentals are the foundation for everything that comes next.