ICS Python Review
Which mathematical operator is used to raise five to the second power in Python? Hint - exponents
**
exponent
**
exponents in python
**
floating point numbers
-Scientific notation in computers -Allows very large and small numbers using exponents -Made up of: *Significand*: 5, 1.5, -2.001 *Exponent*: 2, -2 -Put decimal after integers to make floating point 1 ~ 1.0
Treat if statements like...
..paragraphs. Each if, elif, and else grouping is like a set of sentences. Put blank lines before and after.
What are the values that the variable num contains through the iterations of the following for loop? for num in range(4)
0, 1, 2, 3
function
1. Teaches Python to do something 2. Can be used to simplify code or shorten code 3. Looks like: - def function(): t - call function()
What are the values that the variable num contains through the iterations of the following for loop? for num in range(2, 9, 2)
2, 4, 6, 8
If value1 is 2.0 and value2 is 12, what is the output of the following command? print(value1 * value2)
24.0
The program development cycle is made up of _____ steps that are repeated until no errors can be found in the program.
3
After the execution of the following statement, the variable price will reference the value _____. price = int(68.549)
68
After the execution of the following statement, what will be printed? print int(68.549)
68
Comparison operators
< less than > greater than <= less than of equal to >= greater than or equal to == equal to != not equal to
What's the difference between = and == in Python?
= is the assignment operator. == is the equality operator.
int
A Python data type that holds positive and negative whole numbers
float
A Python data type which stores floating-point numbers. Floating-point numbers are stored internally in two parts: a base and an exponent. When printed in the standard format, they look like decimal numbers
Iteration
A Repeat or Loop. This is where the code uses "while" or "for" loops
Floating Point
A decimal
module
A file containing definitions and statements intended to be imported by other programs.
prompt
A formatter text that is used to give the user the ability to type in a question
boolean function
A function that returns a Boolean value. The only possible values of the bool type are False and True.
A function used in a program or script that causes the Python interpreter to display a value on its output device.
block
A group of consecutive statements with the same indentation.
Variable
A letter or word for a value that can vary or change
nested loop
A loop inside the body of another loop.
for loop
A loop where we have an upper bound on the number of times the body will be executed. Definite iteration is usually best coded as a for loop
close
A method/function/command to close the file
truncate
A method/function/command to empty the file. Be careful if you care about the file
readline
A method/function/command to read just one line of a text file
read
A method/function/command to read the file
function
A named sequence of statements that performs some useful operation. Functions may or may not take parameters and may or may not produce a result
high-level language
A programming language like Python that is designed to be easy for humans to read and write.
algorithm
A set of specific steps for solving a category of problems
operator
A special symbol that represents a simple computation like addition, multiplication, or string concatenation.
conditional statement
A statement that controls the flow of execution depending on some condition. In Python the keywords if, elif, and else are used for conditional statements.
string
A string is a list of characters inside of quotes. Strings can be made of single, double or triple quotes.
strings
A string is usually a bit of text you want to display to someone, or "export" out of the program you are writing. Python knows you want something to be a string when you put either " (double-quotes) or ' (single-quotes) around the text. You saw this many times with your use of print when you put the text you want to go inside the string inside " or ' after the print to print the string. Strings may contain the format characters you have discovered so far. You simply put the formatted variables in the string, and then a % (percent) character, followed by the variable. The only catch is that if you want multiple formats in your string to print multiple variables, you need to put them inside ( ) (parenthesis) separated by , (commas). It's as if you were telling me to buy you a list of items from the store and you said, "I want milk, eggs, bread, and soup." Only as a programmer we say, "(milk, eggs, bread, soup)."
String
A text value such as a word or name
variable
A variable is something that holds a value that may change. In simplest terms, a variable is just a box that you can put stuff in. You can use variables such as numbers. Ex: lucky = 7 print(lucky) 7
Integer
A whole number
The variable used to keep the running total
Accumulator
The variable used to keep the running total
Accumulator / counter
semantic error
An error in a program that makes it do something other than what the programmer intended.
syntax error
An error in a program that makes it impossible to parse — and therefore impossible to interpret.
Syntax error / bug
An error in the code that means it will not run. Incorrect spelling of keywords, leaving off speech marks or brackets, not using colons for "if" statements.
runtime error
An error that does not occur until the program has started to execute but that prevents the program from continuing.
Logic error / bug
An error that means the code will run, but will not do what is expected.
type conversion
An explicit function call that takes a value of one type and computes a corresponding value of another type. Example: print str(age)
boolean expression
An expression that is either true or false.
integer division
An operation that divides one integer by another and yields an integer. Integer division yields only the whole number of times that the numerator is divisible by the denominator and discards any remainder.
=
Assignment operator. Sets one side equal to the other side.
*
Asterisk: used for miltiplication
The following is an example of an instruction written in which computer language? 1011000
Binary / machine language
increment
Both as a noun and as a verb, increment means to increase by 1.
When the + operator is used with two strings, it performs string _______________.
Concatenation
__________________________ is the term that means the "joining character strings end-to-end"
Concatenation
A(n) ??-controlled loop causes a statement or set of statements to repeat as long as a condition is true.
Condition
What type of loop structure repeats the code based on the value of the Boolean expression
Condition-controlled loop
int()
Convert a string to an integer int(input("What is your favorite number? " ))
str(variable_name)
Converts the variable data to a String
int(variable_name)
Converts the variable data to an integer
What type of loop structure repeats the code a specific number of times
Count-controlled loop
#
Creates a single line comment
def
Defines a function def function1(): print "this is function 1"
Which of the following is considered to be the world's first programmable electronic computer
ENIAC
"
Every time you put " (double-quotes) around a piece of text you have been making a string. A string is how you make something that your program might give to a human. You print strings, save strings to files, send strings to web servers, and many other things. ' (single-quotes) also work for this purpose.
True/False: According to the behavior of integer division, when an integer is divided by an integer, the result will be a float.
False
True/False: Both of the following for clauses would generate the same number of loop iterations (repeats): for num in range(4): for num in range(1,5):
False
True/False: In Python, an infinite loop usually occurs when the computer accesses the wrong memory address.
False
True/False: Python allows you to compare strings, but it is not case sensitive.
False
True/False: Python formats all floating-point numbers to two decimal places when outputting using the print statement.
False
True/False: Python limits all floating-point numbers to two decimal places when outputting using the print statement.
False
True/False: The Python language is not sensitive to block structuring of code.
False
.index(item)
Find the index of an item
In Python, you would use the ?? statement to write a count-controlled loop.
For
>=
Greater than or equal
The _______________ statement is used to create a decision structure.
If
What is the disadvantage of coding in one long sequence structure?
If parts of the duplicated code have to be corrected, the correction has to be made many times.
A(n) ?? validation loop is sometimes called an error trap or an error handler.
Input
Input()
Input prompts the user for an input and then turns that input into a string. In between "(" and ")" the programmer writes the prompt that will prompt the user. When you set input() equal to a variable, that variable becomes what the user inputs.
_____ is the process of inspecting data that has been input to a program to make sure it is valid before it is used in a computation.
Input validation
<=
Less than or equal to
A _________________________ causes a statement or set of statements to execute repeatedly.
Loop
Where does a computer store a program and the data that the program is working with while the program is running?
Main memory
"""
Multi-line comment start (or end)
element
One of the values in a list (or other sequence). The bracket operator selects elements of a list.
open(argument, 'w')
Open a file with an extra parameter. Python has several open parameters that open a file different ways
os.path
Operating system path module: allows many functions to occur on a specified path. Ex: os.path exists will return a true or false if a file does or doesn't exist
When using the _____ operator, one or both subexpressions must be true for the compound expression to be true.
Or
PEMDAS
Order of Operations: Mode of Operations: Parentheses Exponents Multiplication Division Addition Subtraction
PEDMAS
Order of operations: parentheses, exponents, multiplication, division, addition, subtraction
Order of operations
PEMDAS (parenthesis, Exponents, Multiplication, Division, Addition, Subtraction)
raw_input
Pauses the script at the point it shows up, gets the answer from the keyboard, then continues the script. It is one of python's built-in functions Looks something like: var = raw_input("Enter_Something") Where "Enter_Something" is what the prompt is, asking you to enter some text, and var is where the text is stored Remember the %r in the prompt
%
Percent: used for modulus. The modulus operation finds the remainder after division of one number after another. Example: 75 % 4 = 3, because 4 * 18 is 72, with 3 remaining
Formatter
Placeholders that "punch out a hole in the code % is the character for this
True False
Python recognizes True and False as Boolean values If you put quotes around them then they are turned into strings and won't work.
Modules
Python's built-in features are called modules. Also called "libraries" by some programmers. Example speech: "you want to use the sys module."
What symbol is used to mark the beginning and end of a string?
Quotes
Main memory is commonly known as _______________.
RAM
What type of volatile memory is usually used only for temporary storage while running a program?
RAM
The ?? function is a built-in function that generates a list of integer values.
Range
input('prompt:')
Reads a line of input from user and returns as string
iteration / looping
Repeated execution of a set of programming statements.
A(n) ?? structure causes a statement or set of statements to execute repeatedly.
Repetition
What is the structure that causes a statement or a set of statements to execute repeatedly?
Repetition
/
Slash: used for division
pixel
Smallest addressable element of a picture.
Function
Some code that has been grouped together so that it can be reused by "calling" the function name. Like a mini-program within a program.
In Python, the variable in the for clause is referred to as the _____ because it is the target of an assignment at the beginning of each loop iteration.
Target Variable
/
Tells Python to divide. Division operator.
How do I get a number from someone so I can do math?
That's a little advanced, but try x = int(input("What number do you want to enter? ")) which gets the number as a string from raw_input() then converts it to an integer using int()
flow of execution
The order in which statements are executed during a program run.
What is the output of the following print statement? print("The path is D:\\sample\\test.")
The path is D:\sample\test.
syntax
The structure of a program
Data type
The type of data being used. Could be any of these: integer, float, string, boolean
FOR Loop
To repeat a commands a set number of times.
iteration / loop
To repeat a section of code.
WHILE Loop
To repeat while a condition is true (e.g. while score < 100)
evaluate
To simplify an expression by performing the operations in order to yield a single value.
IF Statement
To test if a condition is true (e.g. if age >17)
True/False: A better way to repeatedly perform an operation is to write the statements for the task once, and then place the statements in a loop that will repeat the statements as many times as necessary.
True
True/False: An action in a single alternative decision structure is performed only when the condition is true.
True
True/False: An action in a single conditional structure is performed only when the condition is true.
True
True/False: Computer programs typically perform three steps: Input is received, some process is performed on the input, and output is produced.
True
True/False: Expressions that are tested by the if statement are called Boolean expressions.
True
True/False: In Python, print statements written on separate lines do not necessarily output on separate lines.
True
True/False: In a nested loop, the inner loop goes through all of its iterations for every single iteration of an outer loop.
True
True/False: In flowcharting, the decision structure and the repetition structure both use the diamond symbol to represent the condition that is tested.
True
True/False: Nested decision structures are one way to test more than one condition.
True
True/False: Python allows programmers to break a statement into multiple lines.
True
True/False: RAM is a volatile memory used for temporary storage while a program is running.
True
True/False: The CPU understands instructions written in a binary machine language.
True
True/False: The Python language is sensitive to block structuring of code. In other words, code indentation matters.
True
True/False: The \t escape character causes the output to skip over to the next horizontal tab.
True
True/False: The first line in the while loop is referred to as the condition clause.
True
True/False: The if statement causes one or more statements to execute only when a Boolean expression is true.
True
True/False: The quality of a program's output is only as good as the quality of its input. For this reason the program should discard input that is invalid and prompt the user to enter correct data.
True
variables can be reassigned (or changed) within a program
True
"""
Use to make a comment that needs multiple lines for the comment text.
if
Used to determine, which statements are going to be executed
When will the following loop terminate? while keep_on_going != 999 :
When keep_on_going refers to a value not equal to 999
A _________________________ loop causes a statement or set of statements to repeat as long as a condition is true.
While
script
Will input the name of the script into your code when called
Write comments on code ...
You write comments only to explain difficult to understand code or why you did something. Why is usually much more important, and then you try to write the code so that it explains how something is being done on its own. However, sometimes you have to write such nasty code to solve a problem that it does need a comment on every line. In this case it's strictly for you to practice translating code to English.
The line continuation character is a _____.
\
boolean
a data type that is like a light switch. it can only have two values: true, false
What type of loop structure repeats the code a specific number of times
a for loop
variable
a name for a place to store strings, numbers etc.
argument
a value provided to a function when the function is called. This value is assigned to the corresponding parameter in the function.
What type of loop structure repeats the code based on the value of the Boolean expression
a while loop
input
accepts a string, prints it, and then waits for the user to type something and press Enter (or Return).
Floating Point Number
any number with a decimal point showing one or more digits behind the decimal point. e. "4.0" or "0.087"
for loop
applies function to every item in list for x in a: print x can sort functions for number in my_list print number #prints out every number on its own line
=
assigns values from right side operands to left side operand
functiion_name()
calls or runs the function called "function_name"
mutable
can be changed after created
string
can contain letters, numbers, and symbols
variables
can only start with a character (not a number)
Else should have a...
code that prints out an error message, in case the else doesn't make sense. Shows errors.
concatenation
combine text
_______________ are notes of explanation that document lines or sections of a program.
comments
A(n) _______________ expression is made up of two or more Boolean expressions.
compound
In a decision structure, the action is _______________ executed because it is performed only when a certain condition is true.
conditionally
A(n) _____ structure is a logical design that controls the order in which a set of statements execute.
control
A(n) ________________ structure is a logical design that controls the order in which a set of statements execute.
control
while
controls flow of the program with truth statements. Statements inside the while loop are executed until the expression evaluates false.
str
converts to a string
The _______________ is the part of a computer that actually runs programs and is the most important component in a computer.
cpu
how to loop through dictionaries keys
d = {"foo" : "bar"} for key in d: print d[key]
Python uses _______________ to categorize values in memory.
data types
How do you create a new function called "draw_a_square"?
def draw_a_square():
keyword
define the language's syntax rules and structure, and they cannot be used as variable names
A for-loop is...
defined to run a specific number of times
In flowcharting, the _______________ symbol is used to represent a Boolean expression.
diamond
d = {'key1' : 1, 'key2' : 2, 'key3' : 3}
dictionary **not curly braces
Every if statement must have a(n)...
else
In a print statement, you can set the _____ argument to a space or empty string to stop the output from advancing to a new line.
end
Conditional Statement: Else
executes some specified code after finding that the original expression was False (or opposite of the if command)
**
exponent
After the execution of the following statement, the variable sold will reference the numeric literal value as a(n) _____ data type: sold = 256.752
float
sold = 256.752 What type of variable if sold?
float
In Python, you would use the ?? statement to write a count-controlled loop.
for
The _______________ specifier is a special set of characters that specify how a value should be formatted.
formatting
The term _______________ refers to all of the physical devices that a computer is made of.
hardware
data types
i.e. numbers, booleans, strings, and floats
The _______________ statement is used to create a decision structure.
if
The decision structure that has two possible paths of execution is known as _____.
if / conditional
Python provides a special version of a decision structure known as the _______________ statement, which makes the logic of the nested decision structure simpler to write
if elif else
Conditional Statement: if
if is a conditional statement that executes some specified code after checking if its expression is True.
What statements are used to control the flow in Python programs?
if, for, while
A(n) _______________ statement will execute one block of statements if its condition is true, or another block if its condition is false.
if/else
comment
in a program that is meant for other programmers (or anyone reading the source code) and has no effect on the execution of the program
The _____ function collects data from the user and saves it to a variable.
input
The _____ function reads a piece of data that has been entered at the keyboard and returns that piece of data, as a string, back to the program.
input
The _____ built-in function is used to read a number that has been typed on the keyboard and set it into a variable.
input()
The _____ built-in function is used to read a number that has been typed on the keyboard.
input()
.insert(index, item)
insert a certain item into a list at a certain index
statement
instruction that the Python interpreter can execute
integer vs. float
integer is a number w/out a decimal; float is a number with a decimal
The Python _______________ is a program that can read Python programming statements and execute them.
interpreter
<, <=, >, >=
less than, less than or equal to, greater than, greater than or equal to
In Python, a comma-separated sequence of data items that are enclosed in a set of brackets is called a _____.
list
strings
list of characters, spaces, symbols
access dictionary values
list[item]
What type of error produces incorrect results but does not prevent the program from running?
logic
What is the structure that causes a statement or a set of statements to execute repeatedly?
loop
indentations in python
mean that there is a code block
!=
means doesn't equal
==
means equal to
!=
not equal
the != operator means _________________________________
not equal to
else
optional. Used after elif to catch other cases not provided for.
print to console
A _______________ is a set of instructions that a computer follows to perform a task.
program
A(n) _______________ is a set of instructions that a computer follows to perform a task.
program
What is the informal language that programmers use to create models of programs that have no syntax rules and are not meant to be compiled or executed?
pseudocode
for
repeat items of a collection in order they appear a specific number of times
assignment statement
replaces item in list list_name[index number] = "reassignment"
type()
returns what "type" of data ex: int, float, str
In _______________ mode, the interpreter reads the contents of a file that contains Python statements and executes each statement.
script
Conditional Statement: Elif
short for else if...otherwise, if the following expression is true, do this!
dictionary
similar to a list but you access values by looking at a key rather than an index useful for information using strings and values i.e. phonebook, email databases (passwords, and usernames)
key
similar to index but uses a string or number
Programs are commonly referred to as
software
operators
special symbols that represent computations like addition, multiplication and division
elif
stands for else if. if the first test evaluates to False, continues with the next one
variable
stores a piece of data and gives it a specific name
semantic
the meaning of a program
Which of the following represents an example to calculate the sum of the numbers (accumulator)?
total += number
Which of the following represents an example to calculate the sum of the numbers (accumulator)?
total = total + number
Boolean variable can contain one of which two values: _____________.
true or false
Boolean variable can reference one of two values: _____.
true or false
#
use for comments on the code, use to disable code placing a # at the beginning of a line or in the middle of a line tells python to ignore whatever is written on the line after the #
def
used to create a new user defined function
count = count + 1
used to increase the value held in the variable "count" by one
A(n) _______________ is a name that represents a value stored in the computer's memory.
variable
What is the format for the while clause in Python
while condition : statement
What is the format for the while clause in Python?
while condition : statement
for i in range(7):
will loop the code (in the code block) 7 times