Introduction to Computer Programming

¡Supera tus tareas y exámenes ahora con Quizwiz!

What is the "if" keyword followed by?

"if" keyword is always followed by a "condition" that must evaluate to either "True" or "False".

What is an escape character?

"special" characters that have special meaning inside of a string literal. (For example, the '\n' escape character causes your string to print out a line break. The '\t' escape character causes your output to tab over to the next tab stop. You can use escape characters as values that you pass to the 'sep' and 'end' arguments of the print() function as well.)

How do you comment on your code?

#- Commenting is a way for you to leave little "notes" for yourself so that you can understand what your thinking process was when you originally wrote your program.

Runtime error

(Runtime errors generally result from "bad data" and cause a program to crash once it start sup) An error that does not occur until the program has started to execute but that prevents the program from continuing.

What would the following mathematical operations return if they were translated to Python without changes? Enter a single numeric value as your response. five plus six mod three times three = ____________

5

end=

A keyword parameter for the print() function that causes the function to NOT add a newline at the end of the string.

What is a string literal?

A string that is explicitly defined

String

A unit of text that you use in your programs

After the execution of the following statement, the variable price will reference the value _____. price = int(68.549) A. 68 B. 69 C. 68.55 D. 68.54

A. 68

A "variable" is: A. A programmatic construct that lets the programmer store information during the execution of a program B. A sequence of numbers that represents numeric information C. A sequence of characters that represents textual information D. A reusable command that the programmer can "call" to execute a series of statements

A. A programmatic construct that lets the programmer store information during the execution of a program

A loop variable used to keep a running total is called a (n) _____. A. Accumulator B. Total C. total running D. grand total

A. Accumulator

The "break" statement: A. Can be used to immediately end a loop iteration B. Can be used to skip to the next loop iteration C. Can be used to end your program completely D. Can be used to negate a boolean condition

A. Can be used to immediately end a loop iteration

What type of loop structure repeats the code based on the value of a boolean expression? A. Condition-controlled loop B. Number-controlled loop C. Count-controlled loop D. Boolean-controlled loop

A. Condition-controlled loop

In this sample code, what is the data type of y? x = input("Enter a number: ") y = float(x) A. Float B. Int C. String D. Boolean

A. Float

What is the data type of x? y = 5.2 x = 100 + 200 + 300 + 900 / y A. Float B. Int C. String D. Boolean

A. Float

T/F: the variable answer will have a floating point number stored as its data type? answer = 4.0 + 2 * 2 - 5 A. True B. False

A. True

The following variable "answer" will have a floating point number stored as its data type: answer = 4.0 + 2 * 2 - 5A. TrueB. False

A. True

What will the following code print when run? x = 5 y = 10 print (x == 10 or y == 10) A. True B. False C. 5 == 10 or 10 == 10 D. Nothing will print

A. True

How many arguments does the "input" function expect to receive? A. one B. two C. zero D. any number of arguments

A. one

What is repetition structure?

All so called loop. the code for a given operation one time and then placing it into a special structure that causes Python to repeat it as many times as necessary/next.

What is the value of x? y = 10000.98347 x = format(y, ">20,.2f") A. "10,000.98 " B. " (with 20 spaces) 10,000.98" C. "10,000.98" D. 10000.98

B. " (with 20 spaces) 10,000.98"

Which mathematical operator is used to raise five to the second power in Python? A. / B. ** C. ^ D. ~

B. **

How many times will this loop iterate? a = 0 while a < 5: a = a + 1 A. 4 times B. 5 times C. 6 times D. An infinite number of times

B. 5 times

Consider the following statement in Python: a = 123 The variable 'a' is storing a: A. A string B. A numeric value C. A logical value

B. A numeric value

Consider this statement in Python: print ("One", "Two", "Three") "One" is considered a/n: A. The name of a function B. Argument C. A numeric literal D. A variable

B. Argument

The "continue" statement: A. Can be used to immediately end a loop iteration B. Can be used to skip to the next loop iteration C. Can be used to end your program completely D. Can be used to negate a boolean condition

B. Can be used to skip to the next loop iteration

Any sequence of alpha-numeric characters (numbers and letters) can be used as a variable name (i.e. "abc123" and "123abc" are both valid variable names) A. True B. False

B. False

T / F: The first thing that happens in a while loop is that it executes the entire code block located inside the while loop once. A. True B. False

B. False

What will the following code print when run? x = 5 y = 10 print (x == y) A. True B. False C. 5 == 10 D. Nothing will print

B. False

