Introduction to Python Programming
Python Basics
Setting Up Your Workspace
Before you can start writing Python, you need to set it up on your computer. This involves two main parts: the Python interpreter and a place to write your code.
The interpreter is a program that reads your Python code and translates it into instructions your computer can understand and execute. Without it, your code is just a text file.
You can download the official Python interpreter from the Python website. The installation is straightforward, much like installing any other application. During installation, make sure to check the box that says "Add Python to PATH." This lets you run Python from your computer's command line, a text-based interface for interacting with your system.
Once installed, you'll have access to IDLE (Integrated Development and Learning Environment). It's a simple tool that comes bundled with Python, giving you a place to write and run your code immediately. Think of it as a basic text editor that also understands and can run Python.
The Rules of the Language
Every programming language has its own set of rules, called syntax. Python is known for having a clean and readable syntax that's easy for beginners to pick up.
One of Python's most distinctive features is its use of indentation. While other languages might use brackets or keywords to group code, Python uses whitespace. How far you indent a line of code determines its role in the program's structure. This might seem strange at first, but it forces you to write clean, organized code.
You also need a way to leave notes in your code for yourself or other programmers. These notes are called comments. The Python interpreter ignores them, so they don't affect how the program runs. A comment starts with a hash symbol (#).
# This is a comment. The interpreter will ignore it.
# Use comments to explain what your code does.
# The line below will be executed.
print("Hello, Python!") # This is an inline comment.
Storing and Using Information
Programs need to work with information, like numbers and text. To keep track of this information, we use variables. A variable is like a labeled box where you can store a value. You give it a name, and then you can refer to that name later to get the value back.
variable
noun
A named storage location in a computer's memory that holds a value.
The information you store in variables comes in different forms, or data types. Python has several built-in data types, but let's start with the most common ones.
| Data Type | Description | Example |
|---|---|---|
| Integer | A whole number | 42 |
| Float | A number with a decimal point | 3.14159 |
| String | A sequence of text characters | "Hello, world!" |
| Boolean | Represents True or False | True |
Here's how you create variables and assign values of these types:
# An integer variable
user_age = 25
# A float variable
pi_approx = 3.14
# A string variable
user_name = "Alex"
# A boolean variable
is_active = True
Notice that strings are enclosed in quotes (either single ' or double "). This is how Python knows you're working with text. Booleans are special; they can only be True or False (with a capital T and F) and are essential for making decisions in your code.
Interacting with Your Program
A program isn't very useful if it can't communicate. You'll need ways to display information to the user (output) and get information from the user (input).
To display output, Python gives us the print() function. Anything you put inside the parentheses will be shown on the screen.
print("Welcome to the program!")
user_name = "Casey"
print("Hello,", user_name) # You can print variables too!
To get input from a user, you can 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.
# Prompt the user for their name
name = input("What is your name? ")
# Greet the user with their name
print("Nice to meet you,", name)
An important detail:
input()always gives you back a string, even if the user types a number. If you need to treat the input as a number, you have to convert it first.
age_string = input("How old are you? ")
age_number = int(age_string) # Convert the string to an integer
# Now you can do math with it
years_until_100 = 100 - age_number
print("You will be 100 in", years_until_100, "years.")
Working with Operators
Operators are special symbols that perform computations. Python has different kinds of operators for different tasks.
Arithmetic operators are for doing math. You've seen them your whole life: addition (+), subtraction (-), multiplication (*), and division (/). Python has a few others that are also very useful.
| Operator | Name | Example | Result |
|---|---|---|---|
+ | Addition | 5 + 2 | 7 |
- | Subtraction | 5 - 2 | 3 |
* | Multiplication | 5 * 2 | 10 |
/ | Division | 5 / 2 | 2.5 |
// | Floor Division | 5 // 2 | 2 |
% | Modulus | 5 % 2 | 1 |
** | Exponentiation | 5 ** 2 | 25 |
Floor division (//) divides and then rounds down to the nearest whole number. The modulus operator (%) gives you the remainder of a division. It's surprisingly handy, especially for checking if a number is even or odd.
Logical operators are used to combine boolean (True or False) values. They are the foundation of decision-making in programs. The three main logical operators are and, or, and not.
| Operator | Result |
|---|---|
and | True if both sides are true |
or | True if at least one side is true |
not | Inverts the value ( not True becomes False) |
is_sunny = True
is_warm = False
# Is it a good day for the beach?
beach_day = is_sunny and is_warm # False
# Is it an okay day for a walk?
walk_day = is_sunny or is_warm # True
# Is it not sunny?
not_sunny = not is_sunny # False
These are the building blocks. With variables to store data, I/O to interact with users, and operators to process information, you have the tools to write your first simple programs.
During the Python installation process, why is it recommended to check the box that says "Add Python to PATH"?
In Python, what is used to define the structure and grouping of code blocks?

