Functions Flashcards
(32 cards)
bool()
a function which returns the Boolean value of a specified object
classmethod()
a function which converts a method into a class method
input()
a function which asks the user for text input
int()
a function which returns an integer object from any number or string
len()
a function which returns the number of characters
print()
a function which prints the specified message on screen
range()
a function which returns the sequence of numbers defined, incrementing by 1. Stops before a specified number and default is 0.
sum()
a function which returns the sum of values in an iterable
What is a function?
A function is a block of reusable, organised code, used to perform a set of specified instructions when called upon.
Why use a function?
Modularity, reusability, avoiding redundancy, readability
What kinds of functions are there?
Built-in, UDFs, anonymous/lambda
Method
A method is a function which is part of a class. It can be accessed with an instance or object of the class.
Parameter
Name used to define a function or method, listed inside of the function parenthesis
Argument
An argument is the value that are sent to the function when it is called
Default argument
An argument which can be defined within a function to be taken by default if no argument value is given during function call
Required argument
An argument which must be passed during function call in order
Variable number of arguments
When a varying number of arguments can be given for a function, (*args) can be used
Keyword arguments
Used to make sure arguments are given in the correct order, identify arguments using their parameter name
def plus(a, b) return a+b
plus(b=2, a=1)
Global scope variable
A variable which is defined throughout the whole module (globally), as opposed to within a class
Local scope variable
A variable which is defined within a class, outside of the class, the definition is not the same
Arrays
A data structure that stores only values of the same datatype. Lists can store multiple datatypes.
Module
A file containing python definitions and statements which can be imported into other modules.
Function recursion
A function which calls on itself, directly or indirectly
2-D array
An array with 1-D arrays as its elements. Often represents a matrix
arr = numpy.array([[1, 2, 3], [4, 5, 6]])