How many "*" characters will print when the following code is run? x = 5 y = 10 if x == y: print ("*") elif x != y: print ("*") elif x < y: print ("*") A. None B. One C. Two D. Three

B. One

The "while" keyword must always be followed by: A. an "if" statement B. a boolean condition C. a variable D. the "print" function

B. a boolean condition

After the execution of the following statement, the variable sold will reference the numeric literal value as a(n) _____ data type: sold = 256.752 A. int B. float C. str D. currency E. decimal

B. float

After the execution of the following statement, the variable sold will reference the numeric literal value as a(n) _____ data type: sold = 256.752 A. int B. float C. str D. currency E. decimal

B. float

What type of error produces incorrect results but does not prevent the program from running? A. syntax B. logic C. grammatical D. human

B. logic

When using accumulator with "while" loops you must ensure that: A. the variable is initialized inside of the loop B. the variable is initialized outside of the loop C. the variable has a data type of String D. the variable has a data type of Boolean

B. the variable is initialized outside of the loop

What is the format for the while clause in Python? A. while condition B. while condition: C. while condition statement D. while condition: statement

B. while condition

How are decision structure implemented in Python?

By using an "if" statement

Given the following code, how many times will the string "Hello" print a = 5 b = 10 while a == 10: print ("Hello") A. 1 time B. 5 times C. 0 times D. An infinite number of times

C. 0 times

What is the ending value of the variable 'a' when the following program is run? a = 0 c = 0 while c <3: if c% 2 == 0: a + = 1 c + = 1 A. 0 B. 1 C. 2 D. 3

C. 2

If value1 is 2.0 and value2 is 12, what is the output of the following command? print(value1 * value2) A. 24 B. value1 * value2 C. 24.0 D. 2.0 * 12

C. 24.0

If value1 is 2.0 and value2 is 12, what is the output of the following command? print(value1 * value2) A. 24 B. value1 * value2 C. 24.0 D. 2.0 * 12

C. 24.0

What kind of information is being stored in the variable x? x = "1005" A. An integer B. A floating point number C. A String

C. A String

The output of print("I'm ready to begin") is: A. Im ready to begin B. "I'm ready to begin" C. I'm ready to begin D. I"m ready to begin E. error

C. I'm ready to begin

What of the following are a result of writing programs as one long sequence structure? A. Duplicated code makes the program faster to write. B. Having a long sequence of statements makes it easy to find errors. C. If parts of the duplicated code have to be corrected, the correction has to be made many times. D. It does not make use of decision structures.

C. If parts of the duplicated code have to be corrected, the correction has to be made many times.

In this sample code, what is the data type of x? x = input("Enter a number: ") A. Float B. Int C. String D. Boolean

C. String

What is the data type of x? y = 100.7534 x = format(y, "<20.2f") A. Float B. Int C. String D. Boolean

C. String

Imagine you have the following incomplete code: if ______: print ("got here!") elif ______: print ("got here!") if ______: print ("got here!") If a valid boolean expression was filled into each blank, what is the maximum # of times the String "got here!" could theoretically be printed? A. None B. One C. Two D. Three

C. Two

When will the following loop terminate? while keep_on_going! = 999 - 1: A. When keep_on_going refers to a value equal to 999 B. When keep_on_going refers to a value not equal to than 999 C. When keep_on_going refers to a value equal to 998 D. When keep_on_going refers to a value not equal to 998

C. When keep_on_going refers to a value equal to 998

When will the following loop terminate? while keep_on_going != 999: A. When keep_on_going refers to a value less than 999 B. When keep_on_going refers to a value greater than 999 C. When keep_on_going refers to a value equal to 999 D. When keep_on_going refers to a value not equal to 999

C. When keep_on_going refers to a value equal to 999

Which is the order in which operators are executed (from first to last)? A. arithmetic operators, logical operators, comparison operators B. logical operators, arithmetic operators, comparison operators C. arithmetic operators, comparison operators, logical operators D. comparison operators, arithmetic operators, logical operators

C. arithmetic operators, comparison operators, logical operators

Consider the following program: a = 1 while True: a += 1 print ("#" * a) if a % 5 == 0: break How many lines of output will be generated? A. two lines B. three lines C. four lines D. five lines

C. four lines

Which of the following is the correct if clause to determine whether y is in the range 10 through 50? A. if 10 < y or y > 50 B. if 10 > y and y < 50 C. if y > 10 and y < 50 D. if y > 10 or y < 50

C. if y > 10 and y < 50

