Skip to content

Instantly share code, notes, and snippets.

@pknowledge
Created September 6, 2018 14:25
Show Gist options
  • Save pknowledge/2a0fbc7353b2376fc7a93c969df661bb to your computer and use it in GitHub Desktop.
Save pknowledge/2a0fbc7353b2376fc7a93c969df661bb to your computer and use it in GitHub Desktop.
Python For Loop
# Python Tutorial for Beginners - For Loop
print("For loop with Lists")
A = [0, 1, 2, 3, 4, 5] # list
for x in A:
print(x)
print("For loop with Tuples")
B = (0, 1, 2, 3, 4, 5) # tuple
for x in B:
print(x)
print("For loop with Sets")
C = {0, 1, 2, 3, 4, 5} # set
for x in C:
print(x)
print("For loop with Strings")
D = '012345' # string
for x in D:
print(x)
print("For loop with Dictionary")
E = { # dictionary
"name": 'max',
"age": 20
}
for x in E.keys():
print(x)
for x in E.values():
print(x)
for key, value in E.items():
print(key, '-', value)
print("For loop with range")
for x in range(6):
print(x)
for x in range(2, 6):
print(x)
for x in range(2, 30, 4):
print(x)
print("For loop with Else statements")
for x in range(2, 30, 3):
print(x)
else:
print("Finished")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment