Back to List

Python Conditionals and Functions

Learn about Python's if/elif/else conditional statements and function definitions (def) with practical examples.

Beginner
|
7min
|
Verified (2026-07)
Progress0/18 (0%)

Python Conditional Statements and Functions

After Completing This Topic

You will be able to create branches using if/elif/else and define functions using def to reuse code.


Conditional Statements β€” if, elif, else

Programs need to "behave differently depending on the situation." This is what conditional statements are for.

python
score = 85
if score >= 90:
print("A grade")
elif score >= 80:
print("B grade")
elif score >= 70:
print("C grade")
else:
print("Retake")
# Output: B grade

Conditions are checked in order from top to bottom. The first block that evaluates to True is executed, and the rest are skipped.

Indentation is Syntax

python
# βœ… Correct
if True:
print("Executed")
# ❌ IndentationError
if True:
print("Error!")

Python uses indentation (usually 4 spaces) instead of curly braces to define blocks. Incorrect indentation will result in a syntax error. Do not mix tabs and spaces.


Comparison and Logical Operators

python
# Comparison operators
x = 10
print(x > 5) # True
print(x == 10) # True (equal to)
print(x != 10) # False (not equal to)
print(x >= 10) # True (greater than or equal to)
# Logical operators
age = 25
has_id = True
if age >= 18 and has_id:
print("Allowed to enter")
if age < 18 or not has_id:
print("Not allowed to enter")
OperatorMeaningExample
andBoth are TrueA and B
orAt least one is TrueA or B
notInvertnot A

Functions β€” def

Instead of writing the same code multiple times, group it into a function and give it a name.

python
def greet(name):
print(f"Hello, {name}!")
greet("Kim Hoon") # Hello, Kim Hoon!
greet("Lee Soo") # Hello, Lee Soo!

def function_name(parameters): β†’ The indented block is the function body. The function is not executed until it is called.

return β€” Returning a Value

python
def add(a, b):
return a + b
result = add(3, 5)
print(result) # 8
# If there is no return, None is returned
def say_hello():
print("hello")
x = say_hello() # Prints "hello"
print(x) # None

print() displays something on the screen, while return returns a value. These are completely different roles.


Default Values and Keyword Arguments

python
def power(base, exponent=2):
return base ** exponent
print(power(3)) # 9 (Uses the default value of exponent=2)
print(power(3, 3)) # 27 (Specifies exponent=3)
# Keyword arguments β€” order does not matter
print(power(exponent=4, base=2)) # 16

Parameters with default values can be omitted when calling the function.


Variables Inside Functions β€” Scope

python
x = 10 # Global variable
def foo():
x = 20 # Local variable β€” only valid inside the function
print(x) # 20
foo()
print(x) # 10 β€” The global x did not change

Variables created inside a function disappear when the function finishes. Even if they have the same name as variables outside the function, they are separate variables. This is called scope.


Conditional Statements + Functions = Logic

python
def classify_bmi(weight, height):
bmi = weight / (height ** 2)
if bmi < 18.5:
return "Underweight"
elif bmi < 25:
return "Normal"
elif bmi < 30:
return "Overweight"
else:
return "Obese"
print(classify_bmi(70, 1.75)) # Normal
print(classify_bmi(90, 1.70)) # Overweight

Creating branches with conditional statements and making code reusable with functions. Combining these two can express most of a program's logic.


πŸ’¬ Questions & Comments

0 comments

You can post without signing in. Guest comments cannot be edited or deleted by their author.

0/2000

Loading...