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
  Difference and meaning in this scrips Udbytossen 4 922 Jun-29-2025, 03:41 PM
Last Post: Gribouillis
  Unable to understand the meaning of the line of code. jahuja73 0 1,409 Jan-23-2024, 05:09 AM
Last Post: jahuja73
  Csv writer meaning of quoting mg24 2 2,671 Oct-01-2022, 02:16 PM
Last Post: Gribouillis
  meaning of -> syntax in function definition DrakeSoft 5 4,721 Apr-09-2022, 07:45 AM
Last Post: DrakeSoft
  Operator meaning explanation Sherine 3 3,508 Jul-31-2021, 11:05 AM
Last Post: Sherine
  parser.parse_args() meaning vinci 2 5,559 Oct-26-2020, 04:13 PM
Last Post: vinci
  Here what is the meaning of span=(1,2) ,match='1'? srisrinu 1 3,200 Apr-27-2020, 10:22 AM
Last Post: anbu23
  What is the meaning of mutable data type? qliu 3 4,960 Apr-17-2020, 07:20 PM
Last Post: deanhystad
  Upper-Bound Exclusive Meaning Johnny1998 1 5,368 Aug-02-2019, 08:32 PM
Last Post: ichabod801
  Finding phrases of one semantic meaning VladislavMz 2 42,423 Dec-20-2018, 03:29 AM
Last Post: VladislavMz

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020
This forum uses Lukasz Tkacz MyBB addons.
Forum use Krzysztof "Supryk" Supryczynski addons.