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
Változók és típusok
Python teljesen objektumorientált, és nem "statikusan típusos". Nem szükséges változókat deklarálni a használatuk előtt, vagy deklarálni azok típusát. Minden változó a Pythonban objektum.
Ez a tutorial áttekinti néhány alapvető változótípust.
Numbers
A Python kétféle számot támogat - egész számokat és lebegőpontos számokat. (Támogatja a komplex számokat is, amelyek ebben a tutorialban nem kerülnek magyarázatra).
Egy egész szám definiálásához használd a következő szintaxist:
myint = 7
print(myint)
Egy lebegőpontos szám definiálásához az alábbi jelölések valamelyikét használhatod:
myfloat = 7.0
print(myfloat)
myfloat = float(7)
print(myfloat)
Strings
A stringek egyes idézőjelekkel vagy dupla idézőjelekkel vannak definiálva.
mystring = 'hello'
print(mystring)
mystring = "hello"
print(mystring)
A kettő közötti különbség az, hogy a dupla idézőjelek használata megkönnyíti a felkiáltójelek beillesztését (ellenben ezek lezárnák a stringet, ha egyes idézőjelet használnánk).
mystring = "Don't worry about apostrophes"
print(mystring)
Vannak további variációk a stringek definiálására, amelyek megkönnyítik például a kocsivisszatérések, fordított perjelek és Unicode karakterek beillesztését. Ezek túlmutatnak ezen a tutorialon, de megtalálhatóak a Python dokumentációban.
Egyszerű operátorok hajthatóak végre számokon és stringeken:
one = 1
two = 2
three = one + two
print(three)
hello = "hello"
world = "world"
helloworld = hello + " " + world
print(helloworld)
A hozzárendelések egynél több változón is végrehajthatóak "egyszerre" ugyanazon a soron például így:
a, b = 3, 4
print(a, b)
A számok és stringek közötti operátorok keverése nem támogatott:
# Ez nem fog működni!
one = 1
two = 2
hello = "hello"
print(one + two + hello)
Exercise
A feladat célja, hogy készíts egy stringet, egy egész számot és egy lebegőpontos számot. A string neve legyen mystring
, és tartalmazza a "hello" szót. A lebegőpontos szám neve legyen myfloat
, és tartalmazza a 10.0 értéket, az egész szám neve legyen myint
, és tartalmazza a 20 értéket.
# 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!