No history yet

Python Basics

Getting Started with Python

Before you can write any Python code, you need to install it on your computer. Python is a versatile programming language used for everything from web development to data science. The best place to get it is from the official Python website, python.org. The installation process is straightforward for Windows, Mac, and Linux.

Lesson image

Once installed, Python comes with a simple program called IDLE (Integrated Development and Learning Environment). Think of IDLE as a basic text editor and a place to run your Python code, all in one. It’s perfect for beginners. As you advance, you might switch to more powerful code editors like VS Code or PyCharm, but for now, IDLE has everything you need.

Your First Program

A time-honored tradition in programming is to make your first program display the text "Hello, World!". In Python, this is incredibly simple. You use a built-in command called print().

print("Hello, World!")

To run this, open IDLE. You can type that line directly into the interactive shell and press Enter. To create a script you can save and run again, go to File > New File. Type the code into this new window, save it with a .py extension (like hello.py), and then select Run > Run Module.

Lesson image

Understanding Variables

Imagine you need to store a piece of information that you'll use later, like a person's age or a greeting message. You can store it in a variable. A variable is like a labeled box where you can keep a value.

In Python, you create a variable by giving it a name and using the equals sign (=) to assign it a value.

# A variable named 'greeting' that stores text
greeting = "Hello there"

# A variable named 'user_age' that stores a number
user_age = 30

# You can then use the variables with the print() command
print(greeting)
print(user_age)

You can change the value of a variable at any time by assigning it something new. This flexibility is what makes variables so powerful.

Basic Data Types

Variables can hold different kinds of information. These different kinds are called data types. Python has several built-in types, but let's start with the four most common ones.

Data TypeDescriptionExample
IntegerWhole numbers, without decimals.42, -100
FloatNumbers that have a decimal point.3.14, -0.5
StringA sequence of characters, like text."Hello", 'Python'
BooleanRepresents one of two values: True or False.True, False

Python is smart enough to figure out the data type on its own when you assign a value to a variable. You don't need to tell it explicitly.

item_name = "Laptop"      # Python knows this is a string
quantity = 2            # This is an integer
price = 1299.99         # This is a float
in_stock = True         # This is a boolean

Syntax and Comments

Every language has grammar rules, and programming languages are no different. These rules are called syntax. Python is known for its clean and readable syntax. One of the most important rules is indentation. Unlike many other languages that use brackets or keywords to group code, Python uses whitespace. The amount of space at the beginning of a line is significant. We'll see why this is crucial when we get to more complex code structures.

In Python, indentation is not just for style, it's a rule. Incorrect indentation will cause your program to fail.

You can also leave notes in your code called comments. Your program will ignore them completely. In Python, a comment starts with a hash symbol (#) and continues to the end of the line. Comments are useful for explaining what your code does.

# This is a comment. It explains the next line.
# Calculate the area of a rectangle
length = 10
width = 5
area = length * width # The formula for area
print(area)

Getting Input from a User

So far, our programs have been static. They do the same thing every time. To make them interactive, we can ask the user for input. We do this with the input() command.

# The text inside input() is the prompt shown to the user
user_name = input("What is your name? ")

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

When you run this code, the program will pause and wait for you to type something and press Enter. Whatever you type is then stored in the user_name variable as a string.

One important thing to know: input() always gives you back a string. If you need to treat the input as a number, you have to convert it first. For example, user_age = int(input("How old are you? ")) converts the input string to an integer.

Quiz Questions 1/6

What is the official, recommended website for downloading the Python programming language?

Quiz Questions 2/6

Which line of Python code correctly prints the text "Hello, World!" to the screen?

You've just taken your first steps into the world of Python. You know how to set up your environment, write simple programs, store information in variables, and interact with users. These are the essential building blocks for everything that comes next.