Python Programming Fundamentals
Python Basics
Setting Up Your Workspace
Before you can start writing Python, 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.
Once Python is installed, you need a code editor. While you could use a simple text editor, most developers use an Integrated Development Environment (IDE). An IDE is like a supercharged text editor that understands your code, offers suggestions, and helps you spot errors. Python's installer comes with a simple IDE called IDLE, which is great for starting out. Other popular choices include Visual Studio Code, PyCharm, and Thonny.
Python's Golden Rule
Every programming language has rules, or syntax. Python’s most important rule is about indentation. Unlike many other languages that use curly braces {} to group code, Python uses whitespace. This means the spacing at the beginning of a line is crucial.
Think of it like an outline for a paper. Main points are flush left, sub-points are indented under them, and so on. This structure makes Python code clean and easy to read. You'll see how this works more as we get into more complex code, but for now, just remember: indentation matters.
Storing Information
To do anything useful, 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 put something inside it.
greeting = "Hello, world!"
user_age = 30
pi_value = 3.14159
is_learning = True
Here, greeting is a variable holding a piece of text, user_age holds a whole number, pi_value holds a number with a decimal, and is_learning holds a value that's either true or false. These different kinds of information are called data types.
| Data Type | Description | Example |
|---|---|---|
| String | Textual data | "Python" or 'Python' |
| Integer | Whole numbers | 10, -5, 0 |
| Float | Numbers with a decimal | 2.5, -0.01 |
| Boolean | True or False | True, False |
Communicating with Your Program
Now that you can store information, how do you see it or get new information from a user? Python gives us two simple tools for this: print() and input().
The
print()function displays information to the screen. You can give it text directly or tell it to print the value stored in a variable.
# Printing a string directly
print("Welcome to Python!")
# Printing a variable's value
language = "Python"
print(language)
The input() function does the opposite: it pauses the program and waits for the user to type something and press Enter. It then takes that text and lets you store it in a variable.
# Ask the user for their name
user_name = input("What is your name? ")
# Greet the user by name
print("Hello, " + user_name + "!")
Making Things Happen
Operators are the symbols that perform actions, like math or logic. You already know many of them.
Arithmetic operators perform calculations:
+(addition),-(subtraction),*(multiplication), and/(division).
apples = 5
oranges = 3
total_fruit = apples + oranges
print(total_fruit) # Output: 8
Comparison operators check how two values relate to each other. They always result in a Boolean value: True or False.
| Operator | Meaning | Example | Result |
|---|---|---|---|
== | Equal to | 5 == 5 | True |
!= | Not equal to | 5 != 3 | True |
> | Greater than | 5 > 3 | True |
< | Less than | 5 < 3 | False |
>= | Greater than or equal to | 5 >= 5 | True |
<= | Less than or equal to | 5 <= 3 | False |
Finally, logical operators (and, or, not) combine Boolean values to create more complex conditions.
# Is a number between 1 and 10?
num = 7
print(num > 0 and num < 10) # True, because both are true
# Is a number outside the range of 1 to 10?
num = 12
print(num < 1 or num > 10) # True, because one is true
These are the fundamental building blocks. With variables, I/O, and operators, you're ready to start writing simple but powerful programs.
