#the number is how many times you want to loop
for i in range (number):
#here where you put your looping code
#for example
for i in range (4):
print(Hello World)
print(Hi World)
#output
Hello World
Hi World
Hello World
Hi World
Hello World
Hi World
Hello World
Hi World
#check out more:https://www.askpython.com
#From a minimum (inclusive) to maximum (exclusive).
#Minimum is 0 unless otherwise specified
for i in range(4)
print(i) #0, 1, 2, 3
for i in range(2, 4)
print(i) #2, 3
for i in range(1, 7, 2):
print(i) #1, 3, 5
# Planet list
#twitter ----------->: @MasudHanif_
# Happy Coding..
planet = ["Mercury","Venus","Earth","Mars","Jupiter","Saturn","Uranus","Neptune"]
for planets in planet:
print(f"{planets} from solar system")
# if you want to get items and index at the same time,
# use enumerate
fruits = ['Apple','Banana','Orange']
for indx, fruit in enumerate(fruits):
print(fruit, 'index:', indx)
A for loop iterates through an iterable, may that be an array, a string, or a range of numbers
#Example:
myArr = ["string 1", "string 2"]
for x in myArr:
print(x)
#Returns "string 1", "string 2". In here the x is an iterator and does not need to be defined.
# Python for loop
for i in range(1, 101):
# i is automatically equals to 0 if has no mention before
# range(1, 101) is making the loop 100 times (range(1, 151) will make it to loop 150 times)
print(i) # prints the number of i (equals to the loop number)
for x in [1, 2, 3, 4, 5]:
# it will loop the length of the list (in that case 5 times)
print(x) # prints the item in the index of the list that the loop is currently on
# For loop where the index and value are needed for some operation
# Standard for loop to get index and value
values = ['a', 'b', 'c', 'd', 'e']
print('For loop using range(len())')
for i in range(len(values)):
print(i, values[i])
# For loop with enumerate
# Provides a cleaner syntax
print('
For loop using builtin enumerate():')
for i, value in enumerate(values):
print(i, value)
# Results previous for loops:
# 0, a
# 1, b
# 2, c
# 3, d
# 4, e
# For loop with enumerate returning index and value as a tuple
print('
Alternate method of using the for loop with builtin enumerate():')
for index_value in enumerate(values):
print(index_value)
# Results for index_value for loop:
# (0, 'a')
# (1, 'b')
# (2, 'c')
# (3, 'd')
# (4, 'e')
for i in [1,3,5,54,5683]:
print(i+1)
#This is a simple program which prints the
#successor of all numbers in the list.
#Loops are amazing! Here, 'i' is an
#iteration variable which means it changes
#with every next step on to the loop!
#so i is 1 at one point and 3 in the next!
#You can learn more @ https://python.org
#:)
# Program to find the sum of all numbers stored in a list
# List of numbers
numbers = [6, 5, 3, 8, 4, 2, 5, 4, 11]
# variable to store the sum
sum = 0
# iterate over the list
for val in numbers:
sum = sum+val
print("The sum is", sum)
# Range:
for x in range(5):
print(x) # prints 0,1,2,3,4
# Lists:
letters = ['a', 'b', 'c']
for letter in letters:
print(letter) # prints a, b, c
# Dictionaries:
letters = {'a': 1, 'b': 2, 'c': 3}
for letter, num in letters.items():
print(letter, num) # prints a 1, b 2, c 3
for number in range(1, 6): # For each number between 1-6 (not including 6)
print(number)
myList = ["a", "b", "c"]
for element in myList:
print(element)
# a 'while' loop runs until the condition is broken
a = "apple"
while a == "apple":
a = "banana" # breaks loop as 'a' no longer equals 'apple'
# a 'for' loop runs for the given number of iterations...
for i in range(10):
print(i) # will print 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
# ... or through a sequence
array = [3, 6, 8, 2, 1]
for number in array:
print(number) # will print 3, 6, 8, 2, 1
iteration_number = 10 # can be any amount or how many times you want to iterate
for iteration in range(iteration_number): # how many iterations - iteration_number variable
print(iteration)
print(" Welcome to the 100 game
")
print("To start the game you have to enter a number between 1 to 10")
print("To end the game you have to reach the number 100")
print("First one reach 100 win
")
print("Good luck
")
nums = 0
# Display numbers
def display_state():
global nums
print("100/",nums)
# Get number the player wants to play
def get_input(player):
valid = False
while not valid: # Repeat until a valid move is entered
message = player + " player please enter the number between 1 and 10: "
move = input(message) # Get move as string
if move.isdigit(): # If move is a number
move = int(move) # can take 1-10 number only
if move in range(1, 11) and nums + move <= 100:
valid = True
return move
# Update numbers after the move
def update_state(nums_taken):
global nums
nums += nums_taken
# Check if he/she is taking the last move and loses
def is_win():
global nums
if nums > 99:
return True
# define the 100 game
def play__100_game():
display_state()
while (True): # Repeat till one of them loses
first = get_input("First")
update_state(first)
display_state() # Display new numbers
if (is_win()):
print("First player won")
break
second = get_input("Second")
update_state(second)
display_state()
if (is_win()):
print("Second player won")
break
play__100_game()
for i in range(amount of times in intager form):
#do stuff
print(i)# you could use i as a variable. You could set it as u, ool, or anything you want. No blancs tho.
# i will be the amount of times the loop has been ran. You the first lap it will be 0, the second lap it will be 1. You get it.
data = [34,56,78,23]
sum = 0
# for loop is to iterate and do same operation again an again
# "in" is identity operator which is used to check weather data is present or not
for i in data:
sum +=i
print(sum)
for _ in range(1,10,2): #(initial,final but not included, gap) (the "_" underscore symbol mean there are no variables initialize in this loop)
print("hi");
# There are 2 types of loops in python
# while loops and for loops
# a while loops continues for an indefinite amount of time
# until a condition is met:
x = 0
y = 3
while x < y:
print(x)
x = x + 1
>>> 0
>>> 1
>>> 2
# The number of iterations (loops) that the while loop above
# performs is dependent on the value of y and can therefore change
######################################################################
# below is the equivalent for loop:
for i in range(0, 3):
print(i)
>>> 0
>>> 1
>>> 2
# The for loop above is a definite loop which means that it will always
# loop three times (because of the range I have set)
# notice that the loop begins at 0 and goes up to one less than 3.
def take_inputs2():
stop_word = 'stop'
data = None
inputs = []
while data != stop_word:
data = raw_input('please enter something')
inputs.append(data)
print inputs
for item in ["a", "b", "c"]:
for i in range(4): # 0 to 3
for i in range(4, 8): # 4 to 7
for i in range(1, 9, 2): # 1, 3, 5, 7
for key, val in dict.items():
for index, item in enumerate(list):
print(" Welcome to the 100 game
")
print("To start the game you have to enter a number between 1 to 10")
print("To end the game you have to reach the number 100")
print("First one reach 100 win
")
print("Good luck
")
nums = 0
# Display numbers
def display_state():
global nums
print("100/",nums)
# Get number the player wants to play
def get_input(player):
valid = False
while not valid: # Repeat until a valid move is entered
message = player + " player please enter the number between 1 and 10: "
move = input(message) # Get move as string
if move.isdigit(): # If move is a number
move = int(move) # can take 1-10 number only
if move in range(1, 11) and nums + move <= 100:
valid = True
return move
# Update numbers after the move
def update_state(nums_taken):
global nums
nums += nums_taken
# Check if he/she is taking the last move and loses
def is_win():
global nums
if nums > 99:
return True
# define the 100 game
def play__100_game():
display_state()
while (True): # Repeat till one of them loses
first = get_input("First")
update_state(first)
display_state() # Display new numbers
if (is_win()):
print("First player won")
break
second = get_input("Second")
update_state(second)
display_state()
if (is_win()):
print("Second player won")
break
play__100_game()