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

    Objects and lazy iteration · Lesson 21 of 25

    Classes and objects

    Group state and behavior in one object.

    8 minutes2 games

    A class defines objects. __init__ initializes an instance after creation, and self refers to the current instance. Store instance state on self and call methods through the object.

    Python
    class Counter:
        def __init__(self, start=0):
            self.value = start
    
        def increment(self):
            self.value += 1
            return self.value
    
    a = Counter()
    b = Counter(10)
    print(a.increment())
    print(b.value)
    a.value1int
    b.value10int

    Important

    A list defined as a class attribute is shared across instances. For separate lists, create self.items = [] inside __init__.

    Interview question

    Why define self but not pass it explicitly on a method call?

    Complete the code

    Complete storing the student name.

    class Student:
        def __init__(self, name):
            .name = 

    Exercise

    Define Box with a width and a double method returning twice the width. Print Box(4).double().

    Expected output: 8

    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.

    PreviousExchanging data with JSONNextInheritance and composition
    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.