Python Programming Fundamentals
Python Basics
Setting Up Your Workspace
Before you can write Python code, you need two things: the Python interpreter and a place to write your code. The interpreter is what reads your code and runs it. You can download it for free from the official Python website, python.org.
While you can write code in any plain text editor, most developers use an Integrated Development Environment, or IDE. An IDE is a program that combines a text editor with other helpful tools, like a way to run your code easily and spot errors. Python's installer comes with a simple IDE called IDLE, which is great for starting out.
Once you have Python installed, you're ready to write your first program. It's a tradition in programming to start by making the computer say "Hello, World!".
# This is a comment. Python ignores lines that start with #.
print("Hello, World!")
This single line of code uses the print() function to display text on the screen. Whatever you put inside the parentheses and quotes will be printed. Go ahead and run it in your IDE. You should see Hello, World! appear in an output window.
Syntax and Indentation
Every language has rules for grammar and punctuation, and programming languages are no different. These rules are called syntax. Python is known for having a clean, readable syntax. One of its most important rules is about indentation, which refers to the spaces at the beginning of a line of code.
In Python, indentation isn't just for looks; it's a rule. It's how Python groups blocks of code together. You'll see why this is important later, but for now, just remember to start every new line of code without any leading spaces.
Variables and Data Types
Programs need to store and work with information, like a username, a score in a game, or the price of an item. 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 you can change what's inside it later.
# Create a variable named 'player_name' and store the text "Alex" in it.
player_name = "Alex"
# Print the value that is stored in the variable.
print(player_name)
The information we store comes in different types. Python has several built-in data types, but let's focus on four basic ones.
| Data Type | Description | Example |
|---|---|---|
| String | A sequence of characters (text) | "Hello", 'Python is fun' |
| Integer | A whole number | 10, -5, 0 |
| Float | A number with a decimal point | 3.14, -0.5, 2.0 |
| Boolean | Represents true or false | True, False |
Python is smart enough to figure out the data type on its own when you create a variable. If you put text in quotes, it knows it's a string. If you use a whole number, it knows it's an integer.
Working with Data
Now that you can store data, let's do something with it. You can perform basic arithmetic just like you would on a calculator.
apples = 5
oranges = 3
total_fruit = apples + oranges
print(total_fruit) # This will print 8
Here's a quick look at the common arithmetic operators in Python:
| Operator | Name | Example |
|---|---|---|
| + | Addition | 5 + 2 |
| - | Subtraction | 5 - 2 |
| * | Multiplication | 5 * 2 |
| / | Division | 5 / 2 |
| ** | Exponent | 5 ** 2 |
Besides just printing things out, programs often need to get information from the user. We can do this with the input() function.
name = input("What is your name? ")
print("Hello, " + name + "!")
When you run this code, it will first display the question "What is your name? " and then wait for you to type something and press Enter. Whatever you type is stored in the name variable as a string. Then, the program combines "Hello, ", your name, and "!" into a single string and prints it.
Notice that we used the + operator to join strings together. This is called concatenation. It's a simple way to build new strings from existing ones.
What is the primary role of the Python interpreter?
Which line of code will correctly display the text Welcome! on the screen?
You've taken your first steps into Python. You've set up your environment, written a simple program, and learned how to work with basic data. This is the foundation for everything that comes next.
