# NOTE: Range is like slicing: first:1+last:jump
for i in range(10, 100, 2): # for (int i = 10; i < 100; i += 2)
print(i, end = ' ')
10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 52 54 56 58 60 62 64 66 68 70 72 74 76 78 80 82 84 86 88 90 92 94 96 98
x = ["chris", 1, 10, ["a", "b", "c"], 477, "Marcos"]
# Not "pythonic"
for i in range(0, len(x), 1):
print(x[i], end = '\t')
# Shorter version of the above
for i in range(len(x)):
print(x[i], end = '\t')
# Much more pythonic
for elem in x:
print(elem, end = '\t')
chris 1 10 ['a', 'b', 'c'] 477 Marcos chris 1 10 ['a', 'b', 'c'] 477 Marcos chris 1 10 ['a', 'b', 'c'] 477 Marcos
for i, elem in enumerate(x):
print("(", i, elem, ")", end='\t')
( 0 chris ) ( 1 1 ) ( 2 10 ) ( 3 ['a', 'b', 'c'] ) ( 4 477 ) ( 5 Marcos )
# key: value pairs
mydict = {"studious":"Hard working, diligent, steadfast",
0:"a number that tooks us a while to discover",
"a list":[1, 2, 3, 4]}
mydict["chris"] = "The coolest name out there." # Add on the fly
del mydict[0] # Delete something on the fly
mydict["chris"] = ["Describes the best teacher out there", "The best name."]
print(mydict["chris"])
['Describes the best teacher out there', 'The best name.']
if "chris" in mydict: # Is a key there
print("chris is there")
else:
print("Chris is not there")
chris is there
# Loop through the keys
for key in mydict:
print(key, mydict[key])
studious Hard working, diligent, steadfast a list [1, 2, 3, 4] chris ['Describes the best teacher out there', 'The best name.']
print(x)
['chris', 1, 10, ['a', 'b', 'c'], 477, 'Marcos']