Directly create a list or dictionary using a for loop.
countdown = [i for i inrange(3,0,-1)]print(countdown)
[3, 2, 1]
countdown = {"i"*i:i for i inrange(3,0,-1)}print(countdown)
{'iii': 3, 'ii': 2, 'i': 1}
3while Loops
Use while loops when you want to repeat something until a condition becomes False.
courses_left =10while courses_left !=0:print("Still a student!") courses_left -=1
Still a student!
Still a student!
Still a student!
Still a student!
Still a student!
Still a student!
Still a student!
Still a student!
Still a student!
Still a student!
4break
break exits the loop immediately.
math_subjects = ["Linear Algebra", "Calculus", "Probability", "Statistics", "Graph Theory", "Discrete Math"]for subject in math_subjects:if subject =='Probability':print("No more math for me! Thank you 😊")break
No more math for me! Thank you 😊
5continue
continue skips the rest of the current loop iteration and moves on to the next.