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
Program Design Tools:
Algorithms
Flowcharts and Pseudo-codes
Implementation of algorithms.
Basics of Python Programming:
Features of Python
History and Future of Python
Writing and executing Python program
Literal constants
Variables and identifiers
Data Types
Input operation
Comments
Reserved words
Indentation
Operators and expressions
Expressions in Python.
It is a situation which must be overcome by finding out
appropriate applicable solutions OR by achieving expected goals.
Problem solving: The most important skill a
developer needs.
Problem solving is the core thing software
developers do.
The programming languages and tools they use are
secondary to this fundamental skill.
“Problem solving is writing an original program
that performs a particular set of tasks and meets all
stated constraints.” - Think Like a Programmer.
❖ The first step toward solving a problem is to identify the
problem.
❖ In a classroom situation, most problems have been identified
for you and given to you in the form of written assignments or
problems out of a book.
❖ However, when you are doing problem solving outside the
classroom, you need to make sure you identify the problem
before you start solving it.
❖ If you don’t know what the problem is, you cannot solve it.
❖ You must understand what is involved in the problem
before you can continue toward the solution.
❖ This includes understanding the knowledge based of the
person or machine for whom you are solving the problem.
❖ If you are setting up a solution for a person, then you must
know what that person knows.
❖ A different set of instructions might have to be used
depending on this knowledge base.
❖ This list should be as complete as possible.
❖ You might want to talk to other people to find other
solutions than those you have identified.
❖ Alternative solutions must be acceptable ones.
❖ In this step, you need to identify and evaluate the pros and
cons of each possible solution before selecting the best
one.
❖ In order to do this, you need to select criteria for the
evaluation.
❖ These criteria will serve as the guidelines for evaluating
each solution.
❖ These numbered, step-by-step instructions must fall
within the knowledge base set up in step 2.
❖ No instruction can be used unless the individual or the
machine can understand it.
❖ This can be very limiting, especially when working
with computers.
❖ To evaluate or test a solution means to check its result
to see if it is correct, and to see if it satisfies the needs
of the person(s) with the problem.
❖ If the result is either incorrect or unsatisfactory, then
the problem solver must review the list of instructions
to see that they are correct or start the process all over
again.
Algorithmic solution
❖ Problems do not always have straightforward
solutions.
❖ Some problems, such as balancing a checkbook or
baking a cake, can be solved with a series of
actions.
❖ These solutions are called algorithmic solutions.
HEURISTIC SOLUTIONS
❖ The solutions of other problems, such as how to
buy the best stock or whether to expand the
company, are not so straightforward.
❖ These solutions require reasoning built on
knowledge and experience, and a process of trial
and error.
❖ Solutions that cannot be reached through a direct
set of steps are called heuristic solutions.
Problem Definition Phase
Getting started on a problem
The use of specific Examples
Similarities among problems
Working Backwards from the solution
Breaking Problem into sub problem
Choice of suitable data structure
Construction of loops
Establishing initial conditions for loops
Finding the iterative construct
Termination of loops
In Requirements Analysis, user’s expectations are
gathered to know why the software has to be built.
In the Design Phase, a plan of actions is made before
the actual development process can start.
In Implementation phase, the designed algorithms are
converted into program code using any of the high-level
languages.
During Testing, all the modules are tested together to
ensure that the overall system works well as a whole
product.
In Software Deployment, Training, and Support
phase, the software is installed or deployed in the
production environment.
Maintenance and enhancements are ongoing activities
that are done to cope with newly discovered problems
or new requirements
Algorithms
Flowcharts
Pseudo-codes
Implementation of algorithms.
An algorithm provides a blueprint to writing a program to solve a
particular problem. It is considered to be an effective procedure for
solving a problem in a finite number of steps.
Algorithm should be:
Be precise
Be unambiguous
Not even a single instruction must be repeated infinitely.
After the algorithm gets terminated, the desired result must be
obtained.
Top-down approach starts by dividing the complex
algorithm into one or more modules.
Bottom-up approach is just the reverse of top-down
approach. In the bottom-up design, we start with
designing the most basic or concrete modules and
then proceed towards designing higher level modules.
1. Sequence
2. Decision
3. Repetition
Sequence means that each step of the algorithm is
executed in the specified order.
Decision statements are used when the outcome of
the process depends on some condition.
Repetition, which involves executing one or more
steps for a number of times, can be implemented using
constructs such as the while, do-while, and for loops.
A flowchart is a graphical or symbolic representation
of a process. It is basically used to design and
document virtually complex processes to help the
viewers to visualize the logic of the process.
Symbols of Flowchart
They are good communication tools to explain the logic
of a system to all concerned.
They are also used for program documentation.
They act as a guide or blueprint for the programmers to
code the solution in any programming language.
They can be used to debug programs that have error.
Pseudocode is a compact and informal high-level
description of an algorithm that uses the structural
conventions of a programming language.
It facilitates designers to focus on the logic of the
algorithm without getting bogged down by the details
of language syntax.
An ideal pseudocode must be complete, describing
the entire logic of the algorithm, so that it can be
translated straightaway into a programming language.
Write a pseudocode for calculating the price of a
product after adding the sales tax to its original price.
Features of Python
History and Future of Python
Writing and executing Python program
Literal constants
Variables and identifiers
Data Types
Input operation
Comments
Reserved words
Indentation
Operators and expressions
Expressions in Python.
Parallel processing can be done in Python but not as elegantly as done in
some other languages .
Being an interpreted language, Python is slow as compared to C/C++.
Python is not a very good choice for those developing a high-graphic 3d
game that takes up a lot of CPU.
It lacks true multiprocessor support.
Python is slower than C or C++ when it comes to computation heavy tasks
and desktop applications.
It is difficult to pack up a big Python application into a single executable
file.
Step 1: Open an editor.
Step 2: Write the instructions
Step 3: Save it as a file with the filename having the extension .py.
Step 4: Run the interpreter with the command python
program_name.py or use IDLE to run the programs.
To execute the program at the command prompt, simply change your
working directory to C:\Python34 (or move to the directory where
you have saved Python) then type python program_name.py.
If you want to execute the program in Python shell, then just press F5
key or click on Run Menu and then select Run Module.
Program to print Hello Python.
To take input from the users, Python makes use of
the input() function.
The input() function prompts the user to provide
some information on which the program can work
and give the result.
However, we must always remember that the input
function takes user’s input as a string.
Reserve Words
Identifiers and Variables
Literals Constants
Data Types
Comments
Indentation
Operators
These are also known as key words.
These are the words whose meaning is predefined by
Python interpreter and cannot be used like general
purpose words.
Keywords cannot be used as variable name or
function name or as a identifier.
There are 33 reserve words available in python some
of them are : False, True, if, else, elif, while, For
break, pass, continue, def, class, is, is not, return,
in, not in, and, or, import, global, form
Variable:
Variable is a name that is used to refer to memory
location. Python variable is also known as an identifier
and used to hold value.
In Python, we don't need to specify the type of variable
because Python is a infer language and smart enough to
get variable type.
Variable names can be a group of both the letters and
digits, but they have to begin with a letter or an
underscore.
• It is recommended to use lowercase letters for the
variable name.
• Rahul and rahul both are two different variables.
Example:
Number=10
Num=12.5
Name=“Ram”
• When a variable is defined, we must create it with a
value.
• If the value is not assigned the variable gives an error
stating that variable is not defined.
Variables are the example of identifiers.
An Identifier is used to identify the literals used in the
program.
The rules to name an identifier are given below.
The first character of the variable must be an alphabet
or underscore ( _ ).
All the characters except the first character may be an
alphabet of lower-case(a-z), upper-case (A-Z),
underscore, or digit (0-9).
Identifier name must not contain any white-space, or
special character (!, @, #, %, ^, &, *).
Identifier name must not be similar to any keyword
defined in the language.
Identifier names are case sensitive;
for example, my name, and MyName is not the same.
Examples of valid identifiers: a123, _n, n_9, etc.
Examples of invalid identifiers: 1a, n%4, n 9, etc.
In Python, programmers need not explicitly declare
variables to reserve memory space.
The declaration is done automatically when a value is
assigned to the variable using the equal sign (=).
The operand on the left side of equal sign is the name
of the variable and the operand on its right side is the
value to be stored in that variable.
Python Literals can be defined as data that is
given in a variable or constant.
The value of a literal constant can be used
directly in programs.
For example, 7, 3.9, 'A', and "Hello" are
literal constants.
Python supports the following literals:
String literals
Numeric literals
Boolean literals
Special literals
Literal Collections
String literals can be formed by enclosing a text in the
quotes. We can use single, double as well as triple
quotes to create a string.
Example:-"Aman" , '12345‘, ’ ’ ’Hello’ ’ ’
Types of Strings:
There are two types of Strings supported in Python:
a) Single-line String- Strings that are terminated within
a single-line are known as Single line Strings.
Example: text1='hello'
b) Multi-line String - A piece of text that is written in
multiple lines is known as multiple lines string.
There are two ways to create multiline strings:
1) Adding black slash at the end of each line.
Example:
text1='hello\
Students'
print(text1)
Output: 'hellostudents'
2) Using triple quotation marks:-
Example:
str2='''welcome
to
PICT'''
print (str2 )
Output:
Welcome
to
PICT
Numbers refers to a numeric value. You can use four types of
numbers in Python program- integers, long integers, floating point
and complex numbers.
Numbers like 5 or other whole numbers are referred to as
integers. Bigger whole numbers are called long integers. For
example, 535633629843L is a long integer.
Numbers like are 3.23 and 91.5E-2 are termed as floating point
numbers.
Numbers of a + bi form (like -3 + 7i) are complex numbers.
x = 1000
y =1234599668544
#Float Literal
float_1 = 100.5
#Complex Literal
a = 5+3.14j
print(x, y)
print(float_1)
print(a)
Output:
1000 1234599668544 100.5 (5+3.14j)
A Boolean literal can have any of the two values: True or False.
myVar=True myVar1=False
Example - Boolean Literals
x = (1 == True)
y = (2 == False)
a = True + 10
b = False + 10
print("x is", x)
print("y is", y)
print("a:", a)
print("b:", b)
Output:
x is True y is False a: 11 b: 10
Python contains one special literal i.e., None.
None is used to specify to that field that is not created.
It is also used for the end of lists in Python.
Example - Special Literals
val1=10
val2=None
print(val1)
print(val2)
Output:
10 None
Python provides the four types of literal
collection:
1. List literals
2. Tuple literals
3. Dict literals
4. Set literals.
List contains items of different data types. Lists are mutable.
The values stored in List are separated by comma(,) and
enclosed within square brackets([]).
We can store different types of data in a List.
Example - List literals
list=['John',678,20.4,'Peter']
list1=[456,'Andrew']
print(list)
print(list + list1)
Output:
['John', 678, 20.4, 'Peter']
['John', 678, 20.4, 'Peter', 456, 'Andrew']
Python dictionary stores the data in the key-value
pair.
It is enclosed by curly-braces {} and each pair is
separated by the commas(,).
Example
dict = {'name': 'Pater', 'Age':18,'Roll_nu':101}
print(dict)
Output:
{'name': 'Pater', 'Age': 18, 'Roll_nu': 101}
Python tuple is a collection of different data-type.
It is immutable which means it cannot be modified
after creation.
It is enclosed by the parentheses () and each element
is separated by the comma(,).
Example
tup = (10,20,"Dev",[2,3,4])
print(tup)
Output:
(10, 20, 'Dev', [2, 3, 4])
Python set is the collection of the unordered
dataset.
It is enclosed by the {} and each element is
separated by the comma(,).
Example: - Set Literals
set = {'apple','grapes','guava','papaya'}
print(set)
Output:
{'guava', 'apple', 'papaya', 'grapes'}
Number stores numeric values. The integer, float, and
complex values belong to a Python Numbers data-
type.
Python provides the type() function to know the data-
type of the variable.
int - All the numbers without fraction part (Example -
10). For int, there is no upper limit.
float - All the numbers with a fraction part (Example
- 10.5). It’s accurate up to 15 decimal places
complex - All the numbers with real and imaginary
parts (Example - 5 + 10j).
a=5
print("The type of a", type(a))
b = 40.5
print("The type of b", type(b))
c = 1+3j
print("The type of c", type(c))
Output:
The type of a <class 'int'>
The type of b <class 'float'>
The type of c <class 'complex'>
String
The string can be defined as the sequence of characters
represented in the quotation marks.
In Python, we can use single, double, or triple quotes to
define a string.
String handling in Python is a straightforward task since
Python provides built-in functions and operators to perform
operations in the string.
In the case of string handling, the operator + is used to
concatenate two strings as the operation "hello"+"
python" returns "hello python".
The operator * is known as a repetition operator as the
operation "Python" *2 returns 'Python Python'.
str = "string using double quotes"
print(str)
s = '''A multiline
string'''
print(s)
Output:
string using double quotes
A multiline
string
Python Lists are similar to arrays in C. However, the
list can contain data of different types.
The items stored in the list are separated with a
comma (,) and enclosed within square brackets [].
We can use slice [:] operators to access the data of the
list.
The concatenation operator (+) and repetition
operator (*) works with the list in the same way as
they were working with the strings.
a = [5,10,15,20,25,30,35,40]
Print(a)
b=[5,10.5678,”HELLO”]
Print(b)
Output-
[5,10,15,20,25,30,35,40]
[5,10.5678,”HELLO”]
A tuple is similar to the list in many ways.
Like lists, tuples also contain the collection of the
items of different data types.
The items of the tuple are separated with a comma
(,) and enclosed in parentheses ().
A tuple is a read-only data structure as we can't
modify the size and value of the items of a tuple.
tup = ("hi", "Python", 2)
# Checking type of tup
print (type(tup))
#Printing the tuple
print (tup)
Output-
<class 'tuple'>
('hi', 'Python', 2)
Dictionary is an unordered set of a key-value pair
of items.
It is like an associative array or a hash table where
each key stores a specific value.
Key can hold any primitive data type, whereas
value is an arbitrary Python object.
The items in the dictionary are separated with the
comma (,) and enclosed in the curly braces {}.
d = {1:'Jimmy', 2:'Alex', 3:'john', 4:'mike'}
# Printing dictionary
print (d)
Output-
{1: 'Jimmy', 2: 'Alex', 3: 'john', 4: 'mike'}
Boolean type provides two built-in values, True and
False.
These values are used to determine the given
statement true or false.
It denotes by the class bool.
True can be represented by any non-zero value or
'T' whereas false can be represented by the 0 or 'F'
Python Set is the unordered collection of the data
type.
It is iterable, mutable(can modify after creation),
and has unique elements.
In set, the order of the elements is undefined; it may
return the changed sequence of the element.
The set is created by using a built-in
function set(), or a sequence of elements is passed
in the curly braces{} and separated by the comma.
It can contain various types of values.
Comments are the non-executable statements in a program. They
are just added to describe the statements in the program code.
Comments make the program easily readable and understandable
by the programmer as well as other users who are seeing the
code. The interpreter simply ignores the comments.
In Python, a hash sign (#) that is not inside a string literal begins
a comment.
All characters following the # and up to the end of the line are
part of the comment.
Single Line Comment
Multi Line Comment
We must use the hash(#) at the beginning of every line of
code to apply the multiline Python comment.
We can also use the triple quotes ('''''') for multiline
comment.
Consider the following example.
# First line of the comment
# Second line of the comment
# Third line of the comment
Whitespace at the beginning of the line is called
indentation.
These whitespaces or the indentation are very
important in Python.
In a Python program, the leading whitespace
including spaces and tabs at the beginning of the
logical line determines the indentation level of that
logical line.
Python uses 4 spaces as default indentation spaces.
However, the number of spaces can be anything, it is up to
the user. But a minimum of one space is needed to indent a
statement.
The first line of python code cannot have Indentation.
Indentation is mandatory in python to define the blocks of
statements.
The number of spaces must be uniform in a block of code.
It is preferred to use whitespaces instead of tabs to indent in
python. Also, either use whitespace or tabs to indent,
intermixing of tabs and whitespaces in indentation can
cause wrong indentation errors.
Indentation of code leads to better readability,
although the primary reason for indentation in
python is to identify block structures.
Missing { and } errors that sometimes popup in
c,c++ languages can be avoided in python, also the
number of lines of code is reduced.
Operators are used to process data.
The operator can be defined as a symbol which is
responsible for manipulating two operands.
Operators are used to perform operations on
variables and values.
Operators are the basic building blocks of a
program on which the logic is built in a particular
programming language.
Python provides a variety of operators described as
follows.
• The logical operators are used primarily in the expression
evaluation to make a decision.
• Python supports the following logical operators
Identity operators are used to compare the objects, not if
they are equal, but if they are actually the same object,
with the same memory location:
If an expression consisting of multiple operators then
result of such expression is calculated using operator
precedence.
Which operator evaluates first can be confusing. So
we have some rules for this too.
It gives hierarchy in which these operators will be
processed.
An expression is any legal combination of symbols
(like variables, constants and operators) that
represents a value.
In Python, an expression must have at least one
operand (variable or constant) and can have one or
more operators.
On evaluating an expression, we get a value.
Operand is the value on which operator is applied.
Constant Expressions: One that involves only constants.
Example: 8 + 9 – 2
Integral Expressions: One that produces an integer result
after evaluating the expression. Example: a = 10
Floating Point Expressions: One that produces floating
point results. Example: a * b / 2
Relational Expressions: One that returns either true or
false value. Example: c = a>b
Logical Expressions: One that combines two or more
relational expressions and returns a value as True or
False. Example: a>b && y! = 0
Bitwise Expressions: One that manipulates data at bit
level. Example: x = y&z
Assignment Expressions: One that assigns a value to a
variable. Example: c = a + b or c = 10
THANK YOU!!