Sam Vervaeck
1
2
-27
54984388273490
..., -2, -1, 0, 1, 2, 3, ...
9223372036854775807*
*op mijn computer
<expr>
<expr> = 0 <expr> = 1 <expr> = 2 <expr> = 3 ...
1 + 1
126943 - 24
<expr> + <expr> <expr> - <expr> <expr> * <expr> <expr> / <expr>
1 + 2 * 3
7? 9?
( <expr> )
(1 + 2) * 3
of
1 + (2 * 3)
** ↓ *, /, //, % ↓ +, - ↓ ==, !=, >, >=, <, <=
**
*
/
//
%
+
-
==
!=
>
>=
<
<=
2 ** 10 + 37
(2 ** 10) + 37
Trek tien eenheden af van twintig en deel het resultaat door vijf
20 - 10 / 5
20 - (10 / 5)
(20 - 10) / 5
1 * 2 * 3 * 4
1 * (2 * (3 * 4)) ↓ 1 * (2 * 12) ↓ 1 * 24 ↓ 24
1 * (2 * (3 * 4))
1 * (2 * 12)
1 * 24
24
((1 * 2) * 3) * 4 ↓ (2 * 3) * 4 ↓ 6 * 4 ↓ 24
((1 * 2) * 3) * 4
(2 * 3) * 4
6 * 4
* is links-associatief
x = 42
x + 1
<name> = <expr>
x = 1 x = 5 x + 1
Is het resultaat van x + 1 2 of 6?
6
Nieuwe waarden overschrijven oude waarden!
x = 1 y = x * 2 x = 10 y * 2
Wat is het resultaat van de laatste y * 2?
y * 2
4
True
False
0
not <expr>
not True
not is_sunny
not is_rainy
not (2 ** 10 > 10000)
x
not x
<expr> and <expr>
num_passengers >= 2 and num_passsengers <= 4
ready_for_launch = boosters_ready and cockpit_ready and fueled
a
b
a and b
<expr> or <expr>
authorized = is_moderator or is_vip
a or b
Een NAND-gate heeft de volgende waarheidstabel:
a nand b
Bedenk een expressie die NAND correct uitdrukt. Test de expressie met een paar voorbeelden.
not (a and b)
'Dit is een stuk tekst.'
'Een ander stuk tekst!'
's3cr3tp@ssw0rd'
'Emojis gaan ook: '
'133'
"Dit is een stuk tekst."
"Een ander stuk tekst!"
"s3cr3tp@ssw0rd"
"Emojis gaan ook: "
"133"
'<chars>'
"<chars>"
'Emoji's zijn heel leuk'
SyntaxError: unterminated string literal (detected at line 1)
'Emoji\'s zijn heel leuk
'\n'
'\t'
'\x0A'
'\u0398'
'\U0001F600'
f'Uw volgnummer is {count}'
f"Beste {fullname}, bedankt voor uw bestelling nummer {ordernumber}."
f"Twee maal {x} is {x * 2}"
{ <expr> }
Neem aan dat name gegeven is. Maak een string via interpolatie die de gegeven persoon begroet. De groet mag je zelf kiezen.
name
>>> name = "Jan" >>>
Hoe hergebruiken we stukken code?
def foo(a, b): return a * 2 + b ** 10
def <name>(<param>, ...): <statement> ...
return <expr>
def sum(x, y): return x + y sum(1, 2) sum(5, 5) sum(934830, -1)
<expr>(<expr>, ...)
print(42)
print(x)
def test(): print(1)
def test(): print(1) test()
def weird(): print(1) return 42 print(2) weird()
Waar is de 2?
Een return-statement stopt de uitvoering van de functie waar het deel van uitmaakt.
def process_customer(customer): if is_not_paying(customer): return initialize_cluster() expensive_task_on_cluster() result = collect_results() return result
Vind een functie genaamd xor die enkel True geeft indien ofwel a waar is ofwel b waar is maar nooit de twee tegelijk waar of tegelijk onwaar zijn.
xor
Dit wordt ook wel een exclusieve of genoemd.
a xor b
def xor(a, b): return (a and not b) or (not a and b)
def xor(a, b): return not (a and b) and not (not a and not b)
print(text)
>>> print("Hallo daar!") Hallo daar! >>>
message = "Opdracht geslaagd." print(message)
if ready_to_launch: print("Raket wordt gelanceerd ...") else: print("Raket kon niet de ruimte in worden geschoten.")
if <expr>: <statement> ... elif <expr>: <statement> ... else: <statement> ...
if naam == 'bob': print("Hey Bob, hoe gaat het ermee?") elif naam == 'james': print("Oh James, ben jij het weer?") elif naam == "vanessa": print("Lang geleden, Vanessa!") else: print("Hey, ik weet niet wie je bent.")
if temperature < 20: conn = connect_to_heating() conn.send_message("more_gas") conn.close()
if <...>
int(text)
str(value)
input(prompt)
>>> input("Hoe oud ben je? ") Hoe oud ben je? 31 '31' >>>
answer = input("Hoe oud ben je? ")
>>> answer = input("Hoe oud ben je? ") Hoe oud ben je? 31 >>> answer '31' >>> answer > 50 Traceback (most recent call last): File "<python-input-7>", line 1, in <module> answer > 50 TypeError: '>' not supported between instances of 'str' and 'int'
>>> answer '31' >>> int(answer) 31 >>> int(answer) > 50 False
>>> age = int(answer) >>> age 31 >>> str(age) '31'
answer = int(input("Hoe oud ben je? ")) if answer >= 50: print("Wauw zo oud!") else: print("Da's nog jong.")
Vraag aan de gebruiker een nummer en print of het even of oneven is.
x = int(input("Kies een willekeurig nummer: ")) if x % 2 == 0: print("Nummer is even.") else: print("Nummer is oneven.")
>>> s = 'foobar' >>> s[0] 'f'
>>> s = 'foobar' >>> s[1] 'o' >>> s[2] 'o' >>> s[3] 'b'
>>> s = 'foobar' >>> s[3:] 'bar'
<expr>[<start>:<stop>:<step>]
start, stop en step zijn allen <expr> en geheel optioneel
Vind de waarde van stop in de uitdrukking 'foobar'[4] zodat deze nog altijd hetzelfde resultaat geeft
'foobar'[4]
'foobar'[4:5]
>>> s = 'foobar' >>> s[-1] 'r'
>>> s = 'foobar' >>> s[-2] 'a' >>> s[-3] 'b'
>>> s = 'foobar' >>> s[-5:-1] 'ooba'
>>> s = 'foobar' >>> i = 1 >>> j = 4 >>> s[i:j-1] 'oo'