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

  • Nesting while loops

    When dealing with loops, it’s possible to nest conditions in loops, loops in other loops, and virtually any construct we have learned so far as long as the syntax is correct. We can also mix for loops with while loops and conditions within a single program.
    for number in range(100, 115, 2):
    	print('Number:', number, end='::')
    	while number > 0:
    		print(number % 10, end='-')
    		number //= 10
    	print()
    print('The End!')
    Number: 100::0-0-1-
    Number: 102::2-0-1-
    Number: 104::4-0-1-
    Number: 106::6-0-1-
    Number: 108::8-0-1-
    Number: 110::0-1-1-
    Number: 112::2-1-1-
    Number: 114::4-1-1-
    The End!
    So, the program iterates over even numbers ranging from 100 to 114 inclusive. It then generates the individual digits of those numbers and prints them one by one.

    Challenge

    Given n integers, you are asked to perform the following operations for each number:
    • Multiply each digit by 2
    • Print the resulting sequence of digits in reverse order
    The first line of the input contains a single integer n. The next n lines contain integers for which you need to perform the operations. It’s guaranteed that the numbers in the input don’t end with a 0.
    The program should print n lines containing the result of the operations for each integer in the sequence.
    Input
    Output
    2 2345659 874312
    18101210864 42681416
     
    To check your solution you need to sign in
    Sign in to continue