Python Programming Fundamentals
Python Basics
Setting Up Your Workspace
Before you can start writing Python code, you need two basic things: the Python interpreter and a place to write your code. Think of the interpreter as a translator. You write instructions in Python, and the interpreter translates them into a language your computer can understand and execute. When you install Python from its official website, you get this interpreter automatically.
You also get a simple program called IDLE (Integrated Development and Learning Environment). IDLE is a basic text editor and a console rolled into one, allowing you to write, save, and run your Python code. It’s a great starting point for beginners.
The Rules of the Language
Every language has grammar rules, and programming languages are no different. These rules are called syntax. Python is known for having a clean and readable syntax that's easy to pick up.
One of the most important and unique rules in Python is indentation. In many other programming languages, programmers use braces {} or keywords to group lines of code together. Python uses whitespace. Lines of code that are part of the same block must be indented by the same amount. This might seem strange at first, but it forces code to be neat and easy to read. You don't need to worry about creating indented blocks just yet, but it's a core concept to remember.
In Python, indentation isn't just for style, it's a strict rule. Incorrect indentation will cause your code to fail.
Storing Information
Programs need to store and manage information. We do this using variables. A variable is like a labeled box where you can keep 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 else.
To create a variable in Python, you just choose a name, use the equals sign (=), and provide a value.
message = "Hello, world!"
user_age = 25
price = 19.99
Here, message, user_age, and price are variable names. The data they hold belongs to different data types.
| Data Type | Description | Example |
|---|---|---|
| String | A sequence of characters, like text. | "Hello, world!" |
| Integer | A whole number, without a decimal point. | 25 |
| Float | A number with a decimal point. | 19.99 |
| Boolean | Represents one of two values: True or False. | True |
Python automatically figures out the data type when you assign a value to a variable. This makes the language flexible and easy to use.
Making Things Happen
Once you have variables, you can perform operations on them using operators. These are special symbols that perform computations, like the math symbols you already know.
An expression is a combination of values, variables, and operators that Python evaluates to produce a new value.
Arithmetic operators are the most common. They let you perform mathematical calculations.
# Addition
sum = 10 + 5 # sum is 15
# Subtraction
savings = 100 - 20 # savings is 80
# Multiplication
total_cost = 4 * 12.50 # total_cost is 50.0
# Division
per_person_share = 60 / 3 # per_person_share is 20.0
# Modulo (remainder of a division)
remainder = 10 % 3 # remainder is 1
We can also use the + operator to combine, or concatenate, strings.
first_name = "Ada"
last_name = "Lovelace"
full_name = first_name + " " + last_name
# full_name is "Ada Lovelace"
Finally, let's see how to communicate with the user. You can display information to the screen using the print() function and get information from the user with the input() function.
# Displaying a message
print("Welcome to the program!")
# Getting user input
user_name = input("What is your name? ")
# Using the input in our output
print("Hello, " + user_name + "!")
When you run this code, it will first print the welcome message, then pause and wait for you to type your name and press Enter. Whatever you type is stored in the user_name variable, which is then used to print a personalized greeting.
Now let's test your understanding of these fundamental concepts.
What is the primary role of the Python interpreter?
In Python, how are blocks of code grouped together?
