Most of the students who learn Pyhon feel that they can understand it as soon as they learn it. The main thing is that there is less code. Too complex projects, learning and research is too difficult, will hinder our confidence in learning today to teach you to write some simple small cases, consolidate their basic knowledge come on young, write enough 5000 lines of basic pass

First, let’s remember what a 99 times table looks like

image

Let’s get down to business: implementing the 99 times table

You can learn

  • The for loop

  • Range function

  • Format Format string

  • The print function

The source code

For j in range(1, I +1): for j in range(1, I +1): Column # formatting output line {} {x} = {row by column} print (' x = {} {} {} \ t '. The format (j, I, I * j), end = ' ') print ()Copy the code

The results of

The multiplication table

Decomposition of pre-prepared knowledge

range

The Python range() function creates a list of integers, typically used in a for loop. The function of grammar

range(start, stop[, step])
Copy the code

Start: The count starts from start. The default value is 0. For example, range (5) is equivalent to range (0, 5); Stop: Counts to stop, but does not include stop. For example, range (0, 5) is [0, 1, 2, 3, 4]. There is no 5 step. The default value is 1. For example: range(0, 5) is equivalent to range(0, 5, 1)

For example,

> > > range (10) # starting from 0 to 10 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] > > > range (# 1, 10) starting from 1 to 10 [1, 2, 3, 4, 5, 6, 7, 8, 9]Copy the code

format

Str.format (), which enhances string formatting. The basic syntax is exemplified by {}

"{}, {}". The format (" hello ", "world") # does not set the specified position, in the default order 'hello, world'Copy the code

The for loop

Here’s a GIF to illustrate the for loop

The for loop. GIF

The multiplication table dissolves

0-9

for i in range(1, 10):    print(i)
Copy the code

The output

123456789
Copy the code

1 through 9 without newlines

The default execution of print is to end with a newline. There is an end argument that tells print to end with an empty string.

For I in range(1,10): print(I,end= "")Copy the code

The output

123456789
Copy the code

Have the indentation

In Python strings \t represents a TAB indent

For I in range(1,10): print(I,"\t",end= "")Copy the code

Output: “G9G **” is indented second: “G10G” is the same:

1, 2, 3, 4, 5, 6, 7, 8, 9Copy the code

Nested output

Nesting: Imagine a table with the outer layer for representing rows (9 rows) and the content for representing columns (9 columns) wrapped with print()

For I in range(1,10): for j in range(1,10): print("{}\t". Format (j),end= ") print()Copy the code

The results of

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   
Copy the code

Output by table

Where I stands for row and J stands for column, format outputs the multiplication table

For I in range (1, 10) : for j in range (1, 10) : print (x = {} {} {} \ "t". The format (I, j, I * j), end = ' ') print ()Copy the code

Results:

1 * 1 = 1 1 * 2 = 2 1 1 x 4 x3 = 3 = 4 1 * 1 * 6 = 1 * 5 = 5 7 = 7 8 x 8 = 1 x 9 = 9 2 1 = 2 x 2 x 2 = 4 2 * 3 = 6 2 * 4 = 8 2 * 5 = 10 2 * 6 = 12 2 x 7 = 14 2 x 2 x 8 = 16 9 = 18 3 * 1 = 3 3 * 2 = 6 3 * 3 = 9 3 * 4 = 12 3 * 5 = 3 * 6 = 15 18 3 x 7 = 21 24 3 x 3 x 8 = 9 = 27 4 * 1 = 4 4 * 2 = 8 4 x3 = 12 16 4 x 4 x 4 = 5 = 20 24 4 x 4 x 6 = 7 = 28 32 4 x 4 x 8 = 9 5 x 1 = = 36 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= 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=81Copy the code

We found that the multiplication table had half as many rows, and that’s easy to do, because we don’t have more than one row in each column and only one column in the first row and only two columns in the second row and only three columns in the third row… There are only nine columns in the first row. The key is that the second argument in range is I +1

For I in range (1, 10) : for j in range (1, I + 1) : print (x = {} {} {} \ "t". The format (I, j, I * j), end = ' ') print ()Copy the code

The final result is:

1 x 1 = 1 2 x 2 x 2 = 1 = 2 3 4 * 1 = 3 3 * 2 = 6 3 x3 = 9 4 * 1 = 4 4 * 2 = 8 4 x3 = 12 4 * 4 = 16 5 * 1 = 5 5 * 2 = 10 5 * 3 = 15 5 * 4 = 20 5 * 5 = 25 6 x 1 = 6 x 2 = 12 x3 = 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=81Copy the code

GIF showing the multiplication table execution

The multiplication table. GIF

– EOF –

Well, that’s all for today’s sharing. If you’re interested in Python, join us.Python Learning communicationSkirt 】, receive free learning materials and source code.