Skip to content
    SeaCode
    Learning tracksCode ArenaWorkshops & EventsInstructors
    Log inSign up
    PyPython

    Getting started

    • Your first Python program
    • Variables and types
    • Numbers and operators
    • Type conversion and input

    Strings

    • Strings and f-strings
    • String indexing and slicing
    • Cleaning and splitting strings

    Control flow

    • Conditions: if, elif and else
    • Loops: for and while
    • Loop control and nesting

    Functions

    • Functions with def
    • Parameters and scope

    Collections

    • Lists
    • Dictionaries
    • Tuples and sets
    • Comprehensions, enumerate and zip

    Errors, files and libraries

    • Handling exceptions
    • Modules and the standard library
    • Reading and writing files
    • Exchanging data with JSON

    Objects and lazy iteration

    • Classes and objects
    • Inheritance and composition
    • Iterators and generators

    Testing and project

    • Testing and project setup
    • Project: a grade report
    Python

    Functions · Lesson 11 of 25

    Functions with def

    Write code once, use it many times.

    8 minutes3 games

    def defines a function: its name, inputs in ( ), then : and an indented body. return gives back the result. Inputs can have defaults: def greet(name="friend").

    Python
    def area(width, height):
        return width * height
    
    def greet(name="friend"):
        return f"Hello, {name}!"
    
    print(area(4, 5))
    print(greet("Salma"))
    print(greet())

    Important

    print displays a value; return hands a value to the caller and ends the function.

    Interview question

    Does defining a function execute its body?

    Arguments

    Function body

    Return value

    Quick question

    What does a function without return give back?

    Complete the code

    Complete the function that returns double the number.

     double(n):
         n * 2

    Exercise

    Write is_even(n) returning True for even numbers, then print is_even(10).

    Expected output: True

    Python
    def is_even(n):
        
    

    Learn more from

    • Python documentationdocs.python.org
    • Real Python: further readingrealpython.com

    Played 0 of 3 games

    Sign in to save your progress and earn XP.

    PreviousLoop control and nestingNextParameters and scope
    SeaCode

    Learn to code for real, online or at our branch in front of Cairo University, Giza.

    Learn

    • All courses
    • Interactive tutorials
    • Interview practice problems
    • Mock interview
    • Learning tracks
    • Code Arena
    • Workshops & Events
    • Knowledge library
    • Team projects

    Your account

    • Log in
    • Sign up
    • Learning plan
    • Certificates
    • Site map

    Contact us

    • WhatsApp
    • 010 9094 1104
    • In front of Cairo University, Giza

    © 2026 SeaCode. All rights reserved.