New course! Every coder should learn Generative AI!
Try a free lesson+ 15
Welcome to our forum.
We don't do homeworks here as such since we have a lot of other assignments to complete :)
Post your attempt here first.
Also please go through this
https://www.sololearn.com/discuss/333866/?ref=app
https://www.sololearn.com/Discuss/1316935/?ref=app
+ 11
Chaitra C that's fine.. but give a try first..if it fails.. you can post it..and ask it to anyone :D
Well the difference between for and while loops are explained clearly above.. you can also go through this in addition with it
The while loops keeps terminating the code inside it until it evaluates to false and for loop is used for performing operations on each item of the list or iterating it a given number of items.
Example 1:
while True:
print("Hello world") #this will print it repeatedly because the condition will remain always true.
Example 2:
for i in range(10):
print ("Hello world") #it will print Hello world ten times.
+ 3
For loop:
https://www.sololearn.com/learn/JUMP_LINK__&&__Python__&&__JUMP_LINK/2435/
While loops
https://www.sololearn.com/learn/Python/2281/
For i in range10:
is the same as
i=0
while(i<10)
i+=1
But you can also do:
For i in [1,5,3]:
i will be 1, then 5, then 3
Given this: Try, show us your attempt, and we will help to solve issues!👍
+ 2
what is this supposed to be?
A challenge? Then remove it and post it in personal feed!
A problem you have which you want to solve? Show your attempt and we can would like to help you improving it!
A question? Then elleberate your question I only see an order....
+ 2
Use a counter and add numbers like -
sum += n
print (sum) 🤨
+ 1
It is similar to i in[2,5,3]
For word in [hello,world,spam,eggs]:
word will be a different word of those words each iteration,
In the first iteration word=hello,
then word=world, then word=spam, then word=eggs
+ 1
I dont understand what you mean with:
„So the word list is as same as words list right.?“
+ 1
You can try to understand it through english language:
For every word in the words:
Do something (print the word)
Generall:
For every item of the list:
Do something (print the item)
+ 1
sum=0
for i in [1,2,3,4,5]:
sum+=i
print(sum)
results in 15
+ 1
you are welcome ☺️
+ 1
Many replies here with for loops. If you are looking for an alternative to easily sum a given list of any length we have the *:
def result(*nums):
return sum(nums)
result(1,2,3,4,5)
good luck on your learning.
+ 1
The basic approach to solve this problem is to go through every element in list using for loop.
lst is a list of numbers and n will iterate through every element in list.
sum = 0
For n in lst:
sum = sum + n
print(sum)
0
If we want to add them we dont have words but numbers, in the list.
We then declare a variable sum.
sum=0
For i in numbers:
sum+=i
0
which numbers do you eant to add?
0
sum=0
for i in [3,7,5]:
sum+=i
print(sum)
results in 15
0
i = 0
list=[1,2,3]
for num in list:
i += num
print(i)
0
For example
List=[1,2,3]
Sum=0
For i in list:
Sum= sum+ i
Print(sum)