Python cheatsheet
Husk at der kan søges efter hjælp til Python på nettet ved at søge på py <beskrivelse> fx py update variable.
Mere uddybende Python cheatsheet.
Nogle af nedenstående kodeeksempler antager at en eller flere variabler (såsom condition) er defineret.
Variabler
a_variable = 5Funktioner
def a_function():
print("hi")def function_with_parameter_and_return_value(x):
return x**2Kommentarer
# Kommentar på en linje
'''
Kommentar
over flere
linjer
'''Betingelser
if condition:
print("if condition is true")
elif condition2:
print("else if condition2 is true")
else:
print("otherwise")if something == something_else:
print("== is used for comparion. Not equal: !=")
if condition and condition2:
print("both true")
if condition or condition2:
print("one or both are true")
if not condition:
print("not condition")Løkker
while condition:
print("while condition is true")Datastrukturer
a_string = "hi"
a_list = [2,5,8]
a_dict = {"name": "John", "age": 25}
a_tuple = (2,5,8)
a_set = {2,5,8}