CS MIDTERM 2

0.0(0)
Studied by 0 people
call kaiCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play

1/64

encourage image

There are no tags or description

Looks like no tags are added yet.

65 Terms

1
New cards

List-like structures

A data structure that holds multiple individual values gathered together under one variable name, accessed via indices

2
New cards

Index

A number used to access a particular element from a list-like data structure

3
New cards

String

A data structure that holds a list, or a string, of characters

4
New cards

Lists

A data structure that hold multiple individual values gathered together under one variable name, accessed via indices

5
New cards

Dictionaries

A data structure comprised of key-value pairs, where a key is entered into the dictionary to get out a value

6
New cards

Passing by value

a function receives a copy of a variable’s values rather than accessing the original variable itself

python passes basic data types like integers and strings by value

7
New cards

Passing by reference

Giving a function direct access to a variable’s original memory location(its address) rather than making a duplicate copy of its value

python passes more advanced data types like lists by references

8
New cards

Mutable variable

A variable whose value can change after it has been declared

lists


when changed, the new value is stored at the SAME memory address

9
New cards

Immutable variable

A variable whose value cannot change after it has been declared

integers, strings,etc

when changed, the new value is stored at a different memory address

10
New cards

memory address

tells the computer where to find a particular value for a particular variable name

the “id”(function in python), returns the place in memory that the variable points to

11
New cards

Methods

Functions that are contained within data types

Instead of calling directly, it has to be first said what variable is being referred to and then call the method inside that variable.

12
New cards

Unicode

A computing industry standard that sets hexadecimal codes correspond to what characters so that text appears consists across platforms

13
New cards

Hexadecimal

A short-hand expression of the ones and zeroes that comprise computer data, comprised of 16 characters, 0 through 9 and A through F

14
New cards

f string format

name = “Maya”

