Python Loops and Selections: Chapter 3
45 Questions
0 Views

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to Lesson

Podcast

Play an AI-generated podcast conversation about this lesson

Questions and Answers

Repetition statements are also known as loops.

True (A)

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.

False (B)

The for loop in Python is a control statement that easily supports definite iteration.

<p>True (A)</p> Signup and view all the answers

In a for loop, eachPass must be a keyword.

<p>False (B)</p> Signup and view all the answers

The first line of code in a for loop is called the loop body.

<p>False (B)</p> Signup and view all the answers

The loop body of a for loop must be indented and aligned in the same column.

<p>True (A)</p> Signup and view all the answers

If the exponent is 0, the loop body will execute once.

<p>False (B)</p> Signup and view all the answers

A count-controlled loop iterates through a range of numbers.

<p>True (A)</p> Signup and view all the answers

The range function can't be used to specify an explicit lower bound.

<p>False (B)</p> Signup and view all the answers

Augmented assignment operators can only be used with arithmetic operations.

<p>False (B)</p> Signup and view all the answers

An off-by-one error is a syntax error.

<p>False (B)</p> Signup and view all the answers

The range function returns a tuple.

<p>False (B)</p> Signup and view all the answers

Strings cannot be traversed with a for loop.

<p>False (B)</p> Signup and view all the answers

The range function only accepts two arguments: start and stop.

<p>False (B)</p> Signup and view all the answers

Loops can only count upwards, never downwards.

<p>False (B)</p> Signup and view all the answers

Field width refers to the number of additional spaces in formatted output.

<p>False (B)</p> Signup and view all the answers

The letter 's' is used to format integers.

<p>False (B)</p> Signup and view all the answers

The <precision> specifier is required when formatting floating-point numbers.

<p>False (B)</p> Signup and view all the answers

Selection statements allow a program to make decisions based on a condition.

<p>True (A)</p> Signup and view all the answers

Boolean values can only be represented as True or False in Python.

<p>True (A)</p> Signup and view all the answers

4 != 4 evaluates to True.

<p>False (B)</p> Signup and view all the answers

An if-else statement is also known as a one-way selection statement.

<p>False (B)</p> Signup and view all the answers

When using if-else statements for error checking, the else block typically handles the error condition.

<p>True (A)</p> Signup and view all the answers

elif cannot be used in multi-way selection statements.

<p>False (B)</p> Signup and view all the answers

Logical operators combine Boolean expressions.

<p>True (A)</p> Signup and view all the answers

The or logical operator returns True only if both operands are True.

<p>False (B)</p> Signup and view all the answers

The not operator inverts the value of a Boolean expression.

<p>True (A)</p> Signup and view all the answers

In Python, the assignment operator has higher precedence than logical operators.

<p>False (B)</p> Signup and view all the answers

Short-circuit evaluation means that the entire expression is always evaluated, regardless of intermediate results.

<p>False (B)</p> Signup and view all the answers

The while loop is used for definite iteration.

<p>False (B)</p> Signup and view all the answers

A continuation condition is tested within the loop.

<p>True (A)</p> Signup and view all the answers

An infinite loop occurs when the continuation condition always remains true.

<p>True (A)</p> Signup and view all the answers

The while loop is an entry-control loop.

<p>True (A)</p> Signup and view all the answers

The body of a while loop always executes at least once.

<p>False (B)</p> Signup and view all the answers

A loop control variable's initial value affects how the loop operates.

<p>True (A)</p> Signup and view all the answers

A break statement exits the loop.

<p>True (A)</p> Signup and view all the answers

The for and while loops are not interchangeable and cannot be converted into each other.

<p>False (B)</p> Signup and view all the answers

random.randint() generates only integer numbers.

<p>True (A)</p> Signup and view all the answers

The random module only supports generating integer numbers.

<p>False (B)</p> Signup and view all the answers

Repetition statements are also known as segments.

<p>False (B)</p> Signup and view all the answers

The first line of code in a for loop is sometimes called the loop header.

<p>True (A)</p> Signup and view all the answers

In Python, s -= 5 is equivalent to s = s - 5.

<p>True (A)</p> Signup and view all the answers

The range function can only be used to generate sequences of numbers that count upwards.

