Functions Flashcards

1
Q

What important key word is used to create new function in Python?

A

def foo(params):

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

How is called the setion that defines the function. How is it look like?

A
it is called the header, it holds parameters:
def hello_word(name):
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is a good practice to do with every function (and all pieces of code really)

A
def foo(name):
    #to include comments
    """this one prints your name
    if you have one. lol"""
    print name
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How is called the biggest part of every function. (hopefully it is not a comment xD)

A

Body - be a body - ilg

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

How multiline comment is called inside the function

A

””” DOCSTRING “””

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

What have you to do with function to implement it?

A

After defining a function, it must be called to be implemented.

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

Power function

A
def power(base, exponent):  # Add your parameters here!
    result = base**exponent
    print "%d to the power of %d is %d." % (base, exponent, result)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Can function call other functions

A

YES. THAT IS THE BASIC OF RECURSION FOR BRIAN SAKE

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

What is keyword ‘import’ used for

A

For importing MODULES

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

What a ‘module’ is

A

A module is a file that contains definitions—including variables and functions—that you can use once it is imported.

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

What ‘NameError’ means

A

means that Python doesn’t know what used thing is -> it was not defined yet

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

Where sqrt() function is? In which module

A

import math

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

How to properly call sqrt() function

A

math.sqrt()

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

How to import certain variables, functions or classes form module. What is a syntax of this.

A
from module import function
from math import sqrt
# now you don't have to type math.sqrt() - it's now sqrt()
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How to import all things to be in yout scope

A

from module import *

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

How to print everything from given module. What kind of function you should use.

A

everything = dir(math) # Sets everything to a list of things from math

17
Q

BIFS

A

Pythonyeaa

18
Q

Can functions take variable number of arguments?

A
Yes, it is done using *args?
def foo(*args):
19
Q

Some cool functions

A

min(), max(), abs()

20
Q

What is a funtion that returns the type of the data. How it is called? What it returns.

A
print type(1)
#
print type(2.0)
#
print type("Grimes")
#
21
Q

Some function

A
def shut_down(s):
    if s == "yes":
        return "Shutting down"
    elif s == "no":
        return "Shutdown aborted"
    else:  
        return "Sorry"
22
Q

How Pythoon compares strings. Type equal comparision.

A

string == “string value”

23
Q

Another function

A
def distance_from_zero(number):
    if type(number) == int or type(number) == float:
        return abs(number)
    else:
        return "Nope"

distance_from_zero(“lol”)