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 12 of 25

    Parameters and scope

    Defaults, keyword arguments and local variables.

    8 minutes2 games

    Pass arguments by position or by name. Defaults apply when an argument is omitted. A variable assigned inside a function is local; prefer arguments and return values over shared global state.

    Python
    tax = 0.1
    
    def total(price, qty=1):
        subtotal = price * qty
        return subtotal
    
    print(total(50))
    print(total(qty=3, price=50))
    print(tax)
    ToolUse

    total(50)

    qty defaults to 1

    total(50, 3)

    Positional arguments

    total(qty=3, price=50)

    Keyword arguments

    Important

    Defaults are evaluated when the function is defined. Avoid a mutable list as a default; use None and create a fresh list inside.

    Interview question

    Why is a local variable unavailable outside the function?

    What's the output?

    def greet(name="friend"):
        return f"Hi {name}"
    print(greet())

    What will this code print?

    Exercise

    Define discount(price, percent=10) returning the discounted price. Print discount(200).

    Expected output: 180.0

    Python

    Learn more from

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

    Played 0 of 2 games

    Sign in to save your progress and earn XP.

    PreviousFunctions with defNextLists
    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.