"I don't really understand Python's "for" statement." "What should I use as a topic to practice?"
I often hear such concerns from beginners.
What I recommend is a program that prints out multiplication tables.
Multiplication tables are a subject that everyone knows, and this class helps you thoroughly learn the basic structure of a for statement.
In this article,Language that even elementary school students can understandin,How to use the for statement, how to make a multiplication table, and moreWe will explain in detail.
For those who are about to learn Python, we hope to help them grow step by step without giving up.Includes actual code, examples, and suggestions for further learningWe are doing it.
If you read to the end,You will fully understand the basics of for statements and gain confidence in writing your own apps!
- Why create a multiplication table with Python? [Python multiplication table]
- What is a for statement? [Python for statement basics]
- Learn how to output a multiplication table [How to create a multiplication table in Python]
- Tips for making output easier to read [Python multiplication table for easy viewing]
- Advanced learning using multiplication tables [Python multiplication table application]
- Recommended exercises to deepen your learning [Python for statement practice method]
- Summary: The first step to mastering the for statement [Python multiplication table learning effect]
Why create a multiplication table with Python? [Python multiplication table]
Multiplication tables are great for practicing "for" statements.
Conclusion: The multiplication table is a material that allows you to physically memorize the basic structure of a for statement.
The multiplication table lists the multiplication factors from 1x1 to 9x9.
To print it in Python,Using a double loop (for statement)It is necessary.
In other words, by outputting the multiplication table,
• Practice using a for statement twice.
• Learn how to use variables
• Small goals give you a sense of accomplishment
There are three advantages:
Multiplication tables = something that anyone can understandTherefore, it is very effective for people who "don't want to do anything difficult, but still want to learn."
It's easy to learn and apply
Conclusion: Multiplication tables are easy to customize and lead to next steps.
With just a little modification, the code for the multiplication table can be
• Arrange in a table
• Output only a specific stage
• Save to a file
• Add conditions (such as only even columns)
such asDevelopment into applied problemsYou can do this.
Not only are multiplication tables easy to learn, but another great advantage is that it's easy to see what you need to do next.
What is a for statement? [Python for statement basics]
Explaining the role and mechanism of the for statement
Conclusion: The for statement is used to "repeat the same action multiple times."
In Python, the word "for" is used to write "repeated processing (loops)".
For example, to print the numbers 1 to 5, you would write:
1 | for i in range ( 1 , 6 ): print (i) |
• i is a box for extracting numbers one by one
• range(1, 6) is the number from 1 to 5
• print(i) is the process that is executed every time.
In this way,The for statement is used when you want to repeat the same process multiple times.
Also introduces the difference between for and while statements
Conclusion: Use the for statement when the number of times is fixed.
Python also has another loop statement called while.
grammar | When to use |
for | If the number of times is fixed |
while | If you want to continue until a condition is met |
When you want to process numbers 1 to 9 in order, like multiplication tables, the for statement is better.Easy to read and safeis.
Learn how to output a multiplication table [How to create a multiplication table in Python]
Creating a table using a double for statement
Conclusion: Multiplication tables can be created by using nested for statements.
Multiplication tables have repetitions both horizontally and vertically, soCombining (nesting) two for statementsIt is necessary.
The structure is as follows:
1 | for i in range ( 1 , 10 ): for j in range ( 1 , 10 ): print (i * j, end = " " ) print () |
In this code,
• The outer i represents the rows 1 to 9.
• The inner j indicates the number to be multiplied.
• Print horizontally with print(…, end=” “)
• After printing(), insert a new line
Let's actually write some code
Bottom line: the code is short and easy to understand.
1 | for i in range ( 1 , 10 ): for j in range ( 1 , 10 ): print (f "{i*j:2}" , end = " " ) print () |
If you write it like this, the output will be:
1 2 3 4 | 1 2 3 4 5 6 7 8 9 2 4 6 8 10 12 14 16 18 3 6 9 12 15 18 21 24 27 ... |
With just a few lines of text, you can create a beautiful multiplication table.
Tips for making output easier to read [Python multiplication table for easy viewing]
How to format it into a table?
Conclusion: If you output the data with the same number of digits, it will look like a table.
The results of the multiplication tables range from 1 to 81, so there is a mixture of one-digit and two-digit results.
Therefore, if the columns are not aligned, the table will look untidy.
As follows:How to specify the width of a characteris convenient.
1 | print (f "{i*j:2}" , end = " " ) |
This ensures that2 charactersSince it is output with a width ofThe vertical columns are also aligned.
Explains how to adjust line breaks and spacing
Conclusion: Ease of viewing directly translates to ease of learning.
Here are some things you can do to make your output easier to read:
• Use end="" to align the lines
• Use print() to break each line
• Add a title line (headings 1 to 9)
• Add divisions by using horizontal lines or spaces
example:
1 | print ( " " , end = " ") for j in range(1, 10): print(f" {j: 2 } ", end=" ") print(" \n " + " - " * 28) for i in range(1, 10): print(f" {i: 2 } | ", end=" ") for j in range(1, 10): print(f" {i * j: 2 } ", end=" ") print () |
This way,A beautiful multiplication table that looks just like a textbookis completed.
Advanced learning using multiplication tables [Python multiplication table application]
Creating dynamic output based on input
Bottom line: using user input makes it more actionable.
For example, if you want to output only the 5th row, you can do this:
1 | dan = int ( input ( "What column do you want to display?:" )) for i in range ( 1 , 10 ): print (f "{dan} × {i} = {dan*i}" ) |
Basic structure of input → processing → outputYou can learn this here.
Trying file saving and conditional output
Conclusion: Saving and adding conditions are effective for deepening your learning.
Example 1: Save the multiplication table to a text file
1 | with open ( "kuku.txt" , "w" ) as f: for i in range ( 1 , 10 ): for j in range ( 1 , 10 ): f.write(f "{i*j:2} " ) f.write( "\n" ) |
Example 2: Output only the even-numbered rows
1 | for i in range ( 1 , 10 ): if i % 2 = = 0 : for j in range ( 1 , 10 ): print (f "{i*j:2}" , end = " " ) print () |
If applied in this way,It's also perfect for independent research using Python.
Recommended exercises to deepen your learning [Python for statement practice method]
What kind of exercises would you like to see made next after multiplication tables?
Conclusion: Let's gradually increase the size of a simple program using a for statement.
The following themes are recommended after multiplication tables.
• Star-triangle output
• Creating a countdown timer
• Conditional output, such as "output even numbers between 1 and 100"
• FizzBuzz problem (letter out multiples of 3 and 5)
👉Related articles:I made a "rock-paper-scissors game" using Python
Tips for continuing and ways to avoid giving up
Conclusion: It is important to gain experience of success and feel like "I did it!"
• Praise them when they work, even if it's just a short code.
• Achieve one small goal at a time
• Always save and review the code you write.
• If an error occurs, don't panic and reread each line.
If you have these habits,You will be able to continue naturally.
Summary: The first step to mastering the for statement [Python multiplication table learning effect]
• Multiplication tables areThe best material to learn the basic structure of for statementsis
• The code is shorter and less prone to errors.Perfect for beginnersis
• By tweaking the output or adding input,You will also gain practical skills.
The for statement is the foundation of all programming.
Let's take that first step with multiplication tables.
I hope this article will be helpful in your first attempt at learning Python.
There are more practice examples and explanations of other programs, so be sure to check out the related articles!
👉 I made a ToDo list app using Python [with file saving function]
👉 Creating a horse racing winning horse prediction model using Python and XGBoost