More About For Loops in Python & Solutions to the Last 2 Problems (Python Tutorial #7)

Поділитися
Вставка
  • Опубліковано 24 січ 2018
  • This entire series in a playlist: goo.gl/eVauVX
    Download the sample file: www.csdojo.io/python7
    The courses I mentioned at the end of this video:
    Get Ready for Your Coding Interview: goo.gl/RMCaxW
    Introduction to Data Visualization with Python: goo.gl/fZ5oVX
    Keep in touch on Facebook: / entercsdojo
    Subscribe to my newsletter: www.csdojo.io/news
    Support me on Patreon: / csdojo

КОМЕНТАРІ • 550

  • @CSDojo
    @CSDojo  6 років тому +43

    You can download the sample file for this video here: www.csdojo.io/python7
    And here are the courses I mentioned at the end of this video:
    Get Ready for Your Coding Interview: goo.gl/RMCaxW
    Introduction to Data Visualization with Python: goo.gl/fZ5oVX

    • @ShubhamSingh-bj3ms
      @ShubhamSingh-bj3ms 6 років тому +1

      CS Dojo more videos r coming on python or it's the end

    • @Ashishpandey-tk1mf
      @Ashishpandey-tk1mf 6 років тому +2

      Please make a video of make a small project in Python

    • @SKINPOOT
      @SKINPOOT 5 років тому

      In #5, the problem you pose is actually:
      # Can you compute all multiples of 3, 5
      # that are less than 100?
      It doesn't mention finding the sum, so:
      r = list()
      for n in range(1, 100):
      if (n % 3 == 0):
      r.append(n)
      elif (n % 5 == 0):
      r.append(n)
      print(r)

    • @dimplekundu7559
      @dimplekundu7559 4 роки тому

      Ur content is super amazing and accurate.. No extra explaination 😄

    • @jwalapuramrevanth5712
      @jwalapuramrevanth5712 3 роки тому

      2²222223²2222232²2

  • @monarchofrymden
    @monarchofrymden 3 роки тому +79

    "if you don't want to wait for the next tutorial"
    me, watching them 3 years after they were uploaded: yeah...

  • @Artdeto
    @Artdeto 2 роки тому +70

    Maybe this will help someone understand 3:10 . It took me a long time to figure out.
    We can divide the code in 2 parts:
    # 1 - Outside Loop
    for i in range(len(a)):
    # 2 - Inside Loop
    for j in range(i + 1)
    print(a[i])
    Outside loop based on range(3) means there are 3 items in the range (0, 1, 2) and the code inside the loop will run for each item in the range:
    for 0
    run Inside loop
    for 1
    run Inside loop
    for 2
    run Inside loop
    Now figure out the inside loop for each element of the Outside loop. Remember j is just the variable that denotes each item in the range. It's not about calculating the value of j but how many js there will be. How many js there will be is given by the range(i + 1).
    - range for i = 0 -> i + 1 = 1, range(1) means the inside loop will run only once because there is only one item in the range, this item has a value of 0. The value 0 itself doesn't matter because it still counts as one item.
    - range for i = 1 -> i + 1 = 2, range(2) means the inside loop will run twice because there are 2 items in this range, these items are 0 and 1
    - range for i = 2 -> i + 1 = 3, range(3) means the inside loop will run three times because there are 3 items in this range, these items are 0, 1 and 2

  • @kylercrank160
    @kylercrank160 5 років тому +251

    just figured out a shorthand for running the code: shift + enter

    • @freegum123
      @freegum123 5 років тому +11

      Finally! It's been getting annoying to keep on pressing run. Thanks

    • @zero_x93
      @zero_x93 5 років тому +1

      I'm new too... i noticed when i shift+enter the "In[num] increments everytime no matter what cell I am in.
      Wondering if this action is more than just running the specific cell 🤔

    • @roelantdenelzen616
      @roelantdenelzen616 5 років тому +14

      I found another one that works, on mac; cmd + shift + q + enter, on windows; alt + f4

    • @TienVo-il8jy
      @TienVo-il8jy 4 роки тому +1

      You just saved my life

    • @sujalkoirala3675
      @sujalkoirala3675 4 роки тому +1

      Thanks dude.

  • @pilesTV
    @pilesTV 4 роки тому +38

    Personally, adding the negative numbers is simpler with a for loop.
    list3 = [7, 5, 4, 4, 3, 1, -2, -3, -5, -7]
    total3 = 0
    for num in list3:
    if num < 0:
    total3 += num
    print(total3)

    • @samore11
      @samore11 2 роки тому +5

      It is simpler - only thing is that you are iterating through all the positive numbers first instead of starting with the negative numbers in the list.

    • @sujaljain2436
      @sujaljain2436 2 роки тому

      @@samore11 Hey, what would you do if there were negative numbers in between the list and not just at the beginning or ending. I tried his and my own solutions to this problem with while, if else statements but it just doesn't seem to fully work that way. using 'for' worked for me in this regard very easily.

    • @samore11
      @samore11 2 роки тому +5

      @@sujaljain2436 do you mean if the data was not sorted? This works for that case:
      given_list3 = [7, 5, 4, 4, 3, 1, -2, -3, -5, -7]
      total6 = 0
      i=0
      while 1==1:
      if given_list3[i] < 0:
      total6 += given_list3[i]
      i+=1
      if i == len(given_list3):
      break
      print(total6)

    • @mohammedsalifu3955
      @mohammedsalifu3955 2 роки тому

      and vice versa
      very simple but why are we told to use the while loop

    • @tannudeswal97
      @tannudeswal97 Рік тому +3

      @@samore11 i checked many comments but only your solution worked for me. Thanks, I'm grateful.

  • @camilocarrillo2132
    @camilocarrillo2132 5 років тому +60

    instead of len(list)-1 you can just set index = -1 and it will start at the end of the list.
    total = 0
    index = -1
    while list[index] < 0:
    total += list[index]
    index -= 1
    total

  • @henrybs14
    @henrybs14 6 років тому +1

    Just wow, spent most of this week trying to figure out this negative number problem. Please give us some more simple problems! This has been great. Thank you.

  • @shubhindia123
    @shubhindia123 6 років тому +4

    I never thought it would be this much easier to learn for a beginner like me... You are awesome..thanks for the videos

  • @anvegar14
    @anvegar14 6 років тому

    Amazing! I ve learnt so much today! Please carry on that way CS Dojo.

  • @vindicatedafi
    @vindicatedafi 6 років тому +62

    Even if you are going at a slower pace I just repeat the series so I can really understand the idea and concepts and stuff I missed on first viewing. Its very informative and should be viewed multiple times to really get what you cover in these vids keep it up.

    • @finnorthen9845
      @finnorthen9845 4 роки тому +1

      me too

    • @pointer333
      @pointer333 2 роки тому

      yup. I've watched every video up to this one 2 or 3 times so far. i effin love this guy!

  • @dumbfoundtown
    @dumbfoundtown 5 років тому +2

    thanks CS Dojo! Your solutions and the way you explain things is so simple and effective! Occam's Razor in action

  • @paulinz5888
    @paulinz5888 6 років тому +6

    Thanks CSDOJO for your python series videos. Am understanding a lot now with it. Please do one more videos on loops, for better understanding. Thanks.
    Paul from "csdojogang"

  • @laurentiustefan398
    @laurentiustefan398 6 років тому +11

    Thanks you are a great guy for helping out so many people.
    (Interesting your first python video has 9k likes and the 7th has 315, it is like the videos on how to solve rubick's cube.)
    Wish you all the best. :-)

  • @biggee1
    @biggee1 3 роки тому

    Thank you CS Dojo, i am learning more from your channel.

  • @Squiderrant
    @Squiderrant 6 років тому +7

    This is the best python for beginners series I have found on youtube. please please keep it up :). I'm doing computer science at uni this year :0

    • @gbnepali
      @gbnepali 3 роки тому +1

      so how is ur uni?

    • @Squiderrant
      @Squiderrant 3 роки тому

      ​@@gbnepali I enjoyed the CS coursework and did well in my first programming assessment but ended up switching to a more people-orientated degree (paramedicine).

  • @chinchuluunmunkh-achit7962
    @chinchuluunmunkh-achit7962 6 років тому +1

    Absolutely loving it! Thanks YK :)

  • @manuelocana8074
    @manuelocana8074 4 роки тому

    dude its just so much fun programming i dont know how i didn't program before. Thanks for your videos they are helping me so much!

  • @techitright44
    @techitright44 5 років тому

    cs dojo's lessons are simple and easy.
    That's why i learned how to code python in a week or two.
    before this awesome step in python, i didn't know how to code in it at all.

  • @nitish145145
    @nitish145145 4 роки тому +11

    list = [7, 5, 4, 4, 3, 1, -2, -3, -5, -7]
    #sum of all negative numbers
    total = 0
    for i in list:
    if i < 0:
    total = total + i
    print(total)

    • @sentinelbl91
      @sentinelbl91 4 роки тому +1

      given_list = [7, 5, 4, 4, 3, 1, -2, -3, -5, -7]
      total = 0
      list = given_list[::-1]
      for i in list:
      if i >= 0:
      break
      total += i
      print(total)
      It's a little more complicated but I figured it can be a good practice to get in handle of break statement

    • @adiflorense1477
      @adiflorense1477 4 роки тому +1

      @@sentinelbl91 what the purpose -1 in variable list

    • @adiflorense1477
      @adiflorense1477 4 роки тому

      nice ... that with for loop

    • @SkinsFirstGeneration
      @SkinsFirstGeneration 4 роки тому

      As he said, that would go through all the numbers in order. If you have a list of 2 million numbers and only the last two are negative, you would need to go through almost the entire list to get somewhere

  • @cakanwalarora
    @cakanwalarora 4 роки тому +1

    He is always there to help us for free. I have seen UA-camrs who make coding videos and then say to join their paid course but YK is giving his courses for free I have liked each and every video in both the channels.

  • @daffaanzunatama9793
    @daffaanzunatama9793 5 років тому +1

    Thankyou YK, very helpfull. Cheers from Indonesia!

  • @minhvu8682
    @minhvu8682 3 роки тому

    Thank you so much, keep it up CS Dojo.

  • @ryanwilliams8675
    @ryanwilliams8675 4 роки тому

    I took a basic logic course and learned the difference between finding things in union, such as multiples that fit two numbers (one AND the other) and finding intersecting points, such as multiples of two numbers (either one OR the either). Seeing the basics of python so far, it seems like this logic is best understood for future work assignments. This is all stemming from the explanation of tutorial 5's practice problem where I realized we all had different answers mostly from the way the problem was asked by YK. Like any new knowledge, it's best to see WHY people got different answers and HOW they did rather than boasting about who understood YK's way of presenting the problem.
    This will effectively expound on one's foundation for learning programming concepts.
    Cheers

  • @Lion-mh9rq
    @Lion-mh9rq 4 роки тому

    thank you Dojo, for you works, if you can upload more tutorials about Python, it will be great!

  • @dreamphoenix
    @dreamphoenix 4 роки тому

    Great content as always! Thanks

  • @stanleystanyer8831
    @stanleystanyer8831 3 роки тому

    It's been great. Thanks for the help, I really appreciate it.

  • @Itsdrax911
    @Itsdrax911 5 років тому +1

    I did the second exercise using for loops on my own!! I think I'm a genius!

  • @omarmoustafa6672
    @omarmoustafa6672 3 роки тому

    Thank you so much sir; great content as usual!

  • @rizolli-bx9iv
    @rizolli-bx9iv 3 роки тому +2

    Actually a good series known python in a very simple way

  • @merrymouse2008
    @merrymouse2008 3 роки тому

    Thank you so much for explaining the previous problems! =))

  • @pile333
    @pile333 6 років тому +31

    I'm not disappointed at all for the pace of your course.
    I think the videos have the nice length furthermore.
    In order to learn Python properly, I think that no more than 2 lessons a week should be made.

    • @posertoken2989
      @posertoken2989 3 роки тому

      Bruh and here i am trying to learn a lesson per day

    • @sisamghale866
      @sisamghale866 3 роки тому +3

      Everyone has their own learning curve u can attempt any pace.

    • @oxane2722
      @oxane2722 3 роки тому

      @@sisamghale866 yup yup

  • @blakeparker6588
    @blakeparker6588 5 років тому +7

    Thanks YK, really helped! Cheers from China.

  • @ladanavelayati2621
    @ladanavelayati2621 3 роки тому

    Thanks mannn. you're a really good teacher

  • @gottod6895
    @gottod6895 2 роки тому

    Thanks man, You are a great teacher.

  • @pcproff
    @pcproff 5 років тому

    Loved these problems

  • @NandishPatelV
    @NandishPatelV 6 років тому

    YK ;) thnx ! I like small bit size vids along with the longer ones, both help.

  • @jeremydelvalle5992
    @jeremydelvalle5992 6 років тому

    Wow. Should of thought of initializing my index as the final number like you. Very helpful.
    I went a very roundabout way of doing it haha, but it still worked.
    This is the code i used:
    given_list = [7, 5, 4, 4, 3, 1, -2, -3, -5, -7] #you don't actually know the values. Only that they are descending.
    total = 0
    index = 0
    while index < len(given_list):
    if given_list[index] >= 0:
    index += 1
    else: # OR elif given_list[index] < 0:
    total += given_list[index]
    index += 1
    print(total)
    -17

  • @CO8848_2
    @CO8848_2 4 роки тому

    love the video so helpful and clear

  • @mcopanzan
    @mcopanzan 5 років тому

    Great tutorials =D

  • @jerrytomson1759
    @jerrytomson1759 6 років тому +1

    Hey CS Dojo, just wanted to say that during the range(1, 100) and earlier u didn`t told about ELIF :P or i missed it :/ nor the OR btw. But if we assume that we could use ELIF i personally did this this way:
    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:
    i += 1
    elif given_list3[i]

  • @christianw.1656
    @christianw.1656 2 роки тому

    Great vid. Thanks.

  • @user-iq3zv6fy1g
    @user-iq3zv6fy1g 6 років тому

    good tutorial, thanks.

  • @manuelocana8074
    @manuelocana8074 4 роки тому

    you can use negative indexes to reference items from right to left

  • @user-dj8xf3he8y
    @user-dj8xf3he8y 6 років тому

    Thank you so much. 😘😘😘😘😘😘😘😘😘😘😘😘😘😘😘😘😘😘😘😘😘😘😘😘😘😘😘😘😘😘😘😘😘😘😘

  • @YY-bk9bc
    @YY-bk9bc Рік тому +1

    for i in range(len(a)): #0,1,2
    for j in range(i+1):
    #i=0 --->j=0 #i=0,run a[0] once, print a[0], then go back to for loop i=1--> a[1]
    #i=1---->j=0,1 #j in range 2, print a[1] twice, then back to for loop i=2-->a[2]
    #i=2---->j=0,1,2 #j in range 3, print a[2] 3 times
    print(a[i])

  • @qudusolamilekan677
    @qudusolamilekan677 Рік тому +1

    I hope I need this for data analysis

  • @karinarodriguez6784
    @karinarodriguez6784 4 роки тому +1

    Hi, you have very good tutorials. It would help the learning process if you wrote down the original problem statement before writing the code!

  • @rashidkhitilov5308
    @rashidkhitilov5308 6 років тому

    You are awesome!

  • @sebastianmowryvergara2216
    @sebastianmowryvergara2216 3 роки тому +57

    Im having a hard time understanding loops :(

    • @arkamondal6049
      @arkamondal6049 3 роки тому +30

      maybe you should watch the video over and over again

    • @BlackJambaPlaysDota
      @BlackJambaPlaysDota 3 роки тому +3

      just put it on replay until you understand or ask questions in the comments people are ready to help

    • @filippians413
      @filippians413 3 роки тому +8

      @@arkamondal6049 I see what you did there

    • @prodigymedia1659
      @prodigymedia1659 3 роки тому

      @@filippians413 huh? he just recommended what he usually does when he does not understand a topic.

    • @randomuser5780
      @randomuser5780 3 роки тому +2

      @@prodigymedia1659 I do it too when I don't understand something. It really a good way of understanding complex topics

  • @satvikgupta290
    @satvikgupta290 6 років тому +1

    hey YK instead of such a long method we can use this method:-
    a = [7, 6, 5, 4, 3, 2, 1, 0, -1, -2, -3, -4, -5, -6, -7]
    tot = 0
    for i in a:
    if i

  • @poojag.4185
    @poojag.4185 6 років тому

    Thank you ☺

  • @pierpaoloovarini2413
    @pierpaoloovarini2413 6 років тому

    given_list = [7, 5, 4, 4, 3, 1, -2, -3, -5, -7]
    total = 0
    i = (len(given_list) - 1)
    while i < len(given_list) and given_list[i] < 0:
    total += given_list[i]
    i = i - 1
    print (total)
    It's correct to put the i < len(given_list) condition in the loop? Otherwise you'd get an error runnin out of the index, if there's not positive number in the list

  • @sanjidurrahmanchowdhury2807
    @sanjidurrahmanchowdhury2807 5 років тому +1

    #tutorial6 , also works for non descending orders .
    givenl_listrr = [ 8,7,9,-2,-3,-1]
    totalo = 0
    i = len(givenl_listrr)-1
    while givenl_listrr [i] < 0:
    totalo += givenl_listrr [i]
    i -= 1
    print(totalo)
    -6 .

  • @miscmissla4680
    @miscmissla4680 3 роки тому

    Thanks yk its really helpful

  • @olidouze8022
    @olidouze8022 4 роки тому

    I like this series! When correcting exercise 6, I am not sure to fully grasp the reason it was that technique instead of: In [ ]: sum(i for i in given_list3 if i < 0) ,was it because of speeding the process if a lot of data?

  • @izzymccartney1057
    @izzymccartney1057 6 років тому +1

    I see that you do this full time and that’s awesome. it could really help you out if you start to monetize your videos. this way, you can earn a little extra income.

  • @maliduudepana3323
    @maliduudepana3323 6 років тому

    no pace of you ,its interesting please more on python

  • @udhaya_shankar_V
    @udhaya_shankar_V 11 місяців тому

    # printing apple 1 times, banana 2 times, apple 3 times
    # while loop
    a=['apple','banana','orange']
    j=0
    while j

  • @peng9940
    @peng9940 3 роки тому

    you can also do this way to find the sum of all negative integers
    list = [7,5,3,1,-1,-3,-5,-7]
    sum = 0
    for i in list:
    if i>0:
    continue
    sum+=i
    print(sum)

  • @Druid_
    @Druid_ 2 роки тому

    Think I understand the 3:10 example better now. I'm pretty sure that the integer value the iteration(i) of the for loop stores in contingent on the array it's going through. So for example, the integer value of the first iteration of range(3) will be 0, but for range(1, 4), it will be 1. I hope that makes sense. In other words, the integer value of the iteration of the for loop is dependent on the array (list, range etc.) it's iterating through, it literally gets the value from the array.

  • @kadmilossomnium
    @kadmilossomnium 4 роки тому

    i had this solution for summing the negative numbers, seems clean
    b = somelist
    t2 = 0
    for i in b:
    if i * -1 > 0:
    t2 = t2 + i
    print(t2)

  • @217ibcnu2222
    @217ibcnu2222 4 роки тому

    Do you have a video explaining how to add up individual integers of a single value for example input = 451, output = 10? I have found some solutions to this, but I am not understanding the overall meaning and process. Thank you. I love your videos.

  • @anuptirkey358
    @anuptirkey358 3 роки тому

    Thank you.

  • @fulbetocrypto3745
    @fulbetocrypto3745 4 роки тому

    for loop is my fevorite wow

  • @yrew2004
    @yrew2004 Рік тому

    Thanks sir

  • @bwbs7410
    @bwbs7410 4 роки тому

    you should add to this playlist :( im almost done w it

  • @jefflim7020
    @jefflim7020 5 років тому

    Hi CS Dojo,
    this is another alternative to the solution that you posted.
    given_list2=[7, 5, 4, 4, 3, 1, -1,-2, -3, -5, -7]
    total2=0
    i=len(given_list2)-1
    while True:
    total2+=given_list2[i]
    i-=1
    if given_list2[i]>0:
    break
    print(total2)

  • @josephfox9221
    @josephfox9221 6 років тому +1

    a=("badger", "mushroom", "snake", "oooh its big!")
    for i in range (10):
    print (a[0])
    for i in range (2):
    print (a[1])
    for i in range (2):
    print (a[2])
    print (a[3])

  • @ashugarg3196
    @ashugarg3196 6 років тому +1

    more on python please

  • @scat_men
    @scat_men 6 років тому +1

    Can you make a video on Matrices(3x3 matrix), addition,.. using for loop?

  • @ErraticFragrance
    @ErraticFragrance 3 роки тому

    Thanks for the video! That was such an smart answer compared to my solution.

  • @olufagbemi5418
    @olufagbemi5418 4 роки тому

    New to programming. Thanks for all great lessons. Which of the loops is more appropriate to write a function that accepts an exponent and returns the corresponding Mersenne number . I can't seem to get get it. How can you help?... pls...

  • @madushani6844
    @madushani6844 2 роки тому

    Thank u so much.😊😊❤️ YK
    Can u tell me normally,how long it takes to learn python to get into the advanced level ?

  • @rajeshpardha1754
    @rajeshpardha1754 5 років тому

    @CS Dojo ,Brother I think for loop in j, when i = 0 that means j = 1 not 0. It has to be corrected if I'm not wrong.

  • @battleaxesam6042
    @battleaxesam6042 6 років тому

    thank you

  • @lukeaboom
    @lukeaboom 5 років тому

    Is there a way to continue on a sequence in a list? For instance if I have the list [1, 2, 4, 7, 11] is there a way to continue that sequence? thanks

  • @shuu6125
    @shuu6125 4 роки тому

    The variable 'i' used in the problem from tutorial 5 isn't referring to the index right? Is it referring to the element in the list?

  • @AnNguyen-qj7nr
    @AnNguyen-qj7nr 3 роки тому

    i do the exercise seems more easy like this:
    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)
    it's easier to understand and looks shorter as well. I hope I did it correctly.

    • @AnNguyen-qj7nr
      @AnNguyen-qj7nr 3 роки тому

      for those to use "while loop", "for loop" is simple as commented

  • @kslittleworld5198
    @kslittleworld5198 5 років тому

    What about using a for loop to iterate over a bunch of shapefiles you found previously to be able to print out each filename?

  • @HA-cz3xk
    @HA-cz3xk 5 місяців тому

    You rock!

  • @amarnathrajalingam5592
    @amarnathrajalingam5592 6 років тому

    @CS Dojo: I have tried the following
    t= 0
    for i in range(1,100):
    if i % 3 ==0 or i % 5 ==0:
    print(i)
    t += i
    print("total:",(t))

  • @xidd1
    @xidd1 6 років тому

    Will you make videos regarding data visualization on UA-cam in future?

  • @StaglyMusic
    @StaglyMusic 3 роки тому

    given_list = (5, 9, -12, 15, -15, -20, -7, -1)
    totalx = 0
    for i in given_list:
    if i < 0:
    totalx += i
    print(totalx)

  • @filippians413
    @filippians413 3 роки тому +13

    Who else loves the way he says banana? lol

  • @nemasprobleamas8465
    @nemasprobleamas8465 6 років тому

    Hi Cs Dojo , what hardware do you use ?

  • @brwashkurismail2556
    @brwashkurismail2556 20 днів тому

    if you got struggled with the nested for loops, consider that
    for a in range(4):
    print("CS dojo")
    ==>
    CS dojo
    CS dojo
    CS dojo
    CS dojo

  • @thomasphilipo2009
    @thomasphilipo2009 4 роки тому

    I real like much your tutorial ,do you have any other languages like php,MySql,Js, etc to do same

  • @user-ti5ze3nj1s
    @user-ti5ze3nj1s 3 роки тому

    hi = [7, 5, 4, 4, 3, 1, -2, -3, -5, -7]
    total = 0
    i = -1
    while hi[i] < 0:
    total += hi[i]
    i -= 1
    print(total)
    knowing that the last elment on the list is considered as -1 was so helpful

  • @christyn7888
    @christyn7888 5 років тому +3

    can someone please explain the concept of the inner for loop and why a[I] is printed out once, twice and three times accordingly? p.s. you're videos are amazing, thank you so much

    • @saberstreamz
      @saberstreamz 4 роки тому +1

      basically if i = 0 then i + 1 = 1 so you go back to the original list 1 time and that value is 0

  • @muayadrezq4211
    @muayadrezq4211 4 роки тому

    Ty

  • @sunny62140
    @sunny62140 6 років тому +1

    Hi Dojo,
    Can you please help me on below problem:
    This is my list:
    given_list2 = [5, 4, 3, 2, 1-1,-2,-3,-5]
    print (given_list2)
    When I tried to calculate the sum of negative integers using below code:
    i = 0
    total4 = 0
    while given_list2[i]

  • @Austin-wh4yi
    @Austin-wh4yi 4 роки тому

    for the second exercise this worked too
    total9 = 0
    for i in a:
    if i < 0:
    total9 -= 0 - i
    print(total9)

  • @rafnael8807
    @rafnael8807 Рік тому +1

    My solution to Tutorial 5's assignment was:
    hundred = list(range(0, 100))
    total = 0
    for element in hundred:
    if element % 3 == 0 or element % 5 == 0:
    total += element
    print(total)
    output was 2318
    my solution to Tutorial 6's assignment
    list = [7, 5, 4, 4, 3, 1, -2, -3, -5, -7]
    minusTotal = 0
    ei = - 1
    while list[ei] < 0:
    minusTotal += list[ei]
    ei -= 1
    print(minusTotal)

  • @shubhambhagat22
    @shubhambhagat22 6 років тому

    i am done with python ..........but the loops thing is damn hard for me .......good luck!!!!

  • @Genoville
    @Genoville 5 років тому

    Hey CS Dojo, I found this method of finding the sum of the multiples of 3 & 5 on the net. Sum({*range(1, 100, 3)}|{*range(1, 100, 5)}). It'll be great if you could explain it.

  • @harisar17
    @harisar17 6 років тому +1

    love your videos..

  • @YarinM
    @YarinM 6 років тому

    will u publish soon another interview? please!

  • @tarikyassa1076
    @tarikyassa1076 3 роки тому

    I did it this way :
    total = 0
    i = -1
    while given_list[i] < 0:
    total += given_list[i]
    i -= 1
    print(total)

  • @mariano3630
    @mariano3630 4 роки тому

    Hello YK I just started learning python and your tutorials have been of so much help... Can you please explain the concept behind the looping of multiples of 3 & 5 and summing them up?? (What I think is that multiples of 3 and 5 mean that we should sum only numbers which are divisible by both 3 and 5 at the same time and that will be ) 15, 30, 45, 60, 75 and 90 = 315??

    • @mariano3630
      @mariano3630 4 роки тому

      Why did you use if( i%3==0 or i%5==0) instead of if (i%3==0 and i%5==0)? as the question asks??

  • @sonorityscape
    @sonorityscape 6 років тому +59

    Could someone explain the loop inside of the loop 3:09 in more detail? I don't understand why the results are like that and especially don't get how the "i + 1" part is working.

    • @davoap1268
      @davoap1268 6 років тому +24

      I'll try. Think of this code as a bowl of fruits to put on the table.
      1st time: take a fruit from the bowl to put on the table.
      2nd time: take a different fruit from the bowl to put on the table and do it again once.
      3rd time: take a different fruit from a bowl to put on the table and do it again twice.
      And so on as long as the number of fruits within the bowl.

    • @haroonahsin2129
      @haroonahsin2129 6 років тому +60

      a = [0,1,2,3,4]
      for i in a:
      for j in range(i+1):
      print(i,j)
      i j
      0 0 # initially i = 0, and j = range(i + 1)= range (0+1) = range(1) so for this
      value of j for this will be 0
      1 0 # after that i = 1, and j = range(i + 1)= range (1+1) = range(2) so for this
      values of j for this will be 0, 1
      1 1
      2 0
      2 1
      2 2
      3 0
      3 1
      3 2
      3 3
      4 0
      4 1
      4 2
      4 3
      4 4

    • @varanuzz
      @varanuzz 6 років тому +95

      (sorry for my english, im not a native speaker and i might have a wording mistake)
      the lines with the "#" are just to explain the code
      So let's analyze the code part by part
      # so what the first line is doing is that for any value in range of the length of "a" (which is 3), is iterating what's inside # the i loop. Since we know that the length of a is 3 is easy to know that this first loop is going to iterate 3 times with # the values of 0, 1, 2
      for i in range(len(a)):
      # below this i loop we have another loop.
      # See that what the next two lines are doing is printing the element [i] of the list "a" i+1 times.
      # So if i = 0 you are printing the element 0 of "a" 0+1 times
      # if i = 1 you are printing the elemtn 1 of "a" 1+1 times
      # if i = 2 you are printing the element 2 of "a" 2+1 times
      for j in range (i+1):
      print(a[i])
      comments:
      -Remember to take in count 0 for iterations because even though the value of len(a) is 3. You are not iterating i in values of 1, 2, 3 you are iterating i in values of 0, 1, 2
      -Be careful with the condition of the second loop. We are just doing i + 1 because if you were to iterate "for j in range(0)" it would do nothing, but if you were to do "for j in range(0+1)" it would iterate the loop once. i + 1 Doesn't mean we are printing the element a[i+1] also.
      - never forget that the first element of a list is always 0 not 1.
      I hope i was as clear as possible, im also a begginer but just wanted to help. I tried to cover any doubt you could have.

    • @sonorityscape
      @sonorityscape 6 років тому +3

      Thanks for your help!

    • @sonorityscape
      @sonorityscape 6 років тому +2

      Thank you!