Podcast
Questions and Answers
Repetition statements are also known as loops.
Repetition statements are also known as loops.
True (A)
Each repetition of an action in a loop is called a iteration.
Each repetition of an action in a loop is called a iteration.
True (A)
Definite iteration loops repeat an action until the program determines it needs to stop.
Definite iteration loops repeat an action until the program determines it needs to stop.
False (B)
The for
loop in Python is a control statement that easily supports definite iteration.
The for
loop in Python is a control statement that easily supports definite iteration.
In a for
loop, eachPass
must be a keyword.
In a for
loop, eachPass
must be a keyword.
The first line of code in a for
loop is called the loop body.
The first line of code in a for
loop is called the loop body.
The loop body of a for
loop must be indented and aligned in the same column.
The loop body of a for
loop must be indented and aligned in the same column.
If the exponent is 0, the loop body will execute once.
If the exponent is 0, the loop body will execute once.
A count-controlled loop iterates through a range of numbers.
A count-controlled loop iterates through a range of numbers.
The range function can't be used to specify an explicit lower bound.
The range function can't be used to specify an explicit lower bound.
Augmented assignment operators can only be used with arithmetic operations.
Augmented assignment operators can only be used with arithmetic operations.
An off-by-one error is a syntax error.
An off-by-one error is a syntax error.
The range
function returns a tuple.
The range
function returns a tuple.
Strings cannot be traversed with a for
loop.
Strings cannot be traversed with a for
loop.
The range
function only accepts two arguments: start and stop.
The range
function only accepts two arguments: start and stop.
Loops can only count upwards, never downwards.
Loops can only count upwards, never downwards.
Field width refers to the number of additional spaces in formatted output.
Field width refers to the number of additional spaces in formatted output.
The letter 's' is used to format integers.
The letter 's' is used to format integers.
The <precision>
specifier is required when formatting floating-point numbers.
The <precision>
specifier is required when formatting floating-point numbers.
Selection statements allow a program to make decisions based on a condition.
Selection statements allow a program to make decisions based on a condition.
Boolean values can only be represented as True
or False
in Python.
Boolean values can only be represented as True
or False
in Python.
4 != 4
evaluates to True
.
4 != 4
evaluates to True
.
An if-else
statement is also known as a one-way selection statement.
An if-else
statement is also known as a one-way selection statement.
When using if-else
statements for error checking, the else
block typically handles the error condition.
When using if-else
statements for error checking, the else
block typically handles the error condition.
elif
cannot be used in multi-way selection statements.
elif
cannot be used in multi-way selection statements.
Logical operators combine Boolean expressions.
Logical operators combine Boolean expressions.
The or
logical operator returns True
only if both operands are True
.
The or
logical operator returns True
only if both operands are True
.
The not
operator inverts the value of a Boolean expression.
The not
operator inverts the value of a Boolean expression.
In Python, the assignment operator has higher precedence than logical operators.
In Python, the assignment operator has higher precedence than logical operators.
Short-circuit evaluation means that the entire expression is always evaluated, regardless of intermediate results.
Short-circuit evaluation means that the entire expression is always evaluated, regardless of intermediate results.
The while
loop is used for definite iteration.
The while
loop is used for definite iteration.
A continuation condition is tested within the loop.
A continuation condition is tested within the loop.
An infinite loop occurs when the continuation condition always remains true.
An infinite loop occurs when the continuation condition always remains true.
The while
loop is an entry-control loop.
The while
loop is an entry-control loop.
The body of a while
loop always executes at least once.
The body of a while
loop always executes at least once.
A loop control variable's initial value affects how the loop operates.
A loop control variable's initial value affects how the loop operates.
A break
statement exits the loop.
A break
statement exits the loop.
The for
and while
loops are not interchangeable and cannot be converted into each other.
The for
and while
loops are not interchangeable and cannot be converted into each other.
random.randint()
generates only integer numbers.
random.randint()
generates only integer numbers.
The random
module only supports generating integer numbers.
The random
module only supports generating integer numbers.
Repetition statements are also known as segments.
Repetition statements are also known as segments.
The first line of code in a for
loop is sometimes called the loop header.
The first line of code in a for
loop is sometimes called the loop header.
In Python, s -= 5
is equivalent to s = s - 5
.
In Python, s -= 5
is equivalent to s = s - 5
.
The range
function can only be used to generate sequences of numbers that count upwards.
The range
function can only be used to generate sequences of numbers that count upwards.
In Python, 4 != 4
evaluates to True
.
In Python, 4 != 4
evaluates to True
.
Flashcards
Repetition statement
Repetition statement
Statement that repeats an action.
Iteration
Iteration
Each execution of a repetition statement.
Definite iteration
Definite iteration
Loop that repeats an action a predefined number of times.
For loop
For loop
Signup and view all the flashcards
Loop header
Loop header
Signup and view all the flashcards
Loop body
Loop body
Signup and view all the flashcards
Augmented assignment
Augmented assignment
Signup and view all the flashcards
Off-by-one error
Off-by-one error
Signup and view all the flashcards
Range function
Range function
Signup and view all the flashcards
Field width
Field width
Signup and view all the flashcards
Format string and % operator
Format string and % operator
Signup and view all the flashcards
Boolean data type
Boolean data type
Signup and view all the flashcards
== (Equals)
== (Equals)
Signup and view all the flashcards
!= (Not equals)
!= (Not equals)
Signup and view all the flashcards
Selection statement
Selection statement
Signup and view all the flashcards
If
If
Signup and view all the flashcards
If-Else statement
If-Else statement
Signup and view all the flashcards
Multi-way if statement
Multi-way if statement
Signup and view all the flashcards
or
or
Signup and view all the flashcards
and
and
Signup and view all the flashcards
not
not
Signup and view all the flashcards
Short-circuit evaluation
Short-circuit evaluation
Signup and view all the flashcards
While loop
While loop
Signup and view all the flashcards
Conditional iteration
Conditional iteration
Signup and view all the flashcards
Continuation condition
Continuation condition
Signup and view all the flashcards
Infinite loop
Infinite loop
Signup and view all the flashcards
Entry-control loop
Entry-control loop
Signup and view all the flashcards
Loop control variable
Loop control variable
Signup and view all the flashcards
Break statement
Break statement
Signup and view all the flashcards
Random module
Random module
Signup and view all the flashcards
Random.randint
Random.randint
Signup and view all the flashcards
Definite iteration
Definite iteration
Signup and view all the flashcards
Body
Body
Signup and view all the flashcards
Off-by-one error
Off-by-one error
Signup and view all the flashcards
Selection statements
Selection statements
Signup and view all the flashcards
Control statement
Control statement
Signup and view all the flashcards
Entry-control loop
Entry-control loop
Signup and view all the flashcards
Break
Break
Signup and view all the flashcards
Indefinite iteration
Indefinite iteration
Signup and view all the flashcards
Intialization
Intialization
Signup and view all the flashcards
For loops
For loops
Signup and view all the flashcards
Format string and %
Format string and %
Signup and view all the flashcards
Boolean expressions
Boolean expressions
Signup and view all the flashcards
If else
If else
Signup and view all the flashcards
Random.randint
Random.randint
Signup and view all the flashcards
Study Notes
Objectives
- Chapter 3 focuses on loops and selection statements in Python.
- The objectives cover writing different types of loops (fixed number, character traversal, counting up/down).
- Also covered are entry-controlled loops, selection statements, Boolean expressions, and exiting loops.
Definitive Iteration: The for Loop
- Repetition statements, also known as loops, allow for repeating an action.
- Each instance of an action being repeated is a pass or iteration.
- Loops can be definite (repeating a predefined number of times).
- Loops can be indefinite (performing an action until the program determines it needs to stop).
Executing Statements a Given Number of Times
- The
for
loop is a control statement in Python that easily facilitates definite iteration. - Basic structure: `for
in range( ): ... - The first line of code in a
for
loop is the loop header. - The rest of the code, known as the loop body, must be indented and aligned in the same column.
Example: Exponentiation Loop
- Computes the exponentiation for a non-negative exponent.
- If the exponent is 0, the loop body does not execute and the value of
product
remains 1.
Count-Controlled Loops
- Loops can count through a range of numbers using the
range
function. - Explicit lower bound can be specified in the range.
Example: Bound-Delimited Summation
- Summing numbers within a specified range.
Augmented Assignment
- The assignment symbol can be combined with arithmetic and concatenation operators to provide augmented assignment operations.
a += 3
is equivalent toa = a + 3
.- Format:
<variable> <operator>= <expression>
. - This is equivalent to:
<variable> = <variable> <operator> <expression>
.
Loop Errors: Off-by-One Error
- Example of counting from 1 through 4.
- The loop actually counts from 1 through 3 if
range(1,4)
is used. - This represents a logic error, not a syntax error.
Traversing Data Sequence Contents
range
returns a list.- Strings consist of character sequences.
- Values in a sequence can be visited with a
for
loop:for <variable> in <sequence>: <do something with variable>
.
Specifying Steps in the Range
range
expects a third argument to specify a step value.theSum
gives the sum, based on the specified condition.
Loops that Count Down
- Example given for a for loop that counts from 10 to 1.
Formatting Text Output
- Many data-processing applications output data in tabular format.
- Field width refers to the total number of data characters and additional spaces for a datum in a formatted string.
- It automatically starts in the first column
Formatting Text Output Examples using format % operator
- This version uses format strings and format operators but a single data value will be generated
d
is used for integer formatting, instead ofs
- Can also be used to show a sequence of calculations
Formatting Text Output using 'Float" Type
- Use %.f to format as a float and .
is optional
Selection: If and If-Else Statements
- Selection statements enable computers to make choices.
- The choices are based on a condition.
Boolean Type, Comparisons, Boolean Expressions
- Boolean data types include two values: true and false.
- Comparison Operators ( e.g. ==, !=, <, >, <=, >=) provide a "Meaning"
If-Else Statements (1 of 2)
- A two-way selection statement used to check inputs for errors.
If-Else Statements (Syntax)
- if
: - else:
One-Way Selection Statements
- Simplest form of selection is the
if
statement: - if
<condition>
:<sequence of statements>
Multi-Way If Statements
- Programs test conditions that entail more than two alternative courses of action.
- In programming, referred to often as a multi-way selection statement.
Multi-Way If Statements(2 of 3)
- Example of the program to convert number grades to letter grades
- if condition 1 is met do first
- elif condition 2 is met, do second
- else do the default statements if nothing else works
Logical Operators and Compound Boolean Expressions
- If either of two courses of action is true, below are two approaches to resolve
- Youcan use
elif
to go through each statement but it can become complex - Or you can simplify and use
or
in the comparison statement
Logical Operators and Compound Boolean Expressions (Truth Table)
- Some claims can be made from previous tables.
- Use A = True, B= False to show AND, NOT, OR statement
- Can be used with variable assignment
Logical Operators and Compound Boolean Expressions (Operator Precendence)
- Remember
not
has higher precedence thanand
, oror
Short-Circuit Evaluation
- In (A and B), if A is false, the expression is false
- There is no need to run B
- In (A or B), if A is true, the expression is true
- Example is given to count a number, and show the evaluation if a condition is or is not completed
Conditional Iteration: The "While" Loop
while
loop describes the conditional iteration code- Example is given to take any string input until the use enters a sentinel value
""
to quit and end the loop
The Structure and Behavior of a While Loop
- Called a continuation loop when it should continue with another condition
- Improper use may lead to an infinite loop
- Can test conditions at the top of the loop and only execute 0 or more times
The Structure and Behavior of a While Loop(Data)
- The first input statement initializes a variable to an initial value that the loop condition can test
Count Control with a While Loop
- Can create a count-controlled loop instead of a
for
loop to iterate through something
The While True Loop and The Break Statement
- The while loop can and should be written correctly for readability
- Example is given with the true as the initial condition to prompt the statements
- It then tests a condition to
break
from the loop - Use if data == "": (no input)
The While Loop and the Break Statement (Grade conversion with Loop)
- Used to continuous take grades from user, until the user enters in an acceptable value
- You must
break
from the loop, or else it will continue indefinitely
The While True Loop and the Break Statement(Bool Loop)
- A boolean variable can be used to control the loop instead of a direct conditional
Random Numbers
- Programming has generating resources for generating random numbers
- the
random
module provides functions for this
Random Numbers (simple guessing example function)
- used to create a simple random number game
random.randint
* creates a random integer
Studying That Suits You
Use AI to generate personalized quizzes and flashcards to suit your learning preferences.
Related Documents
Description
Explore Python's loops and selection statements. Learn to write fixed number loops and use Boolean expressions. Understand definite iteration using 'for' loops and control statements.