0 1 2 3 4
POP77001 Computer Programming for Social Scientists
0 1 2 3 4
__iter__ method that can be called with iter() function--------------------------------------------------------------------------- TypeError Traceback (most recent call last) Cell In[4], line 2 1 x = 3 ----> 2 iter(x) TypeError: 'int' object is not iterable
items() method allows to iterate over keys and values in a dictionarykeys() method allows to iterate over just keysvalues() method allow to iterate over just valuesforloop block or list comprehension[<expr> for <elem> in <iterable>]
[<expr> for <elem> in <iterable> if <test>]
[<expr> for <elem1> in <iterable1> for <elem2> in <iterable2>]
E.g. the following conditional statement:
for loopsfor loop.startswith for the last task.{<expr> for <elem> in <iterable> if <test>}
{<key>: <value> for <elem1>, <elem2> in <iterable> if <test>}
Element - 1
Length = 10
Element - 2
Length = 9
Element - 4
Length = 8
Element - 5
Length = 8
Element - 7
Length = 8
Element - 8
--------------------------------------------------------------------------- IndexError Traceback (most recent call last) Cell In[16], line 4 2 print('Element - ' + str(i)) 3 if i % 2 == 0: ----> 4 l.pop(i) 5 print('Length = ' + str(len(l))) IndexError: pop index out of range
help() function.__doc__ attribute.def <function_name>(arg_1, arg_2, ..., arg_n):
"""<docstring>"""
<function_body>
def <function_name>(arg_1, arg_2, ..., arg_n):
"""<summary_docstring>
<longer_description>
"""
<function_body>
statistics and external numpy).round for rounding.0.1, 2.7, 3.5, 4, 5.98 supplied as arguments.