Mod 4 Flashcards

1
Q

Where can functions come from? (3)

A
  1. Built in functions from Python (for example print())
  2. Python’s preinstalled modules have functions
  3. The user can write their own functions
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Write the code that builds a function named fun and prompts the user to input their name, and then thanks them and prints the name. Make sure to invoke the function.

A
def fun():
   a = input('What is your name?')
   print('Thanks', a)

fun()

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

What are the rules about functions (2)?

A
  • Cannot invoke a function which is not known at the moment of invocation
  • Cannot have a function and a variable of the same name
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Where in the function is a parameter defined?

A
def function(parameter)
inside the parenthesis
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is the difference between a parameter and argument?

A

Parameters exist inside functions

arguments exist outside functions

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

what is the output of the below code:

def message(number):
    print("Enter a number:", number)

message()

A

TypeError: message() missing 1 required positional argument: ‘number’

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q
What type of parameters are in the below function?
def myFunction(a, b, c):
    print(a, b, c)

myFunction(1, 2, 3)

A

positional parameters

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q
What type of parameters are in the below function? 
def introduction(firstName, lastName):
    print("Hello, my name is", firstName, lastName)
introduction(firstName = "James", lastName = "Bond")
introduction(lastName = "Skywalker", firstName = "Luke")
A

keyword parameters

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q
What is the output of:
def intro(a, b="Bond"):
    print("My name is", b + ".", a + ".")

intro(“Susan”)

A

My name is Bond. Susan.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q
What is the output of:
def sum(a, b=2, c):
    print(a + b + c)

sum(a=1, c=3)

A

SyntaxError - a non-default argument (c) follows a default argument (b=2)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q
What is the output of:
def subtra(a, b):
    print(a - b)

subtra(5, b=2)
subtra(a=5, 2)

A

3

Syntax Error positional argument follows a keyword argument

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

What does the “return” function do? What happens to the code after the return function? What if nothing is assigned to it?

A

It ends the execution of the function and returns the result. Everything after the return statement is not executed. If a value isnt assigned to the return instruction, it returns “None”

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

“None” facts/rules (3) and when can it be used (2)?

A
  • it doesnt hold a value
  • it must not be used in an expression
  • It is a keyword
  • when it is assigned to a variable
  • when it is compared to a variable
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Can a single integer be iterated through the for loop?

A

No

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q
What is the output of:
def multiply(a, b):
    return

print(multiply(3, 4))

A

None

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q
What is the output of:
def wishes():
    print("My Wishes")
    return "Happy Birthday"

wishes()

A

My Wishes

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q
What is the output of:
def wishes():
    print("My Wishes")
    return "Happy Birthday"

print(wishes())

A

My Wishes

Happy Birthday

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q
What is the output of:
def hi():
    return
    print("Hi!")

hi()

A

None

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q
What is the output of:
def isInt(data):
    if type(data) == int:
        return True
    elif type(data) == float:
        return False

print(isInt(5))
print(isInt(5.0))
print(isInt(“5”))

A

True
False
None

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q
What is the output of:
def myFunction():
    global var
    var = 2
    print("Do I know that variable?", var)

var = 1
myFunction()
print(var)

A

Do I know that variable? 2

2

21
Q

What is the output of:
def myFunction(myList1):
print(myList1)
myList1 = [0, 1]

myList2 = [2, 3]
myFunction(myList2)
print(myList2)

A

[2, 3]

[2, 3]

22
Q
What is the output of:
def myFunction(myList1):
    print(myList1)
    del myList1[0]

myList2 = [2, 3]
myFunction(myList2)
print(myList2)

A

[2, 3]

[3]

23
Q

A variable that exists outside a function has a scope inside the function body unless when?

A

The function defines a variable of the same name

24
Q

True or False: A variable that exists inside a function has a scope inside the function body

A

True

25
Q

What does the global keyword do?

A

It makes the variable’s scope global (used inside or outside of a function)

26
Q
What is the output of:
def message():
    alt = 1
    print("Hello, World!")

print(alt)

A

NameError: name “alt” is not defined

27
Q

What is the output of:
a = 1

