Functions Flashcards

1
Q

A function

A

A block of code that is only executed when it is called

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

Write the function to print Hello World!:
The functions name should be “start”

A

Def start():
Print(“Hello World!”)

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

What are arguments?

A

Information you send to a function

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

Return statement

A

Functions send Python values/ objects to the caller. These values/objects are known as the functions return value.

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

Keyword arguments

A

Arguments preceded by an identifier when we pass them to a function, the order of the arguments doesn’t matter, unlike positional arguments, Python knows the names of the arguments that our function receives.

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

Write a function with key word arguments for Alexander cardoza

The function looks like this

Def name(First,Last):
First +” “ + Last

A

name(“alexander”=First, “cardoza” = Last)

Output:
alexander Cardoza

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

Nested function calls

A

Function calls inside other function calls, innermost function calls are resolved first and the returned value is used as an argument for the next outer function

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

In the nested function
Print(Round(Abs(Float(Input(“enter number”)))))

What would happen if you inserted the number -6.9

A

The number 7 would be printed

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

Scope

A

The region that a variable is recognised
A variable is only available from the region it is created
global and locally scoped versions of a variable can be created.

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

Local scope

A

Available only in the function that it was created in

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

Global scope

A

Available inside and outside of functions as it was created outside of a function

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

L
E
G
B

A

Local
Enclosing
Global
Built-in

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

Kwargs

A

Parameter that will pick all arguments into a dictionary
-useful so that a function can accept a varying amount of keyword arguments

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

How do you use kwargs?

A

In the brackets of your function write “**variable” (variable can be anything)

And in the function’s block of code , where you want to insert the variable write the name of your variable and then use the square brackets [] to define which variable you would like to use e.g

Def printName(**kwargs):
Print(“Hello “ +kwargs[name]

printName(name= ‘alex’)

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

Key,value

A

The key has children: (the value)

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

End = “ “

A

This is usually used in print statements, it makes the end of a print statement a space as an oppose to a new line so each print iteration stays on the same line.

17
Q

What is implementation?

A

The defined function’s “function” or process

18
Q

Assigning a function to a variable

A

Def Hello()
Print(“what’s up”)
Hi = Hello

Hi()

Output:
“What’s up”

19
Q

Higher order functions

A

A function that either:
Accepts a function as an argument
Or
Returns a function
(In Python functions are also treated as objects)

20
Q

Lambada function

A

Function written in 1 line using lambda keyword
-accepts any number of arguments, but only has one expression.
(Useful if needed for a short period of time, throw away)

21
Q

Write a lambda function called double that multiplies an argument by 2

A

double = lambda x:x *2

22
Q

In a lambda function where do you input the parameters?

A

Before the colon
E.g(x,y)

Lambda x,y:

23
Q

How would you print your lambda function?

A

Print(name(argument))

24
Q

How can you use the lambda function as an argument / key

A

Per say I wanted to use a function to define the key at a specific index for another function

I could write

GetIndex = lambda data: data[1]

25
Q

How can you exit a function?

A

Use Return

26
Q

Use a recursive for walking 100 steps

A

Recursives re use functions

Def walk(amount):
Print(f”you have walked {amount}
steps”)
If steps == 0
Return
walk(amount-1)

27
Q

Iterative version of walk(100)

A

Def walk(amount)
For steps in range(amount+1):
Print(f”you have walked {steps}
steps)