New course! Every coder should learn Generative AI!
Try a free lesson+ 17
A while loop executes as long as a condition is true.
For loop runs in a range or loops through a list.
#loops python lesson
https://www.sololearn.com/learn/Python/2435/?ref=app
#While loop
count = 0
while (count < 5):
count += 1
print(count)
#for loop in range 1-4
for i in range(1, 5):
i+=1
print(i)
#for loop iterating through items
for j in (1, 2, 8, 4, 65):
print (j)
+ 18
Loop has a condition. When that condition is true, the code inside the loop is executed until the condition becomes false.
In python, for loop basically takes a range as its condition and till then the variable is within that range, the code inside it is executed.
While loop takes any condition and till then that condition is true, it executes the code inside it.
+ 13
See there are three types of control structures in python :
1. Sequences : In this , the code is executed line by line and there are no conditions or iterations
2. Conditionals : In this , the code has been given to conditions on the basis of which it will decide the execution.
It involves ( if ) , ( Elif ) n ( else ) statements
3. Iterations : These type of codes are executed a no. Of time until a specified condition becomes true. And this is what a loop does.
Types of loops
1 . For loop : In this loop , the execution is finite and we have to give a range till where we want to execute.
Syntx : For i in range(start , end +1 , increment):
2. While loop : this loop , executes something until the condition becomes false.
It will execute a thing no. Of times till the condition remains true.
Syntx : while( condition) :
Difference between while and for loop is that for loop has been given a range till which it will execute
But in while loop we don't give range . It is more often used for
InfiniteLoop
+ 6
Loop continue to run until it it satisfy inner condition in loop parenthesis, ().
For loop: a range is specified that how many time it will execute.
While loop: range is not specified, more often use where iteration of loop depends on user's demand.
+ 5
Hlo all..so Aashka Sinha [Bday On 10May]😍 if u have studied loops then i'll say u should know abt one more kind of loop which is, foreach loop, in C++
https://code.sololearn.com/cBqEMh7dECZJ/?ref=app
+ 5
Aashka Sinha [Bday On 10May]😍 welcome for ur support..well if u r talking abt python then..i see Lord Krishna has told python loops very effectively..in my code i have significantly showed that foreach loop for array iteration..
well statement:
for i in range(0,5)
is actually a foreach loop, where 'range' is returning list of integers of 0 to 4 and
i is an element used for iteration for the returned list of 0 to 4..
...if u got misunderstood something plz tell me..
best of luck for ur programming career ahead!
+ 5
Aashka Sinha [Bday On 10May]😍 well the 'for' loop is extended to do the functionality of 'foreach' loop in python..so yes it is!!
+ 5
Aashka Sinha [Bday On 10May]😍 im happy to help u😉😊😊😊
+ 4
The initialization is an assignment statement that is used to set the loop control variable. ... The loop iterates while the condition is true. When the condition becomes false, program control passes to the line of code immediately following the loop
+ 4
Both works same but method is different
+ 4
Iteration is a more formal term for looping. Another method of iteration is called recursion. Read on it.
+ 4
for-loop in python is usually written like this
for i in range(x):
statement
this will execute statement x number of times.
You’ll see in other languages that for loops are written like this:
for(int i = 0; i < x; i++) {statement}
this gives the for-loop a start, end, and increment value.
The same can be done in Python’s range:
for i in range(start, stop, increment):
statement
for example
for i in range(0, 10, 2):
print(i)
outputs:
0
2
4
6
8
+ 4
While loop is used for looping statements and for loop is used to traverse the lists.
+ 4
Hlo again,Aashka Sinha,like u told me that u were learning python,so for u i have created the range function for range purpose,and it is very easy to use(it is however, inspired from the python range function),have a look:
https://code.sololearn.com/c3e54MZ54m6c/?ref=app
+ 3
while loop can be used as an infinite loop like this;
while(true) {
statements;
}
and condition can be checked within a body to break the loop.
while(true) {
if (condition) {
break;
}
}
for loop is used to loop for a limited number of how many time you want to execute its statements or loop a list. But it can be used as an infinite loop like this.
for (int i=0; i>1;i--) {
statements;
}
This for loop will never stops because "i" will never greater than "1." You better check the condition inside the body to break the loop. This is the example of how for loop can be used as an infinite loop.
and most importantly you can use "recursive function or method" to make a loop without using both "for and while" loops.
+ 1
it checks the given condition, if the condition is true it will stop the loop else it will keep on looping until the condition is true.
+ 1
Loops in python are awesome especially it's fun to use for loop everywhere!
WHAT CAN BE CALLED A LOOP?
Any loop must satisfy 3 properties:
1) initialization of iterable variable
2) an exit condition
3) a way to iterate
while loops execute while condition is true
but they are rarely used in python
Most of places you will only see use of for loops due to their easy and understandable iterative nature over lists, tuples, or say any iterables
WHILE LOOP
SYNTAX
while <bool_exp>:
<statments>
[<increment_decrement>]
EXAMPLE
skills = ["Python3", "C", "JavaScript"]
i = 0
while i < len(skills):
print("I love {}".format(skills[i]), end='\n')
i += 1
FOR LOOP
SYNTAX
for <iterable_var> in <iterable_seq>:
<statements>
EXAMPLE
skills = ["Python3", "C", "JavaScript"]
for skill in skills:
print("I love {}".format(skill), end='\n')
OUTPUT
I love Python3
I love C
I love JavaScript
+ 1
Pls put answered.