<p>False (B)</p> Signup and view all the answers

In Python, 4 != 4 evaluates to True.

<p>False (B)</p> Signup and view all the answers

Flashcards

Repetition statement

Statement that repeats an action.

Iteration

Each execution of a repetition statement.

Definite iteration

Loop that repeats an action a predefined number of times.

For loop

Python's control statement for definite iteration.

Signup and view all the flashcards

Loop header

The first line of code in a for loop.

Signup and view all the flashcards

Loop body

The indented block of code within a loop.

Signup and view all the flashcards

Augmented assignment

Symbol combined with arithmetic operators to provide augmented assignment operations.

Signup and view all the flashcards

Off-by-one error

Logic error where the loop iterates one too many or too few times.

Signup and view all the flashcards

Range function

Function that returns a list of numbers.

Signup and view all the flashcards

Field width

Total number of data characters and additional spaces for a datum in a formatted string

Signup and view all the flashcards

Format string and % operator

Allows programmer to format data with field width and precision.

Signup and view all the flashcards

Boolean data type

Data type with two values: True and False.

Signup and view all the flashcards

== (Equals)

Operator that checks for equality.

Signup and view all the flashcards

!= (Not equals)

Operator that checks for inequality.

Signup and view all the flashcards

Selection statement

Statement allowing choices based on conditions.

Signup and view all the flashcards

If

If the condition is true, execute these statements

Signup and view all the flashcards

If-Else statement

A two-way selection statement.

Signup and view all the flashcards

Multi-way if statement

Statement for multiple alternative courses of action.

Signup and view all the flashcards

or

Logical Operator for when either of two conditions is true.

Signup and view all the flashcards

and

Logical operator for "both conditions are true"

Signup and view all the flashcards

not

Logical operator that inverts a Boolean value

Signup and view all the flashcards

Short-circuit evaluation

Where the evaluation stops once the outcome is known

Signup and view all the flashcards

While loop

Loop that continues while a condition is true.

Signup and view all the flashcards

Conditional iteration

Loop that is controlled by a condition being tested within the loop body

Signup and view all the flashcards

Continuation condition

Condition that must be tested wihin the loop to continue

Signup and view all the flashcards

Infinite loop

Loop that never terminates.

Signup and view all the flashcards

Entry-control loop

Halts when condition becomes false.

Signup and view all the flashcards

Loop control variable

Variable whose value determines if a loop continues.

Signup and view all the flashcards

Break statement

Unconditionally exits loop's body.

Signup and view all the flashcards

Random module

Module for simulations and games.

Signup and view all the flashcards

Random.randint

Returns random number between two included arguments.

Signup and view all the flashcards

Definite iteration

Repeating a sequence of actions a fixed number of times

Signup and view all the flashcards

Body

A for loops consists of a set of statements called this

Signup and view all the flashcards

Off-by-one error

Occurs when a loop doesn't perfomr the intended amount of iterations

Signup and view all the flashcards

Selection statements

A selection statement that can enable programs to make choices

Signup and view all the flashcards

Control statement

This determines the order in which other statements execute

Signup and view all the flashcards

Entry-control loop

The while loop uses this type of loop

Signup and view all the flashcards

Break

This exits a loop from its body

Signup and view all the flashcards

Indefinite iteration

Continues to execute until program determines it needs to stop

Signup and view all the flashcards

Intialization

A loop control variable needs an initialized value

Signup and view all the flashcards

For loops

This can traverse the values in a sequence

Signup and view all the flashcards

Format string and %

A format string and its operator allows the programmer to format data

Signup and view all the flashcards

Boolean expressions

Composed of logical operators

Signup and view all the flashcards

If else

Allows for a selection for two choices

Signup and view all the flashcards

Random.randint

Returns a random number between two numbers

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 to a = 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 of s
  • 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 than and, 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.

Quiz Team

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.

More Like This

Closed Loop Control Systems Quiz
3 questions
For Loop Syntax in C Programming
4 questions

For Loop Syntax in C Programming

InspirationalHedgehog464 avatar
InspirationalHedgehog464
Cys-loop Receptors and nAChRs
15 questions
Use Quizgecko on...
Browser
Browser