Skip to main content

For Loops

 The for loop

By polygraphus on shutterstock



Def:
The for loop is also known as the counting loop and is designed to process the items of any sequence, such as a list or a string, one by one.

General form of for loop:

The general form of the for loop is given below:
    for <variable> in <sequence>:
        statements to repeat

Usability of for loops:


We do not always use the for loop, but only for some particular reasons. These specific reasons include the cases when we need to repeat the same task again and again for a particular range of values. Note that these values may be integers, strings, lists, tuples or even dictionaries.

Example of for loop

Copy the following code from below:


for name in ["Raj","Ashish","Puskar","Aditya"]:
	print(name)
Output:

Raj
Ashish
Puskar
Aditya
For loops containing range() function
For a number based loop or a loop containing some known specific values, we use a range function to determine the start, stop and step-size of the for loop.


Example of for loop containg range() function

#Multiplication Table
print("Enter the number whose table you want to obtain:")
x = int(input())
for i in range(1,11):
    print(x,"*",i,"=",x*i)

Output:
Enter the number whose table you want to obtain:
16
16 * 1 = 16
16 * 2 = 32
16 * 3 = 48
16 * 4 = 64
16 * 5 = 80
16 * 6 = 96
16 * 7 = 112
16 * 8 = 128
16 * 9 = 144
16 * 10 = 160

©SGPython

Comments

Popular posts from this blog

Our view towards Python (as a programming language)

  Python - The language of the future Introduction When we hear the term programming language, we usually talk about high-level programming language which are those programming language having a lucid (or easy-to-read) syntax and are later converted to low-level language (binary) , which is understandable by the computer.  Programming language has always been an area of interest over the passing years since the very early days of ' Fortran ' (created by John Backus at  IBM ). To this day, dozens of programming language has been created by different 'Computer Scientists' around the world. What is Python? What is Python? Many of you will describe a large scary snake 🐍, which isn't actually wrong 😅. But here we are talking about a programming language. Similarly, Python is also a high-level and interepreted programming language created by  Guido van Rossum in the late 1980s . It was first released in 1991 as a successor to ABC Programming language . It known fo...

Character Case Swapping (PCTS)

 Python Codes The Series -1 ©Photo by wiki commons Not only in Python but also in the day-to-day life, the letters in English alphabets are characterized into two cases viz. upper case and lower case. Our first program will be a general fun program for the beginners. In this program, we will learn to swap the cases of the letters of English alphabets being entered by the user with the help of a few lines of Python code. For example: If the user enters: " hoLa AmIGo" , the python program would convert this string into " HOlA aMigO" . The Program is noted below: string=input("Enter a string :") length=len(string) s='' for i in range(length): if string[i].islower(): s=s+string[i].upper() elif string[i].isupper(): s=s+string[i].lower() else: s=s+string[i] print(s) FOR QUICK REFERENCE: The len() function gives the length of the string and the the input() function takes input from the user. Hope you like i...