def fun():
    global a
    a = 2
    print(a)

a = 3
fun()
print(a)

A

2

2

28
Q
What is the output of:
def factorialFun(n):
    if n < 0:
        return None
    if n < 2:
        return 1
    return n * factorialFun(n - 1)

print(factorialFun(5))

A

120

because 54321 = 120

29
Q

What is it called when a function calls itself?

A

recursion

30
Q

What is a function that calls itself and contains a specified termination condition?

A

recursive

31
Q
What is the output of:
def factorial(n):
    return n * factorial(n - 1)

print(factorial(4))

A

RecursionError: maximum recursion depth exceeded

32
Q
What is the output of:
def fun(a):
    if a > 30:
        return 3
    else:
        return a + fun(a + 3)

print(fun(25))

A

56

33
Q

What is a sequence data type?

A

Is a type of data which is able to store more than one value, these values can be sequentially browsed.

34
Q

What is mutability?

A

property of Python’s data that describes its readiness to be free changed during program execution.
Mutable data - can be freely updated at any time
Immutable data - restricted in how it can be updated
list.append(1) - acceptable for mutable data, but not immutable.

35
Q

Facts about tuples (5):

A
  • Immutable sequence type
  • created by values in parenthesis or separated by commas (value1, value2) or value1, value2
  • can contain int, float, string, Boolean
  • can appear on the left side of the =
  • tuples elements can be variables (not just literals)
36
Q

Write the code showing 2 ways of creating a single element tuple

A

(1, )

1,

37
Q

What is the output of:
myTuple = (1, 10, 100, 1000)
myTuple.append(10000)

A

AttributeError: ‘tuple’ object has no attribute ‘append’

38
Q

When applied to tuples, what does * and + do?

A
  • replicates the tuple

+ concatenates the tuple

39
Q

Facts about dictionaries (3) and rule (1)

A
  • not a sequence and is mutable
  • created by keys and values separated by colons, pairs separated by commas, surrounded by curly braces
  • set of key-value pairs
  • each key must be unique and can be of any data type
40
Q

Write the code to print the value for cat:

dictionary = {“cat” : “chat”, “dog” : “chien”, “horse” : “cheval”}

A

print(dictionary[“cat”])

output: chat

41
Q

Write a for loop so that the key value pairs of the dictionary can be seen:
dictionary = {“cat” : “chat”, “dog” : “chien”, “horse” : “cheval”}

A
for key in dictionary.keys():
    print(key, "->", dictionary[key])
(output: 
cat -> chat
dog -> chien
horse -> cheval)
42
Q

Write the code to sort the dictionary:

dictionary = {“cat” : “chat”, “dog” : “chien”, “horse” : “cheval”}

A
for key in sorted(dictionary.keys()):
    print(key, "->", dictionary[key])
(output:
cat -> chat
dog -> chien
horse -> cheval)
43
Q

Write the code to use the items() method to return the dictionary as tuples:
dictionary = {“cat” : “chat”, “dog” : “chien”, “horse” : “cheval”}

A
for english, french in dictionary.items():
    print(english, "->", french)
(output:
cat -> chat
dog -> chien
horse -> cheval)
44
Q

Add the key swan and value cygne to the dictionary:

dictionary = {“cat” : “chat”, “dog” : “chien”, “horse” : “cheval”}

A

dictionary[‘swan’] = ‘cygne’

45
Q

Write the code that deletes the dog key-value pair:

dictionary = {“cat” : “chat”, “dog” : “chien”, “horse” : “cheval”}

A

del dictionary[‘dog]

46
Q

What do the clear(), copy(), get(), update() and popitem() methods do to a dictionary?

A

clear() removes all items from the dictionary
copy() copies the dictionary
get() allows access to a dictionary item (value) by referencing the key
update() inserts an item into the dictionary
popitem() removes the last element

47
Q

What is the output of:
tup = 1, 2, 3
a, b, c = tup

print(a * b * c)

A

6

48
Q

What do the dict, tuple and list functions do?

A

dict - converts to a dictionary
tuple - converts to a tuple
list - converts to a list