Py Functions, own Flashcards
what will be returned from a function in which a return statement is not specified?
]] None e.g. ]] def hello_fn(): ]] print " Hello world!" ]] def hello2_fn: ]] print hello_fn() ]] hello2_fn ]] None
when to use a single-line or a multi-line comment?
- to explain design decision or changes
”””
Introduction that will appear from the help-function and include versions etc.
“””
naming standard, how to prefix the name of a fn returning True or False?
‘is’
]] def is_…
how is the keyword in keyed: ‘in’ or ‘IN’?
‘in’ (key word in small letters)
]] if num_p in(1,2,3,4):
]] return True
can NOT IN be used?
yes, works as well as ‘in’
]] if num_p not in(1,2,3,4):
]] return False
how to use ‘range()’ with ‘in’-condition?
]] if val_p in range( 1,12)
how to invoke a global variable ‘greating_message_v’ within a fn?
]] 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 to add, populate values between 0 - 100 to a list?
– 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 to verify whether a provided value is in a range of e.g. 0 - 3600?
]] if in range( 0,3600 ):
- works fine
- Kite
how to find the number of occurrences of a string in another string?
e.g. ‘red’ in ‘red.blue.red.green.yellow.red’
-- 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 to convert a float number into 1 decimal?
]] num_w_decim = round( float_num, 1 )
– round works very well