No history yet

Python Basics

Setting Up Your Workspace

Before you can write Python code, you need two things: the Python interpreter itself and a place to write your code. The interpreter is what reads your code and runs it. You can download it for free from the official Python website, python.org.

While you can write code in a simple text editor, most developers use an Integrated Development Environment, or IDE. An IDE is like a supercharged text editor with helpful features like code completion, debugging tools, and syntax highlighting. It makes writing code faster and easier. Popular choices for beginners include Visual Studio Code, PyCharm Community Edition, and Thonny. For this article, we'll assume you have one installed.

Lesson image

Your First Program

Let's start with a classic tradition in programming: making the computer say hello. In Python, this is incredibly straightforward. You use a built-in command, or function, called print() to display text on the screen. Whatever you want to print goes inside the parentheses. If you're printing text, which programmers call a string, you need to wrap it in quotes.

# This line of code will display the text 'Hello, World!' on the screen.
print("Hello, World!")

That's it. This one line is a complete Python program. The text starting with a # is a comment. The interpreter ignores it, but it's useful for leaving notes for yourself or other programmers.

Building Blocks: Variables and Data

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

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

# Create a variable named 'greeting' and store the text "Hi there" in it.
greeting = "Hi there"

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

The data we store in variables comes in different types. Python is smart and usually figures out the type on its own, but it's important to know the basic categories.

Data TypeDescriptionExample
StringText"Hello"
IntegerWhole numbers42
FloatNumbers with a decimal point3.14
BooleanRepresents True or FalseTrue

Making Decisions and Repeating Actions

So far, our code runs straight from top to bottom. The real power of programming comes from controlling this flow. We can tell our program to make decisions or repeat actions using control structures.

To make decisions, we use if statements. An if statement checks if a condition is true. If it is, it runs a block of code. You can also provide an else block to run if the condition is false.

temperature = 75

if temperature > 80:
    print("It's a hot day!")
elif temperature < 60:
    print("You might need a jacket.")
else:
    print("The weather is perfect.")

Notice the indentation. In Python, whitespace is important. The indented code

belongs to

the if, elif (short for 'else if'), or else statement above it.

To repeat actions, we use loops. A for loop is great for when you want to do something for each item in a list.

# This loop will run 3 times.
# Each time, the 'fruit' variable will hold the next item from the list.
for fruit in ["apple", "banana", "cherry"]:
    print("I love to eat " + fruit + "s!")

A while loop is used to repeat a block of code as long as a certain condition is true. It's like saying, "keep doing this until I tell you to stop."

# Start with a counter at 1
count = 1

# Keep looping as long as 'count' is less than or equal to 5
while count <= 5:
    print("The count is:", count)
    count = count + 1 # Don't forget to increase the counter!

Be careful with while loops. If the condition never becomes false, you'll create an infinite loop, and your program will never end.

Quiz Questions 1/6

What are the two essential components you need to start programming in Python?

Quiz Questions 2/6

Which line of code will correctly print the text Hello, World! to the screen?

These are the fundamental pieces of Python. With variables to hold data and control structures to manage the program's flow, you can start building simple yet powerful scripts.