EXAM 3 Flashcards

(17 cards)

1
Q
  1. What is a function? The syntax for writing a function in Python?
A

def function_name(parameters):
# code block
return result # optional

Ex.
def greet(name):
print(“Hello,”, name)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Formal parameters and actual parameters must match in _______, ________, and _________.

A

number, order, and type

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is a keyword argument? How to call a function with mixed keyword and positional arguments?

A

Keyword Argument: Specifies which argument the value is passed to

def greet(name, greeting):
print(greeting, name)

greet(“Alice”, greeting=”Hello”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What are the two types of functions? Does a void function have a return statement?

A

Void Function: Simply executes the statements it contains and then terminates.

Value-Return Functions: Executes the statements it contains, and then it
returns a value back to the statement that called it.

A void function does NOT have a return statement.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How do you call a function from the main function in the same program? How do you call a function from another module?

A

def main():
greet(“Dennis”)

def greet(name):
print(“Hello,”, name)

main()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is a list? How to create a list?

A

A list is an object that contains multiple data items

myList = [item1, item2, etc.]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is scope? local variable vs. global variable

A

Scope: the part of a program in which a
variable may be accessed

Local Variable: variable that is assigned a value inside a function

Global Variable: created by assignment
statement written outside all the functions

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

pass by value vs. pass by reference

A

Pass by value: The value of the variable is
passed to the function.

Pass by reference: For mutable types like list, the object itself is passed.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What are the values in a list called? What is the position of a value in a list called?

A

Values = elements

Position = index

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Using sample as your list name, how can you represent the length of the list?

A

size = len(my_list)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Find min/max number in list integers

A

min del list[i]
and max functions: built-in functions that
returns the item that has the lowest or highest
value in a list

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Cube function and test in main:

A

def cube(x):
return x ** 3

def main():
print(cube(3)) # Output: 27

main()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Three modes to open a file and their differences:

A

r: To read the file

w: To write in the file

a: To append the file

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Open the text file elements to write to? Does it need to exist? Close it.

A

f = open(“elements.txt”, “w”)
f.write(“Hello\n”)
f.close()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q
  1. Open nobleGrades to read from? Does it need to exist?
A

f = open(“nobleGrades.txt”, “r”)
data = f.read()
f.close()

The file NEEDS to exist!!!!

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q
  1. How do you write a class in Python? How to create an instance of a class?
A

class Dog:
def __init__(self, name):
self.name = name

def bark(self):
    print("Woof!", self.name)

Create instance
my_dog = Dog(“Buddy”)
my_dog.bark()