Py Loops (Ud.10.Apps) Flashcards

1
Q

how to run a for-loop for a dict’s key and value?

A

]] for key, value in dict_1.items():

]] …

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

how to print the dict’s key and value in a way compatible with Python version 3.6?

A

]] print(“%s: %s” % (key, value))

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

how to replace a char in a string with another char?

A

]] phone_numbers = {“John Smith”: “+37682929928”,
“Marry Simpons”: “+423998200919”}
]] for key, value in phone_numbers.items():
]] print( value.replace(‘+’,’00’,))

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

give an example of when to use ‘break’ and ‘continue’

A
while loops
]] while True:
]] username = input( 'Enter username')
]] if username == 'pypy':
]]       break
]]  else:
]]        continue
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

how to run a for-loop for a dict’s values only?

A

]] for val in dict_1.values():

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

how to run a for-loop for a dict’s keys only?

A

]] for val in dict_1.keys():

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

how can strings from a list be concatenated in a print?

A

]] results[ ‘string1’, ‘string2’, ‘string3’ ]
]] print(“ “.join( results ))

– see note in Udemy 10 App course

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

how can a return statement return a capitalized text string?

A

]] return “{ }.”.format( capitalized )

– from Udemy 10 App, when explaining the app

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

what is the syntax of defining a for-loop? e.g. dividing values with 10

A

]] def for_1( lst )

]] new_lst = [i/10 for i in lst]

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

what is the purpose of an in-line for-loop/list comprehension?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

how can an an if-statement be added to the same in-line for-loop? e.g. only for values > 0

A

]] def for_1( lst )

]] new_lst = [ i/10 for i in lst if i > 0 ]

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

how can an an if-else-statement be added to the same in-line for-loop? e.g. only for values > 0

A

]] def for_1( lst )

]] new_lst = [ i/10 if i > 0 else -1 for i in lst ]

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

placeholder: isinstance

A

– pending

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

how to create a function with an arbitrary number of non-key arguments?

A

]] def fn_arb( *args )

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

how to create a function with an arbitrary number of key arguments? is it arbitrary?

A

]] def fn_arb( **args )

– no, because the number of args are defined

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

what types will be created from a function with arbitrary list of non-key and key args?

A

tuple, dictionary

17
Q

is there anything to especially consider in the construction of ta function with an arbitrary number of key-args?

A

yes, each arg needs to be assigned a variable; a=1, b=2, etc.

18
Q

how to define a function with an argument having a default value?

A

]] def my_fn( radius = 27, diameter )