100% found this document useful (1 vote)
14 views19 pages

Unit 1

The document provides an overview of problem-solving concepts, strategies, and Python programming basics. It outlines the steps in problem-solving, types of problems, and the role of computers in solving these problems, along with an introduction to Python's features, history, and future. Additionally, it covers the software development life cycle and the writing and execution of Python programs.

Uploaded by

chinmainair41
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
14 views19 pages

Unit 1

The document provides an overview of problem-solving concepts, strategies, and Python programming basics. It outlines the steps in problem-solving, types of problems, and the role of computers in solving these problems, along with an introduction to Python's features, history, and future. Additionally, it covers the software development life cycle and the writing and execution of Python programs.

Uploaded by

chinmainair41
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

Department of First Year Engineering

Programming and Problem Solving


Unit- 1 Notes

Unit I: Problem Solving, Programming and Python Programming


General Problem Solving Concepts- Problem solving in everyday life, types of problems, problem
solving with computers, difficulties with problem solving, problem solving aspects, top down
design. Problem Solving Strategies,
Basics of Python Programming: Features of Python, History and Future of Python, Programming
Paradigm, Features of Object Oriented Programming, Applications of Python Languages.

1.1 General Problem Solving Concepts

1.1.1 Problem Solving in Everyday Life:

Q1. Define Problem. What are steps in problem solving? Definition of problem
Problem is defined as a situation or issue or condition which needs to be solve to achieve a specific
goal.
Steps in problem solving

1. Identify the problem - You need to be clear about what exactly the problem is. If the problem is
not identified then it cannot be solved. example- when you want to learn a language then first
identify which language do you want to learn
2. Understand the problem - Here problem should be defined properly so that it helps in
understanding the problem more. It helps to understand knowledge base of the problem. It helps in
finding a target for what you want to achieve.
3. Identify alternative ways to solve the problem - This step is very crucial because until and unless
we explore all options, finding a correct solution is next to possible. This involves the process to
create a list of solutions.
4. Select the best way to solve the problem - Identify and evaluate pros and cons of each possible

1 Unit I: Problem Solving, Programming and Python Programming FE PPS 2024


solution and then select the best solution. Selecting proper way from alternatives require a lot of
study and need to study con- sequences as well.
5. List instructions that enable you to solve the problem using selected solution - Here we need
to list out the steps which should be followed so that anyone can solve the problem. This should be in
detail and precise at the same time.
6. Evaluate the solution - It mean to test the solution and check if the result is satisfactory or not.

Example: I am always late for college


(i) Identification: getting late for college.
(ii) Understanding: (List out probable reasons of or
getting late) (Sleeping late, waking up late,
Transportation, etc.)
(iii) List out alternative ways – (Solutions)
(a). Sleeping early at home.
(b). Waking up early.
(c). Waking alarm.
(d). Resolving travelling issue.
(iv) Select the best way – Sleeping early in night.
(v) List out the procedure
(a). Having dinner as early as possible.
(b). Avoid use of mobile phones in night.
(c). Completing homework as early as possible.
(d). Then going early to bed.
(vi) Evaluate the solution: Reached College on time.

1.1.2 Types of problems:


Q. 2. Explain types of problems in detail? Ans:
Problem can be categorized into two types depending on how the solution is found out or the
approach which is followed to get that solution.
(i) Algorithmic solution:
a) These problems are solved by following some procedure or steps.

2 Unit I: Problem Solving, Programming and Python Programming FE PPS 2024


b) For example: baking a cake. It can be done by following a series of instruction And we
can expect results.
c) Here following some predefined process needs to be done.
d) Here solution is quite straight forward.
(ii) Heuristic solution:
(a) These types of problems solved using knowledge based i.e. by collecting information about the
problem.
(b) For example: Expanding one’s business.
(c) In this type of approach there is no straight forward defined path which we can follow to solve a
problem.
(d) We need to build that path based on trial and error.
(e) In this approach experience and knowledge is very important.

