• Home
  • Articles
  • Python
    • Top 10 Reasons to Learn Python
    • Start with Python Basics
    • Python Skills Are in High Demand
  • About Us
Login
Corporate School
  • Home
  • Articles
  • Python
    • Top 10 Reasons to Learn Python
    • Start with Python Basics
    • Python Skills Are in High Demand
  • About Us

Python 3

  • Home
  • Blogs
  • Python 3
  • Python For Loop With Example Part-1 of 3

Python For Loop With Example Part-1 of 3

  • Posted by CS34
  • Categories Python 3
  • Date June 1, 2019
Python For Loop With Example - Corporate School
[vc_row][vc_column][vc_custom_heading source=”post_title” font_container=”tag:h1|text_align:center” use_theme_fonts=”yes”][vc_column_text]Python for loop is one the basic concept or building block of Python. Learning any language is not a challenge when you have enough details with an example to understand the concept. Python is no different. In fact, Python is one of the easiest languages with the efficiency of powerful language. In this tutorial, I will try to explain the concept of Python for loop. This will not only help your language but also save you time when you have to do the repetitive task in the program. Loops in any traditional programming language (Python, in our case) is used when you need a specific set of code lines to be executed for a specific number of times. There are two kinds of loops in Python
  • For Loop – When the number of iterations is known
  • While loop – When the iteration is decided at condition base.
Python is not going to be any different in this case, so it does the same, iterates over the block of code within the for loop and executes it by the number of times as specified. On the contrary, you would want to check your understanding as against the while loop, which also does the same thing, but does it based on a condition to be satisfied or executes it until infinity.

What is a Python for Loop?

A for loop is used to repeat a piece of code n number of times. The for loop is usually used with a list of things. The basic syntax for the for loop looks like this:
for item in list:
         things to do
Translated into common English, this would be: “For each item that is present in the list, do the thing”. There are a few things to note here:
  • Every for loop must reference a list or a range.
  • Every for loop must close with a colon.
Code to be executed as part of the for loop must be indented by four spaces (or one press of the Tab key) or single while space. You can also read the official Python Style Guide to know more about python styling. To use individual items in the list or range, we have to create a variable (item in the syntax above). There’s no need to declare this variable before creating the for loop. You can also reuse the same variable for other for loops. You’ve used for loops extensively in your own life. As an example, suppose you have to go grocery shopping. You walk into the grocery store, grab a cart, and pull out your shopping list. As you walk through the aisles, you pull out each item on the shopping list and place it into the cart. In a way, you are using a for loop – for every item present in your shopping list, you pull it out and keep it in your cart.[/vc_column_text][vc_column_text]

Python for Loop Example – 01

Print each fruit in a fruit list:
fruits = ["apple", "banana", "litchi"]
for x in fruits:
  print(x)
 

Looping Through a String

String is the most common data type used but one of the most complex data types in any programming language. But, Thanks to Python which made this complex object also very simple. You can consider string as group (array) of characters and they contain a sequence of characters:

Python for Loop Example – 02

Loop through the letters in the word “banana”:
for x in "litchi":
  print(x)

Else in For Loop

The else keyword in a for loop specifies a block of code to be executed when the loop is finished:

Python for Loop Example – 03

Print all numbers from 0 to 16, and print a message when the loop has ended:
for x in range(17):
  print(x)
else:
  print("it’s over")
[/vc_column_text][/vc_column][/vc_row][vc_row][vc_column][vc_column_text]You can test your knowledge with our handcrafted  Python Interview Questions[/vc_column_text][/vc_column][/vc_row]
Please login to see your profile content

Tag:Python 3, Python Example, Python For Loop

  • Share:
User Avatar
CS34

Previous post

6 Weeks Summer Training Program in Chandigarh
June 1, 2019

Next post

Python Interview Questions and Answers Challenge 01
June 1, 2019

You may also like

Amazing Python Training in Chandigarh with Advance Syllabus
5 December, 2020

[vc_row][vc_column][vc_gallery interval=”3″ images=”9914″][vc_column_text]”Life is amazing, is not it? You can’t ever tell about. Nobody knows until they go ahead and play the game.”   Amazing Python Training in Chandigarh with Advance Syllabus Life and the situation both are unexpected. In …

python interview questions - corporate school
Python Interview Questions and Answers Challenge 01
1 June, 2019

Top 10 Reason to Learn Top 10 Reason to Learn Python-Corporate School

Start with Basics – Python

Python High in DemandPython-training-in-chandigarh

Future of AIFuture-of-Artificial-Intelligence , Artificial-Intelligence-Corporate-school-chandigarh

CORPORATE SCHOOL - Best Professional Training Institute

(+91) 9878733766

info@corporateschool.com

English

  • Spoken English
  • General English
  • Interview Preparation

IT Training

  • Python Training
  • 6 Month AI Training

Blogs

  • Learn Python FREE
  • Future of AI
  • Python High Demand

Powered By Corporate School

Login with your site account

Lost your password?