Py Loops (Ud.10.Apps) Flashcards
how to run a for-loop for a dict’s key and value?
]] for key, value in dict_1.items():
]] …
how to print the dict’s key and value in a way compatible with Python version 3.6?
]] print(“%s: %s” % (key, value))
how to replace a char in a string with another char?
]] phone_numbers = {“John Smith”: “+37682929928”,
“Marry Simpons”: “+423998200919”}
]] for key, value in phone_numbers.items():
]] print( value.replace(‘+’,’00’,))
give an example of when to use ‘break’ and ‘continue’
while loops ]] while True: ]] username = input( 'Enter username') ]] if username == 'pypy': ]] break ]] else: ]] continue
how to run a for-loop for a dict’s values only?
]] for val in dict_1.values():
how to run a for-loop for a dict’s keys only?
]] for val in dict_1.keys():
how can strings from a list be concatenated in a print?
]] results[ ‘string1’, ‘string2’, ‘string3’ ]
]] print(“ “.join( results ))
– see note in Udemy 10 App course
how can a return statement return a capitalized text string?
]] return “{ }.”.format( capitalized )
– from Udemy 10 App, when explaining the app
what is the syntax of defining a for-loop? e.g. dividing values with 10
]] def for_1( lst )
]] new_lst = [i/10 for i in lst]
what is the purpose of an in-line for-loop/list comprehension?
to transform traversed values in a list without storing the new values in a new list, the code is a more compressed way to do the same; 1 line of code instead of 3
how can an an if-statement be added to the same in-line for-loop? e.g. only for values > 0
]] def for_1( lst )
]] new_lst = [ i/10 for i in lst if i > 0 ]
how can an an if-else-statement be added to the same in-line for-loop? e.g. only for values > 0
]] def for_1( lst )
]] new_lst = [ i/10 if i > 0 else -1 for i in lst ]
placeholder: isinstance
– pending
how to create a function with an arbitrary number of non-key arguments?
]] def fn_arb( *args )
how to create a function with an arbitrary number of key arguments? is it arbitrary?
]] def fn_arb( **args )
– no, because the number of args are defined