1.1.3 Problem Solving with computers:


Q. 3. Write a note on Problem solving with computers.
Computer is a multi-purpose electronic machine which is used for storing, organizing
and processing the data.
Computer is not able to find the solution by itself; rather the computer is a machine which
follows the instruction given by programmer to solve any problem.
So all the efforts required to understand the problem and define procedure to solve that problem
are taken by programmer ( a human being).

Problem Definition:
Before a program is written for solving a problem, it is important to define the problem
clearly.
Define problem: Problem is defined as a situation or issue or condition which needs to
solve to achieve the goal.
For most of the software projects, the system analyst approach system users to collect user
requirements and define the problem that the system aims to solve.
System analyst typically looks at following issues:
o What input is required for achieving expected output?

3 Unit I: Problem Solving, Programming and Python Programming FE PPS 2024


o Expected output of the problem.
o Current method of solving the problem.
o Can the problem or part of the problem be more effectively solved by a software solution?
o Computers are built to deal with algorithmic solutions, which are often difficult or very time
consuming for humans.
Solution:
o Solution means the instructions listed during problem solving – the instructions that must be
followed to produce the best results.
o The result may be: More efficient, faster, more understandable or reusable.
Results:
o The result is outcome or the completed computer assisted answer.
o May take any form: Printout, updated files, output to monitor speakers, etc.
Program:
o Program is the set of instructions that make up the solution after they have been coded into a
particular computer language.

1.1.4 Difficulties with problem solving:


Q.4. Explain the difficulties with Problem solving?
o Some people may not have got training regarding how to solve problems.
o Some may not able to make a decision for fear it will not be the correct one.
o There may be inaccuracy in defining the problem correctly or may not generate the sufficient list
of alternatives.
o While selecting best alternative, they may skip better alternative due to urgency.
o The problem solving process is difficult. It takes practice as well as time to perfect, but in the long
run the process proves to be of great benefit.
o Illogical sequencing of instructions: In the process of problem solving on the computer, one of the
most complicated jobs for the problem solver is writing the instructions. Example: Consider a task
to find which number is the maximum from a set of three numbers. Near about everyone is
immediately able to tell which is the largest but several of them are unable to explain the steps
they followed to get the result. Computer is nothing but a machine that will perform only task that
the user can explain.

4 Unit I: Problem Solving, Programming and Python Programming FE PPS 2024


1.1.5 Problem Solving Aspects:
Q5. What is modularization? Explain different approaches to design an algorithm.
Modularization:
For a complex problem, its algorithm is often divided into smaller units called modules. This
process of dividing an algorithm into modules is called as modularization.

Top down design approach:


 A top-down design approach starts by dividing the complex algorithm into one or more functions.
 These functions can further be decomposed into one or more sub-functions, and this process of
decomposition is iterated until the desired level of module complexity is achieved.
 Top-down design method is a form of stepwise refinement where we begin with the topmost
module and incrementally add functions that it calls.
 Therefore, in a top-down approach, we start from an abstract design and then at each step, this
design is refined into more concrete levels until a level is reached that requires no further
refinement.
Diagram: Top Down Approach

Bottom up design approach:


 It is just reverse of top down approach.
 In bottom up design, we start with designing the most basic or concrete modules and then
proceed towards designing higher level modules.
 The higher level modules are implemented by using the operations performed by lower level
modules.

5 Unit I: Problem Solving, Programming and Python Programming FE PPS 2024


 Thus, in this approach sub-modules are grouped together to form higher level module.
 All the higher level modules are clubbed together to form even higher level modules.
 This process is repeated until the design of the complete algorithm is obtained.

Difference between Top down and Bottom up approach:


Top-Down Approach Bottom-Up Approach

Divides a problem into smaller units and then Starts from solving small modules and
solve it. adding them up together.

This approach contains redundant information. Redundancy can easily be eliminated.

Communication among steps


A well-established communication is not required. is mandatory.

