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
Variabler og Typer
Python er fuldstændig objektorienteret og ikke "statiskt typet". Du behøver ikke erklære variabler, før du bruger dem, eller erklære deres type. Hver variabel i Python er et objekt.
Denne tutorial vil gennemgå et par grundlæggende typer af variabler.
Tal
Python understøtter to typer tal - heltal (hele tal) og flydende kommatall (decimaler). (Det understøtter også komplekse tal, som ikke vil blive forklaret i denne tutorial).
For at definere et heltal, brug følgende syntaks:
myint = 7
print(myint)
For at definere et flydende kommatall, kan du bruge en af følgende notationer:
myfloat = 7.0
print(myfloat)
myfloat = float(7)
print(myfloat)
Strenge
Strenge defineres enten med en enkelt anførselstegn eller et dobbelt anførselstegn.
mystring = 'hello'
print(mystring)
mystring = "hello"
print(mystring)
Forskellen mellem de to er, at brug af dobbelte anførselstegn gør det nemt at inkludere apostroffer (hvorimod disse ville afslutte strengen, hvis man brugte enkelt anførselstegn)
mystring = "Don't worry about apostrophes"
print(mystring)
Der er yderligere varianter af strengdefinitioner, som gør det lettere at inkludere ting som linjeskift, bagstreg og Unicode-tegn. Disse ligger uden for denne tutorials tilgang, men er dækket i Python-dokumentationen.
Enkle operatorer kan udføres på tal og strenge:
one = 1
two = 2
three = one + two
print(three)
hello = "hello"
world = "world"
helloworld = hello + " " + world
print(helloworld)
Tildelinger kan udføres på mere end en variabel "samtidigt" på samme linje sådan:
a, b = 3, 4
print(a, b)
Blanding af operatorer mellem tal og strenge understøttes ikke:
# Dette vil ikke virke!
one = 1
two = 2
hello = "hello"
print(one + two + hello)
Øvelse
Målet med denne øvelse er at skabe en streng, et heltal og et flydende kommatall. Strengen skal hedde mystring
og skal indeholde ordet "hello". Det flydende kommatall skal hedde myfloat
og skal indeholde tallet 10.0, og heltallet skal hedde myint
og skal indeholde tallet 20.
# 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!