Tutorial B1
9 important questions on Tutorial B1
Bij string (for i in range -> for .... in ...)
for ch in text:
print ch
Bij string (for i in range(len(text)): -> bruikbaar/nuttig
for i in range(len(text)):
print 'character at position', i, ':', text[i]
character at position 0 : m
character at position 1 : y
character at position 2 :
character at position 3 : t
character at position 4 : e
character at position 5 : x
character at position 6 : t
character at position 7 : u
character at position 8 : a
character at position 9 : l
character at position 10 :
character at position 11 : d
character at position 12 : a
character at position 13 : t
character at position 14 : a
Integer values of charachters - ord met type Int en Chr met type String
>>> ord('a')
97
>>> chr(97)'a'
- Higher grades + faster learning
- Never study anything twice
- 100% sure, 100% understanding
Controleren van de count bound loop door (2)
opsommen van elementen uit een reeks
An example of the former is counting how many times a given letter occurs in a given string: (loop template)
s = 'Analogous to str method count'
result = 0
for ch in s:
if ch == letter:
result = result + 1
return result
An example of the latter is adding(sum) a series of numbers entered by the user:
n = raw_input('how many numbers to add? ')
n = int(n)
sum = 0
for i in range(n):
num = raw_input('add a number: ')
num = int(num)
sum += num # shorthand for sum = sum + num
print 'the total is', sum
A “sentinel bound loop”
A “data bound loop”
This function multiplies a variable power by base until the power is larger than a given threshold (while loop)
'Find first power of base above threshold'
power = 1
exponent = 0
while power <= threshold:
power *= base # power = power * base
exponent += 1
return exponent, power
The question on the page originate from the summary of the following study material:
- A unique study and practice tool
- Never study anything twice again
- Get the grades you hope for
- 100% sure, 100% understanding