Works on the concept of data-hiding


The individual modules are thoroughly analysed. and encapsulation.

Structured programming languages such as C use OOP languages like C++ and Java,
top-down approach. etc. uses bottom-up mechanism.

The modules must be related for


Relation among modules is not always required. better communication and work flow.

Primarily used in code implementation, test case


generation, debugging and module documentation. Finds use primarily in testing.

1.1.6 Problem Solving Strategies:


Q.5. Explain the problem solving Strategies? Ans.
Computer is a very powerful and versatile machine and can do any task given to it provided that
the programmer has already fed correct instructions to direct what, how and when the steps have
to be done to solve a particular problem. For this he should work to develop a step by step solution
to problem. These steps can be given as:
 Clearly define the problem in very simple and precise language.

6 Unit I: Problem Solving, Programming and Python Programming FE PPS 2024


 Analyse the problem to find out different ways to solve problem and decide the best possible
solution.
 Define the selected solution in a detailed step by step manner.
 Write the steps in a particular programming language so that it can be executed by the computer.
The entire software development process is divided into a number of phases where each phase
performs a well-defined task. Moreover, the outputof one phaseprovides the input for subsequent
phase. The phases in the software development life cycle (SDLC) process are summarized as
follows:

Fig: Software Development Life cycle (SDLC)

 Requirement Analysis: In this phase, the user’s expectations are gathered to know why the
software has to be built. The gathered requirements are analyzed to decide scope of software. The
last activity in this phase includes documenting every identified requirement of the users in order
to avoid any doubts regarding functionality of software. The functionality, capability, performance
and availability of hardware and software components are all analyzed in this phase.
 Design: The requirements documented in previous phase act as an input to the design phase. In
the design phase, core structure of the software is broken down into modules. The solution of the
program is specified for each module in the form of algorithms or flowcharts.
 Implementation: In this phase, the designed algorithms are converted into program code using

7 Unit I: Problem Solving, Programming and Python Programming FE PPS 2024


any of the high level languages. The particular choice of language will depend on the type of
program, such as whether it is system or application program. This phase is also called as
construction phase as the code of the software is generated in this phase. While constructing the
code, the development team checks whether the software is compatible with available hardware
that are mentioned in requirement specification document.
 Testing: In this phase all modules are tested together to ensure that the overall system works well
as a whole product. In this phase the software is tested using a large number of varied inputs, also
known as test data, to ensure the software is working as expected by user’s requirements.
 Software development, training and support: After the code is tested and the software or the
program has been approved by the users, it is installed or deployed in the production environment.
In this phase it becomes very crucial to have training classes for users of software.

 Maintenance: Maintenance and enhancement are ongoing activities that are done to cope with
newly discovered problems or new requirements. Such activities may take a long time to complete
as the requirement may call for the addition of new code that does not fit original design or an extra
piece of code, required to fix an unforeseen problem.

1.2 BASIC PYTHON PROGRAMMING

1.2.1 Features of Python


Q.15. Explain various features of python. Ans.
1. Simple: Python is simple and small language. Reading a program written in python feels
like reading English.
2. Easy to Learn: Python programming is clearly defined and easily readable.
3. Versatile: Python supports wide variety range of applications ranging from text processing to
Web applications
4. Free and Open Source: Python software is freely available on internet
5. High Level Language: Programmers don’t have to worry about low level details
like managing memory used by program etc.
6. Interactive: Programs in python works in interactive mode

8 Unit I: Problem Solving, Programming and Python Programming FE PPS 2024


7. Portable (Platform Independent): Python is portable language. Program can work similarly
on different platform like window, Linux, Macintosh, Solaris etc.
8. Object Oriented: Python supports Object oriented style of programming like C++, Java
languages. It support features like C class, Objects, Inheritance etc.
9. Interpreted: Python is processed at run time by the interpreter. So no need to compile program
before executing it.
10. Scalability: Python support modular programming, so programmer can add module at any
stage of the program
11. Secure: Python programming environment is secure from tempering

