Skip to content
Menu
Code for Java c
  • Home
  • Java
    • Java Examples
    • Java tutorials
  • C
    • C tutorials
    • C Examples
  • C++
    • C++ Tutorials
    • C++ Examples
  • Python
    • Python Tutorials
    • Python Examples
  • About
    • About me
    • contact us
    • disclaimer
    • Privacy Policy
Code for Java c

Python language for loop with example

Posted on June 28, 2017January 22, 2020

Table of Contents

  • Python language for loop with example
      • Syntex
    • For loop in list
    • Else in for loop
    • Pattern printing using for loop

Python language for loop with example

In this tutorial, we will discuss the concept of Python language’s for loop with example

In this post, we are going to learn how to  use for loop in Python language

Python for loop is used to repeat a block of codes up to limited time to keep the computer running a program. We can use any python object such as string, array, list, tuples, dictionary in for loop.

Syntex

Python language for loop with example
Example
var – variable used to read each element form the range of elements
range – the range of elements in python (list, tuple,String any others)

Program 1

When the above code is executed it produces the folowing result
0
1
2
3
4
5

This program prints numbers from 0 to 5

program 2

When the above code is executed it produces the folowing result
1
2
3
4
5
 
When the above code is executed it produces the folowing result
0

2
4
6
8
10
12
14
16
18

Program 3
When the above code is executed it produces the folowing result
0
1
3
6
10
15
21
28
36
45
This program sums up every number  from 0 to 10 as in the example
Program 4
When the above code is executed it produces the folowing result
this is for loop
this is for loop
this is for loop
this is for loop
this is for loop
this is for loop

For loop in list

Program 5
 
Python language for loop with example
Example
When the above code is executed it produces the folowing result
(‘Total value is’, 227)
 
Program 6
When the above code is executed it produces the folowing result
Dog
Cat
Monkey
Donkey
Elephant
Mouse
Program 7
 
When the above code is executed it produces the folowing result
This is a animal name Dog
This is a animal name Cat
This is a animal name Monkey
This is a animal name Donkey
This is a animal name Elephant
This is a animal name Mouse
Program 8
 
When the above code is executed it produces the folowing result
Hen Crow Parrot Duck Ostrich Owl

Else in for loop

for i in range(10):
    print(i);
else:
    print("Program finished")

When the above code is executed it produces the folowing result

0
1
2
3
4
5
6
7
8
9
Program finished

 

Pattern printing using for loop

Program 9
Display  Floyd triangle using for loop in python
When the above code is executed it produces the folowing result
Python language for loop with example
Pattern
Program 10
When the above code is executed it produces the folowing result
Python language for loop with example
Pattern
Program 11
 
When the above code is executed it produces the folowing result
Python language for loop with example
Program 12
 
When the above code is executed it produces the folowing result
Enter a non-negative integer to find factorial : 5
1
2
6
24
120
 
Related post
While Loop in Java      while Loop in C++     While Loop In C

Do-while Loop in Java    Do-while Loop C++      Do-while loop in C 

For Loop in C++       For Loop in C         For loop in java

If condition C++       If condition in Java   If condition in C

 

Related

Recent Posts

  • Subtract two numbers using method overriding
  • PHP Star triangle Pattern program
  • Using function or method to Write temperature conversion : Fahrenheit into Celsius
  • Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user
  • Write temperature conversion program: Fahrenheit into Celsius
  • How to write a program to convert Fahrenheit into Celsius

tag

Addition (6) Array (38) C++ language (91) C language (98) c sharp (23) Division (6) Function (29) if else (32) Java language (102) JavaScript (5) loops (137) Multiply (7) Oop (2) patterns (65) PHP (13) Python Language (38) Subtraction (7) temperature (20)

Archives

Categories

Address

Global information technology

Puloly south, PointPedro

Jaffna

Srilanka

©2025 Code for Java c | Powered by SuperbThemes