1 / 9

CSCI/CMPE 4341 Topic: Programming in Python Chapter 4: Control Structures (Part 2) – Exercises

CSCI/CMPE 4341 Topic: Programming in Python Chapter 4: Control Structures (Part 2) – Exercises. Xiang Lian The University of Texas – Pan American Edinburg, TX 78539 lianx@utpa.edu. Objectives. In this chapter, you will do some exercises about: basic control structures in Python.

Download Presentation

CSCI/CMPE 4341 Topic: Programming in Python Chapter 4: Control Structures (Part 2) – Exercises

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CSCI/CMPE 4341 Topic: Programming in PythonChapter 4: Control Structures(Part 2) – Exercises Xiang Lian The University of Texas – Pan American Edinburg, TX 78539 lianx@utpa.edu

  2. Objectives • In this chapter, you will do some exercises about: • basic control structures in Python

  3. Multiple Choices • Typically, ______ statements are used for counter-controlled repetition. • A. for B. while C. repetition D. until • Typically, ______ statements are used for sentinel-controlled repetition. • A. for B. while C. repetition D. until • The _________statement, when executed in a repetition statement, skips the remaining statements in the loop body and proceeds with the next iteration of the loop. • A. break B. exit C. continue D. return • The _____operator can be used to ensure that two conditions are both true before choosing a certain path of execution • A. and B. && C. or D. || • An infinite loop occurs when the loop-continuation condition in a while statement______. • A. never becomes true B. never becomes false C. is false D. is true for finite times

  4. Multiple Choices (cont'd) • What is the range of function call range(1, 10)? • A. [0, 10] B. [0, 10) C. [1, 10] D. [1, 10) • What is the value of the expression of 4/5*2+2**(1+2)? • A. 8 B. 10 C. 9.6 D. 8.4 • What is the value of 5/4, 5//4, and 5%4? • A. 1, 1.25, 1 B. 1.25, 1, 4 C. 1, 1, 1 D. 1.25, 1, 1 • Which of the following is the appropriate for statement for varying the control variable over the following sequence of values: 5, 10, 15, 20, 25? • A. for i in range(25): B. for i in range (5, 25): • C. for i in range(5, 25, 5): D. for i in range(25, 5, -5):

  5. True/False Statements • The exponentiation operator ** associates left to right. • Function call range (4, 7) returns the sequence 4 to 7, inclusive. • Sentinel-controlled repetition uses a counter variable to control the number of times a set of instructions executes. • String format specifier "%5d" outputs integer with right alignment by default. • The expression ((x>y) and (a<b)) is True if either (x>y) is True or (a<b) is True. • An expression containing the or operator is True if either or both of its operands are True. • The and operator has a higher precedence than the or operator.

  6. What Does the Code Do? for i in range(1, 11): for j in range (1, 11): print ('@', end = ""); print ("");

  7. Debug Errors counter=1 For i in range (1, 10) counter++

  8. Write a Program • Use nested for statement to write a Python program to display the triangle of asterisks. Use the following statements: • print ("*", end="") # displays asterisks one at a time • print ("") # the cursor goes to a new line • print (" ", end="") # inserts a space * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

  9. Write a Program (cont'd) • Use nested for statement to write a program to display a rectangle. Use the following statements: • print ("+", end="") • print ("-", end="") • print ("|", end="") • print ("") • print (" ", end="") + - - - - - - - - - - + | | | | | | + - - - - - - - - - - +

More Related