print(f"Hello, {name}!”)

15
New cards

Expressions in f-string

price = 20

print(f"Total with tax: ${price *1.08}”)

16
New cards

New line

mystringwithnewline = “12345\n67890”

print(mystringwithnewline)

12345

67890

mystring

17
New cards

String declaration

‘“12345”’

“‘12345’”

‘‘‘“‘12345’”’’’

“‘‘‘12345’’’”

18
New cards

string slicing syntax

s[start:stop:step]

19
New cards

omitting components of string syntax

start omitted = s[:4], starts at index 0 and stops at index 4

stop omitted = s[2:], starts at index 2 and stops at the end

step omitted = default step 1(every character

both omitted = returns a full copy of string

20
New cards

negative numbers in string slicing

counts backward from the end of the string where -1 is the last character, -2 is second to last etc.

starts at the letter that u get to when u count backward and stops BEFORE the letter that you count forward to

s = Python

s[-4:-1]

starts at index -4 ‘t’, stops before -1 “n”

21
New cards

step in string slicing

s[::2] grabs every 2nd character of the whole string(when start and step omitted)

s[1::2] grabs every 2nd character from index 1 to end(when stop omitted)

22
New cards

reversing a string using slicing

s[::-1]

goes through whole string(start and stop omitted) and step counts backward by 1

23
New cards

negative step implication

when theres a negative step, start must be larger than stop

24
New cards

out of bound slicing

slicing handles out of range numbers by automating the strings boundaries

25
New cards

.find() method

str.find(“sub”, start, end)

sub = what ur looking for

start = at what index u wanna start looking for sub at

end = at what index u wanna stop looking for sub at

returns -1 when not found

26
New cards

.capitalize()

returns string with first letter capitalized

27
New cards

.lower()

returns all lowercase version of my string

28
New cards

.upper()

returns all uppercase version of my string

29
New cards

.title()

capitalizes any character found immediately after a space

30
New cards

.strip()

strips out any white space before or after the string

31
New cards

.replace(“string”, “string”)

finds all instances of the first argument in the string and replaces it with the second argument

32
New cards

.split()

returns a list of the individual words inside that string

33
New cards

print(“character”.join(myList))

takes each item from that list of strings and concatenates using the string calling the join method as the string to go between each pair of strings being concatenated

34
New cards

Homogeneity

A property of lists determining whether they can accept multiple types of variables.

A homogenous list can only accept one type of variable

a heterogenous list can accept multiple types

35
New cards

Tuples

an immutable form of a list-like structure

myTuple = (1, 2, 3)

set a variable equal to a comma separate series of values or variables
IMMUTABLE

you can put tuples inside if tuples

36
New cards

.sort()

sorts the values of a list for some internal logic like for integers it goes in ascending order

37
New cards

.append()

adds an item to a list

38
New cards

.extend

end current list by another list

39
New cards

.insert(location, value)

location - index of item u wanna replace

value - item u wanna replace it with

40
New cards

.remove(item that u wanna remove)

removes item u want removed

41
New cards

.pop()

removes the last item of a list and also returns the item it removes

42
New cards

del

to delete items by LOCATION in the list instead of by specific item or value

43
New cards

.index(item)

to find the index of a certain item or value

44
New cards

.count(item)

to count how many times a certain item appears in a list

45
New cards

.reverse()

puts list in descending order

46
New cards

stack

“Last in first out” - the last item added(top most item)is the first item removed(only ever able to remove the top most item)

if we have a new item to add to our list the only place we can add it is to the top of the of the stack

We can only access the topmost data

47
New cards

Queue

“First in first out”

we can only access the LEAST recently added item on the list and can only access it by removing it from the list

We queue up our data and the oldest data added is the next data processed

48
New cards

Linked List

A list-like structure where the location of each item in the list is contained in the previous item in the list

data in the memory location of a value that would ITSELF point to another memory location rather than the list next spot pointing to a different memory location

49
New cards

Dictionary

A data structure comprise of key-value pairs where a key is entered into the dictionary to get out a value

ex: myDictionary = { “sprockets”: 5, “widgets”: 11, “cogs”:3, “gizmos”:15}

cannot have multiple copies of the same key

50
New cards

Dictionary key

  • A value that, when passed into a dictionary, returns a corresponding value like a word and its definition

  • dictionary is a compilation of multiple key value pairs that u can pass around together

  • a function can generally only return one value but a dictionary would let us return multiple values from a function

  • Dictionary preserves the ability to give names to those values by way of keys


51
New cards

Defining new keys in dictionaries

  • pretend the keys already exist and assign them a new value → variable for dictionary[“key of choice”] = value

  • del variable for dictionary[“key of choice”] → deletes key and its value from the dictionary

  • adding a key thats already in the dictionary will just change the value of the key

  • if u want to check if the key is already in there because u want both values reflected in the list, make a conditional to check. If there is already that key, make a different key for the value u want to add


52
New cards

key error

trying to access a key from a dictionary when it doesnt exist

53
New cards

Traversing dictionaries

.values(): A dictionary method type that returns a list of all the values of that dictionary

.key(): A method of a dictionary type that returns a list of all the keys in that dictionary

.items(): A method of a dictionary type that returns a list of (key, value) tuples in that dictionary

54
New cards

Object

a custom data structure that organizes and encapsulates variables and methods into a single data type

55
New cards

Instance

A single set of values of a particular class. Classes may be comprised of multiple variables; an instance is a set of values for these variables

referred to as class

when function is called inside of a class, its called a method

56
New cards

init and self

  • called when we first create a new instance of this class

  • self tells python to define the variables after its dot for the instance, any other code inside this class would be able to see those variables as long as it accessed them with self as well

  • every method inside class should have self as its first parameter


57
New cards

Encapsulation

The ability to combine variables and methods into class definitions. it helps avoid modification or misuse of data by other functions or programs

58
New cards

Constructor

A common type of method in writing classes that specifies some code to run whenever a new instance of the class is created

59
New cards

Destructor

specifies how the instance of a class is to be destroyed

60
New cards

getter

returns the value of a variable contained within the class

61
New cards

setter

sets a variable contained within the class to a new value

62
New cards

.get() vs []

ex: stock[“pen”] will raise a KeyError if “pen” doesnt exist in the dictionary

stock.get(“box”, 10) will not crash; it returns the value if its in the dictionary even if the key is missing

check default arguments:

63
New cards

b = a for lists

When you assign a list to a new variable using b = a does not make a copy

64
New cards

In-place methods(mutators) dot notation

Most in place methods return None if u write b = a.sort(), b becomes not

65
New cards

Non mutating functions and methods

dont touch your origninal variable, instead they return a brand new object so u need to assign a variable if u want to keep it

sorted(a)

reversed(a)

list(reverest(a)

s.upper()/s.lower

s.replace(old,new)