Demo Lecture Part 2 Flashcards
what is a function? (4)
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.
what is the return keyword? (3)
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.
what is the syntax for returning variables a, b, and c
return (a, b, c)
will print a tuple in the console
how do you write an empty function?
by using the pass statement
what is the difference between parameter and argument?
Parameter is variable in the declaration of function. Argument is the actual value of this variable that gets passed to function.
what are default parameters? (3)
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!
what is the ‘end’ parameter? (2)
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.
what are the 3 common mistakes when using a return statement?
- returning too early
- return statement inside the loop
- unnecessary else condition
what are keyword arguments? (5)
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 do you format a string using %s?
name=’hayley’
print(“Hey, %s!” % name)
how do you format a string using %d?
age=40
print(“I am %d years old.’ %age)
how do you format for multiple strings? (2)
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
what do you need to remember when using %s in a dictionary?
The strings are printed in whatever order they are appended using the dictionary key in output.
what is *args? (2)
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.
explain %d operator (3)
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.