</> CodeWithMe
Sign in
← 🐍 Python for Beginners

Functions and conditions

A function is a named block of code you can call many times:

def greet(name):
    return f"Hello, {name}!"

print(greet("Mike"))

Inside a function you can make decisions with if / else:

def sign(n):
    if n >= 0:
        return "plus"
    else:
        return "minus"

The % operator gives the remainder: 7 % 2 is 1, and 8 % 2 is 0.

Challenge

Write a function is_even(n) that returns True if the number is even and False if it is odd.

Loading Python (about 5 seconds the first time)…