Python veranderlikes
Varia b l e s
Veranderlikes is houers vir die stoor van datawaardes.
Skep veranderlikes
Python het geen opdrag om 'n veranderlike te verklaar nie.
'n Veranderlike word geskep die oomblik wanneer jy die eerste keer 'n waarde daaraan toeken.
Voorbeeld
x = 5
y = "John"
print(x)
print(y)
Veranderlikes hoef nie met enige spesifieke tipe verklaar te word nie , en kan selfs tipe verander nadat hulle gestel is.
Voorbeeld
x = 4 # x is of type int
x = "Sally" # x is now of type str
print(x)
Casting
As jy die datatipe van 'n veranderlike wil spesifiseer, kan dit met casting gedoen word.
Voorbeeld
x =
str(3) # x will be '3'
y = int(3) # y
will be 3
z = float(3) # z will be 3.0
Kry die tipe
Jy kan die datatipe van 'n veranderlike met die type()
funksie kry.
Voorbeeld
x = 5
y = "John"
print(type(x))
print(type(y))
Enkel- of dubbelaanhalingstekens?
Stringveranderlikes kan verklaar word deur enkel- of dubbelaanhalingstekens te gebruik:
Voorbeeld
x = "John"
# is the same as
x =
'John'
Hooflettergevoelig
Veranderlike name is hooflettersensitief.
Voorbeeld
Dit sal twee veranderlikes skep:
a = 4
A =
"Sally"
#A will not overwrite a