Py Functions, own Flashcards

1
Q

what will be returned from a function in which a return statement is not specified?

A
]] None
e.g.
]] def hello_fn():
]]     print " Hello world!"
]] def hello2_fn:
]]     print hello_fn()
]] hello2_fn
]] None
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

when to use a single-line or a multi-line comment?

A
  • to explain design decision or changes

”””
Introduction that will appear from the help-function and include versions etc.
“””

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

naming standard, how to prefix the name of a fn returning True or False?

A

‘is’

]] def is_…

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

how is the keyword in keyed: ‘in’ or ‘IN’?

A

‘in’ (key word in small letters)

]] if num_p in(1,2,3,4):
]] return True

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

can NOT IN be used?

A

yes, works as well as ‘in’

]] if num_p not in(1,2,3,4):
]] return False

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

how to use ‘range()’ with ‘in’-condition?

A

]] if val_p in range( 1,12)

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

how to invoke a global variable ‘greating_message_v’ within a fn?

A

]] greating_message = ‘Hi, how are you?’
]] def greating( message_p )
]] global greating_message

– a global variable used within a function should explicitly be referenced within the sane

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

how to add, populate values between 0 - 100 to a list?

A

– option.1
# ]] my_list = [ ] NOTE: not needed
]] range0to100 = range( 0,100,1 )
]] my_list = list( range0to100 )
– built-in fn to create a list
– from Kite

– option.2, worked first but not second time
]] list_start = 0
]] list_end = 100
]] my_list = [ ]
]] my_list.extend( range( list_start, list_end, 1) )
– extend: a built-in function which only takes one argument

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

how to verify whether a provided value is in a range of e.g. 0 - 3600?

A

]] if in range( 0,3600 ):

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

how to find the number of occurrences of a string in another string?
e.g. ‘red’ in ‘red.blue.red.green.yellow.red’

A
-- option.1 --
]] c_str = 'red.blue.red.green.yellow.red'
]] print( c_str.count( 'red',0,3 )
]] 1
-- option.2 --
]] print( c_str.count( 'red',0,-1 )
]] 3
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

how to convert a float number into 1 decimal?

A

]] num_w_decim = round( float_num, 1 )

– round works very well

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