How do you use a for loop in python for multiplication?
If Show And you're supposed to multiply, not add. That also means you need to initialize the result with In the previous exercise, Solved tasks for Python lesson 4-5, we've practiced our knowledge from previous lessons. In the previous tutorial, Solved tasks for Python lesson 4-5, we learned about conditions in Python. In today's lesson, we're going to introduce you all to loops. After today's lesson, we'll have covered almost all of the basic constructs to be able to create reasonable applications. LoopsThe word loop suggests that something is going to be repeated. If we wanted a program to do something 100 times, we certainly wouldn't write the same code 100x. Instead, we'll put it in a loop. There are several types of loops. We'll explain how to use them, and of course, make practical examples. The while loopThe while (condition): else: Let's create a simple example. Most of us know Sheldon from The Big Bang Theory. For those who don't, we'll simulate a situation where a guy knocks on his neighbor's door. He always knocks 3 times and then yells: "Penny!". Our code, without a loop, would look like this:
However, using loops, we no longer have to copy the same code over and over:
Console application
Knock
Knock
Knock
Penny! We've introduced an auxiliary variable named Now, let's put the variable incrementation to use. We'll print the numbers from one to ten. We want our output to be separated by spaces rather than lines. Therefore, we'll specify the
The result: Console application
1 2 3 4 5 6 7 8 9 10 As you can see, the SequencesTo be able to work with other loops, we'll have to get familiar with sequences first. Sequences in Python are container data structures, i.e. they're variables that can contain multiple items. We've already met and used a container structure (strings). Strings are sequences of characters. The for loopIf we wanted to work with all of the elements in a sequence, we'd use the for item in sequence: else: The Let's try it out on a string sequence. We'll print the string variable's characters out on separate lines:
The result: Console application
H
e
l
l
o The range() functionYou might have noticed that the
The result: Console application
1 2 3 4 5 6 7 8 9 10 As you can see, the
The
Console application
Knock
Knock
Knock
Penny! Nesting loopsNow, let's print a simple multiplication table that contains multiples of numbers from
Console application
Simple multiplication table using loops:
1 2 3 4 5 6 7 8 9 10
2 4 6 8 10 12 14 16 18 20
3 6 9 12 15 18 21 24 27 30
4 8 12 16 20 24 28 32 36 40
5 10 15 20 25 30 35 40 45 50
6 12 18 24 30 36 42 48 54 60
7 14 21 28 35 42 49 56 63 70
8 16 24 32 40 48 56 64 72 80
9 18 27 36 45 54 63 72 81 90
10 20 30 40 50 60 70 80 90 100 Notice the empty
Makes a big difference, doesn't it? Obviously, we can't use Let's make one more program where we'll practice working with an outer variable. The application will be able to
calculate an arbitrary power for an arbitrary number. Of course, we could just use the
I'm sure we all know how powers (exponents) work. Just to be sure, let me remind that, Console application
Exponent calculator
==========================
Enter the base: 2
Enter the exponent: 3
Result: 8
Thank you for using our exponent calculator Now, we know what some practical uses of the Enhancing our calculatorLet's take our calculator from previous lessons and improve it a little bit. We'll add the ability to enter more math problems. The program will not end immediately. Instead, it'll ask the user whether they wish to calculate another math problem. Here is the original version of the code:
Now, we'll put almost all the code into a print("Welcome to calculator!") goOn = "yes" while (goOn == "yes"): a = float(input("Enter the first number: ")) b = float(input("Enter the second number: ")) print("Choose one of the following operations:") print("1 - addition") print("2 - subtraction") print("3 - multiplication") print("4 - division") option = int(input("")) if (option == 1): result = a + b elif (option == 2): result = a - b elif (option == 3): result = a * b elif (option == 4): result = a / b if option > 0 and option < 5: print("result: %f" % (result)) else: print("Invalid option") goOn = input("Would you like to make another calculation? [yes/no]") print("Thank you for using our calculator.") Console application
Welcome to our calculator
Enter the first number: 12
Enter the second number: 128
Choose one of the following operations:
1 - addition
2 - subtraction
3 - multiplication
4 - division
1
Result: 140.000000
Would you like to make another calculation? [yes/no]
yes
Enter the first number
-10.5
Enter the second number: Our application can now be used multiple times and is almost complete. Note: this is one of the few codes that cannot be run online since our user bot would have to repeat inputs. In the next lesson, Solved tasks for Python lesson 6, we'll show you how to work with arrays. You've already learned quite a lot! Nothing better than a little noggin exercise, right? In the following exercise, Solved tasks for Python lesson 6, we're gonna practice our knowledge from previous lessons. Did you have a problem with anything? Download the sample application below and compare it with your project, you will find the error easily. How do you multiply numbers in a for loop?Algorithm. Step 1: Enter a number to print table at runtime. Step 2: Read that number from keyboard. Step 3: Using for loop print number*I 10 times. // for(i=1; i<=10; i++) Step 4: Print num*I 10 times where i=0 to 10.
How do you make a while loop using multiplication tables?Creating a multiplication table using while loop is shown below: b = int(input('Enter the number of the multiplicaction table : ')) print('The multiplication table of '+ str(b) + 'is : ') a=0 while a<=11: a=a+1 c= a*b print( str(b)+' x '+str(a)+' ='+str(c)) print('done!!!!
How do you multiply multiple numbers in Python?We can use numpy. prod() from import numpy to get the multiplication of all the numbers in the list. It returns an integer or a float value depending on the multiplication result.
Can we use multiplication in for loop?Here, the user input is stored in the int variable n . Then, we use a for loop to print the multiplication table up to 10. The loop runs from i = 1 to i = 10 . In each iteration of the loop, n * i is printed.
|