For anyone watching in 2021, if you press Shift+Enter you will run the code. Not sure if master YK has mentioned that. Also, if you don't understand the why of 'i += 1' like me at first, you're not adding a 1 to the element on the list (i.e. 4+1), you're telling the code to go 1 position up on the list (in this list 5 is in spot #0, 4 in spot #2 and so on) Hope this helps someone!
@cicada33122 жыл бұрын
this helps so much,
@LemonBoi5912 жыл бұрын
yeah u can use shift+enter to run the code but u cannot automatically add a new cell to the file so if u want to do it alongside with adding a new cell its better to use the run button.
@joeroganpodfantasy422 жыл бұрын
@@LemonBoi591 It adds a new cell for me there's three shortcuts Alt+Enter Ctrl+Enter this is like the one you describe doesnt add a new cell Shift+Enter
@joeroganpodfantasy422 жыл бұрын
the first line that has the total is adding the number the second line j+=i is just doing the looping thru the positions that's how I like to think about it. I remember in school learning C or C++ don't know if it was the professor's idea or mine to just go ahead and do the looping by hand everytime it was good practice cause by doing it by hand you get it ingrained in your head whats going on and you can actually visualize the process. You can do it by hand in these simple problems then when it comes time for hard problems you can just visualize it and not go thru the motions and appreciate the value of it,
@LemonBoi5912 жыл бұрын
@@joeroganpodfantasy42 Oh ok which one adds a new cell tho shift enter it alt enter
@bluebellhs5 жыл бұрын
I really love the fact that you give us a homework at the end of each session.
@mohammadzamani724 жыл бұрын
@ i'm asian too, so i get his point, we ALWAYS either taking or giving homework
@Username-lt7lt4 жыл бұрын
Mohammad Zamani I'm caucasian and I love getting homework at the end of these videos
@beelz10424 жыл бұрын
@@Username-lt7lt I'm African and I also love that he gives us homework , it's so entertaining and makes me feel proud of myself when I solve it
@mohammadzamani724 жыл бұрын
@@Username-lt7lt it's ok,but i JUST told that guy that Tanbir's comment is ACUURATE, wh? cause he apperently didn't get the Tanbir's joke! ok i know a lot of guys like homeworks, just like a lot of guys love chickens, but when a person of color says i like chicken it's funny, it's stereotypically funny. it's a JOK.
@oxane27223 жыл бұрын
Hehe, such homeworks where there'd be no problem if don't do😁
@AWGERSS4 жыл бұрын
I love how active this comment section is. Even though it was made 2 years ago more and more people are still saying stuff in the comments. Love it so much
@wlcus48510 ай бұрын
Yes
@filippians4134 жыл бұрын
Clearly this tutorial is the breaking point for a lot of people learning Python. All I can say is the only way for it to make sense is to practice it as much as possible. I'm struggling too, don't worry lol
@Assassin-wt8ij4 жыл бұрын
for the following program. How did we get 10 as the answer. total2 = 0 j = 1 while j < 5: total2 += j j += 1 print(total2) please answer it someone. How did we get 10 out of it?
@xnonqme37163 жыл бұрын
Keep in mind that this is a 'loop' meaning it is repeating/going through each number individually.
@codyjames92093 жыл бұрын
@@Assassin-wt8ij Holy shit i feel you. Right when i saw the answer i was confused haha.
@unstoppableluckq99853 жыл бұрын
Any encouragement being a programmer is my dream and I’m fearing this may as you said be my breaking point. I’ll keep on trying for now!
@mariannedaplin75593 жыл бұрын
@@Assassin-wt8ij it will perform total2 += j so long as j < 5. To break it down, When j = 1, it will perform 0 + 1, which results into a new 'total2' of 1 When j = 2, it will perform 1 + 2, so now total2 = 3 When j = 3, it will perform 3 + 3, so now total2 = 6 When j = 4, it will perform 6 + 4, so now total2 = 10 When j = 5, the condition is no longer met so the loop ends, giving a final answer of 10
@jamesdang2365 жыл бұрын
total6 = 0 i = 0 while i < len(given_list3): if given_list3[i] < 0: total6 += given_list3[i] i += 1 print(total6)
@nellowz54514 жыл бұрын
All this time I was writing while i < given_list3: Thanks man
@にゃ-c2f4 жыл бұрын
I was able to find the answer with the "for loop" statement much easier than the "while loop" statement. It took me an hour before I finally figured out the solution using the "while loop" statement, on the other hand it took me 2 mins to figure it out using the "for loop." HHAHAHAHAHHAHAHAHAH
@MrBenIPresume6 жыл бұрын
given_list = [ ] total = 0 for element in given_list: if element < 0: total += element print(total) This actually works regardless of the order of the indexed numbers, so they don't have to be ascending or descending.
@BLAQ_HINTS5 ай бұрын
although it will work but It will not end since you didn't give it an ending condition. there are infinity negative numbers out there.
@sunbags6 жыл бұрын
This is the first youtube coding tutorial series I've that actually teaches. Very happy with these. Will be recommending to others who wish to learn Python
@ovyeez30634 жыл бұрын
here's something inspirational I am just a 10 year old kid but i'm carrying on with coding even when I get confused. What I do is go through one tutorial every half an hour and by the end of the half an hour i've learnt something new, you don't need to rush just slow down and carry on coding.
@kingdomovies3 жыл бұрын
Why are you so rational at 10 years old
@krle81093 жыл бұрын
@@kingdomovies lol ikr I wish I was the same when I was 10 or 15 or even now,,,,
@xLunarLight6 жыл бұрын
given_list = [7, 5, 4, 4, 3, 1, -2, -3, -5, -7] total = 0 i = 0 while i < len(given_list): if given_list[i]
@jundenverllenares55526 жыл бұрын
list = [7, 5, 4, 4, 3, 1, -2, -3, -5 , -7] i = 0 total=0 # for counting number of elements i=0 to i=9 while list[i] > -7: i+=1 # getting the total backwards, from -7 to -2 while list[i] < 0: total += list[i] i -= 1 print(total) >>>> -17
@jundenverllenares55526 жыл бұрын
using "len" is cheating:D
@wesd33706 жыл бұрын
Thanks posting for that... I had the same thing except that my line for i += 1 was within the if statement... so it was stopping on the first variable because the number wasn't < 0.
@dinocelikovic58476 жыл бұрын
I did it with a for loop list = [7, 5, 4, 4, 3, 1, -2, -3, -5, -7] total = 0 for element in list: if element < 0: total += element print(total) -17
@yuzetian85126 жыл бұрын
Dino Čeliković j
@supaluck55996 жыл бұрын
given_list3 = [7,5,4,4,3,1,-2,-3,-5,-7] total6 = 0 i = 6 while i < len(given_list3): total6 += given_list3[i] i += 1 print(total6)
@rajkumarrathor83726 жыл бұрын
Sir I think the intrest of people in python is not so high according to views. Buy sir I am very interested in python. So don't stop this series of python tutorials. You are my favourite tutor for python . Love u sir
@matthewliu51616 жыл бұрын
Sir, does that matter?
@kalazakan6 жыл бұрын
my cute mammoth because he's indian
@DayumAli6 жыл бұрын
sir
@glowish19936 жыл бұрын
Sir, send booleans and variables
@m.n.uyeahiakhan65176 жыл бұрын
How to read user input until EOF? in C /C++ i use while( scanf("%d",&n) !=EOF ){ } but in Python ??????? please anyone help me
@guygreenleaf5 жыл бұрын
Wow. Im 24, about to turn 25. I've always had an affinity for computers and logic based things, but never properly utilized it due to laziness growing up. Now I work in a retail environment and although the people are cool, I can't stand the meaningless work. I was always scared/nervous to start down the CS/Software Engineering path but just through these simple tutorials alone you've opened my eyes to something I'm now spending every waking chance I get studying, and I'm even going back to school for Software Engineering. Thank you beyond words, I don't think I'll hate my life for much longer :)
@layr35925 жыл бұрын
it's not late yet. become a senior before turning 30 !
@Christopherjweaver6 жыл бұрын
list = [2,-3,4,-5,-1,2,-3,-3,-3,7] total = 0 i = 0 while i < len(list): if list[i] < 0: print("less than 0", list[i]) total += list[i] i += 1 elif list[i] > 0: print("greater than 0", list[i]) i += 1 print(total)
@akim57025 жыл бұрын
I just had a huge brain fart after the elements lesson. I gotta stop and review. On another note, thank you for the lessons! I've moved 5 times this past year, so it made going to school impossible. You've made it possible for me to continue my education through these videos. Thank you, CSDojo!
@madbnm3883 жыл бұрын
list = [7, 5, 4, 4, 3, 1, -2, -3, -5, -7] Sum = 0 i = 0 while i < len(list): if list[i] < 0: Sum += list[i] i += 1 print(Sum)
@BorderlineHypeR4 жыл бұрын
Solution with comments: given_list3 = [7, 5, 4, 4, 3, 1, -2, -3, -5, -7] # Init list total6 = 0 # Init total m = 0 # Init counter while m < len(given_list3): # While the counter is less than list length (to avoid out of index errors) if given_list3[m] >= 0: # If the number at the counter index is >= 0 m += 1 # Just add to the counter, don't do any addition if given_list3[m] < 0: # However, if the number at index m is < 0 total6 += given_list3[m] # Add the number to the total m += 1 # Add 1 to the counter print(total6) # Print the total after the loop completes This is more long winded than JesusJFuncia's solution and I recommend you use theirs.
@rezeromylife66162 жыл бұрын
ty helped a lot
@dp31x22 жыл бұрын
given_list3 = [7, 5, 4, 4, 4, 1, -2, -3, -5, -7] total6 = 0 i = -1 while True: total6 += given_list3[i] i = i - 1 if given_list3[i] >= 0: break print(total6)
@agusbrea14372 жыл бұрын
given_list3 = [7, 5, 4, 4, 3, 1, -2, -3, -5, -7] total8= 0 i = 0 while i < len(given_list3): if given_list3[i] > 0: i += 1 else: total8 += given_list3[i] i += 1 print(total8)
@natnat58313 жыл бұрын
given_list = [7, 5, 4, 4, 3, 1, -2, -3, -5, -7] total = 0 i = 0 while i < len(given_list): if given_list[i] > 0: i += 1 else: total += given_list[i] i += 1 print(total) it also works even if the order of positive and negative numbers are scrambled
@sashaalex4043 жыл бұрын
this is so helpful !!! literally d best comment here : )
@waqasahmed92416 жыл бұрын
# Sum of negative numbers using while loop given_list3 = [7, 5, 4, 4, 3, 1, -2, -3, -5, -7] total3 = 0 i = 0 while i < len(given_list3): if given_list3[i] < 0: total3 += given_list3[i] i += 1 print (total3) # output = -17
@jodux68835 жыл бұрын
I did the same
@constructaware3503 жыл бұрын
list_ = [7, 5, 4, 4, 3, 1, -2, -3, -5, -7] total = 0 i = 0 while i < len(list_): if list_[i] < 0: total += list_[i] i += 1 print(total) -17
@fennydobson13783 жыл бұрын
There's no word to describe the amount of joy I experienced when I could code the homework and did not encounter any errors :D
@ashishsanjayekka72276 жыл бұрын
list2 = [1,2,4,-3,-7,-9] total3 = 0 i = 0 while i < len(list2): if list2[i] < 0: total3 += list2[i] i = i+1 print(total3)
@wwemaniac3714 жыл бұрын
here's my take on the last porblem. see if it helps! happy coding!!! given_list3 = [7, 5, 4, 4, 3, 1, -2, -3, -5, -7] total7 = 0 i = 0 for element in given_list3: # this is to target the elements in the list if element < 0: # checking whether the int are neg or pos total7 += element # if neg, it will add the neg number to total7 i += 1 # for moving from index 0 to the last index print(total7) ans: -17
@zakozahbehni4 жыл бұрын
#Scanning through the list from the left using while loops :) given_list3 = [7,5,4,4,3,1,-2,-3,-5,-7] Total_neg = 0 j = 0 while j < len(given_list3) and given_list3[j] >= 0: j += 1 while j < len(given_list3) and given_list3[j]
@chrispeng70386 жыл бұрын
given_list3 = [7,5,4,4,3,1,-2,-3,-5,-7] total6 = 0 for i in given_list3: if i < 0: total6 = total6 + i print (total6) The answer is -17. I think it's the easiest way.
@omarabubakr66216 жыл бұрын
yep you're right but try to do it with while also it would be better to do both :) good luck
@kristijanross9776 жыл бұрын
It's easier if you put total6 += i instead of total6 = total6 + i
@TedPaul6 жыл бұрын
I guess one of the advantages is that he didn't have to define "i" because it's a FOR loop.
@da_ta6 жыл бұрын
Easy one clever ! Thanks
@LuisMorales-yx8di5 жыл бұрын
what is ''i'' it isnt definite
@venxity69592 жыл бұрын
given_list3 = [5, 4, 4, 3, 1, -2, -3, -5] total = 0 i = 0 while i < len(given_list3): if given_list3[i] < 0: total += given_list3[i] i += 1 print (total) -10 That's the code I came up with, and I'm super proud of it
@m.shakhawathussain68286 жыл бұрын
given_list3 = [7,5,4,4,3,1,-2,-3,-5,-7] total = 0 for x in given_list3: if x
@nawabmakan76075 жыл бұрын
wrong the loop breaks at the first element
@thebedroomintellectual24605 жыл бұрын
@@nawabmakan7607 wrong. The first element it looks at is
@mohankrishna27705 жыл бұрын
lst=[7,5,4,4,3,1,-2,-3,-5,-7] total=0 i=0 for i in lst: if i>=0: break total+=i print(total) can you tell me what's wrong with my code?
@thebedroomintellectual24605 жыл бұрын
@@mohankrishna2770 You defined i=0. No need. You already have total=0.
@mohankrishna27705 жыл бұрын
@@thebedroomintellectual2460 Thank you
@uddalakbiswas20825 жыл бұрын
given_list2=[7,5,4,4,3,1,-2,-3,-5,-7] total3=0 k=0 while k < len(given_list2): while given_list2[k] < 0: total3 += given_list2[k] break k += 1 print(total3)
@Crockerfeller5 жыл бұрын
Another terrific tutorial! Thank you for your time and effort. My result for your "assignment" :) # Sum all negative numbers in given list given_list = [7, 5, 4, 4, 3, 1, -2, -3, -5, -7] total = 0 i = 0 while i < len(given_list3): if given_list[i] < 0: total += given_list[i] i += 1 print(total)
@paul-cl3el4 жыл бұрын
what is the meaning i < len(given_list3)
@Hansulf3 жыл бұрын
@@paul-cl3el len is for length which basicly outputs a number (like 4) for a vector like [1,2,3,-1,-2]... So while i is less than the lenght of the vector (the list of numbers), the loop continues. I made R while in University so I knew about this function, but I was trying so hard to get the result only using the things he teach us in the video. But no way...
@Hansulf3 жыл бұрын
Oh shit, len was in the video, Im just too bad as a students jajajajaja
@MotoCruiseTamil4 жыл бұрын
given_list = [ 7, 5, 4, 4, 3, 1, -2, -3, -5, -7] total = 0 i = 0 while i < len(given_list): if given_list[i] >= 0: i += 1 else: total += given_list[i] i += 1 print(total) output: -17
@burlofreak5 жыл бұрын
I think I actually have an elegant solution: Start from the end of the list to save time; and only loop while your index meets your specifications: list3 = [7, 5, 4, 4, 3, 1, -2, -3, -5, -7] total6 = 0 i = len(list3)-1 while list3[i] < 0: total6 += list3[i] i -=1 print(total6)
@OldManTomJulio4 жыл бұрын
This. You went about about it the more efficient way. Imagine if there a billion elements in the list and only the last number was negative. Everyone else's solution would still be running a "brute force" method while your program was done in a split second.
@sinyiheng37324 жыл бұрын
why have to -1 for the third line tho?
@jaqen72734 жыл бұрын
@@sinyiheng3732 because the list has number starting from 0. so if there are 8 elements in list to get to last element we have to go len(list)-1
@sinyiheng37324 жыл бұрын
@@jaqen7273 Oh ya omg I forgot :( Thank you so much for replying!!!
@PubertPenis3 жыл бұрын
Yep this is exactly what I came up with too 👏🏻
@michaelsimons55026 жыл бұрын
given_list3 = [7, 5, 4, 4, 3, 1, -2, -3, -5, -7] total6=0 i=0 while i < len(given_list3): if given_list3[i] > 0: i += 1 else : total6 += given_list3[i] i += 1 print(total6) -17
@brisaqueque68204 жыл бұрын
I'm pretty sureprise for the views of this video. SO MANY HAS GIVEN UP GUYS KEEP GOING!!!!!!
@Traumbewusstsein6 жыл бұрын
thanks for the tutorials! my two versions of a solution: #using a while-loop, not going through the whole list b = [7, 5, 4, 4, 3, 1, -2, -3, -5, -7] sum2 = 0 j = len(b)-1 while b[j] < 0: sum2 += b[j] j -= 1 print('sum2 = ',sum2) #alternatively, using a for-loop which also doesn't go through the whole list sum3 = 0 for k in range (1, len(b)): if b[len(b)-k] >= 0: break sum3 += b[len(b)-k] print('sum3 = ',sum3)
@KhoiNguyen-jt9mr3 жыл бұрын
can you explain more about the for-loop
@mmmdkrimov12182 жыл бұрын
Why we are using j=len (b) -1?
@dennissh75873 жыл бұрын
list = [7, 5, 4, 4, 3, 1, -2, -3, -5, -7] i = 6 total = 0 while i < len(list) and list[i] < 0: total += list[i] i += 1 print (total) -17
@daredonny224 жыл бұрын
This is my code for this using for loop instead. Found it easier to be honest: given_list=[7,5,4,4,3,1,0,-5,-8,-9,-10] total=0 for i in range(len(given_list)): if given_list[i] < 0: total+=given_list[i] i+=1 print(total) While loop here: total2=0 k=0 while k
@tapfumadimairo47572 жыл бұрын
My code for the for loop was: given_list3 = [7, 5, 4, 4, 3, 1, -2, -3, -5, -7] total5 = 0 for element in given_list3: if element >= 0: break total5 += element print(total5) 0
@sadimohammadomi25565 жыл бұрын
given_list = [5,4,3,2,-2,-3,-4,-5] total=0 i=0 while i
@spacepinda5 жыл бұрын
Thank you this is what I was looking for!
@silkylonghairguy4 жыл бұрын
Here's a solution: given_list = [7, 5, 4, 2, 1, 1, -1, -5, -9, -11] total = 0 i = len(given_list) - 1 while given_list[i] < 0: total += given_list[i] i -= 1 print(given_list) print(total)
@IndigoZZ4 жыл бұрын
This is the only solution working for me, thanks! Could you just explain why i = len(given_list) - 1?
@madhusudansharma56984 жыл бұрын
We can reverse the list after that we can calculate
@arjunk59594 жыл бұрын
Reversing Index
@arjunk59594 жыл бұрын
Can you tell me why you reverse ? won't it work with normal indexing ?
@paitan2 жыл бұрын
@@arjunk5959 Imma answer this even tho it's been a year since people watching now might have the same question as you: Probably because if you decided to do it like this: given_list = [7, 5, 4, 2, 1, 1, -1, -5, -9, -11] total = 0 i = 0 while given_list[i] < 0: total += given_list[i] i += 1 print(total) It would print 0, since it starts from the beginning of the list and the statement we gave tells it to only add to the total or progress to the next number if the number is less than 0. Since the numbers at the beginning of the list are positive numbers aka more than 0 and the statement only works for numbers less than zero, nothing would happen and it would just print total as it is (total = 0).
@김동민-j9p3y4 жыл бұрын
given_list3=[5,4,4,3,1,-2,-3,-5,-7] total6=0 i=0 while i
@philip33214 жыл бұрын
Here's my solution for the last exercise: list = [7, 5, 4, 4, 3, 1, -2, -3, -5, -7] total = 0 for x in list: if x > 0: continue total += x print(total) Output: -17
@oneinabillion6544 жыл бұрын
What is continue? Does it mean skip to next element in list?
@DivaAngelika4 жыл бұрын
finally i found ur comment and its realy fixed my code...thankyouuuu
@bentv6652 Жыл бұрын
given_list = [7, 5, 4, 4, 3, 2, 1, -1, -2, -3, -4, -5, -7] print(given_list) #For Loop total1 = 0 for element in given_list: if element >= 0: continue else: total1 += element print(total1) #While Loop # given_list = [7, 5, 4, 4, 3, 2, 1, -1, -2, -3, -4, -5, -7] print(given_list) total2 = 0 i = 0 while True: if i < len(given_list) and given_list[i] >= 0: i += 1 continue else: if i < len(given_list) and given_list[i] < 0: total2 += given_list[i] i += 1 else: break print(total2) Thanks, CS Dojo!
@gravevoid98853 жыл бұрын
a tip if u don't understand the code he is talking about then keep watching the video over and over again NEVER GIVE UP GUYZ
@Rinaa966 Жыл бұрын
#Sum of all negative numbers from a list #With while loop list = [7, 5, 4, 4, 3, 1, -2, -3, -5, -7] total = 0 i = -1 while True: if list[i] >= 0: break else: total = total + list[i] i = i - 1 print(total) #with for loop total2 = 0 for i in range(len(list)): if list[i] < 0: total2 = total2 + list[i] i = i - 1 print(total2) Result: -17
@ushiealex2295 Жыл бұрын
this makes a lot of sense especially i =i -1, that way, we start to index from behind and we can break when we hit positive numbers as they dont meet the condition of (list[i] < 0). Thank you so much, i was struggling with that.
@elshaddaim37 Жыл бұрын
if you want to explanation watch the next part
@codinglearner32613 жыл бұрын
Coding is not as hard as I think it was!!! Good teaching!!!!! I like the while loop better, it is easier! This is how I do it...... given_list3 = [7, 5, 4, 4, 3, 1, -2, -3, -5, -7] total7 = 0 j = 0 while j < len(given_list3): if given_list3[j]
@IAmATablecloth6 жыл бұрын
given_list = [7, 6, 4, 2, -3, -4, -7] tot = 0 i = len(given_list) - 1 while True: tot += given_list[i] i -= 1 if given_list[i] >= 0: break print(tot) output: -17 Cheers!
@s6l6626 жыл бұрын
the result is -14
@droidzz49356 жыл бұрын
@@s6l662 because the list is different
@cgpriya75265 жыл бұрын
Can someone explain the logic
@kimani35975 жыл бұрын
we're initializing i to the length of the list of numbers which is 7. But since indexing starts from 0 and not 1, we have to -1 from the length of the list in order to give us 7 i.e 0 through 6. This enables us to start the addition operation from the last number value.
@fuhbigbiufb5 жыл бұрын
@@cgpriya7526 this is the i printed step by step : 6 5 4
@mazenl71396 жыл бұрын
given_list3 = [7, 5, 4, 4, 3, 1, -2, -3, -5, -7] total3 = 0 i = 6 while i < len(given_list3): total3 += given_list3[I] i += 1 print (total3) # This code only adds neg numbers without the need to check if element < 0 since we know where the negative numbers start. Probably the fast in terms of cpu clock cycles. Can add element < 0 to make sure element is negative but wanted to show that we can just skip over the positive values without actually checking them!
@joshhostler91356 жыл бұрын
total4 = 0 i=9 while True: total4 += given_list[i] i += -1 if given_list[i] > 0: break print(total4) =-17 Thank you very much for your videos, Im new to programming and code and your videos are helping me exponentially. I do have a question in terms of the code I entered in order to get -17. The only way I could get the answer was to manually put the index number in the i=[x] box. For this particular problem the last number in the index was in the 9th position but I was wondering if there was a way to tell python to start at the end of the index instead of the beginning. Thank you again for your time and I look forward to hearing from you and your future videos.
@Kauroseta Жыл бұрын
Now I've got the same question... I know that the last integer in list is equals to -1.. i wonder how to apply that
@hanhtooaein6368 Жыл бұрын
What about add the line 'given_list.reverse()' before the while loop? Is this legit?
@khaliqshaikh23926 жыл бұрын
it can be done simply by: given_list3 = [7, 5, 4, 4, 3, 1, -2, -3, -5, -7] total4 = 0 for element in given_list3: if element
@venxity69592 жыл бұрын
given_list3 = [5, 4, 4, 3, 1, -2, -3, -5] total = 0 i = 0 while i < len(given_list3): if given_list3[i] < 0: total += given_list3[i] i += 1 print (total) output: -10 Here's the while loop version
@nutman23612 жыл бұрын
@@venxity6959 im wondering why the number one is not included ?? is greater ten zero so why isnt add on the total number??
@WillChaseIV2 жыл бұрын
@@nutman2361 1 is greater than zero but the sum formula only applies to numbers less than zero
@nutman23612 жыл бұрын
@@WillChaseIV oh ok thx
@santoshgareri3775 жыл бұрын
given_list = [7,5,4,4,3,1,-2,-3,-5,-7] i= 0; total = 0 while i < len(given_list): if given_list[i] < 0: total += given_list[i] i += 1
@azmainahnaf84905 жыл бұрын
The solution of answer using for loops: total = 0 for element in given_list3: if element < 0: total += element The solution of answer using while loops: total = 0 i = len(given_list3) - 1 while given_list3[i] < 0: total += given_list3[i] i -= 1
@jaden34332 жыл бұрын
Thank You so much for uploading the results I was going crazy trying to get it! 👏🏼
@joeroganpodfantasy422 жыл бұрын
0:27 Shortcut to move your curser to the left by 4 is to press shift+tab as opposed to tab which move cursor to the right by 4. I kind of love watching these little shortcuts used by programmers and they dint even mention them cause it's second nature for them. But i think in that case he used backspace
@MoeDaliven6 жыл бұрын
this is like a game to me, but the game lately is getting REALLY hard or prob im just not smart enough
@kristijanross9776 жыл бұрын
But it feels good when you complete the game
@MoeDaliven6 жыл бұрын
thanks man for this comment, i lately got bored and quit learning programming, but since your point is totally right i'll continue learning again,
@kristijanross9776 жыл бұрын
heyyy never give up and remember you must enjoy programming because there is always something new (there's no ending) and basically you are completing the game every time you master over a new problem.
@JPAll6 жыл бұрын
I'm with you. I'm watching this and I have no idea what is going on. It will take me a while to get through this
@ghost-47836 жыл бұрын
Just a reminder 3 months later to get back on the horse bro! Don't give up. (:
@laspalmasera4 жыл бұрын
Using 'While Loop': given_list3 = [7, 5, 4, 4, 3, 1, -2, -3, -5, -7] total6 = 0 i = 0 while i < len(given_list3): if given_list3[i] < 0: total6 += given_list3[i] i += 1 print(total6) Using 'For Loop': given_list3 = [7, 5, 4, 4, 3, 1, -2, -3, -5, -7] total7 = 0 for element in given_list3: if element < 0: total7 += element print(total7)
@r.aniketh25425 жыл бұрын
This is of the hw list=[7,5,4,4,3,1,-2,-3,-5,-7] t=0 i=0 while i
@karthickdurai21574 жыл бұрын
our aim is to reduce the no. of iterations as it is going to increase the time complexity of our problem. so the solution is perfectly fine. But imagine we have a list of 1000 elements and only the last two are negative so we are running 998 unneccesary iterations. I hope you get my point. You could use list.reverse() to reverse it first and then probably do the same operation
@alexleebenbaum32964 жыл бұрын
Thank you so much for your coding answer! Could you tell me why it should put "while i
@karthickdurai21574 жыл бұрын
@@alexleebenbaum3296 he's iterating from i to length of the list; for eg: if array = [1 , 2 , 3 , 4] index starts from 0 to 3, length of this list would be 4, so we are giving i = 0 , i < len(list) which is 4 over here, so it runs from 0 to 3
@karthickdurai21574 жыл бұрын
@Jobert Alcantara yes good question in that case my friend just sort it and repeat the procedure we want to reduce the no. Of iterations as much as possible. Hope I answered your question
@tiggygao6 жыл бұрын
given_list3 = [7, 5, 4, 4, 3, 1, -2, -3, -5, -7] total6 = 0 i = 0 while i < len(given_list3): if given_list3[i] < 0: total6 += given_list3[i] i += 1 elif given_list3[i] > 0: total6 = total6 i += 1 print(total6)
@saimirfan9024 жыл бұрын
Hey CS Dojo, I appreciate your hard work and your dedication on this subject but I would like to say this. This course is not easy to understand for someone who is absolutely beginning from the start. It is perfect for someone who is revising or trying to learn a new logic but it is in no way easy for a beginner. Too many vast topics are being covered in a matter of 10 mins and I would say that the sequence of the videos also does not make it easy for the beginner to learn python or programing. The first vid was about variables but the next video you post is about conditional statments. I mean it is more important for a beginner to learn what actually string is or what integer is, and then the next video which gets posted is about functions. I mean come on man :D I understand it is not easy out there, but for a beginner the start should always be easy. Other than that I like your other vids, you are very skilled and intelligent and i envy you sometimes :)
@bhuvanasaravanan73485 жыл бұрын
list=[7,4,4,3,5,-1,-5,-3,-5] i=0 total=0 for i in list: if i
@markdoors21035 жыл бұрын
Thank you CS Dojo for the Lesson. I find it great. I hope the answer below is correct using while loop. given_list4 = [7,5,4,4,3,1,-2,-3,-5,-7] total6 = 0 i = -1 while True: total6 += given_list4[i] i -= 1 if given_list4[i] >= 0: break print (total6) Answer = -17
@swallowedinthesea115 жыл бұрын
Nice! Can you add the negatives: given_list4 = [3, -99, 7, 45, -3, 1, -8, 2, -7]
@markdoors21035 жыл бұрын
Hi. Yes, it is possible. just replace the list as follow: given_list4 = [3, -99, 7, 45, -3, 1, -8, 2, -7] total6 = 0 for element in given_list4: if element
@swallowedinthesea115 жыл бұрын
@@markdoors2103 Nice! Have fun! doc.lagout.org/programmation/python/Python%20for%20Kids_%20A%20Playful%20Introduction%20to%20Programming%20%5BBriggs%202012-12-22%5D.pdf www.digitalocean.com/community/tutorials/digitalocean-ebook-how-to-code-in-python www.cs.uky.edu/~keen/115/Haltermanpythonbook.pdf
@Madvincul2 жыл бұрын
why when summing the negatives i = -1 AND when summing the positives i = 0 (and not +1) ?
@yoel-95544 жыл бұрын
based from what i learn from this video, i can make this given_list3=[6,5,4,4,3,1,-2,-3,-5,-7] i=0 total=0 while i
@caiocardoso62882 жыл бұрын
Total4 = 0 i = len(given_list3) - 1 while given_list3[i] < 0: Total4 += given_list3[i] i -= 1 Print(Total4) -17 This is super useful thanks!
@Fk1rainbowsix2 жыл бұрын
That "i = len(given_list3) - 1" was smart haha
@pickolzi53126 жыл бұрын
Using the for loop instead of while loop, should work. given_list = [7, 5, 4, 4, 3, 1, -2, -3, -5, -7] total = 0 for a in given_list: if a < 0: total += a print(total)
@zero_x935 жыл бұрын
*While Statement* given_list3 = [5, 4, 4, 3, 1, -2, -3, -5, -7] Total6 = 0 I = 0 While i < len(given_3) and given_list3[i] > 0: Total6 -= given_list3[i] I += 1 Print(total6) -17 *For Loop* given_list3 = [5, 4, 4, 3, 1, -2, -3, -5, -7] Total6 = 0 I = 0 For element in given_list3: If element < 0 : Total6 += element I += 1 Print(total6) -17
@Jason0-z7v4 жыл бұрын
list = [7, 5, 4, 4, 3, 1, -2, -3, -5, -7] total = 0 for element in list: if element < 0: total += element print(total)
@Ebishenmansfield4 жыл бұрын
I did List.reverse and then did using for and while. I ll try urs too. Ty
@mal1k_me4 жыл бұрын
That's great Both of you
@ahuttee4 жыл бұрын
gl = [7, 5, 4, 4, 3, 1, -2, -3, -5, -7] total = 0 i = 0 while i < len(gl): if gl[i] < 0: total += gl[i] i += 1 print(total) gives out -17
@nrdawson274 жыл бұрын
Since we know it starts from the highest number it only makes sense to have it start from the back of the list. This way it goes through the least amount of numbers possible. given_list4.reverse() total7 = 0 i = 0 while True: total7 += given_list4[i] i += 1 if given_list4[i] >= 0: break print(total7)
@dcornerofficial24112 жыл бұрын
The list.reverse() command really helped me. So, YK also said that there is a way to do this in a for loop. Whoever is having problems with it, here's the code - #given_list4 = [7, 5, 4, 4, 3, 1, -2, -3, -5, -7] given_list4.reverse() total = 0 for i in given_list4: if i >= 0: break total += i print(total) total will be -17
@jordancooney20362 жыл бұрын
Here is my code for the problem he gave us at the end(it works btw): given_list = [ 7, 5, 4, 4, 3, 1, -2, -3, -5, -10] total = 0 i = 0 while i < len(given_list) and given_list[i] > 0: i += 1 while i < len(given_list) and given_list[i] < 0: total += given_list[i] i += 1 print(total)
@vaishnavij55782 жыл бұрын
Can you please explain the code
@nbgyz6 жыл бұрын
I don't think anybody did it like this but this is what I figured: # while loop given_list3 = [7, 5, 4, 4, 3, 1, -2, -3, -5, -7] total6 = 0 i = 9 while True: total6 += given_list3[i] i -= 1 if given_list3[i] > 0: break print(total6)
@mickeychen526 жыл бұрын
great idea! it's easier to get the length of the list and compute backward from the last element, when there are a bunch of number to handle. thanks for sharing!
@thechannel86166 жыл бұрын
I thought of doing it like that but it's not exactly dynamic and it relies on knowing the number of elements in the list when he said that we should assume we don't know the number of the elements Nice solution nevertheless
@satyad69556 жыл бұрын
This seems great. I thought of using the break function but couldn't figure out a way. One correction, instead of giving number to element we can use- "i = len(given_list3) -1".
@kimiaprh99936 жыл бұрын
Satya D That's exactly what i did!
@jodux68835 жыл бұрын
AWESOME
@junwoojeong4886 Жыл бұрын
It was a bit rough, but very helpful. given_list = [7, 5, 4, 4, 3, 1, -2, -3, -5, -7] total = 0 i = 0 while i < len(given_list): if given_list[i] < 0: total += given_list[i] i += 1 print(total)
@brandonl.garcia27375 жыл бұрын
Here is a while one that works: given_list5 = [7, 5, 4, 4, 3, 1, -2, -3, -5, -7] total7 = 0 i = -1 while given_list5[i] < 0: total7 += given_list5[i] i -= 1 print(total7) -17
@TrungNguyen-is6lq5 жыл бұрын
How can the index be -1 or lower? It starts at 0 right?
@brandonl.garcia27375 жыл бұрын
@@TrungNguyen-is6lq -1 will start index at the end of the list, in this case that's -7
@andarmanullang47974 жыл бұрын
@@brandonl.garcia2737 we supposed to not knowing the content of the list
@kalsummers95554 жыл бұрын
I was so close but I had first put i = 1 instead of -1. thanks to Brandon I was able to fix it after working on it and giving up. Thought I was going dumb lol. We're all in the process together! given_list3 = [7, 5, 4, 4, 3, 1, -2, -3, -5, -7] total6 = 0 i = -1 while True: total6 += given_list3[i] i -= 1 if given_list3[i] >= 0: break print(total6)
@farhanpirjade55944 жыл бұрын
@@andarmanullang4797 CS dojo also said that we know the elements in the list are in descending order .
@anonymousrajat39766 жыл бұрын
Using while loop----: given_list3 = [7, 5, 4, 4, 3, 1, -2, -3, -5, -7] # Q. Add all negative numbers excluding positive numbers total = 0 i = 0 while i < len(given_list3): if given_list3[i]
@mohitsharma46676 жыл бұрын
Your videos are really helpful! This has helped me to progress a lot in python. Your explanations are simple yet very informative.
@רומי-ג8ק2 жыл бұрын
# With the while loop list = [7, 5, 4, 4, 3, 1, -2, -3, -5, -7] total = 0 i = 0 a = True while a: if i < len(list) and list[i] len(list): a = False i +=1 print(total) #With the for loop list = [7, 5, 4, 4, 3, 1, -2, -3, -5, -7] total = 0 for element in list: if element
@hydrargyrumm5 жыл бұрын
total = 0 i = -1 while True: total += given_list[i] i -= 1 if given_list[i] >= 0: break print(total)
@nirwanra5 жыл бұрын
Bhargav Kantheti dude how did u get that 2nd line (i=-1) and 5th line (i-=1) ?? I dont get it
@hydrargyrumm5 жыл бұрын
@@nirwanra I basically went backwards of that list. What I did was going from back since I knew they were in descending order. So for i = -1 the list gives last number...and when I keep decrementing I move from right to left.
@ricardo810904 жыл бұрын
lista3 = [7,5,4,4,3,1,-2,-3,-5,-7] suma = 0 i = 0 while i
@Goordela4 жыл бұрын
given_list3 = [7, 5, 4, 4, 3, 1, -2, -3, -5, -7] total3 = 0 i = -1 while True: total3 += given_list3[i] i -= 1 if given_list3[i] > 0: break print(total3)
@afsirlaghari37384 жыл бұрын
This one worked for me. Thanks bro.
@joehan4s4 жыл бұрын
why i = -1 ? i thought this created to start the count from the list
@Goordela4 жыл бұрын
Flashing Light Since the list is sorted in the descending order, if we start by the -1 index it will be the last digit which will be the last negative number. So by adding -1 to the index it will keep counting until given_list[i] > 0.
@ytlmm5 жыл бұрын
given_list3 = [7,5,4,4,3,1,-2,-3,-5,-7] i = 0 totalNeg = 0 while i < len(given_list3): if given_list3[i] < 0: totalNeg += given_list3[i] i += 1 else: i += 1 print(totalNeg) Anwser is -17
@Crazy17936 жыл бұрын
# practice if the sum of the negative numbers # given_list3 = [7, 5, 4, 4, 3, 1, -2, -3, -5, -7] given_list3 = [7, 5, 4, 4, 3, 1, -2, -3, -5, -7] tot6 = 0 i = -1 while True: tot6 += given_list3[i] i += -1 if given_list3[i] > 0: break print(tot6) output -17 # i starts at - 1 instead of 0 # i += -1, so that it'll will substract 1 instead of adding 1
@elsalin95576 жыл бұрын
nothing came oiut while i type this code
@vijaymirji36543 жыл бұрын
# while loop given_list = [7, 5, 4, 4, 3, 1, -2, -3, -5, -7] total = 0 e = 0 while e < len (given_list): if given_list[e] > 0: e += 1 else: total += given_list[e] e += 1 print (total) # for loop given_list = [7, 5, 4, 4, 3, 1, -2, -3, -5, -7] total2 = 0 i = 0 for i in given_list: if i < 0: total2 += i print (total2) Answer >>> -17
@MasteringWoodwinds6 жыл бұрын
During this video, I wondered: what if you want to add all positive integers, but they're all jumbled up with negatives? I struggled with it for about 20 minutes and finally figured it out. Fun challenge for anyone who wants to try. I'll nest my solution below and hopefully someone can tell me if there's a better way to do it.
@MasteringWoodwinds6 жыл бұрын
some_list = [-2, 2, -2, 2, -2, 2] e = 0 total = 0 while e < len(some_list): if some_list[e] > 0: total += some_list[e] e += 1 elif some_list[e] < 0: e += 1 print(total)
@bryanzeng81872 жыл бұрын
late reply but much easier solution using for loop totalpos = 0 alist = list(range(a,b)) for i in alist : if i > 0 : totalpos += i
@rashidulikramsami80204 жыл бұрын
given_list = [7,5,4,4,3,1,-2,-3,-5,-7] i = 0 total = 0 while i < len(given_list): if given_list[i]>0: i+=1 continue elif given_list[i]
@anthonymusiani39916 жыл бұрын
given_list3 = [-1, -2, -3, -5, -7] total6 = 0 i = len(given_list3) - 1 while i >= 0 and given_list3[i] = 0 is there in case all numbers are negative so the i value doesn't go outside the index range. Thoughts?
@CJTheKitty2 жыл бұрын
given_list3 =[7,5,4,4,3,1,-2,-3,-5,-7] sum_neg = 0 i = 0 while i < len(given_list3): if given_list3[i]
@ericakirarodriguesterai39364 жыл бұрын
I Used this code given_list3 = [7, 5, 4, 4, 3, 1, -2, -3, -5, -7] total6 = 0 i = 0 while i < len(given_list3): if given_list3[i]
@tristanchambers8245 жыл бұрын
given_list = [5,5,4,2,1,-1,-3,-4] total6 = 0 i = -1 while given_list[i] < 0 and i < len(given_list): total2 += given_list[i] i -= 1 print(total6)
@fahimgaming5946 жыл бұрын
Hay YK.. Thanks for awesome guidelines. I'm always waiting for your new video😁
@jainanan9116 жыл бұрын
Had some trouble yesterday. Today went back and listened closely to what the givens were for the homework. Assuming you don't know the elements, yet you know they descend from greatest to least, find sum of negative numbers. and boom! voila! than you so much for videos! given_list3 = [7, 5, 4, 4, 3, 1, -2, -3, -5, -7] total = 0 i = len(given_list3) - 1 while True: total += given_list3[i] i -= 1 if given_list3[i] > 0: break print (total) answer: 17
@jinchengzhu43924 жыл бұрын
This is my answer using While loop: given_list3 = [7, 5, 4, 4, 3, 1, -2, -3, -5, -7] total7 = 0 i = len(given_list3)-1 while True: total7 += given_list3[i] i = i-1 if given_list3[i] > 0: break print(total7) Answer using for loop: total8 = 0 for i in given_list3: if i < 0: total8 += i print(total8)
@KagimuBrian6 жыл бұрын
given_list3 = [7,5,4,4,3,1,-2,-3,-5,-7] total3 = 0 i = 0 while i < len(given_list3) & given_list[i] < 0: total3 += given_list3[i] i += 1 if given_list3[i] >= 0: break print (total3)
@kaushikraghupathrunitechie6 жыл бұрын
Feel free to Check for bugs: given_list3 = [7, 5, 4, 4, 3, 1, -2, -3, -5, -7] total6 = 0 i = len(given_list3)-1 while True: total6 += given_list3[i] i = i - 1 if given_list3[i] >= 0: break print(total6) # output : -17
@renzoaldridgequiballo36866 жыл бұрын
bro can u help me? below is my code I had an output of -17 but i got an error, why? the error is at the bottom. given_list = [7,5,4,4,3,1,-2,-3,-5,-7] total = 0 i = 0 while True: i += 1 if given_list[i]
@kaushikraghupathrunitechie6 жыл бұрын
Renzo Aldridge Quiballo As per the question we need to find out the sum of only -ve numbers in the list, so you need to iterate from back of the list and therefore you need to take the value of i one less than the size of the list and the iteration must go from end to start so you need to decrement the 'i' value every time you compute and update the total value. Then you need to get out of the loop when a positive number is encountered, so you need to use 'break' keyword.Please check my code I posted in the comments.
@swallowedinthesea116 жыл бұрын
given_list = [7,5,4,4,3,1,-2,-3,-5,-7] total = 0 i = 0 while True: i += 1 # i is 1 because i = 0 + 1 if given_list[i]
@superbanane47686 жыл бұрын
I like your Idea. But this doen't work in case the list has no negatives because you're adding the last number regardless of its value. Furthermore if the list contains only negatives the while loop won't end.
@fuzzygamer77646 жыл бұрын
Renzo Aldridge Quiballo, because the last number, that is number -7 in index 9 is True. It is less than 0. Therefore it tries to continue the loop but cannot find Index 10.
@ardasar80323 жыл бұрын
total = 0 i = 0 while i < len(given_list): if given_list[i] < 0: total += given_list[i] i += 1 else: i += 1 print(total)
@adityakadam6524 жыл бұрын
given_list3 = [7, 5, 4, 4, 3, 1, -2, -3, -5, -7] total6 = 0 for x in given_list3: if x < 0: total6 += x print(total6) -17 is this solution to problem question correct?
@cx24venezuela4 жыл бұрын
For visual basic ; "for i=0 to 5 ", end with "next". Use do while and end with loop. You can use "for each (element) in ( list)" And "breaks" equals to "exit do/for"
@lassebak69096 жыл бұрын
CS Dojo can you please put the answer from the problem you gave us in this video, in the next video you upload. Just so we know if we were right :)
@suntexi6 жыл бұрын
I did this and it worked. I'm sure there are other answers though: given_list3 = [7, 5, 4, 4, 3, 1, -2, -3, -5, -7] total5 = 0 i = len(given_list3) - 1 while True: total5 += given_list3[i] i -= 1 if given_list3[i] >= 0: break print(total5)
@atony81556 жыл бұрын
if your last given_list's element is positive, it will still add into total. check if it's less than 0 FIRST
@amanbhargava19045 жыл бұрын
@tonnyu the given condition is you have to have the list in either descending order or ascending. There has to be a negative value for these codes to work as mentioned in the video. U have to write code accordingly, u can’t just use check for positive n break statement.
@MsDestinyS4 жыл бұрын
@@suntexi thanks for the answer. May i know what does i = len(given_list3) -1 mean?
@suntexi4 жыл бұрын
@@MsDestinyS This assigns the number of elements in given_list3 minus 1 to i which is the loop counter. For the record, 'i' is a traditional letter to denote a loop counter in other languages such as IBM's PL/1. If you need a loop within a loop, use 'j' then 'k', or 'i2' then 'i3' etc.
@hasankaya85226 жыл бұрын
Thanks for everything Dojo. You are the best . :) given_list3 = [7, 5, 4, 4, 3, 1, -2, -3, -5, -7] total6 = 0 i=9 while True: total6 += given_list3[i] i-=1 if given_list3[i] >=0: break print(total6) =-17
@PubertPenis3 жыл бұрын
This is supposing you don’t know the amount of numbers in the list so putting “i=9” is wrong
@kaushikraghupathrunitechie6 жыл бұрын
+CSDojo really appreciate what you are doing. It would be really helpful if you could start a data structure and algorithm course too.
@CSDojo6 жыл бұрын
It's coming up soon! :) Currently just focusing on beginners' content, but I do want to start a data structures and algorithms course soon as well.
@kaushikraghupathrunitechie6 жыл бұрын
CS Dojo Thank you so much for what you are doing.
@gerhenry71276 жыл бұрын
CS Dojo your like a juggernaut. Great work ethic. Keep them coming.
@senthilnarayanan54816 жыл бұрын
make it soon and give us a notes of pdf on python
@speedychicken8314 жыл бұрын
fukin indian
@bilalhaseeb19364 жыл бұрын
given_list = [5 , 4 , 4 , 3 , 2 , 1 , -1 , -3 , -4, -5 , -5 , -6] total4 = 0 i = 0 while i < len(given_list): total4 += given_list[i] i += 1 print(total4) # this code adds up all elements in the list and the answer is -5
@ArunrGamer6 жыл бұрын
I used the for loops. given_list3 = [7, 5, 4, 4, 3, 1, -2, -3, -5, -7] total = 0 i = 0 for element in given_list3: if given_list3[i] >= 0: given_list3[i] = 0 total += given_list3[i] i += 1 print(total) #Output: -17
@joerobinson61496 жыл бұрын
thank u i couldn't wrap my head around it
@Puppetjs5 жыл бұрын
Thanks this made sense for me.
@cleopatria13466 жыл бұрын
given_list2 = [5, 4, 4, 3, 1, 0, -1, -2, -4, -5] total6 = 0 i = 0 # while loops while i < len(given_list2): if given_list2[i]
@zakarysperaw62166 жыл бұрын
my solution, but not the same values in the list or the same variables, but it adds numbers less than zero to the total a = [1, 2, 3, 4, -1, -2, -3] total2 = 0 i = 0 while i < len(a): if a[i] > 0: i += 1 elif a[i] < 0: total2 += a[i] i += 1 print(total2) outputs -6
@higher_highs6 жыл бұрын
I went the same way but now after reading other comments here I understand this is not the most elegant way to do so.
@nontontutorial56962 жыл бұрын
My answer for the homework : given_list = [5, 4, 4, 3, 1, -2, -3, -5] i = 0 total = 0 while i < len(given_list) and True: if given_list[i]
@krishnanaudiyal27784 жыл бұрын
FOR LOOPS solution to the first Q-sum of positive integers (alternate solution w/o break statement) : given_list= [5,4,4,3,1,-2,-3,-5] total3=0 for i in given_list: if i>0: total3+= i print(total3) *17* The reason i put this here is because the first Q that YK gave regarding the calculation of positive integers of the list can be done in an easier way with FOR LOOPS instead of using WHILE LOOPS even though he said the latter is more efficient in such conditions. Plus, this could only work for WHILE LOOPS if the numbers are arranged in ascending or descending order but still FOR LOOPS will be more efficient regardless of the arrangement of the numbers. Then why to use WHILE LOOP only for certain arrangements when I can still use FOR LOOP efficiently there or in any condition. So im still confused regarding the primary purpose and function of the WHILE LOOPS. Is this so, or am I missing something? Some assistance would be appreciated ;)