PowerPoint Presentation
Basics in programming
In the dawn of computing, programmers thought about programming in terms of statements.
Throughout the 1970s and 1980s, programmers began thinking about programs in terms of routines.
In the twenty-first century, programmers think about programming in terms of classes.
Python modules
A class is a collection of data and routines that share a cohesive, well-defined responsibility.
A class might also be a collection of routines that provides a cohesive set of services even if no common data is involved.
A key to being an effective programmer is maximizing the portion of a program that you can safely ignore while working on any one section of code.
Classes are the primary tool for accomplishing that objective.
Python modules
The first and probably most important step in creating a high-quality class is creating a good interface.
This consists of creating a good abstraction for the interface to represent and ensuring that the details remain hidden behind the abstraction.
Python modules
A module allows you to logically organize your Python code.
Grouping related code into a module makes the code easier to understand and use.
Simply, a module is a file consisting of Python code. A module can define functions, classes and variables. A module can also include runnable code.
Python modules
Python modules
To define module greet, write this code into greet.py
Python modules
You can use any Python source file as a module by executing an import statement in some other Python source file. The import has the following syntax .
Python modules
import greet
greet.greeting(”Jari”)
Add this to greet.py
Python modules
Now modify main program
a = mymodule.person1[“age”]
print(a)
Python modules
You can use alias to name module in your code
a = mymodule.person1[“age”]
print(a)
Python modules
Python modules
Python modules
Python’s from statement lets you import specific attributes from a module. The from .. import .. has the following syntax :
Python modules
You can use built-in dir-function to list all the modules, variables and functions that are defined in a module
Python modules
Exercise create a simple module called calculator and use it in main program
Python modules
Create module that represents a real-life object Car
It has following functions:
SetSpeed, set current speed of car
GetSpeed, gets current speed of car
SetDirection, sets direction of car
GetDirection, get direction of car
SetColor, sets color of car
GetColor, gets color of car
Print, prints all details of car
Exercies 9.
/docProps/thumbnail.jpeg