VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
presentation_python_11_1569171345_375360.pptx
1. Problem 1
Given the number of hours and minutes browsed, write a
program to calculate bill for Internet Browsing in a browsing
center. The conditions are given below.
(Assume that a user can only browser to maximum of 7 hours)
(a) 1 Hour Rs.50
(b) 1 minute Re. 1
(c) 5 Hours Rs. 200
2. Pseudocode
READ hours and minutes
SET amount = 0
IF hours >=5 then
CALCULATE amount as amount + 200
COMPUTE hours as hours – 5
END IF
COMPUTE amount as amount + hours * 50
COMPUTE amount as amount + minutes * 1
PRINT amount
3. Different patterns in Algorithm
Sequential
Sequential structure executes the program in the order in
which they appear in the program
Selectional (conditional-branching)
Selection structure control the flow of statement execution
based on some condition
Iterational (Loops)
Iterational structures are used when part of the program is to
be executed several times
4. Selection pattern
• A selection control statement is a control
statement providing selective execution of
instructions.
• A selection control structure is a given set of
instructions and the selection control
statement(s) controlling their execution.
6. If Statement
• An if statement is a selection control
statement based on the value of a given
Boolean expression.
The if statement in Python is
If statement Example use
If condition:
statements
statements
else:
statements
If grade >=70:
print(‘pass’)
else:
Print(‘fail’)
7. Indentation in Python
• One fairly unique aspect of Python is that the amount of
indentation of each program line is significant.
• In Python indentation is used to associate and
group statements
8. Selection pattern
Example 1: Write a program to find the average score of a student in
physics, chemistry and maths which help them to qualify for a
medical education or engineering education. If the average score
is greater than or equal to 70, the candidate is eligible for
scholarship and not eligible for scholarship otherwise
Algorithm:
Step 1 : Start
Step 2: Get the input marks for physcis,chemistry and maths
Step 3 : Accumulate all the marks and store it in Total
Step 4 : Divide Total by 3 and store it in Average
Step 5 : If the Average score is greater than or equal to 70; candidate qualified for
scholarship
Else not qualified for scholarship
Stop 6: Stop
10. Pseudo code:
begin
accept phy_mark,che_mark,math_mark
compute total = phy_mark +che_mark+math_mark
compute average=total/3
if (total >= 98 )
begin
display ‘ candidate eligible for scholarship’
end
else
begin
display ‘candidate not eligible for scholarship’
end
end
11. Python code : mark.py
phy_mark=97
che_mark=91
mat_mark=99
total_mark=phy_mark+che_mark+mat_mark
print(‘ candidate obtained ‘, total_mark)
if total_mark >= 98 :
print(‘candidate eligible for scholarship ')
else :
print(' candidate not eligible for scholarship ')
12. Nested if Statements
• There are often times
when selection among
more than two sets of
statements (suites) is
needed.
• For such situations, if
statements can be
nested, resulting in
multi-way selection.
13. Example1: Write a program to find the biggest
among three given number?
Algorithm:
Step1: Start
Step2: Get three input value namely a, b and c
Step3: Compare a with b, if a is greater than b
compare a with c and if a is bigger say a is bigger
else say c is bigger
Step 4: Compare b with c , if b is greater than c say
b is bigger else c is bigger
Step 5: Stop
15. Pseudo code:
read a, b and c
if (a > b)
if (a>c)
display ‘ a is greater’
else
display ‘ c is greater’
else
if (b>c)
display ‘ b is greater’
else
display ‘ c is greater’
16. Python code : big.py
a=15
b=6
c=7
if a>b:
if a>c:
print(" a is greater")
else:
print(" c is greater")
else:
if b>c:
print(" b is greater")
else:
print(" c is greater")
17.
18. Else if Ladder
• if-else statements may be nested to achieve
multi-way selection
Example:
19. Example: Write a program to find out the gift amount to be given for the students
based on their entrance exam marks:
If mark>=90 then 10000
>=80 mark and <90 then 5000
>=70 mark and <80 then 2000
Else ‘no gift’
Algorithm :
Step1 : Start
Step2: input the entrance mark
Step 3: if mark greater than or equal to 90 say then amount is 10000
else if mark greater than or equal to 80 then gift amount is 5000
else if mark greater than or equal to 70 then gift amount is 2000
else say ‘no gift’
Step 4: Stop
21. Pseudo code:
read entrance_mark
if (entrance_mark >=90)
display('amount is 10000 ')
else
if(entrance_mark >=80)
display(' amount is 5000 ')
else
if(entrance_mark >=70)
display(' amount is 2000 ')
else
display(‘no gift')
22. Example 1: Python code
entrance_mark=75
if entrance_mark >=90:
print(' amount is 10000 ')
elif entrance_mark >=80:
print(' amount is 5000 ')
elif entrance_mark >=70:
print(‘amount is 2000 ')
else:
print(‘no gift')
23.
24. Multiple Conditions
• Multiple conditions can be check in a ‘if’
statement using logical operators ‘and’ and
‘or’.
• Python code to print ‘excellent’ if mark1 and
mark2 is greater than or equal to 90, print
‘good’ if mark1 or mark2 is greater than or
equal to 90, print ‘need to improve’ if both
mark1 and mark2 are lesser than 90
25. if mark1>=90 and mark2 >= 90:
print(‘excellent’)
if mark1>=90 or mark2 >= 90:
print(‘good’)
else:
print(‘needs to improve’)
26. Exercise Problem
Write a python code to check whether a
given number of odd or even?
number = int(input('Enter Number'))
if(number%2==0):
print('even')
else:
print('odd')
27. Input Processing Output Solution
Alternative
Number Apply
modulus
operator for
the Number
Odd or Even Repeated
subtraction of
2 from
Number till the
Number
becomes less
than 2
28. READ Number
COMPUTE Remainder = Number modulus 2
if Remainder == 0 then
print ‘Number is even’
else
print ‘Number is odd’
Pseudocode
29. Exercise Problem
Write a python code to check whether a
given year is leap year or not?
A year is a leap year if it is divisible by 4 and
not divisible by 100 or if it is divisible by 400
30. Input Processing Output Solution
Alternative
Year Take modulus
of year with 4,
100 and 400
and then
check for
remainder
Leap year or
not
-
31. READ year
value1 = year modulus 4
value 2 = year modulus 100
value 3 = year modulus 400
if (value1==0 and value2!=0) or (value3 == 0)
print ‘leap year’
else
print ‘Not leap year’
Pseudocode
32. Leap year Program
year = int(input('Enter year'))
if(((year%4==0)and(year%100!=0))or(year%400 == 0)):
print('leap year')
else:
print('not leap year')
33. Exercise Problem
Write a python program to segregate student
based on their CGPA. The details are as
follows:
<=9 CGPA <=10 - outstanding
<=8 CGPA <9 - excellent
<=7 CGPA <8 - good
35. READ CGPA
if CGPA >=9 and CGPA <=10 then
print ‘outstanding’
else if CGPA >=8 and CGPA <=9 then
print ‘excellent’
else if CGPA >=7 and CGPA <=8 then
print ‘good’
Pseudocode
36. Python Program
CGPA = int(input('enter CGPA'))
if((CGPA>=9) and (CGPA<=10)):
print('Outstanding')
elif((CGPA>=8)and(CGPA<=9)):
print('Excellent')
elif((CGPA>=7)and(CGPA<=8)):
print('Good')
37. Exercise Problem
Write a python code in finding the roots of a
quadratic equation of the form ax2+bx+c = 0?
38. Input Processing Output Solution
Alternative
a,b,c Find the value
of discriminant
Compute
roots based
on value of
discriminant
Roots of
quadratic
equation
39. READ a,b,c
COMPUTE discriminant = b*b – 4*a*c
if discrminant<0 then
roots are imaginary
else if discrminant==0
roots are real and equal
else:
roots are real and unequal
Pseudocode
40. Pseudocode
If roots are real and equal then
r1 = r2 = -b/2*a
else if roots are real and unequal:
r1 = (-b+sqrt(discrminant))/2*a
r2 = (-b-sqrt(discrminant))/2*a
else if roots are imaginary:
r1 = -b/2*a + (sqrt(abs(discrminant))/2*a)i
r2 = -b/2*a - (sqrt(abs(discrminant))/2*a)I
#print roots
41. import math
a = int(input('Enter value of a'))
b = int(input('Enter value of b'))
c = int(input('Enter value of c'))
#find the value of b2 - 4ac
discrminant = b**2 - 4*a*c
if (discrminant<0):
#roots are imaginary
nature_Of_Roots = 0
elif (discrminant==0):
#roots are real and equal
nature_Of_Roots = 1
else:
#roots are real and unequal
nature_Of_Roots = 2
print("dis is",discrminant)