Python Forum
What is the meaning of k in this function?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
What is the meaning of k in this function?
#1
Dear all,

I am in the process of learning python and going through the mod operator.
I am having a hard time understanding what is the meaning of k in the following. Why does it decide k == 2?

Highly appreciate your support.


def smallest_factor(n):
    for k in range(2, n):
        if n % k == 0:
            return k

ans = smallest_factor(33)
if ans == 3:
    print("CORRECT: 3 is the smallest factor of 33")
else:
    print("WRONG: 3 is the smallest factor of 33 but the code returned", ans)
Reply
#2
range(2, n) will yield all numbers from 2 to n-1.
Because in loop, k will take each of these value and you will test if n%k is 0, i.e. n is divisible by k. the function will return first such value
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
Ohhh boy...
Sorry for the question... of course k is an instance of range(2, n)...
Makes sense.

Highly appreciate your help buddy.

G
Reply
#4
(Aug-15-2020, 12:18 PM)giladal Wrote: of course k is an instance of range(2, n)...
no, it's not an instance of range object. because of the loop, it takes the values (int), produced by range, one by one in each iteration of the loop.
you can visualise the execution here: http://www.pythontutor.com/visualize.html

compare

def smallest_factor(n):
    numbers = range(2, n)
    print(f'numbers is {type(numbers)}')
    for k in numbers:
        print(f'k is {type(k)}')
        if n % k == 0:
            return k
smallest_factor(33)
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Csv writer meaning of quoting mg24 2 267 Oct-01-2022, 02:16 PM
Last Post: Gribouillis
  meaning of -> syntax in function definition DrakeSoft 5 827 Apr-09-2022, 07:45 AM
Last Post: DrakeSoft
  Operator meaning explanation Sherine 3 1,199 Jul-31-2021, 11:05 AM
Last Post: Sherine
  parser.parse_args() meaning vinci 2 1,539 Oct-26-2020, 04:13 PM
Last Post: vinci
  Here what is the meaning of span=(1,2) ,match='1'? srisrinu 1 1,434 Apr-27-2020, 10:22 AM
Last Post: anbu23
  What is the meaning of mutable data type? qliu 3 1,957 Apr-17-2020, 07:20 PM
Last Post: deanhystad
  Upper-Bound Exclusive Meaning Johnny1998 1 2,420 Aug-02-2019, 08:32 PM
Last Post: ichabod801
  Finding phrases of one semantic meaning VladislavMz 2 1,649 Dec-20-2018, 03:29 AM
Last Post: VladislavMz
  Meaning of some term? hsunteik 2 3,368 Dec-21-2016, 05:47 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020