Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8931393
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T09:06:25+00:00 2026-06-15T09:06:25+00:00

Possible Duplicate: What is the reason for having ‘//’ in Python? While trying to

  • 0

Possible Duplicate:
What is the reason for having ‘//’ in Python?

While trying to do an exercise on summing digits, I stumbled on this solution:

def sum_digits(n):
   import math
   total = 0
   for i in range(int(math.log10(n)) + 1):
      total += n % 10
      n //= 10
   return total

My question is, what does the second to last line do? How is that proper syntax?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T09:06:27+00:00Added an answer on June 15, 2026 at 9:06 am

    That implements what is called floor division. Floor division (indicated by // here) truncates the decimal and returns the integer result, while ‘normal’ division returns the answer you may ‘expect’ (with decimals). In Python 3.x, a greater distinction was made between the two, meaning that the two operators return different results. Here is an example using Python 3:

    >>> 10 / 3
    3.3333333333333335
    >>> 10 // 3
    3
    

    Prior to Python 3.x, there is no difference between the two, unless you use the special built-in from __future__ import division, which then makes the division operators perform as they would in Python 3.x (this is using Python 2.6.5):

    In [1]: 10 / 3
    Out[1]: 3
    
    In [2]: 10 // 3
    Out[2]: 3
    
    In [3]: from __future__ import division
    
    In [4]: 10 / 3
    Out[4]: 3.3333333333333335
    
    In [5]: 10 // 3
    Out[5]: 3
    

    Therefore when you see something like n //= 10, it is using the same +=/-=/*=/etc syntax that you may have seen, where it takes the current value of n and performs the operation before the equal sign with the following variable as the second argument, returning the result into n. For example:

    In [6]: n = 50
    
    In [7]: n += 10
    
    In [8]: n
    Out[8]: 60
    
    In [9]: n -= 20
    
    In [10]: n
    Out[10]: 40
    
    In [11]: n //= 10
    
    In [12]: n
    Out[12]: 4
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: What is the reason for having '//' in Python? What is the
Possible Duplicate: What is System.Void? I have no practical reason for knowing this answer,
Possible Duplicate: Are do-while-false loops common? Is there a reason to have code like:
Possible Duplicate: jquery ajax call taking too long or something for some reason this
Possible Duplicate: Is there any reason to use the 'auto' keyword in C /
Possible Duplicate: How can I understand nested ?: operators in PHP? Why does this:
Possible Duplicate: Can main function call itself in C++? I found this problem very
Possible Duplicate: Are there any O(1/n) algorithms? This just popped in my head for
Possible Duplicate: Any reason my Facebook LIKE used to display a normal BOX and
Possible Duplicate: Any reason to overload global new and delete? In c++ you can

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.