Python For Loop

Python For Loop

Python For Loop is a Python statement which repeats a group of statements a given number of times. For loop is very useful for any programming language. In python, for loop is very flexible and powerful. You can use any object (such as arrays, strings, lists, tuples, dictionary and so on) in a for loop in Python.

In this tutorial, we’ve explained the Python for loop examples.

For loop program to print the OnClick360 word in character

for letter in 'OnClick360': print ('Current Letter', letter)

For loop program to print the value of the list

# Print the value from list name = ["John", "Ram", "Wilard"] for i in name: print (i)
Print name value from for loop

For loop program to print sum of list value

# Print the SUM of value from list no = [1,2,3,4,5,6] sum = 0 for i in no: sum = sum + i print (sum)
Print name value from for loop

For loop program for Range

#Python loop to print 1 to 9 for i in range(1,10): print (i) # Prints out the numbers 0,1,2,3 for i in range(4): print(i) # Prints out 3,4,5 for i in range(3, 6): print(i) # Prints out 3,5,7 for i in range(3, 8, 2): print(i)
Print name value from for loop
#program to print a square of given range(3,8) for i in range(3, 8): print(i, i ** 2) print('end of loop') #Program to print word for given range (4) for i in range(2 ** 2): print('Hello, OnCLick360')

Function range(max_value, min_value) generates a sequence with numbers min_value, min_value + 1, …, max_value – 1. The last number is excluded.

print(3, 2, 1)
print(6, 5, 4)
print(3, 2, 1, sep=', ', end='. ')
print(6, 5, 4, sep=', ', end='. ')
print()
print(6, 5, 4, sep=' * ', end='.')
print(3, 2, 1, sep='', end=' <--> ')

By using sep (separator) and end arguments we can change the output of print statements.

Program to print color name from range

i = 1 for color in 'green', 'violet', 'orange', 'red', 'cyan', 'blue', 'yellow: print('#', i, 'color of rainbow is', color) i = i + 1

Program to Find the Prime number in given range

lower = 10 upper = 20 print("Prime numbers between",lower,"and",upper,"are:") for num in range(lower,upper + 1): for i in range(2,num): if (num % i) == 0: break else: print ('%d is a prime number \n' %(num)),

Program to print to list value same time

distros = ["centos", "redhat", "ubuntu"] arch = ["32-bit", "64-bit"] for i in distros: for j in arch: print(i + " " + j) print("-----------")

print 2 list value same time from zip function

colors = ["red", "green", "blue", "purple"] ratios = [0.2, 0.3, 0.1, 0.4] for color, ratio in zip(colors, ratios): print("{}% {}".format(ratio * 100, color))

 

[maxbutton id=”1" ]

 

 

Q 1. Print below pattern 

O
On
OnC
OnCl
OnCli
OnClic
OnClick
OnClick3
OnClick36
OnClick360
OnClick36
OnClick3
OnClick
OnClic
OnCli
OnCl
OnC
On
O

my_string = "OnClick360"
x = 0

for i in my_string:
    x = x + 1
    print(my_string[0[0:x]

for i in my_string:
    x = x - 1
    print(my_string[0[0:x]/pre>

Q2. Python code to print three list value same time by using zip function.

names = [ [ 'john', 'scott', 'adam' ]hobbies = [ [ 'painting', 'blogg', 'dnasing']ages = [ [ 23, 15, 25 ]
#combine those list using zip() function
for person,age, hobby in zip(names,ages,hobbies):
        print (person+" is "+str(age)+"  years old and his/her hobby is "+hobby)

Q3. Python Program to print  triangle pattern

for i in range(1,5):
    for j in range(i):
        print("*",end=' ')
    print("\n",end='')

Q4. Print Triangle pattern

size = int(input("Enter the Height of Triangle: "))
for i in range(size):
	print (((size-(i+1))*" ")+(i+1)*'*')

Q5. Print pyramid pattern

k = 0
rows = 10
for i in range(1, rows+1):
    for space in range(1, (rows-i)+1):
        print(end="  ")
    while k != (2*i-1):
        print("* ", end="")
        k = k + 1
    k = 0
    print()

 

You May Also Like