1/71
i am so cooked
Name | Mastery | Learn | Test | Matching | Spaced | Call with Kai |
|---|
No study sessions yet.
What is a comment in a computer program?
Text intended only for the human reader that is completely ignored by the interpreter.
How do you start a comment in Python?
Using the "#" token.
Value
One of the fundamental things that a program manipulates.
Variable
A name that refers to a value.
Integer
Positive or negative whole numbers
float
decimal numbers
bool
(boolean: True, False)
NoneType
(default data type returned when a function stores nothing)
String
Anything surrounded by single or double quotes
What is the purpose of the type() function?
To find out what "class" a value falls into.
Variable Naming Rules (Python)
Names can be arbitrarily long , contain letters, underscores, and digits, but must begin with a letter or underscore. Case matters.
Assignment Operation
The assignment statement gives a value to a variable.
int() function
Takes a floating point number or a string and turns it into an int. For floats, it discards the decimal portion (truncation towards zero).
float() converter
Can turn an integer, a float, or a syntactically legal string into a float.
str() converter
Turns its argument into a string.
What does the + operator do with strings?
It causes concatenation, which means joining the two operands end to end.
What does the * operator do with strings?
It performs repetition.
len() function
Returns the number of characters in a string.
Are strings mutable?
No, strings are immutable, meaning you cannot change an existing string.
Function (Python)
A named sequence of statements that belong together, primarily to organize programs into logical chunks.
Syntax for defining a function
def NAME(Parameter): followed by indented STATEMENTS.
Parameters
Specifies what information, if any, is needed to use the new function.
Arguments
Values provided in a function call that are assigned to the parameters in the function definition.
Scope of Variables and Parameters
They are local, meaning a variable created inside a function only exists inside the function definition and cannot be used outside.
print vs. return (Key Difference)
print displays stuff to the screen and the function continues execution; return stops the execution of further statements and returns/stores a value to the function. A function can only return one thing.
Boolean Expression
True or False
Conditional Statement
A statement that controls the flow of execution based on some condition.
if Statement Execution Rule
At most one block of statements will be executed; the first block that evaluates to True gets executed.
Iteration
Repeated execution of a set of programming statements.
for loop
Used for iterating over a sequence(string, list, tuple, range)
while loop
Executes a set of statements as long as a condition is true
break statement
stops the loop entirely, “breaks” out of it
List
Collection of data stored in between brackets
Are lists mutable?
Yes, lists are mutable; you can change their elements by a simple assignment.
Tuple
Collection of data stored in between parenthesis
Are tuples mutable?
No, tuples are immutable, but if there is a list inside a tuple, that list can be modified.
Dictionary
Python's built-in mapping type that maps keys to values.
Dictionary Keys
Must be of an immutable type.
Dictionary Values
Can be of any type.
Are dictionaries mutable?
Yes, dictionaries are mutable.
myDict.keys()
Returns a list of all the keys in the dictionary.
myDict.items()
Returns a list of all the key-value pairs as tuples in the format (key, value).
What are the four basic data types listed, and what is a common non-basic type?
Basic: int, float, bool, NoneType. Common Non-Basic: str (String).
What is the rule for the very first character in a Python variable name?
It has to begin with a letter or underscore.
What is the name of the type conversion process that the int() function performs on a floating point number?
Truncation towards zero on the number line (discarding the decimal portion).
If you enclose the number 123 in quotes, what data type is it classified as?
String (str).
What is the priority order for mathematical operations in Python, according to PEMDAS?
Parentheses $>$ Exponentiation $>$ Multiplication = Division = Modulo $>$ Addition = Subtraction.
What is the term for what the + operator does when used with two strings?
Concatenation, which means joining the two operands end to end.
What value does the expression 2 == 2 evaluate to?
A Boolean value: True.
What is a characteristic of strings that means you cannot change an existing string?
Strings are immutable.
What is the primary purpose of functions in programming?
To help organize programs into chunks that match how we think about the problem.
In a function definition, variables and parameters are considered to be what?
Local.
What is the main effect of a function hitting a return statement?
It stores the value returned and stops executing any further statements in the function definition.
What is the default value returned by the print statement?
None.
In an if Statement group, when does the first block that evaluates to True get executed?
It gets executed irrespective of whether the next blocks in the same family evaluate to True or not.
When should you choose a for loop over a while loop?
Use a for loop if you know, before you start looping, the maximum number of times you'll need to execute the body.
What is an infinite loop?
A loop that repeats forever because the body of the loop does not change the value of variables necessary to make the condition eventually become False.
What does the range(start, stop) function return?
A list of numbers from start up to but not including stop.
What is the key difference between lists and strings in terms of mutability?
Lists are mutable (you can change elements) , while strings are immutable (you cannot change them).
What are the two types of components in a Python dictionary?
They map keys (must be immutable type) to values (can be of any type).
What dictionary method returns a list of all the key-value pairs as tuples?
myDict.items()
What are the three access modes for opening a file?
"r" (read), "w" (write), "a" (append).
What happens if you open a file in "w" (write) mode when the file already exists?
Writing to the file will overwrite the file.
Mutable types
Lists and dictionaries
Immutable types
Int, Float, String, Tuple, Boolean
Dictionary
Collection of keys and values store in between curly braces
x = ‘Hello World’
What index would we use to get “ello”?
x[1:5]
fruits = [“Apple”, “Pear”, “Berry”, “Banana”]
What index is Berry at?
fruits[2]
list = [3, 6, 8, 1, 2, 5, 0, 7, 2, 9]
i = 0
j = 0
while i < len(list) and list[i+1] != 0
if list[i] % 2 == 1:
j = j + 1
i = i + 1
What are the values of i and j after this loop is executed?
i = 5
j = 2
What range function generates the numbers 1, 5, 9, 13?
range(1, 14, 4)
What numbers ger generated by range( 6, 2, -1)
6, 5, 4, 3
n = 4
for i in range (1, n + 1):
for j in range ( 1, n)
if j < i:
print(i * j)
What does this loop print out?
2
3
6
4
8
12