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 called your development environment. The first step is to install Python itself from the official website, python.org. The installation is straightforward and includes a program called IDLE (Integrated Development and Learning Environment), which is a simple editor you can use to get started right away.

Lesson image

Once you open IDLE, you'll see a window called the Python Shell. You can type commands directly into it and see immediate results. For writing longer programs, you'll want to open a new file (File > New File), which gives you a blank text editor.

Python's Simple Rules

Python is known for its clean and readable syntax. Unlike many other programming languages that use curly braces {} to define blocks of code, Python uses indentation. This means the amount of whitespace at the beginning of a line is very important.

This might seem strange at first, but it forces you to write clean, organized code. The standard is to use four spaces for each level of indentation. Most code editors can be set up to do this automatically when you press the Tab key.

In Python, indentation isn't just for style. It's a rule the language enforces.

Storing Information

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 you can put things in it, take them out, or replace them with something new.

message = "Hello, world!"
count = 10
pi = 3.14
is_active = True

Each piece of data has a type. Python automatically figures out the data type when you create a variable. The most common types are:

  • Strings (str): Text, enclosed in single or double quotes.
  • Integers (int): Whole numbers, like -5, 0, or 100.
  • Floats (float): Numbers with a decimal point, like 2.5 or -0.01.
  • Booleans (bool): Represents one of two values: True or False.

You can check the type of any variable using the type() function.

message = "Hello, world!"
print(type(message))  # Output: <class 'str'>

count = 10
print(type(count))    # Output: <class 'int'>

Interacting with Your Program

A program isn't very useful if it can't communicate. We've already seen the print() function, which is the standard way to display output to the user.

To get input from a user, you can use the input() function. It prompts the user to type something and then returns whatever they entered as a string.

Remember, input() always gives you a string, even if the user types a number. You'll often need to convert it.

For example, if you ask for a user's age, you'll get a string like "25". To treat it as a number, you need to convert it using int().

name = input("What is your name? ")
age_str = input("How old are you? ")

age = int(age_str) # Convert the string to an integer

print(f"Hello, {name}! You will be {age + 1} next year.")

Working with Operators

Operators are special symbols that perform computations. Python has several types, but let's start with the most common ones: arithmetic operators.

OperatorNameExampleResult
+Addition5 + 38
-Subtraction5 - 32
*Multiplication5 * 315
/Division5 / 31.666...
//Floor Division5 // 31
%Modulus5 % 32
**Exponentiation5 ** 3125

Floor division always rounds the result down to the nearest whole number. The modulus operator gives you the remainder of a division. These are incredibly useful for solving certain types of problems.

# Calculate the area of a circle
radius = 5
pi = 3.14159
area = pi * (radius ** 2)
print(area)

# Check if a number is even or odd
number = 10
remainder = number % 2 
print(remainder) # Output is 0, so the number is even

Now let's check your understanding of these fundamental concepts.

Quiz Questions 1/5

In Python, what is the primary purpose of indentation?

Quiz Questions 2/5

What is the data type of the value -3 in Python?

You've just taken your first steps into Python. By understanding these basics, you've built the foundation for everything that comes next.