Phone

+919997782184

Email

support@roboticswithpython.com

Geeks of Coding

Join us on Telegram

Viewing 2 reply threads
  • Author
    Posts
    • #938
      Abhishek TyagiAbhishek Tyagi
      Keymaster

      1. How are while loops and for loops different in Python?

      Answer-
      While loops iterate while a condition is true, for loops iterate through a sequence of elements.

      2. Fill in the blanks to make the factorial function return the factorial of n.
      Then, print the first 10 factorials (from 0 to 9) with the corresponding number.
      Remember that the factorial of a number is defined as the product of an integer and all integers before it. For example,
      the factorial of five (5!) is equal to 1*2*3*4*5=120. Also, recall that the factorial of zero (0!) is equal to 1.

      Answer-

      def factorial(n):
          result = 1
          for x in range(1,n+1):
              result = result * x
          return result
      for n in range(0,10):
          print(n, factorial(n+0))

      3. Write a script that prints the first 10 cube numbers (x**3), starting with x=1 and ending with x=10.

      Answer-

      for x in range(1,11):
        print(x**3)
    • #940
      Abhishek TyagiAbhishek Tyagi
      Keymaster

      5. Write a script that prints the multiples of 7 between 0 and 100.
      Print one multiple per line and avoid printing any numbers that aren’t multiples of 7.
      Remember that 0 is also a multiple of 7

      Answer-

      def retry(operation, attempts):
        for n in range(attempts):
          if operation():
            print("Attempt " + str(n) + " succeeded")
            break
          else:
            print("Attempt " + str(n) + " failed")
      retry(create_user, 3)
      retry(stop_service, 5)
    • #941
      Abhishek TyagiAbhishek Tyagi
      Keymaster

      4. Write a script that prints the multiples of 7 between 0 and 100. Print one multiple per line and avoid printing any numbers that aren’t multiples of 7. Remember that 0 is also a multiple of 7.

      Answer-

      `for num in range(0,100):

          if(num % 7==0):
            print(num)
          else:
            num= num + 1`
Viewing 2 reply threads
  • You must be logged in to reply to this topic.