Python Programming Fundamentals
Python Basics
Setting Up Your Workspace
Before you can start writing Python, you need a place to write and run your code. This is called your development environment. The first step is to install the Python interpreter, which is the program that understands and executes your Python code. You can download it for free from the official Python website.
Once Python is installed, you can write code in a simple text editor, but most developers use an Integrated Development Environment (IDE). An IDE is like a supercharged text editor with helpful features like code completion, debugging tools, and syntax highlighting, which colors your code to make it easier to read.
The Rules of the Language
Every language has rules for grammar and structure, and programming languages are no different. Python is famous for its clean and readable syntax. The rules are designed to be straightforward, which helps you write code that is easy to understand, both for you and for others.
Unlike many other programming languages that use brackets or keywords to define blocks of code, Python uses indentation.
This isn't just for style, it's a strict rule. How you indent your lines of code determines how they are grouped and executed. A misplaced space can change the meaning of your program or cause an error. The standard is to use four spaces for each level of indentation.
Storing Information
To do anything useful, programs need to store and work with 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 replace them with something new.
In Python, you create a variable and give it a value using an assignment operator, which is just the equals sign (=).
greeting = "Hello, world!"
user_age = 25
price = 19.99
The name of the variable is on the left, and the value you want to store is on the right. Now, whenever you use the variable user_age, Python will substitute in the value 25.
Of course, we work with different kinds of information. The price of an item isn't the same as someone's name. Python classifies values into different data types.
| Data Type | Description | Example |
|---|---|---|
Integer (int) | Whole numbers, positive or negative. | 42, -100 |
Float (float) | Numbers with a decimal point. | 3.14, -0.01 |
String (str) | A sequence of characters, like text. | "Hello", 'Python' |
Boolean (bool) | Represents one of two values: True or False. | True, False |
For strings, you can use either single quotes (') or double quotes ("). Just be consistent!
Making Things Happen
Variables and data types are the nouns of programming. To get things done, we need verbs. These are called operators. Operators are special symbols that perform operations on values and variables.
Arithmetic operators work just like they do in math. You can add, subtract, multiply, and divide numbers.
sum = 5 + 3 # Result is 8
difference = 10 - 4 # Result is 6
product = 7 * 6 # Result is 42
quotient = 8 / 2 # Result is 4.0
remainder = 10 % 3 # Result is 1 (10 divided by 3 is 3 with a remainder of 1)
Comparison operators are used to compare two values. The result of a comparison is always a Boolean value: either True or False.
5 == 5 # True (equal to)
5 != 6 # True (not equal to)
10 > 2 # True (greater than)
4 < 8 # True (less than)
7 >= 7 # True (greater than or equal to)
Finally, logical operators (and, or, not) are used to combine Boolean values. They help you make more complex decisions in your code.
# Both conditions must be true
(10 > 5) and (3 < 4) # True
# At least one condition must be true
(10 > 5) or (3 > 4) # True
# Reverses the boolean value
not (5 > 10) # True (because 5 > 10 is False)
That covers the absolute fundamentals. Let's see if you've got them down.
What is the primary role of the Python interpreter?
In Python, indentation is a strict syntax rule used to define blocks of code.
With these building blocks, you're ready to start constructing more complex programs.
