112 Final Study Guide Chapters 8-13 Flashcards
What are the four collection data types in python?
List (ordered and mutable)
Tuple (unordered and immutable)
Dictonary (stores key:value pairs)
Set (unordered and unindexed)
How do tuples function?
Tuples store multiple values.
They are made up of multiple ordered and indexed elements.
How do tuples differ from lists?
Tuples are immutable (elements are unchangeable)
They are more secure than lists, and are used in situations where security matters.
Tuples also process faster than lists, making them suitable for larger chunks of data.
How are tuples created?
Tuples are created using parentheses, or by seperating values with commas.
You can create empty tuples with an empty pair of parentheses
tuple_1 = (1, 2, 3, 4, 5)
tuple_2 = 1., .5, .25, .125
tuple_3 = ()
What do you need to include when creating a single element tuple?
If you are creating a single element tuple, you must include a comma with the element.
tuple = (1,)
Name some functions, operators, and methods 1you can use on tuples.
len(), max(), min(), sum() functions can all be used on tuples.
+, *, in and not in operators can be used on tuples.
count() and index() methods can be used on tuples.
How do dictionaries work?
Dictionaries store data values in key:value pairs.
The word to you look for is called a key
The word you get back from the dictionary is a value
How do you create dictionaries?
You write dictonaries using curly brackets {}.
key:value pairs are written side by side, each key and value surrounded with quotation marks, and seperated by colons.
dict1 = {“cat” : “aaaaaa”, “dog” : “AAAAAA”, “fish” : “ooooooo”}
What are some rules you must follow when creating keys and values?
Keys must be uniquie, but may be a number or a string.
Keys are case sensitive.
Values can be any data type.
How is the dict() function used?
The dict() function can be used instead of braces to create a dictionary.
x = dict()
How is the items() method used on dictionaries?
You can use the items() method to loop through dictionaries.
Name some methods used on dictionaries.
items () loops through dictionary
update() can add / edit keys and values in a dictionary
keys() get all keys from a dictionary
values() get all values from a dictionary
len(), in and not in can all be used on dictionaries as well.
What are sets?
Sets are an uordered and unindexed collection. Sets contain no duplicate elements, and are written with curly brackets {}
What functions or operations can be used on sets?
len() in, not in are all used on sets.
How can the del keyword be used?
The del keyword can be used to delete elements from collection data types.
How is the copy () method used?
The copy() method creates copies of collection data types.
What are the data type conversion functions?
- int() - converts string, floating point to integer
- float() - converts string, integer to floating point number
- str() - converts integer, float, list, tuple, dict into string
- list() - converts data types into a list
- tuple () - converts data types into a tuple
What are functions?
Functions are blocks of organized, reusable code that together performs a specific task.
Functions only run when called.
Why are functions needed?
Reusability
Functions allow you to define the code a single time, and reuse it as many times as the code needs.
Modularity
Functions allow you to break up long strings of code into smaller steps, allowing you code to be easy to read and maintain.
What 3 groups are functions called from?
Built in Python functions
Functinos imported from modules.
User-defined functions.
How are functions created?
Functions are freated b defining and adding parameters.
def is always put before the function name
The name is followed by a pair of empty brackets () and then a colon :
After this, the body of the function is added on the next line.
When do functions run?
Functions only run when they are called.
Functions are called by writing the name of the function into the code.
def message(): # message is the function name
print(“Enter a value: “) #function body
message()
a = int(input())
Define function parameters and arguments.
Paramaters are the variables listed inside the parentheses next to the function name when defining a function.
Arguments are the values sent to the function when it’s called.
What are the two methods of passing arguments to functions?
Positional argument passing- this commonly used way of passing arguments follows the positions of the paramaterrs and arguments and passes them based on their positions.
Keyword (named) argument passing. The more flexable way of passing arguments. Arguments are passed based on their names.
What are arbitrary arguments?
Arbitrary arguments occur when the number of arguments sent to a function are variable.
Also known as variable length arguments.
Define returning values through functions.
Many functions can have results and return values. You can achieve this using the return keyword either
With an expression causing immediate termination of the functions execution.
Without an expression which immediately terminates the functions execution and returns to the point of invocation.
What are Called and Calling functions?
Called functions are functions which are called from other functions.
The function that calls the called function is called the Calling function.
im sorry guys this shit is so hard to read im trying to make something legible
Define scopes and their role with functions.
The scope of a name is the part of a code where the name is available / recognizable.
the scope of a function’s parameters and the functions variables is the function itself. They are local to the function (inaccessible from outside the function)
What are global variables?
Global variables exist outside of functions, but have a scope inside of a function’s body. This means that the function can use the global variable, but if the same variable name is re-assigned INSIDE the function, they become two different variables.
How does the global keyword function?
The global keyword is used inside a function to change the value of a variable created outside the function.
This will skip creating a local variable, and will use the global variable instead. If the global variable doesnt exist, it will be created.
What is a recursive function?
A recursive function calls upon itself. This technique is called recursion.
How do you prevent infinite recursion?
If you do not provide a condition, recursion will condinue indefinitely.
if-else statements can be used to prevent this, where one branch makes the recursive call and the other branch doesn’t.
What are the advantages and disadvantages of recursion?
Advantages:
It makes the code shorter and cleaner
It is required in problems with data structures and advanced algorithms.
Disadvantages
It uses more memory, and processor time compared to iteration.
What is the maximum filename length on Windows and Linux?
Windows, 256 characters
Linux, 255 Characters.
Name some text file types in Python.
- Web standards
- Source code
- Documents
- Tabular data
- Config files
Name some binary file types in python.
- Document files
- Image files
- Video files
- Audio files
- Database files
- Archive files
- Executable files
What are file paths?
A file path is the description of where the file can be found in in the folder structure, and how to access it.
Which slash direction is used for Windows? Which for linux?
For windows the back slash is used
For Linux the forward slash is used.
What basic file operations can be done in Python?
Basic File operations include:
- Open
- Close
- Read
- Write
What mode paramaters are used for opening files?
- r - open a file for reading only (the default open setting)
- w - open for writing, truncating the file first
- x - open file for exclusively for creation, fails if the file already exists.
- b - opens in binary mode
- t - opens in text mode (default mode)
- open for updating (both reading and writing)
Name some combined access modes.
What methods are used for reading a file’s contents?
read () and readline() are both used.
By default read() returns the entire text of the file, however you can specify how many characters the method will return.
readline() returns the file contents line by line.
readlines() returns a list of each line in the file as a list item.
How does looping through read files work?
If you loop through the lines of a file, you can read the entire file line by line.
How can the open() method create a new file?
The open() method creates files using one of 3 parameters:
“w” - Opens a new file for writing, Overwrites a file with the same name if it exists.
“a” - opens a file for appending, if the file doesn’t exist it will create a new file.
“x” - open a file for exclusive creation, returns an error if the file already exists.
How can you add to a newly created file?
Using the write() and writelines() methods, we can add text to a newly created file.
write () will add text to a file
writelines() will add a list into a file.
How does the with statement interact with files?
The with statement automatically closes the file after a block of code has been resolved.
list of file methods
What are modules?
A module is a file containing Python definitions of functions, variables, and classes.
The file name for modules have the suffix .py