AIMS Quiz #3

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

Operator precedence chart

1. + , - (unary) 2. ** (exponent) 3. not 4. * , / , //, % (mult, division, remainder) 5. +, - (binary) 6. <, <=, >, => (comparison) 7. ==, != (equality) 8. and 9. or 10. =, +=, *=, /=, //=, %= (assignment operators) unary means x = 1 binary means x= 2-1 multiple operands

5.1 How many times will the following code print "Welcome to Python"? count = 0 while count < 10: print("Welcome to Python") count += 1

10

5.19 How many times is the print statement executed? for i in range(10): for j in range(10): print(i * j)

100

number = 6 while number > 0: number -= 3 print(number, end = ' ')

3 0 6-3 in first run of code no display 6

What is the output for y? y = 0 for i in range(0, 10): y += i

45 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 = 45

Comparison operators

< less than <= less than or equal to > greater than => greater than or equal to == equal to != not equal to

Conditional expression

A conditional expression evaluates an expression based on a condition if x > 0: y = 1 else: y = -1 y = 1 if x>0 else -1

Which of the following loops prints "Welcome to Python" 10 times?

B: for count in range(0, 10): print("Welcome to Python") C: for count in range(1, 11): print("Welcome to Python")

Code 3 is preferred but all are correct:

Code 1: if number % 2 == 0: even = True else: even = False Code 2: even = True if number % 2 == 0 else False Code 3: even = number % 2 == 0

Which of the following operators are right-associative. A. * B. + C. % D. and E. =

E. =

Which of the pattern is produced by the following code? for i in range(1, 6 + 1): for j in range(6, 0, -1): print(j if j <= i else " ", end = " ") print()

Pattern C 1 2. 1 3 2 1 4 3 2 1 5 4 3 2 1 6 5 4 3 2 1 column goes from 6 to 1 row starts at 1

Nested loops

consist of an *outer loop and one or more inner loops*. Each time the outer loop is repeated, the inner loops are reentered and started anew.

Control While loop with *user confirmation*

continueloop = 'Y' while continueloop == 'Y' ... continueloop = input("enter Y to continue and N to quit: " )

Loop

controls *how many times in succession an operation* (or sequence of operations) is performed checks if the statement is true and if so performs the *loop body*

5.3 Analyze the following code. count = 0 while count < 100: # Point A print("Welcome to Python!") count += 1 # Point B # Point C

count < 100 is always True at Point A and count < 100 is always False at Point C

Infinite loop example (no update the counter)

count = 0 while count < 10: print("Welcome to Python")

For loop example

for i in *range(initial value, end value)* loop body range(a, b, step) ex: range(3, 9, 2) 3 5 7 *if step value is negative* the last number must be greater than b

Nested loop multiplication table example

for i in range(1,10): #outer loop #row for j in range(1,10): #inner loop. column print(format(i * j, '4d'), end = " ") print() # want to print a new line

While loop example

i = initial value while i < end value: Loop Body i += 1 example: count = 0 while count < 100: print( ) count += 1

Not operator

if not( __ ) is False then statement inside is true not(gender M) is true because gender if actually F

What is the number of iterations in the following loop: for i in range(1, n): # iteration

n - 1

iteration of the loop

one-time execution of a loop body

Which of the following function returns a sequence 0, 1, 2, 3?

range(0, 4) range(4)

Ending While Loop with *Sentinel Value*

sentinel value signifies the end of the input example input ends if it is 0 data = int(input("Enter an integer (the input ends if it is 0): ")) sum = 0 while data != 0: sum = sum + data data = int(input("Enter an integer (the input ends if it is 0): ")) print("The sum is", sum)

Which of the following loops correctly computes 1/2 + 2/3 + 3/4 + ... + 99/100?

sum = 0 for i in range(1, 100): sum += i / (i + 1) print("Sum is", sum)

Break

use in a loop to immediately terminate a loop *break out of a loop* break will break out of while loop into statements outside of the statement

Conditional Operator

year = eval(input("Enter a year: ")) *isLeapYear =* (year % 4 == 0 *and* year % 100 != 0) *or* (year % 400 == 0) print(year, "is a leap year." *if isLeapYear else* "is not a leap year.")

Given abs(x-2) <= 4 which is true?

(x - 2 <= 4) and (x - 2 >= -4)

The order of the precedence (from high to low) of the operators +, *, and, or is:

*, +, and, or

And operator

*If one* of the operands of an and operator *is False, the expression is False* When evaluating p1 and p2, *Python first evaluates p1* and then *if p1 is True, evaluates p2* *if p1 is False*, it does *not evaluate p2.*

Continue

*breaks out of an iteration* ends the current iteration and program control goes to the end of the loop body sum = 0 number = 0 while number < 20: number += 1 if number == 10 or number == 11: continue sum += number print("The sum is", sum) continue loop *when number becomes 10 or 11* it *skips sum += number line* and goes to print

For loop

*count controlled* loop (repeats specified *# of times*) *more precise* than while loop for i in *range* (*initialization, stop condition, step*): numbers in *range must be integers*, no decimals

While Loop

*loop continuation condition* - true or false/Boolean expression that control body execution executes the loop body as long as True make sure the loop continuation condition eventually becomes false to avoid *Infinite loop* *count* = 0 or *sum* = 0

Logical operators

*not* ( ) ( ) *and* ( ) ( ) *or* ( )

Two types of loops / what loops must have

*while* - *condition controlled* loop (*true or false* statements) *for* - *count controlled* loop (repeats specified *# of times*) Loops must have: -*Initialization* of a counter -Checking the counter *against a condition* and -*Update* of the *counter*

Or operand

-*If one* of the operands of an or operator *is True, the expression is True.* When evaluating p1 or p2, Python *first evaluates p1 and then, if p1 is False evaluates p2*; *if p1 is True*, it does *not evaluate p2.*

The function range(5) return a sequence _____ .

0, 1, 2, 3, 4


Kaugnay na mga set ng pag-aaral

Histoire Révolution Française date

View Set

CHF 4400 - Greenberg: Chapter Three

View Set

Matt's CFCM Questions: FAR Part 32. Contract Financing

View Set

Stats 118 - Chapters 1-8 Homework, In-class Activities & Excel Labs

View Set

Networks Final Exam REVIEW PART 2

View Set

Fundamentals for Success in Business

View Set