1.2.2 History and Future of Python


 Python is a general purpose interpreted interactive object oriented and high level programming
language.
 It was first introduced in 1991 by Guido van Rossum, a Dutch computer programmer.
 The language places strong emphasis on code reliability and simplicity so that the
programmers can develop applications rapidly.
 Python is multi-paradigm programming language, which allows user to code in several
different programming styles.
 Python supports cross platform development and is available through open source. Python
is widely used for scripting in Game menu applications effectively

1.2.3 History of Python:


 Python is created by Guido Van Rossum in the 1980s.
 Rossum published the first version of Python code (0.9.0) in February 1991 at the CWI
(Centrum Wiskunde & Informatics) in the Netherlands , Amsterdam.
 Python is derived from ABC programming language, which is a general-purpose
programming language that had been developed at the CWI.
 Rossum chose the name "Python", since he was a big fan of Monty Python's Flying
Circus.
 Python is now maintained by a core development team at the institute, although Rossum

9 Unit I: Problem Solving, Programming and Python Programming FE PPS 2024


still holds a vital role in directing its progress.

1.2.4 Future of Python:


 Python’s user base is vast and growing – it’s not going away any time soon.
 Utilized by the likes of Nokia, Google, and even NASA for it’s easy syntax, it looks
to have a bright future ahead of it supported by a huge community of OS developers.
 Its support of multiple programming paradigms, including object-oriented Python programming,
functional Python programming, and parallel programming models makes it a highly adaptive
choice – and its uptake keeps growing.

1.2.5 Writing and Executing Python Programs


 Open IDLE.
 Go to File > New.
 Write Python code in the file.
 Then save the file with .py extension. For example, hello.py, example.py etc. You can give any
name to the file. However, the file name should end with .py
 Run the program.

1.3 Data Types


Q. Explain different data types supported by python? Ans:
1. Numbers
 They are defined as int, float and complex class in Python.
o int: It consists of all integer numbers. Integers can be either positive or negative. Buil in size of
integers is unlimited. Ex: -5, 0, 890 etc
o float: Float point numbers are written as decimal numbers separated by decimal point. It is
expressed in the form of integer number and fractional part sepaprated by decimal point. Ex: -
5.2, 11.325 etc.
o complex: Complex numbers are expressed in the form of a+bi, where a is real part and b is
imaginary part. Ex: 3+2i etc

10 Unit I: Problem Solving, Programming and Python Programming FE PPS


2024
2. String
 String is sequence of Unicode characters.
 We can use single quotes or double quotes to represent strings.
 Multi-line strings can be denoted using triple quotes, ''' or """.
>>>s = "This is a string"
>>>s = '''a multiline
3. Python List
 List is an ordered sequence of items.
 List represents collection of data elements (items).
 These data items can be of different data types.
 Items in list are separated by commas are enclosed within brackets [ ].
 Ex: a = [1, 2.2, 'python']

4. Python Tuple
 Tuple is an ordered sequence of items same as list.The only difference is that tuples are
immutable. Tuples once created cannot be modified.
 It is defined within parentheses () where items are separated by commas.
 Ex: t = (5,'program', 1+3j)

5. Dictionary
 Dictionary is an unordered collection of items in key:value pair of any type. In dictionary values
are separated by comma inside braces { }. ·
 In dictionary, key should be unique.
 Ex: a = {‘name’:’Bob’,’Age’: 21}

1.4 Input and Output operation


input() and print() functions are widely used for standard input and output operations
respectively.
1. input()

11 Unit I: Problem Solving, Programming and Python Programming FE PPS


2024
To allow flexibility we might want to take the input from the user. In Python, we have the input()
function to allow this.
Syntax:

input (expression)

2. print()
We use the print() function to output data to the standard output device (screen). print()
evaluates the expression before printing it on the monitor. Print statement outputs an entire
(complete) line and then goes to next line for subsequent output (s). To print more than one item
on a single line, comma (,) may be used.
Syntax:

