Professional Documents
Culture Documents
UNIT-III-PART-B
UNIT-III-PART-B
in/
GE8151 PROBLEM SOLVING AND PYTHON
PROGRAMMING
Problem Solving and Python Programming
Unit – III
/
in
Control Flow Functions
n.
PART-B
aa
Boolean Expressions:
riy
It is an expression that is either true or false
o
True
.p
True
w
>>> 5 = = 6 Print (5 = = 6)
w
False False
//w
The examples use the operator = = which comparies two operands and produces
True if they are equal, and False otherwise. True and False are special values the
s:
O/P < type ‘bool’ > O/P < type ‘bool’ >
The condition in a selection statement often takes the form of a comparison. The
result of comparison is the Boolean value (either True or False)
eg >>> 4! = 4 False
https://www.poriyaan.in/ https://eee.poriyaan.in/
>>> 4 > = 3 True
/
In order to write useful programs, we almost need the ability to check conditions and
in
change the behaviour af the program accordingly conditional statements give us this
n.
ability.
aa
Syntax af if statement: Syn:
iy
< Sequence of statements
or
>
Print ‘x is positive ‘
.p semantics:
w
w
//w
s:
Or
tp
in the syntax
If x > 0:
The Boolean expression after the if is condition if it is true then the indented
statement gets executed if not nothing happens
O/P
/
The second form of the if statement is alternative execution in which there are two
in
possibilities and the condition determines which one gets executed. The syntax is
n.
if < condition >:
aa
< Sequence of statements – 1 >
Else:
riy
< Sequence of statements – 2 >
Semantics:
o
.p
w
w
w
s ://
tp
eg
ht
if ∩ % 2 = = 0: O/P
else:
Since the condition must be true or false exactly one of the alternatives will be
executed. The alternatives are called branches.
Multiway if statements:
Sometimes there are more than two possibilities and we need more than two
branches. One way to express a computation like that is chained conditional
/
n .in
aa
riy
O/P
o
.p
eg: x = int (input (“Enter x value “ ) Enter x value 10
w
y = int (input (“Enter y value “ ) Enter y value 5
w
X is greater than y
// w
s:
tp
ht
Nested Conditionals:
One conditional can also be nested with in another. We could have conitten
the syntax like this.
https://www.poriyaan.in/ https://eee.poriyaan.in/
/
in
n.
aa
Greatest among 2 numbers pgm to check two number is equal or greater or lesser.
iy
x = int (input (“x value “ )
or
y = int (input (“y value “ ) .p
w
w
//w
s:
tp
ht
The outer conditional contains 2 branches. The first branch contains simple
statements and second branch contains another if statements. Which has two
branches of its own.
/
in
n.
aa
iy
Iterators (Looping)
or
In some situation there is a need to repeat a set of instructions in specified numbers
.p
of times or until a particular condition is satisfied. The repetitive operations are done
through a loop control structure.
w
w
Loop: is defined as the block of statements which are repeatedly executed for
certain number of times.
// w
Steps:
Types:
While loop
For loop
/
Syn: While < Condition >:
in
< Sequence of statements >
n.
aa
iy
or
.p
w
Eg pgm to find the sum of n numbers using while – loop
w
>>> i = 1
//w
>>> Sum = 0
s:
>>> n = 10
tp
For loop: is another repetitive control structure and is used to execute the set of
/
in
instructions repeatedly until the condition becomes false.
n.
The assignment incrementation / decrementation and condition checking is done
in for statement only.
aa
Syntax:
iy
For < variable > in range (< an integer expression >):
or
< Statement 1 > .p Or
: For < variable > in sequence
w
< Statement n> < Statements >
w
//w
Explanation: The first line of code in a loop is called as loop header. The colon
(:) ends the loop header. The loop body comprises the statements in the
s:
Eg, O/P
Print (list (range (2, 20, 3)) → [2, 5, 8, 11, 14, 17]
Char in word y
Char in word t
/
in
Char in word h
n.
Char in word o
aa
Char in word N
iy
Eg, executing a statement a given number of times
or
>>> For each pass in range (4):
.p
Print (“It’s alive! “ End = “)
w
O/P
w
>>> Exponent = 3
s:
>>> Product = 1 ≠ for each iteration each pass variable holds one value
tp
In range.
ht
248
>>> Sum = 0
≠ End = 11 – 1 = 10
https://www.poriyaan.in/ https://eee.poriyaan.in/
Sum = Sum + i
/
Syn:-
in
For < variable > in < sequence >:
n.
Eg >>> for number in [1, 2, 3]:
aa
Print (number, end = ““ )
iy
O/P 1, 2, 3
or
≠ Just printing each element in list and ““ → gives space after the element
Eg
.p
w
>>> For char in “Hi there “:
w
No space
//
O/P
s:
Hi there
tp
Space
Nested Loops:
Syn:-
For < Var > in < Sequence >: White < exp >:
For < var > in < Sequence >: While < exp >:
https://www.poriyaan.in/ https://eee.poriyaan.in/
Statements Statements
Statements Statements
/
in
≠ j = 1, 2 j=1 j=1
n.
Print (“x “ ) j=2 j=2
aa
O/P x i=2 i=4
iy
X j=1 j=1
or
X j=2 j=2
i=i+1 O/P
s:
Else:
tp
Loop control statements change execution from the normal sequence. When
execution leaves
/
Syn: continue af its body and immediately
in
Its condition prior to reiterating
n.
Pass: → The pass statement in python is used
aa
When a statement is required.
iy
Syn : pass syntactically but you do not want any code to execute.
or
break → Terminates loop & transfer to next immediate statement
.p
Continue → Continues the working of loop & skip the other statements below the
w
Continue.
Pass → ∩∪ 𝑐𝑐 operation, used with in loop or function that is not yet
w
Sum = 0.0
s:
Eg
tp
While True
ht
If (data = = ““ ):
10.0
Break O
/
in
Print i current letter is “, i) M
n.
Continue:
aa
For i in “Computer “: O/P
iy
If (i = = p): Current letter is C
or
Continue O
t
//w
r
s:
Pass:
tp
Return values:
https://www.poriyaan.in/ https://eee.poriyaan.in/
Some of the built in functions we have used, such as the math functions, produce results.
Calling the function generates a value. Which we usually assign to a variable.
Import Math
/
in
Print (“The value of sine of pi is “, math sin (a) )
n.
≠ returning the value of sine of pi
aa
The return statement may or may not send back any values to the main program (calling
program) it can done using the return statement.
iy
Eg def sum (arg 1, arg 2): ≠ fun definition ≠ arg 1 = 100
or
Total = arg 1 + arg 2 .p ≠ arg 2 = 200
O/P
tp
Eg, pgm to find distance b/w two points, given by the co-ordinates ( x1 , y1 ) and ( x2 , y2)
dx = x2 – x1
dy = y2 – y1
Squared = dx ** 2 +dy ** 2
Return = result
https://www.poriyaan.in/ https://eee.poriyaan.in/
Ans = distance u (1, 2, 4, 6)
Scope of variables:-
/
in
All variables in a program may not be accessible at all locations in that program. This
n.
depends on where you have declaned a variable.
aa
1. The scope of variable determines the portion of the program where you can acess a
particular Identifier Two basic scopes :-
iy
1. Local scope
or
2. Global scope
2. Local variables can be accessed only inside the function in which they are declared,
whereas global variables can be accessed through out the program body by all
//
functions.
s:
a = 5 ≠ global variable
tp
x = 3 ≠ local variable
Fun () O/P
↓ Value of a is 5
Function Parameters:-
Parameters are just like variables except the values of the variables are defined
when we call the function and are already assigned values when the functions runs.
/
in
Parameters are specified within the pair of parentheses in the function definition
n.
separated by commas. When we call the function. We supply the values in the save way.
The names given in the function definition are called parameters whereas the values you
aa
supply in the function call one called arguments.
iy
or
.p
w
w
//w
s:
tp
You can call one function from within another This ability is known as composition.
Return (SA S)
Return nes
16
def inner (0 :
/
in
Print (“Inside fun “ )
n.
Print (“Welcome “ )
aa
Print (“Outside function “ )
iy
Print (“In calling fun “ ) O/P
or
Inner () Outside function
.p In calling fun
w
Outer () Inside fun
w
Welcome
w
In Direct
s:
A recur sine function is a function that cause itself. To present a function from
tp
If n = = 0:
Return n * fact (n – 1)
12, 14 = 2
If (y = = 0): 12 → 1,2,3,4,6,12
/
in
Rem = 0
n.
Return x 14 → 1, 2,7,14
aa
Else:
iy
Return 𝑔𝑐𝑑 (𝑦 , 𝑥 % 𝑦 )
or
n1 = int (input (“Enter a value “ ) ;
.p
n2 = int (input (“Enter a value “ ) ;
w
Ans = gcd (n1, n2) O/P 12, 14
w
If (exp = = 1):
tp
Return (base)
ht
Else: 2 * 2 1
Leap of faith: - (follow the flow of execution and checking the recursive cause works
Correctly)
https://www.poriyaan.in/ https://eee.poriyaan.in/
def Fibonacci (n):
If n = = 0
Return 0
elif n = = 1:
/
in
Return 1
n.
Else:
aa
Return Fibonacci (n – 1) + Fibonacci (n – 2)
iy
n = int (input (“Enter a value “ )
or
For i in range (n):
O/P 0
F=1
https://www.poriyaan.in/ https://eee.poriyaan.in/
For i in range (1, n + 1)
f=f*i
Return f
/
in
r = int (input ())
n.
Ncr = fact (n) / fact (r) * fact (n – r)
aa
Print (“NCR = “, ncr )
iy
Pgm to find square root of a number
or
n=8
Or
w
num = ∩ * * 0.5
tp
Return num
ht
Print (nes)
Strings
A String is a sequence:-
String is a sequence of characters you can access the characters one at a time with
bracket operator or index
The 2nd statement selects character number 1 from fruit and assign it to letter. The
expression in bracket is called an index. The index indicates which character in the
sequence you want to access.
/
in
Explanation: String is a sequence of characters enclosed
n.
& Def in single or double quotes
aa
Syn
iy
Eg Fruit = ‘banana ‘
or
↓ ↓ .p
Variable String sequence of character
w
Fruit
w
b a n a n a
w
↓ Index → used to a
s:
Quotes.
2 types of index
String slices :-
/
Syn:- variable name [ start : end : step size ]
in
A segment of a string is called a slicesis similer to selecting a character
n.
>>> s = “ Monty python “
aa
>>> Print s [ 0 : 5 ]
iy
Range of characters
or
O/P Monty
O/P ban ( if you omit the first index before colon the slice starts at the
w
beginning
//
at string, if you omit the second index, The slice goes to the end of
s:
the
tp
string)
ht
eg
if the first is greater than or equal to the second the result is Empty string it is
represented by two Quotation mark.
Print ( str [ 0 ] ) → p
Print ( str [ - 1 ] ) → n
Print ( str [ 0 ; 6 : 2 ] ) → p t o
/
Print ( str [ : 3 ] → pyt
in
Print ( str [ : ] ) → python
n.
Strings are Immutable
aa
immutable → not possible to change or delete the existing string the values or
iy
characters in the string cannot be changed.
or
eg str = “ Hello python “
.p
str [ ] = M ⟹ error : object does not support item assignment .
w
But you can create a new string that is a variation on the orginal.
w
String Functions:-
ht
▪ Len ( ) → returns the length of the string ie how many character present
in the string
>>> S = “ python “
https://www.poriyaan.in/ https://eee.poriyaan.in/
>>> Print ( Maxcs ) O/P Y
/
>>> s = “ python “
in
>>> print ( min ( s )
n.
Count ( ) :- Count how many times the particular char is present in the string.
aa
Syn :- Variable
riy
String Methods () :-
o
2. Lower () :- Conuerts upper case letter to lower case letter.
.p
Syn:
w
>>> Str = “ Hello world “
w
//w
HELLO WORLD
tp
the string.
/
in
eg, Str = “ hello world “
n.
Print ( “ , “ . join (str ) ) O/P h,e,l,l,o, w,o,r,l,d
aa
Used to combine 2 string also
iy
O/P hello world
or
Print ( “” . Join ( Strl )
6. String reversed () :-
.p
w
To reverse the given string used reversed () function is used along
with Join ()
w
7. Split () :-
ht
Splits a single multi word string in to list of individual words and that
are seperated by the given character or spaces.
8. Replace () :-
It can taken an orginal string and returnan updated string with some
replacement.
Print ( a )
/
in
String Swap case () method () :-
n.
⟹ returns a copy of the string in which all the case base characters
aa
have had their case swapped.
Str1 = “ Hello “
iy
or
Print ( Str1 . swapcase () ) O/P hELLO
▪ Conuerts the upper case character to lower case and lower case
.p
character to upper case.
w
String Operaters:-
w
1. Concatenation :- (+)
//w
eg Str1 = “ Hai “
tp
Str 2 = “ Welcome “
ht
Point ( str 3)
▪ Repetition :-
Str 3 = Str 1 * 3
▪ Boolean methods :-
https://www.poriyaan.in/ https://eee.poriyaan.in/
String methods that will evaluate to a Boolean value. The methods are
usefull when we creating forms for the user to fill. eg, for postal code it
will accept only numeric string.
Methods :-
/
.in
Str . isalnum ( ) → Check whether the string contain alpha numeric
characters.
n
Str. isalpha ( ) → String consists only alphabetic char
aa
Str. is lower ( ) → Check whether the string characters are in lower case.
iy
Str. is upper ( ) → Check thestring characters are in upper case.
or
Str. is numeric ( ) → Checks the string contains only numeric character.
Str. is space ( )
.p
→ Checks all the characters are white space character.
w
Str. is title ( ) → String is in title case. ( Python )
w
↓
//w
title case
• String Modules :-
s:
eg import String
ht
find ( ) =0
/
in
count ( ) =2
n.
List as array
aa
Instead of array we can use list and list functions & Methods.
iy
List in python is a collection of items which can be of any type array is a
or
collection of items of single type. When comparing list and array, list is more
flexibility. .p
List is also a dynamic mutable type and this means you can add and delete
w
elements from the list at any time
w
mylist = [ 1, 2, 3, 4, 5, 6, 7 ]
://
my list [ 2 ] = 100
ht
Print ( [ 0 : ] ) ⟹ [ 1, 2, 100, 4, 5, 6, 7 ]
Print ( [ : ] ) ⟹ [ 1, 2, 100, 4, 5, 6, 7 ]
Print ( [ : 5 ] ) ⟹ [ 1, 2, 100, 4, 5 ]
eg , a>[1,2]
https://www.poriyaan.in/ https://eee.poriyaan.in/
b=[4,5]
c=a+b
print ( c ) → O/P [ 1, 2, 4, 5 ]
/
Repetition : - used to repeat the list n times
in
eg a = [ 1, 2 ]
n.
c=a*2
aa
print ( c ) ⟹ [ 1, 2, 1, 2 ]
riy
Binary Search using list ;-
o
Print ( “ Binary search pgm “ )
Count = 1
.p
w
List 1 = [ ]
w
for i in range ( 0 , n ) :
s:
a = int ( input () )
tp
list 1 . append ( a )
ht
First = 0
Last = n – i
First = mid + 1
/
in
Print ( “ Element found “ ) Enter the element
n.
else : to be search = 40
aa
Print ( “ Element not found “ ) Element found
iy
or
.p
w
w
//w
s:
tp
ht
https://www.poriyaan.in/ https://eee.poriyaan.in/
Linear Search Using List [ ] : -
Count = 0
/
List 1 = [ ]
in
n = int ( input ( “ Enter the element size “ )
n.
Print ( “ Enter elements one by one “ )
aa
for i in range ( 0, n ) :
iy
a = int ( input ( ) )
or
List 1 . append ( a )
.p
S = int ( input ( “ Enter a search Element “ ) )
w
for i in range ( 0, n ) :
w
if ( list 1 [ i ] = = s ) :
//w
Count = count + 1
if ( Count > 1 ) :
tp
else :
O/P
list 1 = [ ]
/
in
for i in range ( 0 , n ) :
n.
a = int ( input ( “ Enter a elements “ )
aa
list 1 . append ( a ) O/P n value = 5
iy
minno = list 1 [ 0 ] Enter a element
or
for i in range ( 0, n ) : 1, 2, 3, 4, 5
.p
List 1 [ i ] < minno The minimum No is 1
w
minno = list 1 [ i ]
w
Sum = 0
s:
list 1 = [ ]
tp
for i in range ( 0, n ) :
Enter a eleement : 40
Enter a erlement : 50
https://www.poriyaan.in/ https://eee.poriyaan.in/
Sum =
Multidimensional List ;-
Syn : variable = [ [ ], [ ] ]
/
00 01 10 11
in
eg : a = [ [ 1, 2 ] [ 3, 4 ] ]
n.
for i in range ( 0, len ( a ) ) :
aa
for j in range ( 0, len ( a ) :
iy
Print ( a [ i ] [ i ] )
or
O/P 1
.p 2
w
3
w
4
//w
a = [ [ 2, 6 ], [ 7, 8 ] ]
s:
b = [ [ 1, 3 ], [ 2, 4 ] ]
tp
c[i][j]=a[i][j]+b[i][j]
Print ( c [ i ] [ j ] , end = ) 9 12
Print ( 1 n )
a = [ [ ], [ ] ] for i in range ( 0, n ) :
https://www.poriyaan.in/ https://eee.poriyaan.in/
n = int ( input ( ) ) For j in range ( 0, n ) :
a [ i ] [ j ] = int ( input ( ) )
Matrix Multiplication :-
/
a = [ [ 1, 1, 1 ] , [ 1, 1, 1 ] , [ 1, 1, 1 ] ]
in
b = [ [ 1, 1, 1 ] , [ 1, 1, 1 ] , [ 1, 1, 1 ] ]
n.
c = [ [ 0, 0, 0 ] , [ 0, 0, 0 ] , [ 0, 0, 0 ] ]
aa
for i in range ( 0, len ( a ) ) :
iy
for j in range ( 0, len ( a ) ) :
or
for k in range ( 0, len ( a ) ) :
.p
c[i][j]=c[i][j]+a[i][k]+b[k][j]
w
for i in range ( 0, len ( a ) ) :
w
Print ( c [ i ] [ j ] , end = ‘ 1t ‘ )
Print ( “ In “ )
s:
O/P
tp
3 3 3
ht
3 3 3
3 3 3
i/p 1 1
1 1 1 1 1 1
𝑎= 1 1 1 b=1 1 1
1 1 1 1 1 1
Problem Solving and Python Programming (GE3151) – Reg 2021
Unit I: Computational Thinking and Problem Solving
Computational Thinking and Problem Solving | Fundamentals of Computing | Identification of Computational Problems |
Algorithms | Building Blocks | Notation | Algorithmic Problem Solving | Simple Strategies for Developing Algorithms |
Illustrative Problems | Anna University Two Marks Questions & Answers | Multiple Choice Questions and Answers
https://www.poriyaan.in/