ACIS 2504 Python Unit 3 11/17/20-12/1/20

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

C. both operands are true.

A Boolean expression using the and operator returns True when A. neither operand is true. B. one operand is true. C. both operands are true. D. both operands are equal.

for

A ____ loop is the best way to do a definite iteration

A. counting through a sequence of numbers. and D. running a set of statements a predictable number of times.

A for loop is convenient for A. counting through a sequence of numbers. B. making choices in a program. C. describing conditional iteration. D. running a set of statements a predictable number of times.

indented and aligned in the same column

A loop body must be

data

A string is a _____ structure

if and if else statements

Allow a computer to make choices based on a condition

1. a = a + 3 2. a = a -3 3. a = a * 3 4. a = a / 3

Augmented Assignment 1. a +=3 2. a-=3 3. a*=3 4. a/=3 write the equivalents to these 4 loops.

A. entry-controlled loop.

By default, the while loop is an A. entry-controlled loop. B. exit-controlled loop. C. infinite loop D. continuation loop

B. The loop is infinite.

Consider the following code segment:count = 1while count <= 10:print(count, end = " ")Which of the following describes the error in this code? A. The loop is off by 1. B. The loop is infinite. C. The loop control variable is not properly initialized. D. The comparison points the wrong way.

B. 5 4 3 2

Consider the following code segment:count = 5while count > 1:print(count, end = " ")count -= 1What is the output produced by this code? A. 1 2 3 4 5 B. 5 4 3 2 C. 2 3 4 5 D. 5 4 3 2 1

B. At least one

Consider the following code segment:theSum = 0.0while True:number = input("Enter a number: ")if number == ":breaktheSum += float(number)How many iterations does this loop perform? A. None B. At least one C. Zero or more D. Ten

C. 4

Consider the following code segment:x = 5y = 4if x > y:print( y )else:print( x )What value does this code segment print? A. 0 B. > C. 4 D. 5

pass or iteration

Each repetition of action of a loop is called a ________ or i_______

9

Go Hokies! What is the index of "!" in the above string?

0

Go Hokies! What is the index of "G" in the above string?

A. 10 times

How many times does a loop with the header for count in range (10): execute the statements in its body? A. 10 times B. 1 time C. 11 times D. 9 times

one-way selection statement

IF Statement is also known as a

two-way selection statement

IF-Else Statements are also known as a

Identation

IF-Else Statements required

depends

Indefinite loop is different from definite look because an indefinite loop _______ on something/condition

true

Print function automatically begins printing in the first available column

or

Returns True if one of the statements is true. Which logical operator is this?

not

Reverse the result, returns False if the result is true. Which logical operator is this?

salary = float(input("Enter the starting salary: ")) increase = int(input("Enter the annual % increase: ")) years = int(input("Enter the number of years: ")) print("Year Salary\n-------------") multiplier = 1 + increase / 100 nextSal = salary for year in range(1, years + 1): print("%2d%12.2f" % (year, nextSal)) nextSal *= multiplier

Teachers in most school districts are paid on a schedule that provides a salary based on their number of years of teaching experience. For example, a beginning teacher in the Lexington School District might be paid $30,000 the first year. For each year of experience after this first year, up to 10 years, the teacher receives a 2% increase over the preceding value. Write a program that displays a salary schedule, in tabular format, for teachers in a school district. The inputs are: 1. Starting salary 2. Annual percentage increase 3. Number of years for which to print the schedule Each row in the schedule should contain the year number and the salary for that year An example of the program input and output is shown below: Enter the starting salary: $30000 Enter the annual % increase: 2 Enter the number of years: 10 Year Salary ------------- 1 30000.00 2 30600.00 3 31212.00 4 31836.24 5 32472.96 6 33122.42 7 33784.87 8 34460.57 9 35149.78 10 35852.78

definitie iteration and indefinite iteration

Two types of loops

D. 0 1 2 3 4

What is the output of the loop for count in range(5): print(count, end = " ")? A. 1 2 3 4 5 B. 5 6 7 8 9 C. 1 2 3 4 D. 0 1 2 3 4

random module

What module should you use if you want to generate random numbers?

syntax of IF-Else Statements

What syntax is this for? if <condition>: <sequence of statements-1> else: <sequence of statements-2>

syntax of IF Statement

What syntax is this for? if <condition>: <sequence of statements>

>>> print('%5d' % (25)) 25 -5 represents field width -d represents an integer data is indented to the right by 3 spaces because 5 is the field width and the integer is 2 characters, 5-2= 3

What will the result of this code and what does each area of the code represent? >>> print('%5d' % (25))

