Skip to document

Accenture 3 - Practice notes

Practice notes
Course

python programming (cs8151)

86 Documents
Students shared 86 documents in this course
University

Anna University

Academic year: 2014/2015
Uploaded by:
0followers
4Uploads
0upvotes

Comments

Please sign in or register to post comments.

Preview text

Python Programming

Fundamentals

Decision Making and Loops in Python

Hands-on Exercise

Index

  • 1 Objectives
  • 2 Overview
  • 3 Prerequisites
  • 4 Preliminary Concepts
    • 4 Types of Decision-making Statements
      • 4.1 If Statements
      • 4.1 If.. Statements
      • 4.1 If ...elif.. Statements
      • 4.1 Nested if else Statements
    • 4 Loops in Python
      • 4.2 For Loop
      • 4.2 While Loop
      • 4.2 Nested Loop
    • 4 Break and Continue
      • 4.3 Break Statement...............................................................................
      • 4.3 Continue Statement
    • 4 Range Function
  • 5 Practice Exercise

2 Overview

This document provides information on decision making statements and loops in Python, along with guided and practice exercises to help you gain knowledge about the concepts. Estimated time needed: 40 minutes Note: The links provided in the document navigate to the respective web sources and open in the same window. The users can use the back arrow in the browser, which navigates back to the beginning of the document.

3 Prerequisites

  1. Visit the edX website and check the reference videos on Conditions and Branching and Loops.

Guided Exercise # Write code below bat_avg = 95. if bat_avg > 70: print(“Good Batsman”) print(“print always”) Output Good Batsman Print always 4.1 If.. Statements Guided Exercise # Write code below bat_avg = 55. if bat_avg > 70: print(“Good Batsman”) else: print("Not a Good Batsman") Output Not a Good Batsman

4.1 If ...elif.. Statements Guided Exercise # Write code below bat_avg = 65. if bat_avg >= 80: print("Very Good Batsman") elif bat_avg >= 60: print("Average Batsman") else: print("Poor Performer") Output Average Batsman 4.1 Nested if else Statements

4.2 For Loop For loop will traverse sequentially. For example, iterating a string or list or tuple etc. Guided Exercise # Write the code teams = ["India","Australia","England","West Indies"] for series in teams: print(series) Output India Australia England West Indies

4.2.1 For Loop with Else For loop can be combined with Else block. - Else block will execute as soon as For block completes its execution - If the For block uses break statement, then it will skip Else block Guided Exercise # Write the code teams = ["India","Australia","England","West Indies"] for series in teams: print(series) else: print("End of the Teams") Output India Australia England West Indies End of the Series

Write the code

teams = ["India","Australia","England","West Indies"] for series in teams: if series == "France": print(series) else: print("Teams not in order")

Guided Exercise # Write the code i = 0 while i < 4: print(i) i = i + 1 else: print("End of the loop") Output 0 1 2 3 End of the loop

Write the code

i = 5 while i < 4: #conditional false print(i) i = i + 1 else: print("End of the loop") Output End of the loop

4.2 Nested Loop In Nested loop, Python allows to use one or more loop inside another loop. 4 Break and Continue 4.3 Break Statement Break statement terminates an existing loop and the program control goes after the last statement of the loop. Guided Exercise # Write the code i = 0 while i < 6: i += 1 if i == 3: break print(i, end=" ") Output 1 2

print (x,end=" ") Output 3 6 9 12 15 18

Write the code

for x in range(20,3,-3): print (x,end=" ") Output 20 17 14 11 8 5

5 Practice Exercise

a. Write a code to find the summation of first 15 numbers using For loop in Python # Write the code below Output b. Write your code using decision making condition in Python to find the house price with following parameters: - One BHK cost 10000 - Two BHK cost 20000 - Three BHK cost 3000 Declare a variable “Price” with value 23000 # Write your code below Output c. Write a code to find a sequence from 40 to 2 with an interval of 3 using range function # Write your code below Output

Copyright © 2021 Accenture All rights reserved. Accenture and its logo are trademarks of Accenture.

Was this document helpful?

Accenture 3 - Practice notes

Course: python programming (cs8151)

86 Documents
Students shared 86 documents in this course

University: Anna University

Was this document helpful?
Python Programming
Fundamentals
Decision Making and Loops in Python
Hands-on Exercise