ONLINE SUMMER TRAINING Online Courses Free Tutorials 
 Placement Preparation 
Artificial Intelligence(AI) & Machine Learning(ML) Training in Jaipur
0 like 0 dislike
1k views
in RTU/BTU B.Tech(CSE-VI SEM) PYTHON LAB by Goeduhub's Expert (7.6k points)

Write a program using for loop that loops over a sequence

Goeduhub's Online Courses @Udemy

For Indian Students- INR 570/- || For International Students- $12.99/-

S.No.

Course Name

Apply Coupon

1.

Tensorflow 2 & Keras:Deep Learning & Artificial Intelligence

Apply Coupon

2.

Computer Vision with OpenCV | Deep Learning CNN Projects

Apply Coupon

3.

Complete Machine Learning & Data Science with Python Apply Coupon

4.

Natural Language Processing-NLP with Deep Learning in Python Apply Coupon

5.

Computer Vision OpenCV Python | YOLO| Deep Learning in Colab Apply Coupon

6.

Complete Python Programming from scratch with Projects Apply Coupon

1 Answer

0 like 0 dislike
by Goeduhub's Expert (7.6k points)
edited by
 
Best answer

Ques.Write a program using for loop that loops over a sequence

Answer : 

For loops iterate over a given sequence.In sequence either (dictionary, tuple, set, list and string ) 

for loop syntax (for variable in sequence or range)

for loop concept

Examples

#For loop in list  

color_list=['red','green','white','purple','pink','marron']

for i in color_list:

   print(i)


#For loop in string 

fr_string="Goeduhub technologies"

for i in fr_string:

    print(i)


#For loop in dictionary 

fr_dict={1:'q',2:'w',3:'e',4:"r"}

for i in fr_dict:

    print(i)


#Sum of all the list elements 

Num=[1,2,3,4,5,6,7,8,9,10]

Sum=0

for i in Num:

    Sum=Sum+i

print(Sum)

Output

red green white purple pink marron


o e d u h u b t e c h n o l o g i e s


1 2 3 4


55

For loop with range()  Function

range() syntax

range(a) - Generates a sequence started from 0 to a , where a is excluded 

range(a,b)- Generates a sequence started from a ,end at b , where b is excluded 

range(a,b,c)- Generates a sequence started from a, end at (b-1) and c is difference (steps) 

Note:-By default range() function take difference of 1, if difference is not given.

Example

color_list=['red','green','white','purple','pink','marron']

for i in range(len(color_list)):

print(color_list[i])


s=0

for i in range(1,14,3):

    print(i)

    s=s+i

print("sum is =",s)

Output

red green white purple pink marron


1

4

7 10 13 

sum is= 35

For more Rajasthan Technical University CSE VI Sem Python Lab Experiments Click here

Our Mentors(For AI-ML)


Sharda Godara Chaudhary

Mrs. Sharda Godara Chaudhary

An alumna of MNIT-Jaipur and ACCENTURE, Pune

NISHA (IIT BHU)

Ms. Nisha

An alumna of IIT-BHU

Related questions

 Goeduhub:

About Us | Contact Us || Terms & Conditions | Privacy Policy || Youtube Channel || Telegram Channel © goeduhub.com Social::   |  | 
...