Coding Exercise for Beginners in Python |Exercise 16 | Python Tutorials for Beginners #lec47

00:03:01
https://www.youtube.com/watch?v=sI_Wb3JPQg8

概要

TLDRIn this video, the instructor presents a Python coding exercise to calculate the sum of all even numbers from 1 to 100. Building on the previous lesson about the range function, two methods are demonstrated. The first method uses the range function with a step size of 2 to directly sum even numbers. The second method iterates through all numbers from 1 to 100, checking each number for evenness using the modulus operator. Both methods yield the same result: a total sum of 2550.

収穫

  • 📝 Exercise: Sum even numbers from 1 to 100
  • 🔍 Method 1: Use range with step size of 2
  • 🔍 Method 2: Check each number for evenness
  • 💡 Expected output: 2550
  • 🔧 Use of modulus operator to check even numbers
  • 📊 Understanding the range function
  • 💻 Simple coding practice
  • 📈 Building on previous lessons
  • ⏳ Pause and try the exercise
  • 🎉 Successful completion of the exercise

タイムライン

  • 00:00:00 - 00:03:01

    In this segment, the instructor introduces a coding exercise to sum all even numbers from 1 to 100 using Python. They reference the previous video on the range function and emphasize that this task focuses solely on even numbers. The instructor suggests two methods to achieve this: the first method involves using the range function with a step size of 2, starting from 2 up to 101, which directly generates even numbers. The second method involves iterating through all numbers from 1 to 100 and checking if each number is even using the modulus operator. The instructor encourages viewers to try the exercise before proceeding and assures that both methods will yield the same result of 2550.

マインドマップ

ビデオQ&A

  • What is the goal of the coding exercise?

    To find the sum of all even numbers from 1 to 100.

  • What function is primarily used in this exercise?

    The range function.

  • What is the expected output of the program?

    The sum of even numbers from 1 to 100, which is 2550.

  • How many methods are discussed to solve the exercise?

    Two methods.

  • What does the modulus operator check for?

    It checks if a number is even.

ビデオをもっと見る

AIを活用したYouTubeの無料動画要約に即アクセス!
字幕
en
オートスクロール:
  • 00:00:00
    so in the series of learning Python
  • 00:00:01
    programming language in the previous
  • 00:00:02
    video we have seen range function in
  • 00:00:04
    Python So based on that we'll be using
  • 00:00:06
    we'll be doing one more coding exercise
  • 00:00:08
    so this is very simple although you have
  • 00:00:10
    to write down a program to sum
  • 00:00:13
    to find out some of all the even numbers
  • 00:00:14
    from 1 to 100 including one and hundred
  • 00:00:18
    right so hint you just you can use range
  • 00:00:20
    function but some of even numbers only
  • 00:00:23
    from 1 to 100. in the previous video we
  • 00:00:26
    have seen sum of all the numbers from 1
  • 00:00:28
    to 100 but this time only even numbers
  • 00:00:31
    so there are two ways to do this
  • 00:00:33
    exercise
  • 00:00:35
    so I think this this is very simple no
  • 00:00:37
    need to give you something
  • 00:00:39
    so let's pause the video and try this
  • 00:00:42
    out
  • 00:00:44
    so I hope now you have done this let's
  • 00:00:46
    do this
  • 00:00:47
    there are two ways to do this exercise
  • 00:00:49
    right we will discuss both the ways
  • 00:00:51
    total is equal to 0 and in range
  • 00:00:54
    one two one zero one
  • 00:00:58
    one way is
  • 00:00:59
    we can do something like this we'll be
  • 00:01:03
    doing from 2 to 1 0 1 and
  • 00:01:07
    will increase will take the step size
  • 00:01:09
    two see by default first would be two
  • 00:01:13
    what it will print
  • 00:01:14
    first would be to I then I plus k k is 2
  • 00:01:18
    2 plus 2 4 then 6 then 8
  • 00:01:24
    all the way till 100.
  • 00:01:27
    right so these all are even numbers you
  • 00:01:30
    got even numbers now you can simply do
  • 00:01:32
    total
  • 00:01:34
    okay first I in I we will be having two
  • 00:01:37
    second time in I will be having four so
  • 00:01:39
    the PSI would be added in this total and
  • 00:01:41
    we will be getting sum of even number
  • 00:01:43
    only two five five zero
  • 00:01:46
    second second way is if you do not
  • 00:01:49
    provide here this third thing
  • 00:01:52
    so till 1
  • 00:01:53
    so now it will print
  • 00:01:55
    what
  • 00:01:57
    one two three four five till
  • 00:02:01
    100 write the range but I want to do
  • 00:02:05
    some of only these even numbers so yeah
  • 00:02:09
    we have an idea okay sorry
  • 00:02:11
    here what we can do we can write down we
  • 00:02:13
    can check if this I we can check the
  • 00:02:16
    number is even or not if this I
  • 00:02:20
    mode 2 equal to equal to 0 means
  • 00:02:25
    it will be completely divisible by two
  • 00:02:27
    then definitely it would be even number
  • 00:02:29
    so there only then only we will do total
  • 00:02:33
    okay
  • 00:02:35
    so this total would be now in this four
  • 00:02:37
    Loop indented right and after that we
  • 00:02:40
    can simply print this
  • 00:02:43
    okay I hope now you got this
  • 00:02:45
    the second way is this let's run this
  • 00:02:48
    and you will get the same two five five
  • 00:02:49
    zero
  • 00:02:51
    so I hope now you get familiar with this
  • 00:02:52
    range function how to use this right
  • 00:02:55
    so I hope you have done this exercise
  • 00:02:57
    successfully so now we'll do the next
  • 00:02:58
    video till then bye take care
タグ
  • Python
  • coding exercise
  • even numbers
  • range function
  • sum
  • programming
  • tutorial
  • modulus operator
  • loop
  • step size