fb pixel

Introduction to Python

  • Profound Academy

    • Status
      • 1
        Input and Output
      • 2
        Variables and Integer Arithmetic
      • 3
        Conditional Operators
      • 4
        Advanced Conditions - Nesting
      • 5
        Types and Variables
      • 6
        Strings
      • 7
        Lists
      • 8
        For Loops
      • 9
        While Loops
      • 10
        continue, break, while...else
      • 11
        String and List methods
      • 12
        Nested Loops
      • 13
        List Comprehension
      • 14
        Tuples and Sets
      • 15
        dict
      • 16
        Functions
      • 17
        Functions 2
      • 18
        Lambda and higher-order functions
      • 19
        Files

  • Digits

    While loops are very convenient when dealing with the digits of a number. If we have an integer, say 1486743242498701, we can take each of the digits one by one starting from the end, and process it. Every time we can take the remainder when dividing by 10 and then actually divide the number by 10. This will remove the last digit of the number:
    n = 1486743242498701
    while n != 0:
    	print('The last digit:', n % 10, end=' => ')
    	n //= 10
    	print('Resulting n:', n)
    The last digit: 1 => Resulting n: 148674324249870
    The last digit: 0 => Resulting n: 14867432424987
    The last digit: 7 => Resulting n: 1486743242498
    The last digit: 8 => Resulting n: 148674324249
    The last digit: 9 => Resulting n: 14867432424
    The last digit: 4 => Resulting n: 1486743242
    The last digit: 2 => Resulting n: 148674324
    The last digit: 4 => Resulting n: 14867432
    The last digit: 2 => Resulting n: 1486743
    The last digit: 3 => Resulting n: 148674
    The last digit: 4 => Resulting n: 14867
    The last digit: 7 => Resulting n: 1486
    The last digit: 6 => Resulting n: 148
    The last digit: 8 => Resulting n: 14
    The last digit: 4 => Resulting n: 1
    The last digit: 1 => Resulting n: 0

    Challenge

    Given an integer, your task is to print its digits separated by a space.
    Input
    Output
    123
    1 2 3
    36
    3 6
    8
    8
     
    To check your solution you need to sign in
    Sign in to continue