No history yet

Python Basics

Setting Up Your Workspace

Before you can write Python code, you need a place to write and run it. This is your development environment. The first step is to install Python itself. You can download the latest version from the official website, python.org. The installation is straightforward, and the installer will typically handle all the necessary setup for you.

Once Python is installed, you'll have access to a program called IDLE (Integrated Development and Learning Environment). Think of IDLE as a basic text editor and a command window combined. It’s a great place to start because it's simple and comes bundled with Python, so you don't need to install anything else.

Lesson image

You'll write your code in the editor window and see the results in the shell window. For now, we'll work directly in the interactive shell, which lets you type commands and see the results immediately.

Your First Program

A long-standing tradition in programming is to make your first program display the text "Hello, World!". In Python, this is incredibly simple. You just need one line of code.

print("Hello, World!")

Here, print() is a built-in function that tells the computer to display something on the screen. Whatever you put inside the parentheses will be printed. Because we want to print text, we wrap it in double quotes ("") to tell Python that this is a string of characters.

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 keep a piece of data. You give the box a name and put something inside it.

message = "Welcome to Python!"

In this example, message is the name of our variable. The equals sign (=) is the assignment operator; it takes the value on the right and stores it in the variable on the left. Now, the variable message holds the text "Welcome to Python!". We can use the variable's name to access its value.

print(message)

This will output "Welcome to Python!" to the screen. Notice we don't use quotes around message when printing it, because we want the value inside the variable, not the word "message" itself.

Variables can hold different kinds of data. Python automatically detects the data type for you. Let's look at a few basic types.

string

noun

A sequence of characters, used for text. Strings are enclosed in single (' ') or double (" ") quotes.

book_title = "The Hitchhiker's Guide to the Galaxy"

integer

noun

A whole number, without any fractional or decimal parts.

year = 1979
quantity = 5

float

noun

A number that has a decimal point.

price = 19.95
pi_approx = 3.14159

boolean

noun

A data type that can only have one of two values: True or False.

is_in_stock = True
is_on_sale = False

Basic Input and Output

We've already seen how to output information using the print() function. But what if you want your program to be interactive and get information from the user? For that, we use the input() function.

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

When this code runs, it will display the message "Please enter your name: " and then wait for you to type something and press Enter. Whatever you type is then stored as a string in the user_name variable.

Now you can combine input and output to create a simple, interactive program.

user_name = input("Please enter your name: ")
print("Hello, " + user_name + "!")

Run the code above. If you type 'Maria' and press Enter, the program will respond with: Hello, Maria!

In the print() statement, we used the + operator to combine, or concatenate, the strings. It joins them together into one longer string before displaying the final result.

You now have the fundamental building blocks of Python: setting up your environment, displaying output, storing information in variables, and getting input from a user.