콘텐츠로 이동

Chapter 2: Operators and Conditionals — Python's Decision Making

Expressions and Operators

An expression is code that calculates and produces a result.

value1  operator  value2  →  result
  3       +        5    →   8

Arithmetic Operators

print(10 + 3)    # 13  (addition)
print(10 - 3)    # 7   (subtraction)
print(10 * 3)    # 30  (multiplication)
print(10 / 3)    # 3.3333...  (division, result is float)
print(10 // 3)   # 3   (floor division)
print(10 % 3)    # 1   (modulo / remainder)
print(2 ** 10)   # 1024  (exponentiation)
Operator Name Example Result
+ Addition 7 + 3 10
- Subtraction 7 - 3 4
* Multiplication 7 * 3 21
/ Division 7 / 2 3.5
// Floor division 7 // 2 3
% Modulo 7 % 2 1
** Exponentiation 2 ** 8 256

The bool Type

bool has only two values: True or False.

print(type(True))    # <class 'bool'>
print(type(False))   # <class 'bool'>

Comparison Operators

Compare two values and return a bool.

print(5 > 3)     # True
print(5 < 3)     # False
print(5 >= 5)    # True
print(5 <= 4)    # False
print(5 == 5)    # True
print(5 != 5)    # False
Operator Meaning Example Result
> Greater than 5 > 3 True
< Less than 5 < 3 False
>= Greater than or equal 5 >= 5 True
<= Less than or equal 4 <= 3 False
== Equal 3 == 3 True
!= Not equal 3 != 4 True

Logical Operators

Combine multiple conditions.

print(True and True)    # True
print(True and False)   # False
print(False or True)    # True
print(False or False)   # False
print(not True)         # False
print(not False)        # True
Operator Meaning Result
and Both must be True True and FalseFalse
or At least one True True or FalseTrue
not Flips True↔False not TrueFalse
age = 20
has_ticket = True

if age >= 18 and has_ticket:
    print("Admitted")

Conditionals (if)

Basic Structure

if condition:
    # runs when condition is True
score = 85
if score >= 60:
    print("Pass")

if-else

score = 45
if score >= 60:
    print("Pass")
else:
    print("Fail")

if-elif-else

score = 75

if score >= 90:
    print("A")
elif score >= 80:
    print("B")
elif score >= 70:
    print("C")
else:
    print("F")

elif is checked only when the previous condition is False. Python checks from top to bottom and executes only the first block that is True, skipping the rest.

Indentation

In Python, indentation defines code blocks. Use 4 spaces.

# O (correct indentation)
if True:
    print("Inside block")

# X (no indentation → error)
if True:
print("Error!")

Operator Precedence

Like math, Python has an order of operations (highest first):

()          Parentheses (highest)
**          Exponentiation
*, /, //, % Multiplication, Division
+, -        Addition, Subtraction
>, <, >=, <=, ==, !=  Comparison
not         Logical NOT
and         Logical AND
or          Logical OR
=           Assignment (lowest)
print(2 + 3 * 4)      # 14  (multiplication first)
print((2 + 3) * 4)    # 20  (parentheses first)
print(2 ** 3 + 1)     # 9   (exponentiation first)

Practice Missions

Mission 1: Circle Area Calculator

Take the radius as input and print the area.

Area = π × r²   (π = 3.14)
r = float(input("Radius: "))
# Write your code here

# Radius: 5
# Circle area: 78.5

Mission 2: BMI Calculator

Take weight (kg) and height (m), calculate BMI, and print the status.

BMI = weight / height²

BMI < 18.5   → Underweight
BMI < 25.0   → Normal
BMI < 30.0   → Overweight
Otherwise    → Obese
weight = float(input("Weight (kg): "))
height = float(input("Height (m): "))
# Write your code here

# Weight (kg): 70
# Height (m): 1.75
# BMI: 22.86
# Normal weight

Mission 3: Odd or Even

Take a number and print whether it's odd or even.

n = int(input("Number: "))
# Write your code here

# Number: 7
# 7 is odd

Mission 4: Grade Calculator

Take a score and print the letter grade.

90+ → A
80+ → B
70+ → C
60+ → D
Otherwise → F
score = int(input("Score: "))
# Write your code here

Mission 5: Projectile Distance

Take initial velocity v and launch angle θ, then calculate the range R.

R = v² × sin(2θ) / g   (g = 9.8)
import math

v = float(input("Initial velocity (m/s): "))
theta = float(input("Launch angle (degrees): "))
# Write your code here  (Hint: math.sin(), math.radians())

Key Summary

Concept Description
Arithmetic operators +, -, *, /, //, %, **
Comparison operators >, <, >=, <=, ==, != → returns bool
bool Only True or False
Logical operators and, or, not — combine conditions
if Execute block when condition is True
elif Check next condition when previous is False
else Execute when all conditions are False
Indentation Python syntax that defines code blocks
Operator precedence () > ** > */ > +- > comparison > not > and > or