Introduction to Loops in Python: While and For Statements, Slides of Object Oriented Programming

This is an easily understandable brief description of concepts for problem solving and object oriented programming.

Typology: Slides

2019/2020

Uploaded on 08/07/2020

muskan278
muskan278 🇮🇳

4

(1)

5 documents

1 / 32

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
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
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20

Partial preview of the text

Download Introduction to Loops in Python: While and For Statements and more Slides Object Oriented Programming in PDF only on Docsity!

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

Class Average Input Processing Output Number of students in class, mark scored by each student Determine total of marks secured by students Find average of marks Class average of marks

Test Cases

Input 5 90 85 70 50 60 Output

Processing Involved

Already Know

  • (^) To read values from user
  • (^) To check if a condition is satisfied
  • (^) Print characters

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.”

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.

Example use Sum of first ‘n’ numbers sum = current = n= while current <= n: sum=sum + current current = current + 1

Print values from 0 to 9 in a line

a=0; b= while a < b: # One way to code counter loops print(a, end=' ') a += 1 # Or, a = a + 1 Output: 0 1 2 3 4 5 6 7 8 9 Include end=‘ ‘ in print statement to suppress default move to new line

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

Pass statement

  • (^) Infinite loop
  • (^) while True: pass

    Type Ctrl-C to stop me!

Check if a given number is Prime

Class Average

Pattern Generation Input Processing Output Staircase height Create steps one by one To create a step print character equal to length of step Pattern

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”