0% found this document useful (0 votes)
15 views

Module 3

Triple integration Maths

Uploaded by

Saket Pentapati
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Module 3

Triple integration Maths

Uploaded by

Saket Pentapati
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 39

CSE1012-Python

L.Pavithra
Assistant Professor
SCOPE
VIT AP

1
Python loop concepts

2
Need of iterative control
Repeated execution of set of statements
• An iterative control statement is a control
statement providing repeated execution of a
set of instructions
• Because of their repeated execution, iterative
control structures are commonly referred to as
“loops.”

3
While statement
• Repeatedly executes a set of statements based
on a provided Boolean expression (condition).
• All iterative control needed in a program can
be achieved by use
• of the while statement.

4
Syntax of While in Python
while test: # Loop test
statements # Loop body
else: # Optional else
statements
# Run if didn't exit loop with break
Example:
count = 0
while count < 5:
print (count, " is less than 5")
count = count + 1
else:
print (count, " is not less than 5")
5
Example use
Sum of first ‘n’ numbers
sum =0
current =1
n=3
while current <= n:
sum=sum + current
current = current + 1

6
Print values from 0 to 9 in a line
a=0; b=10
while a < b: # One way to code counter loops
print(a, end=' ')
a += 1 # Or, a = a + 1

Output:
0123456789

Include end=‘ ‘ in print statement to suppress default


move to new line
7
Break, continue, pass, and the
Loop else
• break Jumps out of the closest enclosing loop
• continue Jumps to the top of the closest
enclosing loop
• pass Does nothing at all: it’s an empty
statement placeholder
• Loop else block Runs
• if and only if the loop is exited normally (i.e.,
without hitting a break)
8
Break statement
• while True:
name = input('Enter name:')
if name == 'stop': break
age = input('Enter age: ')
print('Hello', name, '=>', int(age) ** 2)
Output:
Enter name: bob
Enter age: 40
Hello bob => 1600
9
Pass statement
• Infinite loop
• while True: pass
# Type Ctrl-C to stop me!

10
Print all even numbers less than 10
and greater than or equal to 0

11
Check if a given number is Prime

12
Class Average
• Given marks secured in CSE1001 by the
students in a class, design an algorithm and
write a Python code to determine the class
average. Print only two decimal digits in
average

13
Class Average

Input Processing Output

Number of students in Determine total of marks secured Class average of


class, mark scored by by students marks
each student
Find average of marks

14
Average marks scored by
‘N’ number of Students
Step 1: Start
Step 2 : Read Number Of Students
Step 3 : Initialize counter as 0
Step 4 : Input mark
Step 5 : Add the mark with total
Step 6 : Increment the counter by 1
Step 7: repeat Step 4 to Step 6 until counter less than number of
students
Step 7: Divide the total by number of students and store it in
average
Step 8: Display the average
Step 9: Stop
15
Test Cases
Input
5
90 85 70 50 60
Output
71.00
Processing Involved

16
Class Average

17
For iteration
• In while loop, we cannot predict how many
times the loop will repeat
• The number of iterations depends on the input
or until the conditional expression remains
true
• While loop is ideal when stop criteria is not
explicit

18
Control flow of for
statement

19
Syntax of for Statement
for target in object:
# Assign object items to target statements
if test: break # Exit loop now, skip else
if test: continue # Go to top of loop now
else: statements # If we didn't hit a 'break’

20
For and Strings
for iterating_var in sequence or range:
statement(s)
Example:
for letter in 'Python':
print( 'Current Letter :', letter)

21
For and Strings
When the above code is executed:

Current Letter : P
Current Letter : y
Current Letter : t
Current Letter : h
Current Letter : o
Current Letter : n

22
For and Sequence Index
fruits = ['banana', 'apple', 'mango']
for index in range(len(fruits)):
print ('Current fruit :', fruits[index])

Output:
Current fruit : banana
Current fruit : apple
Current fruit : mango
23
For and Range
for n in range(1, 6):
print(n)
When the above code is executed:
1
2
3
4
5
24
range function call
Syntax - range( begin,end,step )
where
Begin - first value in the range; if omitted, then default value
is 0
end - one past the last value in the range; end value may not
be omitted
Step - amount to increment or decrement; if this parameter is
omitted, it defaults to 1 and counts up by ones

begin, end, and step must all be integer values;


floating-point values and other types are not allowed
25
Example for Range
range(10)
Output : 0,1,2,3,4,5,6,7,8,9
range(1, 10)
Output : 1,2,3,4,5,6,7,8,9
range(1, 10, 2)
Output : 1,3,5,7,9
range(10, 0, -1)
Output : 10,9,8,7,6,5,4,3,2,1
range(10, 0, -2)
Output : 10,8,6,4,2
26
range(2, 11, 2)
Output : 2,4,6,8,10
range(-5, 5)
Output : −5,−4,−3,−2,−1,0,1,2,3,4
range(1, 2)
Output : 1
range(1, 1)
Output : (empty)
range(1, -1)
Output : (empty)
range(1, -1, -1)
Output : 1,0
range(0)
Output : (empty) 27
Print Even Numbers Using Range
>>> for i in range(2,10,2):
print(i)

Output:
2
4
6
8
28
Using else Statement with For Loop

for num in range(10,20): #to iterate between 10 to 20


for i in range(2,num): #to iterate on the factors of the number
if num%i == 0: #to determine the first factor
j=num/i #to calculate the second factor
print ('%d equals %d * %d' % (num,i,j))
break #to move to the next number, the #first FOR
else: # else part of the loop
print (num, 'is a prime number')
break
29
• Output:
10 equals 2 * 5
11 is a prime number
12 equals 2 * 6
13 is a prime number
14 equals 2 * 7
15 is a prime number
16 equals 2 * 8
17 is a prime number
18 equals 2 * 9
19 is a prime number
30
Pattern Generation
• Your teacher has given you the task to draw the structure of a
staircase. Being an expert programmer, you decided to make a
program for the same. You are given the height of the
staircase. Given the height of the staircase, write a program to
print a staircase as shown in the example. For example,
Staircase of height 6:
#
##
###
####
#####
######
Boundary Conditions: height >0
31
Pattern Generation
Input Processing Output

Staircase height Create steps one by one Pattern


To create a step print character
equal to length of step

32
Pseudocode
READ staircase_height
if staircase_height > 0
x=1
Repeat
y=1
Repeat
print #
y=y+1
Until y <= x
x=x+1
Until x <= staircase_height
End if
Else
Print “Invalid input”

33
Test Cases
Input
3
Output
#
##
###

Processing Involved
Print step by step

34
Test Cases
Input
-1
Output
Invalid input

Processing Involved
Boundary condition check fails

35
36
Exercise Problem
1. Write a program that read a group ‘g’ of five
numbers and another number ‘n’ and print a number
in ‘g’ if it is a factor for a given number n?
2. Write a program to find the factorial of a number n?
3. Write a menu driven program which get user choice
to perform add/sub/mul/div with the obtained two
input?
4. Write a program to display few odd multiples of a
odd number n ?

37
Exercise Problem
5. The Head Librarian at a library wants you to make a program
that calculates the fine for returning the book after the return
date. You are given the actual and the expected return dates.
Calculate the fine as follows:
a. If the book is returned on or before the expected return date,
no fine will be charged, in other words fine is 0.
b. If the book is returned in the same month as the expected
return date, Fine = 15 Rupees × Number of late days
c. If the book is not returned in the same month but in the same
year as the expected return date, Fine = 500 Rupees × Number
of late months
d. If the book is not returned in the same year, the fine is fixed
at 10000 Rupees.
38
39

You might also like