print (expression/constant/variable)

Example:

x = input("Enter any value: ")


# printing value
print("Entered value is: ", x)

1.5 Expressions in Python


 An expression is any valid combination of symbols like variables, constants and operators that
represents a value.
 In python, an expression must have at least one operand and can have one or more operators.

 An Example of valid expression:


o A+B*C-5
o X=A/B
o Y=A^B

 An example of invalid expression:


12 Unit I: Problem Solving, Programming and Python Programming FE PPS
2024
o A+
o A*
 Python supports different types of expressions can be classified as:
1. Based on the position of operators in an expression:
 Infix expression:
 Example: a=b-c
 Prefix expression:
 Example: a=+bc
 Postfix expression:
 Example: a=bc+

2. Based on the data type of the result obtained on evaluating an expression:


 Constant expression:
 Example: 8+2-3
 Integral expression:
 Example: a=10
 Relational expression:
 Example: c=a>b
 Logical expression:
 Example: a>b and a>c

1.6 Programming Paradigms in Python


Paradigm can also be termed as a method to solve some problems or do some tasks. A
programming paradigm is an approach to solve the problem using some programming language
or also we can say it is a method to solve a problem using tools and techniques that are available
to us following some approach. There are lots of programming languages that are known but all
of them need to follow some strategy when they are implemented and this methodology/strategy
is paradigms. Apart from varieties of programming languages, there are lots of paradigms to
fulfill each and every demand.
Python supports three types of Programming paradigms
 Object Oriented programming paradigms

13 Unit I: Problem Solving, Programming and Python Programming FE PPS


2024
 Procedure Oriented programming paradigms
 Functional programming paradigms
Object Oriented programming paradigms
In the object-oriented programming paradigm, objects are the key element of paradigms. Objects
can simply be defined as the instance of a class that contains both data members and the method
functions. Moreover, the object-oriented style relates data members and methods functions that
support encapsulation and with the help of the concept of an inheritance, the code can be easily
reusable but the major disadvantage of object-oriented programming paradigm is that if the code
is not written properly then the program becomes a monster.
Advantages
 Relation with Real world entities
 Code reusability
 Abstraction or data hiding
Disadvantages
 Data protection
 Not suitable for all types of problems
 Slow Speed
Example:

# class Emp has been defined here


class Emp:
def __init__(self, name, age):
self.name = name
self.age = age

def info(self):
print("Hello, % s. You are % s old." % (self.name, self.age))

# Objects of class Emp has been


# made here
Emps = [Emp("John", 43),

14 Unit I: Problem Solving, Programming and Python Programming FE PPS


2024
Emp("Hilbert", 16),
Emp("Alice", 30)]

# Objects of class Emp has been


# used here
for emp in Emps:
emp.info()

Output:
Hello, John. You are 43 old.
Hello, Hilbert. You are 16 old.
Hello, Alice. You are 30 old.

Procedural programming paradigms


In Procedure Oriented programming paradigms, series of computational steps are divided modules
which means that the code is grouped in functions and the code is serially executed step by step so
basically, it combines the serial code to instruct a computer with each step to perform a certain
task. This paradigm helps in the modularity of code and modularization is usually done by the
functional implementation. This programming paradigm helps in an easy organization related
items without difficulty and so each file acts as a container.
Advantages
 General-purpose programming
 Code reusability
 Portable source code
Disadvantages
 Data protection
 Not suitable for real-world objects
 Harder to write
Example:

15 Unit I: Problem Solving, Programming and Python Programming FE PPS


2024
# Procedural way of finding sum
# of a list

mylist = [10, 20, 30, 40]

# modularization is done by
# functional approach
def sum_the_list(mylist):
res = 0
for val in mylist:
res += val
return res

print(sum_the_list(mylist))

Output:
100

Functional programming paradigms


