Py Basics Flashcards
How can text, references, functions, calculations and more be added into one and the same Print statement?
by using
]]print( f’…{ calc }…{ref}…’ )
For the Print f’-function, how are references done?
by using curly brackets, { }, e.g. Print( f’….{ p_varname}…{p_int_var * 12} …’)
How can ‘or’ be used in a while loop?
Enter conditional statements within brackets within the WHILE-loop;
while ( not p_num.isdigit( ) ) OR ( p_num <1 )
ref: w10.assignment_001.py
How to save user input to a list?
assume ages as input, end when 0, and print count, sum, avg
user_input = int( input( ‘Age: ‘))
ages = []
while user_input > 0:
ages.append( user_input)
user_input = int( input( ‘Next age: ‘))
print( f’sum of ages:{sum(ages)}, count ages: {len(ages)}, avg age: {sum(ages)/len(ages)}’)
ref: w10.assignment_001.py
How to sort a list?
p_list.sort( )
ref: w10.assignment_001.py
How to sort a list in reverse order? p_list
p_list.sort( reverse=True )
ref: w10.assignment_001.py
How to count values in a list? p_list
len( p_list )
ref: w10.assignment_001.py
How to summarise on values in a list? p_list
sum( p_list )
ref: w10.assignment_001.py
Can an integer be traversed in a for-loop?
No, only a range including the same integer can
How can you traverse a for-loop with an integer?
p_num = int( input( “Please give me a number: “ )
for i in range( 1, p_num ):