Which of the following is not a valid way to control a while-loop? A. checking an accumulator variable B. checking a boolean value C. performing an arithmetic expression D. performing a comparison expression

C. performing an arithmetic expression

Assume you wanted to perform some mathematical calculation using the variable "x" (such as adding 5 to its value) - which line of code would allow you to do this? A. x = "1000" B. x = "one thousand" C. x = 1000

C. x = 1000

What is the value of x and y respectively at the end of this while loop? x = 0 y = 1 while x <5: y = y + x x = x + 2 A. x = 0, y = 1 B. x = 4, y = 11 C. x = 6, y = 7 D. x = 6, y = 13 E. x = 6, y = 6

C. x = 6, y = 7

Which of the following is both a good and valid variable name? A. 3rdAve = "Bowery" B. my name = "Sally" C. your_grade = 98 D. greeting! = "Hi, there!" E. x = "John" F. time? = 4.00 G. "my name" = "Mary"

C. your_grade = 98

Which of the following lines of code is valid? A. print("Hello, World!) B. print("Hello, World!') C. """print("Hello, World!") D. #print("Hello, World!)

D. #print("Hello, World!)

A "string" is: A. A sequence of numbers that represents numeric information B. A programmatic construct that lets the programmer store information during the execution of a program C. A reusable command that the programmer can "call" to execute a series of statements D. A sequence of characters that represents textual information

D. A sequence of characters that represents textual information

Given the following code, how many times will the string "Hello" print a = 5 b = 10 while a == 5: print ("Hello") A. 1 time B. 5 times C. 0 times D. An infinite number of times

D. An infinite number of times

Imagine that you have a single line of Python code that reads: print ("Hello, world!") If you wanted to run this statement and immediately see the result you could do so by using __________ mode in IDLE. A. Functional B. Object Oriented C. Script D. Interactive

D. Interactive

What is the structure that causes a statement or a set of statements to execute repeatedly? A. Sequence B. Decision C. Module D. Repetition

D. Repetition

Imagine you have the following incomplete code: if ______: print ("got here!") if ______: print ("got here!") if ______: print ("got here!") If a valid boolean expression was filled into each blank, what is the maximum # of times the String "got here!" could theoretically be printed? A. None B. One C. Two D. Three

D. Three

The following line of code will cause your program to crash: #print( Hello World! ) True False

False

The following line of code will cause your program to crash: #print( Hello World! ) True False

False

You always need to include "elif" and "else" statements after writing an "if" statement. True False

False

You would use Python's "Interactive Mode" to write a long program that contains many lines of code. True False

False

What is a function?

Functions are pre-written pieces of code that performs a specific action or set of actions.

What does IDLE stand for?

Integrated Development Environment

Logic error

Logic errors are due to faulty algorithms — your program technically works but it doesn't produce the output you're expecting it to

How do you create more complex math statements in Python?

PEMDAS (Python using the standard "order of operations" rules that we all learned in Algebra. Python supports these rules as-is (i.e. PEMDAS - Parenthesis, Exponents, Multiplication, Division, Addition, Subtraction).)

What are four examples of data types?

Strings (str) Numeric data typesIntegers (int) Floating point numbers (float) Boolean (bool)

Syntax error

Syntax errors have to do with a misuse of the language itself. (An error that results when an instruction does not follow the syntax rules or grammar of the programming language.)

What are the three classes of errors?

Syntax, Runtime and Logic

What is IF-ELSE structure?

The IF-ELSE structure allows you to respond to a condition if it evaluates to True (like an IF statement) as well as if it evaluates to False.

What does an input() function always "return"?

The input() function always "returns" a string (meaning that anything that "comes out" of the input function is treated as text. If you want to do anything to that text (say, treat it as a number and use it in a calculation) you must convert it to a number first. You can use the float() and int() functions to convert string data into numeric data as follows:)

Modulo operator ("%")

The modulo operator evaluates to the remainder of the division of two integer operands. Examples: 24 % 10 is 4. Reason: 24 / 10 is 2 with remainder 4. 50 % 50 is 0. Reason: 50 / 50 is 1 with remainder 0. 1 % 2 is 1. Reason: 1 / 2 is 0 with remainder 1 (returns the remainder of a division operation as a whole number. The modulo operator is used a lot in game programming to perform an action at a certain frequency. It's also used a lot in time and date calculation as it allows you to extract out the whole number remainder of a division operation.)

An expression attached to an "if" statement should evaluate to a Boolean data type. True False

True

Python supports two different division operators True False

True

The following line of code will cause your program to crash: print( Hello World! ) True False

True

Is the order of an IF - ELIF - ELSE block important?

Yes (conditions are tested in the order in which they are written. Once a condition evaluates to True all future conditions are skipped. An ELSE statement at the end of a decision structure is considered the "catch all" statement — if all conditions above end up failing then the statements inside the ELSE block will execute. However, using an ELSE statement at the end of your decision structure is optional. There is no logical need for an IF-ELIF-ELSE statement. You can always write a program without it by using a standard IF-ELSE block. The advantage of an IF-ELIF-ELSE statement is that your code may end up being be more readable / understandable.)

Can you write conditions using "Boolean Expressions"?

Yes,

Is Boolean a data type?

Yes, ( Python supports a Boolean data type the same way it supports ints, floats and strings. Called a 'bool', the Boolean data type lets you store Boolean data (True / False) in a variable. Boolean variables can be very useful when writing complex programs with many "states". We sometimes refer to them as "Flags" as they allow us to mark specific occurrences or "modes" of operation in our programs)

What is data type?

a particular kind of data item, as defined by the values it can take, the programming language used, or the operations that can be performed on it. (Ex. Strings, Numeric data types: Integers (int), Floating point numbers (float) )

sequence structure

a set of statements that execute in the order that they appear (linear "laundry lists" of code that are executed in the order in which they appear.)

Basic Math

addition = 5 + 2 subtraction = 10 - 5 multiplication = 2 * 5 division = 10 / 3 # floating point division

What is nested decision structures?

allow you to ask "follow up" questions after you've evaluated the value of a Boolean expression. The technique essentially allows you to place an "if" statement inside another "if" statement.

decision structure

allows programs "ask questions" based on the data at hand (This is useful if you only want some of your code to run if a certain condition exists (i.e. only print out "you won!" if a variable called "points" is greater than 5))

format() function

allows you to create formatted string versions of numeric values. The format() function takes two arguments: a numeric value and a "pattern" — and it returns a string

What is len() function?

allows you to determine the length of any string as an integer

What is the ELIF structure?

allows you to test multiple Boolean expressions inside the same selection statement. (This allows you to essentially ask a list of questions, and once Python finds one condition that is True it will execute that block and skip the rest.)

Logical operators

and, or, not

Variables

are identifiers that allow you to store information in your computer's memory. Variables can store lots of things, including strings and numbers.

What are the two number data types Python supports?

floating point (float) and integer numbers(int) (Integers (int) are whole numbers such as 5, -5, 1000, etc. Floating Point number (float) are numbers that contain a decimal point such as 5.0, -5.0, 1000.99, etc.)

What are the two different division operators that Python supports?

floating point division and integer division. (Floating point division (operator = '/') divides two numbers and returns a float along with the fractional value expressed as a decimal sequence. Integer division (operator = '//') divides two numbers and throws away the fractional value, returning an integer. For positive numbers it will always round down. For negative numbers it will always round away from zero.)

What are three functions examples?

input(), format(), and print() functions

What are the two modes in IDLE?

interactive mode and script mode (Interactive mode functions a lot like a calculator—you issue a command to the program, the command is executed and the result is immediately displayed. Script mode allows you to compose your code in a separate window and save it as a new text document on your computer.)

Exponent operator ("**")

raises one number to the power of another

sep=

tells R to not separate objects by a space (The print() function also is designed to automatically place spaces between elements. You can override this behavior by using the "sep=")

condition controlled loop

the looping behavior of the structure is dependent on the evaluation of a condition (i.e. a Boolean expression, The syntax for a condition controlled loop is sometimes called a "while" loop).

What are "Boolean Expressions"?

these expressions allow you to compare multiple values to determine their relationship to one another. A Boolean expression always uses a Boolean operator to compare two or more values. (a > b # is a greater than b? a < b # is a less than b? a == b # is a equal to b? a >= b # is a greater than OR equal to b? a <= b # is a less than OR equal to b? a != b # is a not equal to b?)

input

user input and how you can use Python to prompt a user to type in values using their keyboard. This input can then be used in your program in the place of string literals or numeric values. Gathering information in this way requires the use of the input() function. Here's an example that asks the user to input their name and stores the result in a variable called 'username'


Conjuntos de estudio relacionados

Laryngology ENT MCQS- 4th Year- PMU

View Set

fr2 passe-compose w/ être (to prepare for avalanche)

View Set

BIOL 1001 Hrincevich Chapter 2 HW/Quiz Questions

View Set