Variabelen (Python): verschil tussen versies
Naar navigatie springen
Naar zoeken springen
Regel 57: | Regel 57: | ||
* https://en.wikibooks.org/wiki/Python_Programming/Variables_and_Strings | * https://en.wikibooks.org/wiki/Python_Programming/Variables_and_Strings | ||
+ | * https://en.wikibooks.org/wiki/Python_Programming/Dictionaries |
Versie van 13 jan 2019 10:17
Variabelen moeten eerst geïnitieerd worden
Dit
#!/usr/bin/python3 print(MijnGeheimeVariabele)
geeft foutmelding
NameError: name 'MijnGeheimeVariabele' is not defined
Dit geldt ook voor attributen van een klasse. Simpele oplossing: Variabelen initiëren in de constructor (?) van die klasse.
Print & variabelen
#!/usr/bin/python3 # Met cijfers ############# # lucky=7 print (lucky) # Met strings ############# # lucky="Hallo" print(lucky) # Je kunt variabelen niet embedden in een string zoals in Bash ############################################################## # print("En hier komt lucky voorbij") print("En hier komt $lucky voorbij")
Dictionaries
Voorbeeld:
>>> d={"stad":"Amsterdam", "stad":"Rotterdam"} >>> d {'stad': 'Rotterdam'}
Zie https://en.wikibooks.org/wiki/Python_Programming/Dictionaries voor meer.