Python Note
Python Note
Python Note
Computer Programs are set of instructions written in any particular computer programming language that tells
the computer what to do and how to do it.
It is a series of instructions written in the language of the computer which specifies processing operations that
the computer is to carry out on data. It is a coded list of instructions that tell" a computer how to perform a set of
calculations or operations.
❖ Readability: The program should be easy for any programmer to read and understand.
❖ Maintainability: A carefully written program should be very easy to amend and maintain if need be.
❖ Efficiency: One of the characteristics of a good program is it ability to solve a particular problem
skillfully.
❖ Clarity: Every good and tested program must be clear, straightforward and easy to understand.
PROGRAMMING LANGUAGES:
A programming language is a set of grammatical rules for instructing a computer to perform a particular task.
Programmers write instructions in various programming languages, some directly understandable by computers
and others requiring intermediate translation steps. We have hundreds of computer programming languages in
use today.
2. Orthogonality
This refers to the attribute of being able to combine various features of a language in all possible
combinations, with every combination being meaningful. Orthogonality feature makes it easier to learn a
programming language it also makes it easier to write program because there are fewer exceptions.
6. Programming environment
The availability of a reliable, efficient and well-documented implementation of a language is important. This
ensures that the speed of creation, testing, maintaining and modifying large programs much simpler.
7. Portability of programs.
Good programming language should have the attributes of transportability. That is, it should be easy to move
program projects from the computer on which they are developed to other computer systems. In order words,
the definitions of the program should be independent of the features of a particular machine.
CODING (PROGRAMMING)
Once an algorithm is finalized, it should be coded in a high-level programming language as selected by the
programmer. The ordered set of instructions are written in that programming language by following its syntax.
Source Code:
A program written in a high -level language is called source code.
The source code is translated into machine language using a compiler or interpreter, so that it can be understood
by the computer/machine.
Decomposition:
Decomposition is a process of braking down complex problem into smaller sub problem. It is helpful, when we
have to solve any big complex problem.
➢ Breaking down a complex problem into sub problems also means that each sub problem can be examined
in details.
➢ Each sub problem can be solved independently and by different persons (or teams).
➢ Having different teams working on different sub problems can also be advantageous because specific sub
problems can be assigned to teams who are experts in solving such problems.
➢ Once the individual sub problems are solved, it is necessary to test them for their correctness and
integrate them to get complete solutions.
Global Variable
A Global variable is the kind of variable that is accessible from other classes outside the program or class in which
it is declared. Different programming languages have various of ways in which global variables are being declared,
when the need arises.
Local Variable
A local variable is the kind of variable that is not accessible from other classes outside the program or class in
which it is declared. These are variables that are used within the current program unit (or function) in a later section
we will looking at global variables - variables that are available to all the program's functions.
TESTING
When first written, many programs contain bugs. Syntax errors and runtime errors are usually quickly removed,
but it can take a long time to deduce where a logic error lies and why. The purpose of testing is to help
programmers remove such bugs and to ensure that the program functions as intended. you should be building and
testing components as you develop the program. If you encounter a problem, there should be only a small
amount of new code that is not known to be correct.
It is important to note the following during program testing:
➢ The program created should be tested on various parameters.
➢ The program should meet the requirements of the user.
➢ The program must respond within the expected time.
➢ The program should generate correct output for all possible inputs.
➢ In the presence of syntactical errors, no output should be obtained from the program.
➢ In case the output generated is incorrect, then the program should be checked for logical errors.
WRITING MAINTAINABLE CODE
It is important to write program code that is easy to read and maintain so that extra features can be added later on.
Additionally, another programmer may wish to modify the program in order to improve it or debug an error.
In both situations, the understanding of the program, how it works and the purpose of the code will be made
easier if the program is written in a maintainable style. This includes using:
➢ comments
➢ descriptive names for variables, constants and subprograms
➢ indentation
➢ Comments
Comments are lines in programs that provide information about what the different parts of the program do.
They serve no other purpose and are not executed when the program is run - the program simply ignores
them and moves on to the next line of code.
Different languages express comments in different ways. In Python, for example, a comment begins with a
hash symbol, and is printed in red in some integrated development environments (IDEs), eg:
#ensures the input is restricted to a y or an n
➢ Descriptive names
Many programmers choose meaningless variable names such as x or y. While a program might run with such
names, it is difficult to understand the purpose of the variable. Programmers should choose a variable name
that reflects the purpose of the variable and the data the variables intended to hold, eg:
h - poor choice. What does it mean?
height - better choice as it describes the value the variable will hold.
The preferred use of descriptive names also applies to constants and sub-programs. For example, a constant is
often used to hold the value of Pi since this does not change. This can be called ‘PI’.
Subprograms usually perform a specific task. Giving a descriptive name like ‘multiply’ for a function that
multiplies two numbers together is sensible.
➢ Indentation
Code within selection or iteration construct should be indented. This allows programmers to easily see which
code falls within the selection or iteration, and where it ends.
In object-oriented programming, OOP, a program code is organized into objects that contain state that is only
modified by the code that is part of the object. Examples of Object-Oriented Programming (OOP) languages
include: Java, C++, Python, Smalltalk, C# and a host of others.
➢ Inheritance
Inheritance is an OOPS concept in which one object acquires the properties and behaviors of the parent object. It’s
creating a parent-child relationship between two classes. It offers robust and natural mechanism for organizing and
structuring of any software.
Inheritance lets programmers create new classes that share some of the attributes of existing classes. Using
Inheritance lets us build on previous work without reinventing the wheel.
This concept allows you to reuse the fields and methods of the existing class without having to rewrite the code in
a new class. In this scenario, the existing class is called the superclass and the derived class is called the subclass.
➢ Polymorphism
Polymorphism refers to the ability of a variable, object or function to take on multiple forms. For example, in
English, the verb run has a different meaning if you use it with a laptop, a foot race, and business. Here, we
understand the meaning of run based on the other words used along with it. The same also applied to
Polymorphism.
It Allows programmers to use the same word in a program to mean different things in different contexts. One form
of polymorphism is method overloading. That’s when the code itself implies different meanings. The other form
is method overriding. That’s when the values of the supplied variables imply different meanings.
The most common use of polymorphism in OOP occurs when a parent class reference is used to refer to a child
class object.
➢ Class
A class can be defined as a template/blueprint that describes the behavior/state that the object of its type supports.
A class determines how an object will behave and what the object will contain. In other words, it is a blueprint or
a set of instruction to build a specific type of object. A class is a group of similar entities. It is only a logical
component and not a physical entity. For example, if you had a class called “Expensive Cars” it could have objects
like Mercedes, BMW, Toyota, etc. Its properties (data) can be price or speed of these cars. While the methods may
be performed with these cars are driving, reverse, braking etc.
A class system allows the program to define a new class (derived class) in terms of an existing class (superclass)
by using a technique like inheritance, overriding and augmenting.
➢ Object
An object is nothing but a self-contained component which consists of methods and properties to make a particular
type of data useful. An object can be defined as an instance of a class, and there can be multiple instances of a class
in a program. Objects have states and behaviors. Example: A dog has states - color, name, breed as well as behavior
such as wagging their tail, barking, eating. An Object contains both the data and the function, which operates on
the data.
From a programming point of view, an object can be a data structure, a variable or a function. It has a memory
location allocated. The object is designed as class hierarchies.
➢ Methods
A method is basically a behavior. A class can contain many methods. It is in methods where the logics are
written, data is manipulated and all the actions are executed.
➢ Instance Variables
Each object has its unique set of instance variables. An object's state is created by the values assigned to these
instance variables.
➢ Dynamic binding
Binding refers to linking of procedure call to the code to be executed in response to the call. Dynamic binding (or
late binding) means the code associated with a given procedure call is not known until the time of call at run
time.
Application of OOP
OOP has become one of the programming buzzwords today. There appears to be a great deal of excitement and
interest among software engineers in using OOP.
Applications of OOP are beginning to gain importance in many areas. The most popular application of object-
oriented programming, up to now, has been in the area of user interface design such as window. Hundreds of
windowing systems have been developed, using the OOP techniques. Real-business system are often much more
complex and contain many more objects with complicated attributes and method. OOP is useful in these types of
application because it can simplify a complex problem.
The promising areas of application of OOP include:
➢ Real-time system
➢ Simulation and modeling
➢ Object-oriented data bases
➢ Hypertext, Hypermedia, and expertext
➢ AI and expert systems
➢ Neural networks and parallel programming
➢ Decision support and office automation systems
➢ CIM/CAM/CAD systems.
❖ Pascal Case
Each word starts with a capital letter:
MyVariableName = "John"
❖ Snake Case
Each word is separated by an underscore character:
my_variable_name = "John"
print(a)
print(b)
print(c)
print(type(a))
print(type(b))
print(type(c))
UPPER CASE METHOD
The upper() method returns the string in upper case:
a = "Hello, World!"
print(a.upper())
a = "Hello, World!"
print(a.lower())
REMOVE WHITESPACE
Whitespace is the space before and/or after the actual text, and very often you want to remove this space.
The strip() method removes any whitespace from the beginning or the end:
a = " Hello, World! "
print(a.strip()) # returns "Hello, World!"
PYTHON SAMPLE PROGRAMS
PROGRAM TO FIND THE ROOTS OF QUADRATIC EQUATION (METHOD 1)
def find_Diameter(radius):
return 2 * radius
def find_Circumference(radius):
return 2 * math.pi * radius
def find_Area(radius):
return math.pi * radius * radius
diameter = find_Diameter(r)
circumference = find_Circumference(r)
area = find_Area(r)
1. pi=22/7
2. radian = float(input('Radius of sphere: '))
3. sur_area = 4 * pi * radian **2
4. volume = (4/3) * (pi * radian ** 3)
5. print("Surface Area is: ", sur_area)
6. print("Volume is: ", volume)
PYTHON PROGRAM TO FIND VOLUME AND SURFACE AREA OF SPHERE (METHOD 1)
4
(The surface area of a sphere is: 𝐴𝑟𝑒𝑎 = 4𝜋𝑟 2 and the volume is: 𝑉𝑜𝑙𝑢𝑚𝑒 = 3 𝜋𝑟 3 where r is the radius).
1. import math
2. def Area_of_Triangle(radius):
3. sa = 4 * math.pi * radius * radius
4. Volume = (4 / 3) * math.pi * radius * radius * radius
5. print("\n The Surface area of a Sphere = %.2f" %sa)
6. print("\n The Volume of a Sphere = %.2f" %Volume)