Parts of Chapter 3,5,6

Ace your homework & exams now with Quizwiz!

What is the terminating condition?

A condition that occurs which causes a loop to stop repeating its body. In the for loops we saw in this chapter, the terminating condition has been when there are no more elements to assign to the loop variable.

What are for loops?

A statement in Python for convenient repetition of statements in the body of the loop.

(T/F) Functions without return values don't return anything

F- All Python functions return None whenever they do not return another value

(T/F) You can't have an if statement without an else statement

F- Another form of the if statement is one in which the else clause is omitted entirely. In this case, when the condition evaluates to True, the statements are executed, otherwise the flow of execution continues to the statement after the if. ex: if x < 0: print("The negative number ", x, " is not valid here.") x = 42 print("I've decided to use the number 42 instead.") print("The square root of ", x, "is", math.sqrt(x))

What are conditional statements?

Statements that check conditions and change the behavior of the program accordingly- if/else statements

(T/F) Regarding chained conditionals, even if more than one condition is true, only the first true branch executes.

T

(T/F) Within a for loop, when the editor reaches a return statement, the for loop will immediately stop and the code will stop running entirely

T

(T/F)- bool vals can be assigned to vars

T Ex: >>> age = 18 >>> old_enough_to_get_driving_licence = age >= 17 >>> print(old_enough_to_get_driving_licence) True >>> type(old_enough_to_get_driving_licence) <class 'bool'>

(T/F) The return statement can terminate a function before it has finished going through all of the code within the program

T- The return statement, with or without a value, allows us to terminate the execution of a function before (or when) we reach the end. Note: One reason to use an early return is if we detect an error condition def print_square_root(x): if x <= 0: print("Positive numbers only, please.") return result = x**0.5 print("The square root of", x, "is", result) The function print_square_root has a parameter named x. The first thing it does is check whether x is less than or equal to 0, in which case it displays an error message and then uses return to exit the function. The flow of execution immediately returns to the caller, and the remaining lines of the function are not executed.

What is a bool expression

Tests if two Vals are equal (==)

What are the two boolean Vals?

True and False Note: Capitals are important

What are the 3 logical operators

and- produces True only if both statements are True or- produces True if either of the conditions are True not- finds the opposite (its True if the condition is false or not true)

For loop syntax

for f in ... Ex: for f in ["Joe","Zoe","Brad","Angelina","Zuki","Thandi"]: invite = "Hi " + f + "Please come to my party at 1:00!" print(invite) loop body- in this case its the invite and print statement On each iteration or pass of the loop, first a check is done to see if there are still more items to be processed. If there are none left (the terminating condition), the loop has finished. Program execution continues at the next statement after the loop body, If there are items still to be processed, the loop variable is updated to refer to the next item in the list. This means, in this case, that the loop body is executed here 7 times, and each time f will refer to a different friend. At the end of each execution of the body of the loop, Python returns to the for statement, to see if there are more items to be handled, and to assign the next one to f.

Syntax of conditional statements

if BOOLEAN EXPRESSION: STATEMENTS_1 # Executed if evaluates to True else: STATEMENTS_2 # Executed if evaluates to False The indented statements that follow are called a block. The first unindented statement marks the end of the block. Each of the statements inside the first block of statements are executed in order if the Boolean expression evaluates to True. The entire first block of statements is skipped if the Boolean expression evaluates to False, and instead all the statements indented under the else clause are executed.

Chained conditionals syntax

if x < y: STATEMENTS_A elif x > y: STATEMENTS_B else: STATEMENTS_C elif is an abbreviation of else if. Again, exactly one branch will be executed. There is no limit of the number of elif statements but only a single (and optional) final else statement is allowed and it must be the last branch in the statement:

Nested conditionals syntax

if x < y: STATEMENTS_A else: if x > y: STATEMENTS_B else: STATEMENTS_C Note: Logical operators often provide a way to simplify nested conditional statements. For example, we can rewrite the following code using a single conditional: if 0 < x: # Assume x is an int here if x < 10: #nested with indent print("x is a positive single digit.") The print function is called only if we make it past both the conditionals, so we could rewrite this using the and operator and a single if statement: if 0 < x and x < 10: print("x is a positive single digit.")

Comparison operators

x == y # Produce True if ... x is equal to y x != y # ... x is not equal to y x > y # ... x is greater than y x < y. # ... x is less than y x >= y # ... x is greater than or equal to y x <= y. # ... x is less than or equal to y


Related study sets

Insurance - Florida Statutes/Rules/Regulations for Life Insurance

View Set

Amoeba Sisters Video Recap; Photosynthesis and Cellular Respiration

View Set

7.04 Unit Test: Savings Accounts

View Set

Adams Pharmacology Chapter 23-32

View Set

Leadership and Management Questions

View Set

Frankenstein Chapter 1-21 Final Test

View Set