In my first Python session, I learned the basics: variables, data types, input(), print(), and basic arithmetic.
But programs don't just calculate things. They also need to make decisions.
For example:
- Is the user old enough?
- Is the number even or odd?
- Did the student pass?
- Should a discount be applied?
This is where operators and conditionals come in.
Arithmetic Operators
Arithmetic operators are used to perform calculations.
| Operator | Meaning | Example |
|---|---|---|
+ |
Addition | 5 + 3 |
- |
Subtraction | 5 - 3 |
* |
Multiplication | 5 * 3 |
/ |
Division | 5 / 3 |
// |
Floor division | 5 // 3 |
% |
Modulo (remainder) | 5 % 3 |
** |
Exponent | 5 ** 3 |
For example, a simple shopping calculation:
price = 500
quantity = 3
total = price * quantity
print(total)
The output is:
1500
The Modulo Operator %
One operator I found particularly useful is %.
It gives us the remainder after division.
print(10 % 3)
Output:
1
This becomes useful when checking whether a number is even or odd:
number = 10
print(number % 2)
If the result is 0, the number is even.
Comparison Operators
Comparison operators allow Python to compare values.
They return either True or False.
| Operator | Meaning |
|---|---|
== |
Equal to |
!= |
Not equal to |
> |
Greater than |
< |
Less than |
>= |
Greater than or equal to |
<= |
Less than or equal to |
Example:
age = 25
print(age == 25)
print(age > 18)
print(age < 18)
Output:
True
True
False
= vs ==
This is important for beginners.
= is used to assign a value:
age = 25
== is used to compare values:
age == 25
Think of it this way:
=means "store this."
==means "are these equal?"
Logical Operators
Logical operators allow us to combine conditions.
There are three main ones:
and
Both conditions must be True.
age = 25
has_id = True
print(age >= 18 and has_id)
Result:
True
or
At least one condition must be True.
has_email = True
has_phone = False
print(has_email or has_phone)
Result:
True
not
not reverses a Boolean value.
is_raining = True
print(not is_raining)
Result:
False
Conditional Statements
Now we get to the part that allows Python to make decisions.
Python mainly uses:
ifelifelse
if
An if statement tells Python:
"If this condition is true, do this."
age = 20
if age >= 18:
print("You are an adult.")
Notice the indentation. The code inside the if statement must be indented.
else
else tells Python what to do when the condition is false.
age = 16
if age >= 18:
print("You are an adult.")
else:
print("You are a minor.")
elif
elif means "else if."
It allows us to check multiple conditions.
For example, grading a student:
score = 75
if score >= 80:
print("Grade A")
elif score >= 70:
print("Grade B")
elif score >= 60:
print("Grade C")
elif score >= 50:
print("Grade D")
else:
print("Grade F")
Nested if Statements
Sometimes one decision depends on another decision.
This is where nested if statements come in.
A nested if is simply an if statement inside another if statement.
For example:
age = 20
has_id = True
if age >= 18:
if has_id:
print("You can enter.")
else:
print("You need an ID.")
else:
print("You are too young to enter.")
Here, Python first checks whether the person is 18 or older.
Only if that condition is True does it check the second condition: whether they have an ID.
The structure looks like this:
if first condition:
if second condition:
do something
else: do something else
else: do something different
Nested if statements are useful when you have a decision within a decision.
However, sometimes the same logic can be written more simply using and:
if age >= 18 and has_id:
print("You can enter.")
Both approaches can work. As programs become more complex, choosing the simpler and clearer structure becomes important.
Python checks the conditions from top to bottom and stops when it finds one that is True.
A Practical Example: Even or Odd
Let's combine what we've learned.
number = int(input("Enter a number: "))
if number % 2 == 0:
print(f"{number} is even.")
else:
print(f"{number} is odd.")
Here we're using:
-
input()to get information from the user -
int()to convert it to an integer -
%to find the remainder -
==to compare -
ifandelseto make a decision - an f-string to display the result
That's a lot of Python concepts coming together in a small program.
Another Example: Student Grade
We can also combine input(), conditionals, and f-strings to create a simple grading program:
name = input("Student name: ")
score = int(input("Score: "))
if score >= 80:
grade = "A"
elif score >= 70:
grade = "B"
elif score >= 60:
grade = "C"
elif score >= 50:
grade = "D"
else:
grade = "F"
print(f"{name}, your grade is {grade}.")
For example:
Student name: Mary
Score: 75
Mary, your grade is B.
My Mental Model
I'm finding it helpful to think about these concepts as:
Calculate → Compare → Decide → Act
For example:
Calculate → 75
Compare → Is 75 >= 70?
Decide → Yes
Act → Print "Grade B"
This simple way of thinking makes conditionals much easier to understand.
Key Takeaways
The main things I'm taking away from this session are:
- Arithmetic operators help Python calculate.
- Comparison operators help Python ask questions.
- Logical operators help combine questions.
-
if,elif, andelseallow Python to make decisions. -
%is useful for checking remainders, including whether numbers are even or odd. -
=assigns a value, while==compares values.
The more I practice these small concepts, the more I realize that programming isn't about memorizing everything at once. It's about understanding the building blocks, putting them together, and gradually becoming more confident in solving problems with code.
Top comments (1)
对新手来说肯定很有帮助, 我当年也是这么入门的