Functional programming paradigms is a paradigm in which everything is bind in pure
mathematical functions style. It is known as declarative paradigms because it uses declarations
overstatements. It uses the mathematical function and treats every statement as functional
expression as an expression is executed to produce a value. Lambda functions or Recursion are
basic approaches used for its implementation. The paradigms mainly focus on “what to solve”
rather than “how to solve”. The ability to treat functions as values and pass them as an argument
make the code more readable and understandable.
Advantages
 Simple to understand
 Making debugging and testing easier
 Enhances the comprehension and readability of the code
Disadvantages
16 Unit I: Problem Solving, Programming and Python Programming FE PPS
2024
 Low performance
 Writing programs is a daunting task
 Low readability of the code
Example:

# Functional way of finding sum of a list


import functools

mylist = [11, 22, 33, 44]

# Recursive Functional approach


def sum_the_list(mylist):

if len(mylist) == 1:
return mylist[0]
else:
return mylist[0] + sum_the_list(mylist[1:])

# lambda function is used


print(functools.reduce(lambda x, y: x + y, mylist))

Output:
110

1.7 Features of Object-Oriented Programming (OOP)


Python fully supports OOP, which helps in creating structured and modular programs:
1. Encapsulation:
It bundles data (variables) and methods (functions) into a single unit called a class. Access to the
data can be controlled using access specifiers (public, private, protected).
Example: Protecting variables using getter and setter methods.
2. Inheritance:
17 Unit I: Problem Solving, Programming and Python Programming FE PPS
2024
A mechanism where a new class (child class) can reuse the properties and methods of an existing
class (parent class). It reduces code redundancy.
Example: A Car class inheriting properties of a Vehicle class.
3. Polymorphism:
The ability of a function or method to behave differently based on the object it acts upon.
Example: A method draw() can work for both Circle and Square objects.
4. Abstraction:
Hides implementation details and exposes only the necessary features. It is achieved using
abstract classes and methods.
Example: A Payment class with methods process_payment() implemented differently for
CreditCard and PayPal.

1.8 Applications of Python


1. Web Development:
o Python frameworks like Django, Flask, and Bottle simplify the development process.
o Offers features like amazing visualization, enhanced security, and a fast development process.
2. Machine Learning & Artificial Intelligence (AI):
o Facilitates the creation of AI/ML algorithms with libraries like NumPy, Keras, and SciPy.
o Provides simple, readable code for developing complex algorithms.
3. Data Science:
o Supports data collection, sorting, analysis, and visualization.
o Libraries like TensorFlow, Pandas, and Scikit-learn streamline the process for professionals.
4. Game Development:
o Used in games like Battlefield 2 and Pirates of the Caribbean.
o Libraries like Pygame, Panda3D, and Cocos2D make 2D/3D game development effortless.
5. Audio and Visual Applications:
o Powers platforms like Netflix, Spotify, and YouTube.
o Libraries such as Dejavu, Pyo, SciPy, and OpenCV support multimedia applications.
6. Software Development:
o Ideal for creating software due to features like platform independence, enhanced readability, and
high compatibility.
18 Unit I: Problem Solving, Programming and Python Programming FE PPS
2024
o Used in popular applications like Google, Netflix, and Reddit.
7. CAD Applications:
o Used in computer-aided design with tools like Blender, FreeCAD, and Open Cascade.
o Helps create 2D/3D models for architects and product designers with high precision.
8. Business Applications:
o Features excellent security and scalability for high-performance apps.
o Tools like Odoo and Tryton automate business processes like accounting, CRM, and sales.
9. Desktop GUI Applications:
o Python’s interactive nature and tools like PyQt, Kivy, and wxWidgets simplify GUI development.
10. Web Scraping:
o Automates data extraction from websites using tools like Pandas, Matplotlib, and Selenium.
o Offers a concise syntax for enhanced readability and faster implementation.

19 Unit I: Problem Solving, Programming and Python Programming FE PPS


2024

You might also like