In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces how to use python to achieve the multiplication table, the text is very detailed, has a certain reference value, interested friends must read!
First, let's remember what the multiplication table 99 looks like.
Get to the point: implement the 99 multiplication table
You can learn the for loop range function format string print function
source code
#Outer for loop means print 9 lines for i in range(1, 10): #Inner layer for loop print column #First row 1 column, second row 2 columns, and so on for j in range(1, i+1): #Format output {rows}x{columns}={rows multiplied by columns} print('{}x{}={}\t'.format(j, i, i*j), end='') print()
results
nine-ninth multiplication table
Previous knowledge decomposition range
The python range() function creates a list of integers, typically used in for loops.
function syntax
range(start, stop[, step])
start: Counting starts from start. The default starts at 0. For example, range (5) is equivalent to range (0, 5);
stop: Counts to the end of stop, but does not include stop. For example: range (0, 5) is [0, 1, 2, 3, 4] without 5
step: step size, default is 1. For example: range (0, 5) is equivalent to range(0, 5, 1)
example
>>>range(10) #from 0 to 10[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]>> range(1, 10) #1 to 10[1, 2, 3, 4, 5, 6, 7, 8, 9]format
str.format(), which enhances string formatting.
Basic syntax is via {}
example
"{}--{}".format("hello", "world") #Do not set the specified position, in the default order 'hello--world'for loop
Here's a animation to demonstrate the for loop
Multiplication table decomposition outputs 0-9for i in range(1, 10): print(i)
output result
1234567891 to 9 without linebreaking
print ends with a newline by default
There is an end parameter that tells the print function to end with an empty string (output does not wrap)
for i in range(1,10): print(i,end='')
output result
123456789 with indentation
In python strings\t represents a tab indent
for i in range(1,10): print(i,"\t",end='')
Output result: ″ G9G ** indented second writing ** ″ G10G
The output is consistent:
1 2 3 4 5 6 7 8 9 nested outputs
nesting: imagine a table with outer layers for rows (9 rows) and contents for columns (9 columns)
The outer layer is printed ().
for i in range(1,10): for j in range(1,10): print("{}\t".format(j),end='') print()
results
1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 Output multiplication table
where i stands for row and j stands for column, and the multiplication table is output in format.
for i in range(1,10): for j in range(1,10): print("{}×{}={}\t".format(i,j,i*j),end='') print()
Results:
1×1=1 1×2=2 1×3=3 1×4=4 1×5=5 1×6=6 1×7=7 1×8=8 1×9=9 2×1=2 2×2=4 2×3=6 2×4=8 2×5=10 2×6=12 2×7=14 2×8=16 2×9=18 3×1=3 3×2=6 3×3=9 3×4=12 3×5=15 3×6=18 3×7=21 3×8=24 3×9=27 4×1=4 4×2=8 4×3=12 4×4=16 4×5=20 4×6=24 4×7=28 4×8=32 4×9=36 5×1=5 5×2=10 5×3=15 5×4=20 5×5=25 5×6=30 5×7=35 5×8=40 5×9=45 6×1=6 6×2=12 6×3=18 6×4=24 6×5=30 6×6=36 6×7=42 6×8=48 6×9=54 7×1=7 7×2=14 7×3=21 7×4=28 7×5=35 7×6=42 7×7=49 7×8=56 7×9=63 8×1=8 8×2=16 8×3=24 8×4=32 8×5=40 8×6=48 8×7=56 8×8=64 8×9=72 9×1=9 9×2=18 9×3=27 9×4=36 9×5=45 9×6=54 9×7=63 9×8=72 9×9=81
We found that the multiplication table was half full.
This is easy to handle. We do not loop more than the number of rows per column.
There's only one column in the first row.
The second row has only two columns.
In the third row, there are only three columns.
….
When the first row is on, there are only nine columns.
The key is that the second parameter of range is i+1.
for i in range(1,10): for j in range(1,i+1): print("{}×{}={}\t".format(i,j,i*j),end='') print()
The final result is:
1×1=1 2×1=2 2×2=4 3×1=3 3×2=6 3×3=9 4×1=4 4×2=8 4×3=12 4×4=16 5×1=5 5×2=10 5×3=15 5×4=20 5×5=25 6×1=6 6×2=12 6×3=18 6×4=24 6×5=30 6×6=36 7×1=7 7×2=14 7×3=21 7×4=28 7×5=35 7×6=42 7×7=49 8×1=8 8×2=16 8×3=24 8×4=32 8×5=40 8×6=48 8×7=56 8×8=64 9×1=9 9×2=18 9×3=27 9×4=36 9×5=45 9×6=54 9×7=63 9×8=72 9×9=81 The animation shows the execution process of the nine-nine multiplication table
The above is "how to use python to achieve the multiplication table" all the contents of this article, thank you for reading! Hope to share the content to help everyone, more relevant knowledge, welcome to pay attention to the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.