ISE 150 Midterm
To get the character equivalent of an ASCII code use...
"chr"
To get the ASCII value of a single character use...
"ord"
The arithmetic operators of Python
+ = addition - = subtraction * = multiplication / = true division (float) // = integer division % = modulus (remainder) ** = exponent
What are the ASCII coded for a capital A and a lowercase a?
A = 65, a = 97
What is the key and the value in a dictionary?
A key is like a word to look up A value is the word's definition
What must one do to Boolean values to satisfy Python?
Capitalize them (True and False)
How do you convert data types?
Cast a variable as another data type Casting is not permanent, it lasts only for that line of code
What is an iteration in a loop?
Each time the statements in the body of a loop execute
What can delimiters be used for?
Either used to: 1) separate list items in a new string 2) determines where to break up a string
True or False: Python guarantees that sets are stored in ascending order
False
True or False: We are guaranteed an order when looping through a dictionary.
False We are not guaranteed any kind of order
Representing booleans in numbers and strings:
For numbers: 0 is false anything else is true For strings: the empty string ("") is False anything else is True
5 Characteristics of a tuple:
Is a data type Is immutable Can be heterogeneous Is a sequence Has individually accessible elements
Characteristics of a string include: (5 things)
Is a data type Is immutable (once created, it cannot be changed) It is homogenous (it can only hold a series of characters) Is a sequence (we can use loops to iterate over each item in the string) Has individually accessible characters
5 characteristics of sets:
Is a data type Is mutable Can be heterogeneous Is a sequence Does NOT have individually accessible element
5 Characteristics of a dictionary:
Is a data type Is mutable Can be heterogeneous Is a sequence Has individually accessible values
In Python a list: (5 characteristics)
Is a data type (you can assign it to a variable) Is mutable (you can change individual elements) Can be heterogenous (it can hold multiple items of different data types) Is a sequence (we can use loops to iterate over each item in the list) Has individually accessible elements
What does the randrange function do?
It takes an integer for input and will pick a random value between 0 and one before that integer
How do the negative indices in Python work?
Last item in string is at index -1, first item in string is at index (negative length)
Differences between lists and dictionaries (how they are stored and how they are accessed):
List items are stored sequentially, and the data stays in insert order List items are accessed sequentially (by index) VS Dictionary items are NOT stored sequentially (stored to optimize access) Dictionary items are accessed by key
syntax of list vs. tuple
Lists use square brackets, tuples use parentheses Both separate items with commas
What does "plus equals" do?
Manipulates a variable to reassign it a new value x = 11.5 x += 5.2 is the same as x = x + 5.2
2 conditions of a value in a dictionary:
May not be unique May be mutable or immutable
2 rules for a key in a dictionary:
Must be unique Must be immutable (string, number)
Do string abilities change the original variable?
No
How many lines of output will each print command display?
One line
How does Python follow operator precedence?
PEMDMAS 2nd M is for modulus
How to create an empty set:
Python thinks this is an empty dictionary: fibSet = {} Need to tell Python it's a set: fibSet = set( )
True or False: Sets ignore duplicates
True
True or False: The elif must always occur somewhere after an if and before an else
True
How to check against a set
print(1 in fibSet) "True"
What is "not" in Python?
the logical negation (makes what is True, False, and what is False, True)
How to access individual items in the dictionary:
use same square brackets as list, but has string inside instead print(airports[ "DFW" ]
How to check if an element is in a sequence or a list?
use the "in" operator
how to add or remove from a set:
use the add function fibSet.add(8) use the remove function fibSet.remove(8)
How to get "prettier" string output?
use { } brackets and the .format function
What is the syntax for slicing strings in Python?
name = message [ 3 : 6 ] square brackets required colon required message = sequence to retrieve parts from 3 = index to start "slicing"; if omitted assume the beginning 6 = slice up to BUT NOT INCLUDING this position (or index); if omitted, assume the end
How to access parts of a module?
Begin with the module name followed by a period, then whatever part you need
difference between "and" and "or" in Python
Both things must be true in an "and" statement in order for the whole statement to be True Only one item has to be true in an "or" statement in order for the whole statement to be True
True/real division will always yield a...
float
How to access a module?
import keyword
What is the only way to create code blocks in Python?
indenting
Integer division only yields the... (what does this mean??)
integer part of the division (the remainder is lost) If one of the values is a float, the remainder is still lost but the result will still be a float Ex: 5 // 2.0 = 2.0
How to determine the length of a sequence or list?
len()
If you have a slice on the left, you need a...
list on the right
3 different list functions and their descriptions:
.append(value) -- adds value to end of a list .sort( ) -- sorts the elements, smallest value first del someList[ i ] -- removes the element at the specified index
How to generate a sequence with range (with 1 parameter and 3 parameters):
1 parameter: range(num) -- will generate a sequence beginning at 0, increasing by 1, and goes up to (but not including) num 3 parameters: range(start, stop, step) -- begins at start, increases by step, goes up to but does not include stop
Number type rules: 1) Operations with 2 ints yields... 2) Operations with 2 floats yields... 3) Operations with an int and a float yields...
1) a new int 2) a new float 3) a new float
What data type does "input" give?
A string
What is a sentinel value?
A value we watch to see if it changes to a specific value
When strings are converted to lists, what characters are converted to list elements?
All characters, including white-space (spacebar, tab key, or enter)
What is a sequence in Python? What can they be thought of as?
An ordered set of items; they can be thought of as a collection of items with a distinct starting and stopping item the items can be of any value
Definition of a tuple; what differentiates them from lists?
Another kind of sequence though unlike lists, tuples are collections that cannot change once they are created
What question does the exclamation point + equals sign ask?
Are these two values not equal
What question does the double equals sign ask? HOW IS IT DIFFERENT FROM ONE EQUALS SIGN?
Are these two values the same? Assignment is one equals sign, comparison is two equals signs
When is a loop considered infinite?
If there is no way the condition can ever be false
What is the difference between compiled code and interpreted code?
In compiled languages, all the code is processed by a tool once and the resulting output can be run any number of times (C++) In interpreted languages each line of code is converted to executable code every time the program runs (Python)
Difference between integers and real numbers. What is each called in Python?
Integers are numbers with no fractional part (called ints) Real numbers are numbers with fractional parts (called floats)
Declaring a set (syntax):
Set items appear inside curly braces Set items are comma separated Variable to hold the set is on left side of equals sign
What data types are immutable and which are mutable?
Strings, ranges, tuples are immutable Lists, dictionaries, and sets are mutable
What are the first and last index of a string in a sequence or list?
The first index is 0 The last index is the (length - 1)
When creating variables, which item receives and which item gives?
The item on the left side of the equal sign receives a value The item on the right side gives a value
How to add items to a new dictionary (syntax):
The new key appears inside the square brackets, the new value must be to the right of the equals sign airports[MDW] = "Midway Airport"
Characteristics of escape sequences
They are a variation (or escape) from the character's normal meaning Preceded by a backslash Indicated by 2 characters (backslash + character), but Python reads it as 1
True or False: the item at index 0 does not exist in empty lists
True
String abilities and how to format them
UPPER CASE ( print(variable.upper()) lower case (.lower()) Title Case (.title()) sWAP cASE (.swapcase()) Capitalize (.capitalize()) Strip (eliminates leading and trailing spaces)
How to display union and intersection for sets:
Union: print (primeSet | evenSet) Intersection: print (primeSet & evenSet)
General syntax of a dictionary
Use curly braces Colon separates keys and values Keys appear before the colon, values after the colon Each key-value pair is comma separated from the next
How to convert lists into strings (what command is used and what must your first step be)
Use the join command Must decide on a delimiter (something that separates the list items in the new string) delimiter = " " newString = delimiter.join(nameList)
What is the camelCase naming convention? When is it used?
When naming variables, run the words together but capitalize each subsequent word the first letter is always lowercase
To use an index to access a sequence or list, use the...
[ ] operator
List the Escape Sequences + Descriptions of each
\n = new line \t = horizontal tab \b = backspace \r = carriage return \\ = backslash \" = double quote
What is required after every conditional???
a colon (:) and a tab indent on the nested statement after
What is a loop?
a type of statement that allows any number of statements to continue to execute so long as a particular condition is true
Variable names in Python: (4 things)
must begin with a letter or underscore may consist of letters, numbers, or underscores are case-sensitive cannot be named after Python "keywords"
Arithmetic is performed _____ to _____ Assignment is performed _____ to _____
arithmetic = left to right assignment = right to left
Every value has a _______ in Python
data type
Just like in algebra, you must _____ a variable before you can use it in later expressions
declare
How to shift a range in Python?
diceNumber = random.randrange(6) + 1
Text in Python can be thought of as a...
string of individual characters, is often just called a string
the set and order of commands in a high level programming language (i.e. the grammar the computer understands)
syntax
The effect of sort does NOT....
yield a newly sorted list, it changes the original list example: nums = [6, 9, 12, 3 ] newList = nums.sort( ) print(newList) print(nums) print(newList) would yield "none" as a new list has not been generated print(nums) would generate the list from least to greatest