Python Programming Fundamentals
Python Basics
Getting Started with Python
Before you can start programming, you need to have Python installed on your computer. The best place to get it is from the official website, python.org. The installation is straightforward, just like any other software.
Once Python is installed, you need a place to write your code. While you can use a simple text editor, most programmers use an Integrated Development Environment, or IDE. An IDE is a special text editor with helpful features like code completion and debugging tools. Popular choices include Visual Studio Code, PyCharm, and Thonny. There are also great online options like Replit that let you start coding right in your browser without any installation.
The Rules of the Road
Every programming language has its own set of rules, called syntax. Python is famous for its clean and readable syntax, which often looks a lot like plain English. One of the most important and unique rules in Python is how it handles code structure: with indentation.
Unlike many languages that use curly braces {} to group lines of code, Python uses whitespace. This means the number of spaces at the beginning of a line is crucial. Consistent indentation isn't just for style; it's a strict rule that Python's interpreter uses to understand your code. This might feel strange at first, but it forces you to write clean, organized code from day one.
In Python, indentation is not optional. It's syntax.
Storing and Using Information
To do anything useful, programs need to work with information, like numbers and text. We store this information in variables. Think of a variable as a labeled box where you can put a piece of data. You give the box a name, and then you can refer to that name to get the data inside.
# Assigning the value 'Alice' to the variable 'name'
name = "Alice"
# Assigning the number 42 to the variable 'user_age'
user_age = 42
The type of data you store determines what you can do with it. Python has several basic data types you'll use all the time.
| Data Type | Description | Example |
|---|---|---|
| Integer | A whole number, positive or negative. | 10, -5, 0 |
| Float | A number with a decimal point. | 3.14, -0.001 |
| String | A sequence of characters, like text. | "Hello, world!", 'Python' |
| Boolean | Represents one of two values: True or False. | True, False |
Notice that strings are wrapped in either double quotes (") or single quotes ('). It doesn't matter which you use, as long as you're consistent. Booleans are essential for logic and decision-making, which we'll explore later.
Interacting with Your Program
A program isn't very useful if it can't communicate. Python gives us simple tools for basic input and output.
To display information to the user, we use the print() function. Whatever you put inside the parentheses will be shown on the screen.
greeting = "Welcome to Python!"
print(greeting)
# You can also print values directly
print(123)
print(True)
To get information from the user, we use the input() function. This function pauses your program, displays a prompt message, and waits for the user to type something and press Enter.
# The prompt message is optional, but helpful
user_name = input("What is your name? ")
print("Hello, " + user_name + "!")
An important note: the
input()function always gives you back a string, even if the user types in a number. If you need to treat the input as a number, you'll have to convert it first.
Doing the Math
Python is a powerful calculator. You can perform arithmetic operations just like you would expect, using standard mathematical symbols. These operations work with integers and floats.
| Operator | Name | Example | Result |
|---|---|---|---|
+ | Addition | 5 + 3 | 8 |
- | Subtraction | 5 - 3 | 2 |
* | Multiplication | 5 * 3 | 15 |
/ | Division | 15 / 3 | 5.0 |
** | Exponent | 5 ** 2 | 25 |
One thing to watch for is that standard division (/) always results in a float, even if the result is a whole number (like 10 / 2 gives 5.0). You can also combine these operations and use parentheses () to control the order of operations, just like in algebra.
cost = 19.99
tax_rate = 0.07
tax_amount = cost * tax_rate
total_cost = cost + tax_amount
print(total_cost)
# Output will be 21.3893
Let's review what we've covered before you start writing your own code.
In Python, what is the primary role of indentation?
Which of the following best describes the purpose of a variable?
