Page 406 - Computer_Science_F5
P. 406

Modules in Python                        (b)  Creating and using custom modules:
              Modules  are  files  that  contain  Python   You can create your own modules
              code and define functions, classes, and      by writing Python code in a separate
              variables. They enable code organisation     .py file. To use functions or classes   Chapter Six: Object oriented programming with Python
              into reusable components  and help           defined in a custom module, you
          FOR ONLINE READING ONLY
              manage namespaces. Python includes a         import the module into your script.
              standard library with modules like math,     Once imported, you can access the
              random, and datetime that offer various      functions and variables using dot
              functionalities. The following are some      notation (module_name.function_
              important points about module:               name).
              (a)  Importing modules: To use functions   (c)  Built-in functions: Python provides
                  or variables from a module, you          a set of built-in functions that are
                  import it into your Python script. You   available without the need to import
                  can import an entire module using the    any modules. These functions cover
                  “import” statement followed by the       a wide range of functionalities,
                  module name. Alternatively, you can      including mathematical operations,
                  import specific functions or variables   string manipulation, type conversion,
                  using the “from” keyword.                and more.


              Using modules: You can use modules depending on the context or situations you
              want.
              (a)  You can use the random module to generate a random number between a specified
                   range where you can import the random module and employ the random.randint()
                   function to create a random integer falling within the specified start and end
                   values. Subsequently, the outcome is stored in the variable random_number and
                   showcased through console printing. For example,
                       import random

                       start = 1
                       end = 10
                       random_number = random.randint(start, end)
                       print(“Random number between”, start, “and”, end, “is:”, random_number)
              (b)  You can use the datetime module to display the current date and time. Where
                   you can import the datetime module and utilize the datetime.datetime.now()
                   function to fetch the present date and time. The outcome is then captured in the
                   variable current_datetime and exhibited through console printing. For example,
                        import datetime

                        current_datetime = datetime.datetime.now()
                        print(“Current Date and Time:”, current_datetime)

                                                    397
               Student’s Book  Form Five



     Computer Science Form 5.indd   397                                                     23/07/2024   12:34
   401   402   403   404   405   406   407   408   409   410   411