Demo Lecture Part 2 Flashcards

1
Q

what is a function? (4)

A

1 A process for executing a task
2 It can accept input and return an output
3 Useful for executing similar procedures over and over
4 store a piece of code that can be reused at different places in our program.

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

what is the return keyword? (3)

A

1 The return statement causes your function to exit and hand back a value to its caller.
2 If you don’t supply an explicit return statement with an explicit return value, then Python will supply an implicit return statement using None as a return value.
3 You can return more than one piece of information and it ill be represented in the form of a tuple.

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

what is the syntax for returning variables a, b, and c

A

return (a, b, c)

will print a tuple in the console

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

how do you write an empty function?

A

by using the pass statement

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

what is the difference between parameter and argument?

A

Parameter is variable in the declaration of function. Argument is the actual value of this variable that gets passed to function.

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

what are default parameters? (3)

A

1 default values declared when the function is defined. eg def a(m=2)

2 default parameters must be defined last in the list of parameters.

3 if you don’t add anything in their position when you call the function, the default parameter will be used, so remember what it is!

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

what is the ‘end’ parameter? (2)

A

1 parameter of the last character of the print statement.
2 by default it’s \n so the \n will be completely replaced unless you add it.

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

what are the 3 common mistakes when using a return statement?

A
  1. returning too early
  2. return statement inside the loop
  3. unnecessary else condition
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

what are keyword arguments? (5)

A

1 also known as kwargs

2 allows you to take in more arguments than the number of formal arguments that you previously defined.

3 When passed to the functions, identified by the name of the parameter.

4 So the order of the arguments does not matter anymore

5 We can alter the order that the things are passed

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

how do you format a string using %s?

A

name=’hayley’

print(“Hey, %s!” % name)

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

how do you format a string using %d?

A

age=40
print(“I am %d years old.’ %age)

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

how do you format for multiple strings? (2)

A

1 The strings are replaced in the order of their position in the brackets, wherever there is an %s sign

2 However, the number of occurrences of this operator must be equal to the number of strings to replace with after the % sign. Otherwise, an error o

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

what do you need to remember when using %s in a dictionary?

A

The strings are printed in whatever order they are appended using the dictionary key in output.

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

what is *args? (2)

A

1 used to pass a non-keyworded, variable-length argument list.
2 so you can take in more arguments than the number of formal arguments that you previously defined.

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

explain %d operator (3)

A

1 The %d operator is used as a placeholder to specify integer values, decimals, or numbers.

2 It allows us to print numbers within strings or other values.

3 Floating-point numbers are converted automatically to decimal values.

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

how do you access the documentation of a function? (2)

A

1 print(functionname.methodname__doc__)

or just

2 print(functionname.__doc__) without the methodname

17
Q

break vs return vs pass

A

Break is used for LOOP, exits the loop ONLY.
return is used for functions, exits the WHOLE FUNCTION and throws a value back to the caller

pass is to avoid throwing error but no code written, does nothing.

18
Q

how do you reverse a list?

A

list[::-1]

19
Q

what is the count method? what is its syntax?

A
  • returns the number of elements with the specified value.
  • syntax: listname.count(value)
20
Q
A