You are on page 1of 37

Programming Techniques - Python

• Use of problem solving process

• Use algorithmic approach to solve problems

• Compares and contrast different programming paradigms

• Uses data structure in program

• Manage data in database

ALPINE SKI HOUSE 1


Computer programming

• Computer Programming is defined as the process of creating computer software


using a programming Language. Computer programs are written by Human
individuals(Programmers)
• A computer program is a step by step set of instructions that a computer has to
work through in a logical sequence in order to carry out a particular task. The
computer executes these instructions (obeys the instructions) when told to do so
by the user.

ALPINE SKI HOUSE 2


Programming Languages

• A programming language is an artificial language that can be used to control the


behavior of a machine, particularly a computer. Programming languages, like
human languages, are defined through the use of syntactic and semantic rules, to
determine structure and meaning respectively.

ALPINE SKI HOUSE 3


Syntax

• Syntax is the set of rules that define what the various combinations of symbols
mean. This tells the computer how to read the code. Syntax refers to a concept in
writing code dealing with a very specific set of words and a very specific order to
those words when we give the computer instructions.

if 5 > 2: thislist = ["apple", "banana", "cherry"]


 print("Five is greater than two!")  print(thislist)
if 2 > 5:
print(“Two is greater than five!") 

ALPINE SKI HOUSE 4


Levels of programming languages

• Machine Language
• Assembly Language
• High level Language

ALPINE SKI HOUSE 5


Machine Language

• The fundamental language of the computer’s processor, also called Low Level Language.
• All programs are converted into machine language before they can be executed.
• Consists of combination of 0’s and 1’s that represent high and low electrical voltage.
• Fastest to execute because it is already in the language that the computer can understand
• It is difficult to identify mistakes made Time-consuming and tedious to write Machine
dependent
• Programing becomes more difficult as the complexity of the program increases

ALPINE SKI HOUSE 6


Assembly Language

• A low level language that is similar to machine language (machine dependent).


• Uses symbolic operation code to represent the machine operation code.
• Faster and more efficient in the use of hardware than high-level programming
languages.
• Easier to write than machine language
• The code is not very easy to understand, hence the introduction of high level
programming languages.

ALPINE SKI HOUSE 7


High Level Language

• Computer (programming) languages that are easier to learn.


• Uses English like statements.
• Examples are C ++, Visual Basic, Pascal, Fortran and …....
• They are machine independent hence portable
• They are user friendly and easy to learn
• High-level language programs are easy to debug
• They are executed much slower than low-level programming languages

ALPINE SKI HOUSE 8


Compilers and Interpreters

Compiler Interpreter
A compiler takes the entire program in one go. An interpreter takes a single line of code at a time.

The compiler generates an intermediate machine code The interpreter never produces any intermediate
(object code). machine code.

The compiler is best suited for the production An interpreter is best suited for a software
environment. development environment.

The compiler is used by programming languages as C, An interpreter is used by such programming languages
C++, C#, Scala, Java, etc. such as Python, PHP, Perl, Ruby, etc.

Memory requirement -> More (since object code is Memory requirement -> Less
generated)

ALPINE SKI HOUSE 9


Compilers and Interpreters

ALPINE SKI HOUSE 10


Procedural Programming

• Procedural Programming involves writing down a list of instructions to tell the


computer what it should do step-by-step to finish the task at hand.
• This paradigm uses a linear top-down approach and treats data and procedures as
two different entities. Based on the concept of a procedure call, Procedural
Programming divides the program into procedures, which are also known as
routines or functions, simply containing a series of steps to be carried out.

ALPINE SKI HOUSE 11


Pros and Cons of Procedural Programming

• Pros
• Excellent for general-purpose programming
• Enhances the reusability of the code.
• Simplicity of use
• Easy accessibility
• Tracking the program flow is easy as the same flow linearly.

• Cons
• Data is vulnerable.
• Not much practical for solving real-world problems.
• Programs created using this programming paradigm are complex.

ALPINE SKI HOUSE 12


Object-Oriented Programming

• Object-oriented programming (OOP) is a programming paradigm based on the


concept of "objects", which may contain data, in the form of fields, often known
as attributes; and code, in the form of procedures, often known as methods.

For example, a person is an object which has certain properties such as height,
gender, age, etc. It also has certain methods such as move, talk, and so on.

ALPINE SKI HOUSE 13


Object-Oriented Programming

ALPINE SKI HOUSE 14


Objects

• An object is a single instance of a class. All data members and member functions
of the class can be accessed with the help of objects.

• Since many houses can be made from the same description, we can create many
objects from a class.

ALPINE SKI HOUSE 15


Classes and Objects

ALPINE SKI HOUSE 16


OOP Concepts

• Abstraction − It refers to, providing only essential information to the outside world and hiding their background details.
For example, a web server hides how it processes data it receives, the end user just hits the endpoints and gets the data back.

• Encapsulation − Encapsulation is a process of binding data members (variables, properties) and member functions
(methods) into a single unit. It is also a way of restricting access to certain properties or component. The best example for
encapsulation is a class.

• Inheritance − The ability to create a new class from an existing class is called Inheritance. Using inheritance, we can
create a Child class from a Parent class such that it inherits the properties and methods of the parent class and can have its
own additional properties and methods. For example, if we have a class Vehicle that has properties like Color, Price, etc.,
we can create 2 classes like Bike and Car from it that have those 2 properties and additional properties that are specialized
for them like a car has number Of Windows while a bike cannot. Same is applicable to methods.

• Polymorphism − The word polymorphism means having many forms. Typically, polymorphism occurs when there is a
hierarchy of classes and they are related by inheritance.

ALPINE SKI HOUSE 17


Algorithm

An algorithm is a process or set of rules which must be followed to complete a


particular task. This is basically the step-by-step procedure to complete any task.

There are some basics steps to make an algorithm:


• Start – Start the algorithm
• Input – Take the input for values in which the algorithm will execute.

• Conditions – Perform some conditions on the inputs to get the desired output.
• Output – Printing the outputs.
• End – End the execution.

ALPINE SKI HOUSE 18


Algorithm

Let’s take an example to make a cup of tea,


Activity:
• Step 1: Start Write an algorithm to add two
• Step 2: Take some water in a bowl. numbers.
• Step 3: Put the water on a gas burner.
• Step 4: Turn on the gas burner 
• Step 5: Wait for some time until the water is boiled. 
• Step 6: Add some tea leaves to the water according to the requirement.
• Step 7: Then again wait for some time until the water is getting colorful as tea.
• Step 8: Then add some sugar according to taste.
• Step 9: Again wait for some time until the sugar is melted.
• Step 10: Turn off the gas burner and serve the tea in cups with biscuits.
• Step 11: End

ALPINE SKI HOUSE 19


Algorithm

Example 2. Find the area of a rectangle


Step 1: Start

Step 2: Take the Height and Width of the rectangle as input.


Step 3: Declare a variable as “area”
Step 4: Multiply Height and Width
Step 5: Store the multiplication to “Area”, (its look like area = Height x Width)
Step 6: Print “area”;
Step 7: End

ALPINE SKI HOUSE 20


Algorithm

Example 3. Find the greatest between 3 numbers.


•Step 1: Start
•Step 2: Take 3 numbers as input, say A, B, and C.
•Step 3: Check if(A>B and A>C)
•Step 4: Then A is greater
•Step 5: Print A
•Step 6: Else
•Step 7: Check if(B>A and B>C)
•Step 8: Then B is greater
•Step 9: Print B
•Step 10: Else C is greater
•Step 11: Print C
•Step 12: End

ALPINE SKI HOUSE 21


Algorithm

Advantages of Algorithms

• An algorithm uses a definite procedure.


• It is easy to understand because it is a step-by-step definition.
• The algorithm is easy to debug if there is any error happens.
• It is not dependent on any programming language
• It is easier for a programmer to convert it into an actual program because the algorithm divides a problem into
smaller parts.

Disadvantages of Algorithms

• An algorithm is Time-consuming, there is specific time complexity for different algorithms.


• Large tasks are difficult to solve in Algorithms because the time complexity may be higher, so programmers have
to find a good efficient way to solve that task.
• Looping and branching are difficult to define in algorithms.

ALPINE SKI HOUSE 22


 Pseudo Code

Pseudo code is simply an implementation of an algorithm in the form of annotations


and informative text written in plain English. It has no syntax like any of the
programming language and thus can’t be compiled or interpreted by the computer.

Example 1: Write pseudo code that reads two numbers and multiplies them together and print out
their product.
Read num1 , num2
Set multi to num1*num2
Write multi

ALPINE SKI HOUSE 23


 Pseudo Code

Example 2: Write pseudo code that tells a user that the number they entered is not a 5 or a 6.

Read isfive
If(isfive = 5)
Write "your number is 5"
Else if (isfive = 6)
Write "your number is 6"
Else
Write "your number is not 5 or 6"

ALPINE SKI HOUSE 24


 Pseudo Code

Example 2: Write pseudo code that performs the following: Ask a user to enter a number. If the number is between 0 and 10,
write the word blue. If the number is between 10 and 20, write the word red. if the number is between 20 and 30, write the
word green. If it is any other number, write that it is not a correct color option.

Write "Please enter a number"


Read colornum
If (colornum >0 and colornum <= 10)
Write blue
else If (colornum >10 and colornum <= 20)
Write red
else If (colornum >20 and colornum <= 30)
Write green
else
Write "not a correct color option"

ALPINE SKI HOUSE 25


 Pseudo Code

Advantages of Pseudocode
• Improves the readability of any approach. It’s one of the best approaches to start implementation
of an algorithm.
• Acts as a bridge between the program and the algorithm or flowchart. Also works as a rough
documentation, so the program of one developer can be understood easily when a pseudo code is
written out. In industries, the approach of documentation is essential. And that’s where a pseudo-
code proves vital.
• The main goal of a pseudo code is to explain what exactly each line of a program should do, hence
making the code construction phase easier for the programmer.

ALPINE SKI HOUSE 26


 Flow Charts

• Flowchart is a graphical representation of an algorithm. Programmers often use it


as a program-planning tool to solve a problem. It makes use of symbols which are
connected among them to indicate the flow of information and processing.

ALPINE SKI HOUSE 27


Basic Symbols Used in Flowchart Designs

- Start / Stop

- Input/ Output

- Process

- Decision

- Control Flow

- Connector

ALPINE SKI HOUSE 28


 Flowchart Rules

• Flowchart is generally drawn from top to bottom


• All boxes of flowchart must be connected with arrow
• All flowchart start with a terminal or process symbol
• Decision symbol have 2 exit points, one for YES (TRUE) and another for NO
(FALSE)

ALPINE SKI HOUSE 29


Exercise 01 : - Add 10 and 20

Algorithm
1.Initialize sum = 0 (process)
2.Enter the numbers (I/O)
3.Add the numbers and store
the result in sum (process)
4.Print sum (I/O)

ALPINE SKI HOUSE 30


 Flowchart Structures

1) Sequence
2) Selection
3) Repetition

ALPINE SKI HOUSE 31


 Sequence

ALPINE SKI HOUSE 32


 Selection (Decision)

ALPINE SKI HOUSE 33


 Repetition (Loop)

ALPINE SKI HOUSE 34


 Flowchart Structures

pseudo-code for a simple program that calculates the total amount payable when
you include the hourly and hourly rate

Begin
input hours
input rate
pay=hours*rate
print pay
End

ALPINE SKI HOUSE 35


 Software Development Life Cycle (SDLC)

ALPINE SKI HOUSE 36


 Software Development Life Cycle (SDLC)

• The Software Development Life Cycle (SDLC) is a structured process that


enables the production of high-quality, low-cost software, in the shortest possible
production time.
• The goal of the SDLC is to produce superior software that meets and exceeds all
customer expectations and demands.
• It consists of a detailed plan describing how to develop, maintain, replace and
alter or enhance specific software. The life cycle defines a methodology for
improving the quality of software and the overall development process.

ALPINE SKI HOUSE 37

You might also like