Computer science midterm 2

0.0(0)
studied byStudied by 0 people
0.0(0)
full-widthCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/25

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

26 Terms

1
New cards

can functions read global variables without any special declarations

  • yes, global variables can be refered to just by their name

  • global variables can be accessed anywhere

2
New cards

is this code allowed:

def my_func():

print(“in my_func()”, num1)

yes this is allowed

3
New cards

What about if you want to modify a global variable within a function

  • you have to declar it with the global keyword

  • this tells python that you want to modify the variable to be global and not create a new local scope of the variable

4
New cards

give an example of making a local variable a global variable

num1 = 10

def my_func():

num 1 = 50 (this is a local variable underneath my_func():

print(in my_func()”, num1) refers to the local variable

def myfunc2():

print(“in my_func2()”, num1) refers to the global variable

5
New cards

num1 = 10

def my_func

global num1 (refers to global num1)

num1 = 50 (resets num1 and sets it to 50)

This changes how you treat num1 and a local or global variable 

6
New cards

why is it bad practice to use global variables in python?

  • much better to pass parameters and use return values

7
New cards

What are optional parameters

parameters that the programmer do not have to pass into thefunction but can if they want 

8
New cards

Give an example of an optional parameter

9
New cards

What are the pass and none keywords

pass is a placeholder

#none is an instance of class Nonetype which is an object that indicates no value 

10
New cards

Passing lists into functions

lists are a mutable data type and can be changed after they get created

  • when you pass a list into a function they are passed by referenced (references the original memory location)

  • so you dont need to return values out of the function

11
New cards

Give an example of passing a list into a function

def calc_sum(values)

total = 0

for element in values

total = total + element

return total

12
New cards

How do you access a dictionary via a key

dict[key] 

needs to use closed brackets

13
New cards

how do you check if an item is in the dictionary by the key

if 123 in dict or

if 1234 not in dict

14
New cards

how do you get the number of items in a list

len(list)

not closed brackets

15
New cards

how do you add an item using the key

dict[list] = ‘Daniel’ where daniel is the term to be added

16
New cards

how do you delete items using a key

del students[123] where students is the dict and [123] is the key

17
New cards

how do you turn a dict into a list

list(dict.keys())

where it will only return the keys in your dict

18
New cards

how do you sort the dictionary by keys"?

by using the sorted(dict.keys())

19
New cards

how do you get all the keys in a dictionary

dict.keys()

20
New cards

how do you get al the items from a dict

dict.items()

21
New cards

how do you clear a dictionary

dict.clear()

22
New cards

what is the difference between a list and a dict?

a list contains a single element while a dictionary contains a key value pair

23
New cards

how do you access an element on a list if the list is my_list

my_list[element]

24
New cards

What do keys have to be?

immutable data type, unwise to use floats

python allows value sin a dictionary to be any type

(key, value)

25
New cards

how does dictionary overwriting work

  • the latest entry over writes all others because you can only have one unique key-value pair

26
New cards

how are dictionaries similar to lists

you can access items in a dictionary by its key not its index (in lists this is done via index)