No history yet

Python Basics

Getting Started with Python

Python is a language that lets you give instructions to a computer. It's popular because its rules, or syntax, are designed to be clean and readable, almost like plain English. This makes it a great language for beginners.

To start writing Python, you need a couple of things. First is the Python interpreter. Think of it as a translator that converts your Python code into instructions the computer's hardware can understand and execute. Second, you need a place to write your code. While a simple text editor works, most developers use an Integrated Development Environment, or IDE. An IDE is a program that combines a text editor with helpful tools, like the interpreter itself and debugging aids, all in one place.

Lesson image

The Rules of the Road

Every language has grammar rules, and Python is no different. One of its most important and unique rules is about indentation. In Python, the empty space at the beginning of a line is not just for looks; it's part of the syntax. It's how you group lines of code together.

Unlike many other languages that use brackets or keywords to group code, Python uses whitespace. This enforces a clean, organized layout.

For now, just remember that lines of code that belong together should have the same level of indentation. Most Python programmers use four spaces for each level of indentation. We'll see why this is so important when we start looking at more complex code structures later on.

Storing Information

Programming is all about working with information, or data. To work with data, we first need a way to store it. In Python, we use variables. You can think of a variable as a labeled box where you can put a piece of information. You give the box a name, and then you can refer to the information inside by using that name.

player_score = 100
player_name = "Alex"

In this example, player_score is a variable holding the number 100, and player_name is a variable holding the name "Alex". The equals sign = is the assignment operator. It tells the computer to put the value on the right into the variable on the left.

Data comes in different types. Python automatically figures out the type of data you store in a variable. Here are a few fundamental types:

Data TypeDescriptionExample
intInteger, or a whole number.42
floatFloating-point number, which has a decimal.3.14
strString, which is a sequence of characters (text)."Hello!"
boolBoolean, which can only be True or False.True

A Simple Conversation

Now that we know how to store data, let's learn how to interact with our program. Python makes it easy to display information and ask the user for input.

To display output on the screen, we use the print() function. Anything you put inside the parentheses will be shown to the user.

print("Welcome to the game!")
player_name = "Alex"
print(player_name)

To get input from the user, we use the input() function. This function will pause your program and wait for the user to type something and press Enter. Whatever the user types is then returned as a string, which you can store in a variable.

# Ask the user for their name
user_name = input("What is your name? ")

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

Try running that code. It asks for your name and then uses it to say hello. You've just written your first interactive program!

Ready to check your understanding of these basic building blocks?

Quiz Questions 1/5

What is the primary function of the Python interpreter?

Quiz Questions 2/5

In Python, indentation is used to group lines of code together and is a strict part of the language's syntax.

Great job. You've taken your first steps into Python by setting up your environment, learning the basic rules, and writing a script that can store data and interact with a user.