>>> print('%6.2f' %(3.141592653589793)) 3.14 - .2 represents the decimal amount - 6 represents the field width data is indented to the right by 3 spaces, 6 is the field width and 3.14 is 3 characters, therefore 6-3= 3 spaces

What will the result of this code and what does each area of the code represent? >>> print('%6.2f' %(3.141592653589793))

>>>print('%12s' % ('example')) example -12 reprsents field width -s represents data is a string - 5 spaces then word example because 12 is the field width and there are 7 characters in the word example, 12-7=5 word will be indented to the right by 5 spaces

What will the result of this code look like and what does each area of the code mean? >>>print('%12s' % ('example'))

>>>print('%12s' % ('example')) example -12 represents field width -s represents data is a string - 5 spaces then word example because 12 is the field width and there are 7 characters in the word example, 12-7=5 word will be left of 5 spaces

What will the result of this code look like and what does each area of the code represent? >>>print('%-12s' % ('example'))

C. The last value in a sequence of integers minus 1

When the function range receives two arguments, what does the second argument specify? A. The last value in a sequence of integers B. The first value in a sequence of integers C. The last value in a sequence of integers minus 1 D. The last value in a sequence of integers plus 1

+1

When using a for loop, you must ass what to the upper variable?

not

Which has a higher precedence than and and or?

condition

Which iteration can the while loop be used for?

right

Which symbol does this represent right or left justified? %

left

Which symbol does this represent right or left justified? -

for loop

Which type of loop is this? For <variable> in range(<an integer expression>): <statement-l> . . <statement-n>

first = int(input("Enter the first side: ")) second = int(input("Enter the second side: ")) third = int(input("Enter the third side: ")) if first==second and second==third: print("The triangle is equilateral.") else: print("The triangle is not equilateral.")

Write a program that accepts the lengths of three sides of a triangle as inputs. The program output should indicate whether or not the triangle is an equilateral triangle. Use The triangle is equilateral. and The triangle is not equilateral. as your final outputs. An example of the program inputs and output is shown below: Enter the first side: 2 Enter the second side: 2 Enter the third side: 2 The triangle is equilateral.

loop body

any number of statements nested inside a loop

==

comparison operator for equals

!=

comparison operator for not equals

Data Structure

consists of smaller pieces of data

Boolean data type

consists of two values: true and false

The count is: 0 The count is: 1 The count is: 2 The count is: 3 The count is: 4 The count is: 5 The count is: 6 The count is: 7 The count is: 8 The loop has ended!

count = 0 while (count < 9): print ('The count is:', count) count += 1 print ('The loop has ended!') What will be the outcome of this while loop?

loop header

first line of the for loop

for loop

loops a specific number of times

n is 4 n is 3 The loop has ended!

n = 5 while (n > 0): n -= 1 if n == 2: break print('n is:', n) print ('The loop has ended!')

new line

n------------------- means what?

Step Value

number you use to increase a loop control variable on each pass through a loop

Indefinite iteration:

perform action until program determines it needs to stop

Definite iteration

repeat action a predefined/specific number of times

Loops

repetition statements or repeat an action

Conditional iteration

requires that condition be tested within loop to determine if it should continue

uniform(a, b)

returns a random floating point number between a and b inclusive

randint(a, b)

returns a random integer between a and b inclusive

Len function

returns the string's length, which is the number of characters it contains

and

returns true if both statements are true. Which logical operator is this?

2 = right side column -2= left side column

right column vs left column for loop output

elif statement

statements that can be indefinitely added to an if-else statement to allow it to make additional decisions

Multi-Way Selection Statements

testing conditions entail more than two alternative courses of action

Left Justified

text is aligned to the left

Right Justified

text is aligned to the right

Enter a number : you enter a number until you stop entering numbers then the sum will appear as The sum is

theSum = 0.0 whileTrue: number = input('Enter a number: ') if number == "": break theSum += float(number) print ('The sum is:', theSum)

Field width for formatting output text

total number of data characters and additional spaces for an item in a formatted string

End Parameter

used to append any string at the end of the output of the print statement

logical operators

used to combine conditional statements

while loop syntax

while (condition) { statement(s); }


Kaugnay na mga set ng pag-aaral

Unit 4-14 Broker Real Estate Cont Ed

View Set

2.Scanning electron microscopy (SEM)

View Set

Religion chapter 5 sections 3,4,&5

View Set

Biostatistics Odds Ratio & Relative Risk

View Set

Patho CH 43 Disorders of the Exocrine Pancreatic and Hepatobiliary Systems

View Set

Physical Science Practice Test 3

View Set

management information systems chapter 12

View Set