101.school
CoursesAbout
Search...⌘K
Generate a course with AI...

    Python

    Receive aemail containing the next unit.
    • Refreshing Python Basics
      • 1.1Python Data Structures
      • 1.2Syntax and Semantics
      • 1.3Conditionals and Loops
    • Introduction to Object-Oriented Programming
      • 2.1Understanding Class and Objects
      • 2.2Design Patterns
      • 2.3Inheritance, Encapsulation, and Polymorphism
    • Python Libraries
      • 3.1Numpy and Matplotlib
      • 3.2Pandas and Seaborn
      • 3.3SciPy
    • Handling Files and Exception
      • 4.1Reading, writing and manipulating files
      • 4.2Introduction to Exceptions
      • 4.3Handling and raising Exceptions
    • Regular Expressions
      • 5.1Introduction to Regular Expressions
      • 5.2Python’s re module
      • 5.3Pattern Matching, Substitution, and Parsing
    • Databases and SQL
      • 6.1Introduction to Databases
      • 6.2Python and SQLite
      • 6.3Presentation of Data
    • Web Scraping with Python
      • 7.1Basics of HTML
      • 7.2Introduction to Beautiful Soup
      • 7.3Web Scraping Case Study
    • Python for Data Analysis
      • 8.1Data cleaning, Transformation, and Analysis using Pandas
      • 8.2Data visualization using Matplotlib and Seaborn
      • 8.3Real-world Data Analysis scenarios
    • Python for Machine Learning
      • 9.1Introduction to Machine Learning with Python
      • 9.2Scikit-learn basics
      • 9.3Supervised and Unsupervised Learning
    • Python for Deep Learning
      • 10.1Introduction to Neural Networks and TensorFlow
      • 10.2Deep Learning with Python
      • 10.3Real-world Deep Learning Applications
    • Advanced Python Concepts
      • 11.1Generators and Iterators
      • 11.2Decorators and Closures
      • 11.3Multithreading and Multiprocessing
    • Advanced Python Concepts
      • 12.1Generators and Iterators
      • 12.2Decorators and Closures
      • 12.3Multithreading and Multiprocessing
    • Python Project
      • 13.1Project Kick-off
      • 13.2Mentor Session
      • 13.3Project Presentation

    Introduction to Object-Oriented Programming

    Understanding Class and Objects in Python

    general-purpose programming language

    General-purpose programming language.

    Python is an object-oriented programming language. This means that almost all the code is implemented using a special construct called classes. Programmers use classes to keep related things together. This is done using the keyword class, which is a grouping of object-oriented constructs.

    What is a Class?

    A class is a code template for creating objects. Objects have member variables and have behaviour associated with them. In python, a class is created by the keyword class.

    An object is created using the constructor of the class. This object will then be called the instance of the class.

    Creating a Class in Python

    In Python, a class is created by the keyword class. An object is created using the constructor of the class. This object will then be called the instance of the class.

    class MyClass: x = 5

    Creating an Object in Python

    You can create an object of the above class as follows:

    p1 = MyClass() print(p1.x)

    Understanding the __init__ method

    In Python, __init__ is a special method which is automatically called when an object of that Class is created. We use the __init__ method to initialize the attributes of an object.

    class Person: def __init__(self, name, age): self.name = name self.age = age p1 = Person("John", 36) print(p1.name) print(p1.age)

    Object Methods

    Objects can also contain methods. Methods in objects are functions that belong to the object.

    class Person: def __init__(self, name, age): self.name = name self.age = age def myfunc(self): print("Hello my name is " + self.name) p1 = Person("John", 36) p1.myfunc()

    The self Parameter

    The self parameter is a reference to the current instance of the class and is used to access variables that belong to the class.

    It does not have to be named self, you can call it whatever you like, but it has to be the first parameter of any function in the class.

    class Person: def __init__(mysillyobject, name, age): mysillyobject.name = name mysillyobject.age = age def myfunc(abc): print("Hello my name is " + abc.name) p1 = Person("John", 36) p1.myfunc()

    By the end of this unit, you should have a solid understanding of classes and objects in Python, and you should be able to create and use them in your programs.

    Test me
    Practical exercise
    Further reading

    My dude, any questions for me?

    Sign in to chat
    Next up: Design Patterns