Page 408 - Computer_Science_F5
P. 408

Document them in your portfolio.                  OOP concepts in Python


               Questions:                              The fundamental OOP concepts in Python
               1.  Reflect on how using modules can    include class and objects, Polymorphism,    Chapter Six: Object oriented programming with Python
          FOR ONLINE READING ONLY
                   help in organising and reusing code.  encapsulation,  inheritance, and data
               2.  Discuss how you can apply the       abstraction
                   concept of modules in larger        Class and objects
                   projects.
                                                       Python class
                                                       In Python, a class serves as a grouping
                    Exercise 6.1                       of objects.  It acts as a blueprint  or
               1.  Create a Python script that defines   template for creating individual objects.
                   variables of different data types   A class represents a logical entity that
                   (integer, float, string, and boolean).   encompasses  certain  attributes  (data)
                   Print the type of each variable.    and  methods (functions).  It  provides
               2.  Write  a  Python  program  that     the structure and definition for creating
                                                       objects with specific characteristics and
                   takes two numbers as input from
                   the user and performs addition,     behaviors..
                   subtraction, multiplication, and
                   division. Display the results for   To understand the need of creating a class,
                   each operation.                     let us examine an illustration. Suppose
                                                       you wish to keep track  of numerous
               3.  Develop a  Python script that       dogs with  distinct  characteristics,  such
                   accepts a string input from the user,   as breed and age. If a list is employed,
                   converts it to uppercase, and prints   the  first  element  might  indicate  the
                   the length of the string.           dog’s breed, while the second element
               4.  Create a  Python program that       represents its  age. However, when
                   initialises a list with five integer   dealing with a collection  of 100
                   elements. Use a for loop to iterate   different dogs, it becomes challenging to
                   through the list and  print each    determine which element corresponds to
                   element doubled.                    which attribute. Moreover, if you want
               5.  Write a Python script that takes    to  incorporate  additional  properties
                   a number as input from the user     for these dogs, such an approach lacks
                   and prints whether the number is    organisation  and becomes  unwieldy.
                   positive, negative, or zero.        This is precisely  where classes come

               6.  Define a Python function that takes   into  play, as they  provide  a structured
                   two parameters and returns their    solution to effectively  manage  and
                   sum. Call the function with two     organise data by defining attributes and
                   numbers and print the result.       behaviors for each individual object.


                                                    399
               Student’s Book  Form Five



     Computer Science Form 5.indd   399                                                     23/07/2024   12:34
   403   404   405   406   407   408   409   410   411   412   413