Python Programming Fundamentals
Python Basics
Setting Up Your Workspace
Before you can write Python, you need a place for your code to run. This is handled by the Python interpreter, a program that reads and executes your code. Many computers, especially Macs and Linux machines, come with Python already installed. However, it's always a good idea to have the latest version. You can download it for free from the official Python website, python.org.
Once installed, you'll have access to a basic programming environment called IDLE (Integrated Development and Learning Environment). It's a simple tool that's perfect for beginners. It gives you a place to write your code and see the output immediately.
Python's Clean Syntax
Python is known for its readable and clean syntax. Unlike many other programming languages that use curly braces {} or semicolons ; to structure code, Python uses simple indentation. This means whitespace at the beginning of a line is not just for looks—it's part of the program's logic. This design choice forces you to write code that's easy to read for both you and others.
Let's write our first line of code. The classic "Hello, World!" program in Python is just one simple line.
print("Hello, World!")
That's it. The print() function simply displays whatever you put inside the parentheses on the screen.
Storing Information
Programs need to remember things. We use variables to store information so we can use it later. 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 back.
To create a variable in Python, you just choose a name, use the equals sign =, and provide the value you want to store. This is called assignment.
# Assigning a number to the variable 'age'
age = 30
# Assigning text to the variable 'name'
name = "Alex"
The data we store comes in different forms, or data types. For now, we'll focus on four fundamental types.
| Data Type | Description | Example |
|---|---|---|
| Integer | Whole numbers, positive or negative. | 42, -10 |
| Float | Numbers with a decimal point. | 3.14, -0.001 |
| String | A sequence of characters, like text. | "Hello", 'Python' |
| Boolean | Represents truth values, either true or false. | True, False |
Working with numbers is straightforward. Python can handle all the basic arithmetic you'd expect. You can add, subtract, multiply, and divide integers and floats.
# Addition
sum = 10 + 5 # result is 15
# Subtraction
difference = 20 - 8 # result is 12
# Multiplication
product = 7 * 6 # result is 42
# Division
quotient = 100 / 25 # result is 4.0
Notice that division always results in a float, even if the numbers divide evenly. This ensures that calculations are precise.
Now, let's make our programs interactive. We can display information using the print() function and get information from the user with the input() function.
# Ask the user for their name
user_name = input("What is your name? ")
# Print a greeting using the name they provided
print("Hello, " + user_name + "!")
When you run this code, it will pause and wait for you to type something and press Enter. The text you type is then stored in the user_name variable. It's important to know that the input() function always gives you back a string, even if the user types a number.
Time to check your understanding of these core concepts.
What is the primary way Python structures its code blocks, distinguishing it from languages that use curly braces {} or semicolons ;?
Which line of code correctly assigns the integer 25 to a variable named age?
With variables, data types, and basic operations, you have the fundamental building blocks for writing any Python program.
