No history yet

Python Basics

Setting Up Your Workspace

Before you can write Python code, you need two things: the Python interpreter and a place to write your code. The interpreter is what reads your Python files and carries out their instructions.

You can download Python for free from the official website, python.org. The installation is straightforward, just like any other application. Once installed, you have the power to run Python programs.

Next, you need a code editor. While you could use a simple text editor like Notepad, it's better to use one designed for programming. Editors like Visual Studio Code, Sublime Text, or PyCharm offer features like syntax highlighting, which colors your code to make it easier to read. For now, any plain text editor will do.

Let's write your first program. It's a tradition for a programmer's first program to simply display "Hello, World!" on the screen. Open your text editor, type the following line, and save the file as hello.py.

print("Hello, World!")

To run it, open your computer's terminal or command prompt, navigate to the directory where you saved the file, and type python hello.py. You should see the words "Hello, World!" printed back at you.

Lesson image

Python's Clean Look

One of the first things you'll notice about Python is its clean, readable syntax. It's designed to be uncluttered. Unlike many other programming languages that use curly braces {} or semicolons ; to define the structure of the code, Python uses something much simpler: indentation.

Indentation refers to the spaces at the beginning of a code line. Where in other languages the indentation is for readability only, in Python it is mandatory and has meaning.

Think of it like an outline for a paper. Main points are at the far left, and sub-points are indented underneath them. Python uses this same visual structure to understand how your code is organized. We'll see this in action later when we learn about loops and functions. For now, just remember: indentation matters. A lot.

Variables and Data

In programming, we need a way to store information. We do this using variables. A variable is like a labeled box where you can keep a piece of data. You can put data in the box, and you can see what's inside by using its label.

To create a variable in Python, you just pick a name, use the equals sign =, and give it a value.

message = "Welcome to Python!"
user_age = 25
pi = 3.14

Here, message, user_age, and pi are variable names. The data they hold comes in different types. Python is smart and figures out the data type automatically.

Data TypeDescriptionExample
StringA sequence of characters. Used for text."Hello there" or 'Python'
IntegerA whole number, without a fractional part.10, -5, 2023
FloatA number with a decimal point.98.6, 3.14159, -0.5
BooleanRepresents one of two values: True or False.True or False

You can change the value of a variable at any time.

# Start with a score of 0
score = 0

# The player scores 10 points
score = 10

# The player scores another 5 points
score = score + 5

After this code runs, the score variable will hold the value 15.

Talking to the User

Programs aren't very useful if they can't communicate. We've already seen how to display information using the print() function. This is our primary tool for output.

username = "Alex"
level = 7

print("Username:", username)
print("Current level:", level)

To get information from the user, we use the input() function. It shows the user a message, waits for them to type something, and then returns whatever they typed as a string.

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

# Greet the user
print("Hello,", user_name)

If you run this code, it will pause after displaying "What is your name? ". After you type your name and press Enter, it will greet you personally. This simple combination of input and output is the foundation of any interactive program.

Now let's review what we've covered.