lesson 07.11.2021
dictionary comprehension: dictionary comprehension is a method for transforming one dictionary into another dictionary. During this transformation, items within the original dictionary can be conditionally included in the new dictionary and each item can be transformed as needed.
dict1 = {'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5}
# Double each value in the dictionary
double_dict1 = {k:v*2 for (k,v) in dict1.items()}
Object Oriented Programming (OOP) : Python is an “object-oriented programming language.” Object-oriented programming (OOP) is a programming paradigm based on the concept of “objects”, which can contain data, in the form of fields (often known as attributes), and code, in the form of procedures (often known as methods). A feature of objects is an object’s procedures that can access and often modify the data fields of the object with which they are associated (objects have a notion of “this” or “self”). In OOP, computer programs are designed by making them out of objects that interact with one another
Solid: SOLID is an acronym for the first five object-oriented design (OOD). These principles establish practices that lend to developing software with considerations for maintaining and extending as the project grows. Adopting these practices can also contribute to avoiding code smells, refactoring code, and Agile or Adaptive software development.
SOLID stands for:
- S – Single-responsiblity Principle
- O – Open-closed Principle
- L – Liskov Substitution Principle
- I – Interface Segregation Principle
- D – Dependency Inversion Principle
The Unified Modeling Language (UML) is a language used in the field of software engineering that represent the components of the Object-Oriented Programming concepts. It is the general way to define the whole software architecture or structure.
demo
class Person:
pass
danny = Person()
danny.id = 200 # same as danny.__dict__['id'] = 200
danny.__dict__['fname'] = 'danny'
links
- class code: homework-solution: click here
- class code: dictionary comprehension demo: click here
- class code: oop-start: click here
- class code: oop-1: click here
- class code: oop-2: click here
- class code: oop-3: click here
Homework
- create a Book class. now, create an instance of your favorite book with the following data fields: title, author, genre, year_of_publish, no_of_pages (1) create the instance data using dictionary [for example: myBook.__dict__[‘year_of_publish’] = 1961] (2) create the instance data using dot [for example: myBook.title = ‘catch 22’] (3) create a method which creates a new books instance and data (see the create_Person method we did in lesson)
- create a UML for your book class
- *etgar: create a list of 3 three books. now create a dictionary which contains these 3 books where the key is the book-title and the value is the book instance. now input a string from the user (the title) and try to pop the book out of the dictionary using the title as the key (and print the book.__dict__ you just poped)
- create a class Car. now create a new instance and generate data fields for the car as you like