Understanding Python Classes and Objects

classes l.w
1 / 27
Embed
Share

Learn about Python classes and objects in object-oriented programming. Explore how classes serve as blueprints for creating objects with properties and methods, and understand the concept of instances and attributes. Dive into the use of methods and the significance of the __init__() special method. Discover how all data types in Python are treated as objects and gain insights into the fundamental principles of object-oriented programming.

  • Python
  • Object-Oriented Programming
  • Classes
  • Objects
  • Attributes

Uploaded on | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. Classes Unit 11.4A: Object Oriented Programming ( P)

  2. Learning objectives 11.4.1.1 create classes and instances of classes; 11.4.1.2 develop methods for the class; 11.4.1.3 use special method __init__ to set default properties;

  3. Python Classes and Objects Python is an object oriented programming language. Almost everything in Python is an object, with its properties and methods. A Class is like an object constructor, or a "blueprint" for creating objects. A blueprint is a detailed plan or design that guides the creation of something. In object-oriented programming (OOP), a class is called a blueprint because it defines the structure (attributes and methods) of objects but does not create actual objects itself.

  4. All data in Python is an object. Numbers of all types, strings, lists, dictionaries, even functions, modules, and finally data types themselves are all objects! An object in programming is an entity that has properties (attributes) and methods (operations on them). Class is a data type that is created to describe complex objects. An instance is an object of a class that has its properties. Attribute - a characteristic of each instance. An instance has all the attributes of a class. A method is an action that an object can perform an instance of.

  5. We should know! All the data types we have studied are classes. For example, All values are objects. For example, 5 is an instance of class 'int' {1:"one", 2: "two", 3: "three"} is an instance of class 'dict'

  6. The __init__() Function The examples above are classes and objects in their simplest form, and are not really useful in real life applications. To understand the meaning of classes we have to understand the built-in __init__() function. All classes have a function called __init__(), which is always executed when the class is being initiated. Use the __init__() function to assign values to object properties, or other operations that are necessary to do when the object is being created:

  7. Note: The __init__() function is called automatically every time the class is being used to create a new object.

  8. task1 Create a Plate class, then create an instance of it called saucer with the attributes color="white" and size=100. Get output "White saucer, size - 100"

  9. task2 Create a new AdvancePlate class that initializes the color and size attributes with the __init__() method. Also, the class must have a output() method that prints information about the plate in the form: "I am <color> a plate with a radius of <size> mm." Get output "I am violet a plate with a radius of 120 mm."

  10. task3 Create a class Car, then create an instance of it named midget_car (subcompact) with brand and length attributes with values KIA and 2600 respectively. Get output "KIA is a model of car, length - 2600"

  11. task4 Create a new AdvanceCar class that initializes the brand and length attributes with the __init__() method. Also, the class should have an info() method that prints information about the car in the form: "I got a taxi <brand> with a length of <length> mm." Get output "I got a taxi BMW with a length of 2800 mm."

  12. Task 5 Analyze the code

  13. Task 5 Write a program that asks for the user's number. If the number belongs to the range from -100 to 100, then the created instance belongs to the first class, otherwise, the created instance belongs to the second class. The class includes the __init__ method, which in the first-class calculates the square of a number, and in the second class, it multiplies by two.

  14. Practice tasks 1. Create a Vehicle class without any variables and methods 2. Write a simple Python class named Student and display its type. 3. Write a Python program to create a Vehicle class with max_speed and mileage instance attributes. 4. Write a Python program to create an instance of a specified class and display the namespace of the said instance. 5. Write a Python class named Student with two attributes student_name, marks. Modify the attribute values of the said class and print the values of the said attributes.

  15. Reflection Reflection

More Related Content