Get started learning Python with DataCamp's free Intro to Python tutorial. Learn Data Science by completing interactive coding challenges and watching videos by expert instructors. Start Now!
This site is generously supported by DataCamp. DataCamp offers online interactive Python Tutorials for Data Science. Join 11 million other learners and get started learning Python for data science today!
Good news! You can save 25% off your Datacamp annual subscription with the code LEARNPYTHON23ALE25 - Click here to redeem your discount
Variabelen en Typen
Python is volledig objectgeoriënteerd en niet "statisch getypt". Je hoeft geen variabelen te declareren voordat je ze gebruikt, of hun type te declareren. Elke variabele in Python is een object.
Deze tutorial behandelt enkele basistypen variabelen.
Numbers
Python ondersteunt twee soorten getallen - gehele getallen (hele getallen) en zwevende komma getallen (decimale getallen). (Het ondersteunt ook complexe getallen, die echter niet in deze tutorial worden uitgelegd).
Om een geheel getal te definiëren, gebruik je de volgende syntaxis:
myint = 7
print(myint)
Om een zwevend komma getal te definiëren, kun je een van de volgende notaties gebruiken:
myfloat = 7.0
print(myfloat)
myfloat = float(7)
print(myfloat)
Strings
Strings worden gedefinieerd met een enkele aanhalingsteken of een dubbele aanhalingstekens.
mystring = 'hello'
print(mystring)
mystring = "hello"
print(mystring)
Het verschil tussen de twee is dat het gebruik van dubbele aanhalingstekens het gemakkelijk maakt om apostroffen op te nemen (terwijl deze de string zouden beëindigen als je enkele aanhalingstekens zou gebruiken).
mystring = "Don't worry about apostrophes"
print(mystring)
Er zijn aanvullende variaties op het definiëren van strings die het gemakkelijker maken om zaken als regeleinden, backslashes en Unicode-tekens op te nemen. Deze vallen buiten het bestek van deze tutorial, maar worden behandeld in de Python-documentatie.
Eenvoudige operators kunnen worden uitgevoerd op getallen en strings:
one = 1
two = 2
three = one + two
print(three)
hello = "hello"
world = "world"
helloworld = hello + " " + world
print(helloworld)
Toewijzingen kunnen op meer dan één variabele "tegelijkertijd" op dezelfde regel worden gedaan als volgt
a, b = 3, 4
print(a, b)
Het vermengen van operators tussen getallen en strings wordt niet ondersteund:
# Dit zal niet werken!
one = 1
two = 2
hello = "hello"
print(one + two + hello)
Exercise
Het doel van deze oefening is om een string, een geheel getal en een zwevend komma getal te maken. De string moet mystring
heten en moet het woord "hello" bevatten. Het zwevend komma getal moet myfloat
heten en moet het getal 10.0 bevatten, en het gehele getal moet myint
heten en moet het getal 20 bevatten.
# change this code
mystring = None
myfloat = None
myint = None
# testing code
if mystring == "hello":
print("String: %s" % mystring)
if isinstance(myfloat, float) and myfloat == 10.0:
print("Float: %f" % myfloat)
if isinstance(myint, int) and myint == 20:
print("Integer: %d" % myint)
# change this code
mystring = "hello"
myfloat = 10.0
myint = 20
# testing code
if mystring == "hello":
print("String: %s" % mystring)
if isinstance(myfloat, float) and myfloat == 10.0:
print("Float: %f" % myfloat)
if isinstance(myint, int) and myint == 20:
print("Integer: %d" % myint)
test_object('mystring', incorrect_msg="Don't forget to change `mystring` to the correct value from the exercise description.")
test_object('myfloat', incorrect_msg="Don't forget to change `myfloat` to the correct value from the exercise description.")
test_object('myint', incorrect_msg="Don't forget to change `myint` to the correct value from the exercise description.")
test_output_contains("String: hello",no_output_msg= "Make sure your string matches exactly to the exercise desciption.")
test_output_contains("Float: 10.000000",no_output_msg= "Make sure your float matches exactly to the exercise desciption.")
test_output_contains("Integer: 20",no_output_msg= "Make sure your integer matches exactly to the exercise desciption.")
success_msg("Great job!")
This site is generously supported by DataCamp. DataCamp offers online interactive Python Tutorials for Data Science. Join over a million other learners and get started learning Python for data science today!