Akin Can
0
Q:

python loops

#x starts at 1 and goes up to 80 @ intervals of 2
for x in range(1, 80, 2):
  print(x)
38

  for x in "banana":
  print(x)
 
4
# For Loops — In Python, for loops are used for iterating over a 
# sequence (a list, a tuple, a dictionary, a set, or a string)
# Basic syntax:
for variable in sequence_youre_iterating_over:
  something you want to do each iteration
  something else you want to do each iteration

# Note, you don't need to define an indexing variable in Python
# Note, "variable" takes the value of the current item in the 
# 	sequence_youre_iterating_over. If you want it to take on numbers 
# 	(E.g. 0,1,2,3,4...) use: for variable in range(end_number):
# Note, the stuff you do during each iteration is indicated by indenting
# 	it (tab or 4 spaces) - you don't need to surround it in braces { }
# Note, use break to exit the loop entirely if some condition is true and
# 	use continue to skip to the next iteration if some condition is true

# Example usage:
my_list = ["some", "crazy", "text"]
for words in my_list:
  print(words)
--> some
--> crazy
--> text

# Example usage 2:
for i in range(1, 6, 2): # Range takes arguments (start, stop, step)
  print(i)
--> 1
--> 3
--> 5

# Example usage 3:
for i in range(7):
  if i == 2:
    i += 1   # += is shorthand for: i = i + 1
    continue # If i = 2, continue skips to the next iteration of the loop
  if i == 4:
    break # If i = 4, break exits the loop entirely
  print(i)
  i += 1
--> 0
--> 1
--> 3

# While Loops
# Basic syntax:
while condition_to_check:
  something you want to do as long as condition_to_check is true
  something else you want to do as long as condition_to_check is true

# Note, make sure that the condition_to_check will return false at some
# 	point, otherwise you'll be stuck in an infinite loop
# Note, for more complicated conditions_to_check, you may need to use
# 	parentheses to clarify the argument. E.g.:
#	while (i < 10 and (j >= 5 or k == "some_text")):
# Note, indentation, break, and continue work the same as for for loops
  
# Example usage:
i = 0
while i < 3: # True as long as i is less than 3
  print(i)
  i += 1 # += is shorthand for: i = i + 1
--> 0 # Python is 0-indexed, so zero is printed first
--> 1
--> 2

# Example usage 2:
i = 0
while i < 3:
  print(i)
  i += 1
else:
  print("Something to do once, immediately after the loop terminates")
--> 0
--> 1
--> 2
--> Something to do once, immediately after the loop terminates
2
for i in range(1, 10);
	print(i)
2
for x in loop:
	print(x)
6
#While Loop:
while ("condition that if true the loop continues"):
  #Do whatever you want here
else: #If the while loop reaches the end do the things inside here
  #Do whatever you want here

#For Loop:
for x in range("how many times you want to run"):
  #Do whatever you want here
5
#simple for loop to print numbers 1-99 inclusive
for i in range(1,100):
  print(i)
  
#simple for loop to loop through a list
fruits = ["apple","peach","banana"]
for fruit in fruits:
  print(fruit)
1

  fruits = ["apple", "banana", "cherry"]
for x in fruits:
  print(x)
  
  if x == 
  "banana":
    break

 
3
x = 0
while True: #execute forever
	x = x + 1
	print(x)
1

  fruits = ["apple", "banana", "cherry"]
for x in fruits:
  if x == 
  "banana":
    continue
  print(x)
 
1

New to Communities?

Join the community