Python Programming Fundamentals
Python Basics
Setting Up Your Workspace
Before you can start writing Python code, you need to set up your environment. This just means installing the Python interpreter, which is the program that understands and runs your code. You can download it for free from the official Python website, python.org. The installation is straightforward for Windows, macOS, and Linux.
Once Python is installed, you can write code in a simple text editor, but most programmers use an Integrated Development Environment, or IDE. An IDE is like a specialized word processor for code, with features that help you write, run, and debug your programs more easily. Some popular choices for beginners are VS Code, PyCharm, and Thonny.
Let's start with the traditional first program. In your editor, type the following line and run it:
print("Hello, World!")
Congratulations! You've just written and executed your first Python program. The print() command simply tells the computer to display whatever is inside the parentheses on the screen.
Syntax and Spacing
One of the first things you'll notice about Python is its clean and readable style. Unlike many other programming languages that use curly braces {} to define blocks of code, Python uses indentation. This means the amount of space at the beginning of a line is very important.
Consistent indentation is not just for style in Python, it's a rule. It tells the interpreter how your code is structured.
The standard is to use four spaces for each level of indentation. Most code editors can be configured to insert four spaces automatically when you press the Tab key. While you won't see complex code blocks just yet, it's a fundamental concept to grasp from the start.
Storing Information
In programming, we need a way to store and manage data. We do this using variables. Think of a variable as a labeled box where you can put information. You can give a variable a name and assign it a value using the equals sign =.
# The variable 'name' now holds the text "Alice"
name = "Alice"
# The variable 'age' holds the number 30
age = 30
The information you store comes in different forms, or data types. Python automatically detects the type of data you assign to a variable. The most common types are:
- Integers (
int): Whole numbers, like 10, -5, or 0.- Floats (
float): Numbers with a decimal point, like 3.14 or -0.5.- Strings (
str): Text, enclosed in single'or double"quotes.- Booleans (
bool): Represents one of two values:TrueorFalse.
# An integer
item_count = 5
# A float
price = 19.99
# A string
greeting = "Hello there"
# A boolean
is_in_stock = True
Performing Operations
Once you have variables, you can perform operations on them using operators. These are special symbols that carry out computations. The most familiar are the arithmetic operators.
| Operator | Name | Example | Result |
|---|---|---|---|
+ | Addition | 5 + 3 | 8 |
- | Subtraction | 5 - 3 | 2 |
* | Multiplication | 5 * 3 | 15 |
/ | Division | 10 / 4 | 2.5 |
% | Modulus | 10 % 3 | 1 |
** | Exponent | 2 ** 3 | 8 |
// | Floor Division | 10 // 4 | 2 |
The modulus operator (%) gives you the remainder of a division, and floor division (//) gives you the result of a division, rounded down to the nearest whole number. You can also use comparison operators to compare values, which always results in a Boolean (True or False) value.
| Operator | Description | Example |
|---|---|---|
== | Equal to | x == y |
!= | Not equal to | x != y |
> | Greater than | x > y |
< | Less than | x < y |
>= | Greater than or equal to | x >= y |
<= | Less than or equal to | x <= y |
Interacting with the User
Programs are much more interesting when they're interactive. Python provides simple functions to handle input and output.
We've already seen the
print()function, which displays information to the user. You can print text, numbers, or the values stored in variables.
planet = "Earth"
moons = 1
print("We are on planet", planet)
print("It has", moons, "moon.")
To get information from the user, you can use the input() function. It prompts the user to type something and then returns their input as a string.
# The text in parentheses is the prompt shown to the user
user_name = input("What is your name? ")
print("Hello,", user_name)
One important detail: the input() function always gives you back a string. If you need to treat the input as a number, you have to convert it using int() or float().
# Get age as a string from the user
age_string = input("How old are you? ")
# Convert the string to an integer
age_number = int(age_string)
# Now you can do math with it
years_until_100 = 100 - age_number
print("You will be 100 in", years_until_100, "years.")
Now that you have a handle on these building blocks, it's time to test your knowledge.
What is the primary purpose of an Integrated Development Environment (IDE) like VS Code or PyCharm?
In Python, curly braces {} are used to define blocks of code, such as the body of a loop or function.
These are the fundamental concepts that every Python program is built upon. With variables, operators, and a way to interact with the user, you're well on your way.
