Getting started with Python(13)

Getting started with Python (13/18)
Section 13****Application Cases (1)

Hello everyone! After the intense study in the previous stage, we finally ushered in the moment to witness the miracle.

Today we are going to experience the surprise that python brings to us through the design and programming of 6 application cases, and to consolidate our first phase of learning results.

6 In this case, there are 3 mathematical problems and 3 program algorithm problems, which can help us better understand python programming ideas and methods. In this lesson, we first introduce the first three cases.

**Case 1. Compile the formula table of nine-nine multiplication **

demand:

1、 At least you have to know what a multiplication formula table is.

2、 Please list all the calculations and results.

3、 According to the multiplier 1~9, the display is arranged in 9 rows.

analysis:

1、 Define two variables as the two multipliers for multiplication calculation: i, the value is 1-9; j, the value is 1-9.

2、 Two traversals: traverse the 1-9 numbers in i (this is the first-level loop), and each number is multiplied by the 1-9 numbers in j. This is the second traversal, and it is also the second-level loop inside the first-level loop. Therefore, they have a nested loop operation process.

3、 List the formulas and arrange them in 9 rows. It's just a matter of display format. The solution is simple: just need to traverse i, and arrange the calculation formula and result of multiplying it with all the numbers of j in the same row.

PS: How to display multiple calculation formulas and calculation results in the same row? The method is simple: every calculation and calculation result can be presented with a string expression, right? Then concatenate (add) all the strings of one iteration to get a long string? Then show it, ok!

Nine-nine multiplication formula table source code

**Code analysis: **

1、 The loop statement in the first line defines an i value from 1-9. I don’t understand the concept of for and range.

2、 Define the y variable, which is used to display the string formed by multiplying a value of i by 1-9 in j and the result.

3、 In the third line, the second loop statement defines the j value from 1-9.

4、 In the fourth line, use the formatting (placeholder) method to define an independent expression and the string x of the calculation result.

5、 Use the "addition" of character strings to concatenate x into y.

6、 Display the y string, pay attention to its code indentation, located in the for loop of i, so it will be displayed in 9 lines.

Case 2. Looking for a qualified integer

demand:

Set the integer i, i+100 is a perfect square number, plus 168 is a perfect square number, please find the value of i.

analysis:

1、 A perfect square means that the square can be rooted and the result is still an integer.

2、 Requirements: i+100 is a perfect square number, and i+100+168 is also a perfect square number.

Looking for a solution:

1、 Since i+100 is a perfect square number, the result of its square root is assumed to be an integer x. Conversely, the square of x should also be equal to i+100; in the same way, if the result of square root of i+100+168 is an integer y , Then the square of y is also equal to i+100+168.

2、 Intuitively, we cannot guess the value of i that meets the above conditions. However, the advantage of programming is that you can try and error in a larger data range to find a qualified i value.

3、 You can consider trial and error within 100,000, first square i+100, take the integer, and then assign it to x, and then round i+268 to the square and assign it to y. Then, try to determine whether the squares of x and y are equal to i+100 and i+268. If so, the value of i is a positive solution.

Find the source code of a qualified integer

**Code analysis: **

1、 The first line imports the math module math through import, which is used for mathematical calculations (square root).

2、 Define the i variable and take the value from 0-100,000.

3、 Squares on lines 4 and 5 are rounded and assigned to x and y.

4、 Line 6, conditional judgment.

5、 If the conditions are met, the loop is terminated (trial and error), and the current i value is output (displayed).

Case 3. Realize permutation and combination without repeated numbers

**Requirements: **Please list all the possible combinations of 4 numbers (such as 1-4) that are different from each other and have no repeated numbers. (Learning from network cases)

analysis:

1、 First, we must enumerate 4 numbers, for example: range(1,5).

2、 Calculate their permutations and combinations and assign them to a displayable variable.

3、 Remove duplicate numbers.

Achieve permutation and combination source code without repeated numbers

**Code analysis: **

1、 First, create an empty set called myset. (Collection can help us to remove the weight)

2、 Create 4 for loops, define 4 variables i, j, k, m, they can take values from 1 to 4, which represent one of the 4 digits.

3、 Please note that for the indentation of the code, the value of i will correspond to all the values of j, the value of j will correspond to all the values of k, and the value of k will correspond to all the values of m.

4、 After all the values in each round are obtained, they are added to myset to remove duplicates.

5、 Finally, all iterations are completed, and the final myset obtained is the result we want. Reading and displaying a set of data also applies to the for loop iteration method.

summary

In this section, we shared three Python application cases. It seems that they are relatively focused on mathematical applications, but in fact, it is beneficial to deepen the understanding of the basic syntax and data structure of Python. There are three more applications left to share in the next lesson.

In the following three applications, we will focus on exploring and experiencing the practice of data structures and algorithms, which will be a more important practice for programmers.

Recommended Posts

Getting started with Python(18)
Getting started with Python(9)
Getting started with Python(8)
Getting started with Python (2)
Getting started with python-1
Getting started with Python(14)
Getting started with Python(7)
Getting started with Python(17)
Getting started with Python(15)
Getting started with Python(10)
Getting started with Python(11)
Getting started with Python(6)
Getting started with Python(3)
Getting started with Python(12)
Getting started with Python(5)
Getting started with Python (18+)
Getting started with Python(13)
Getting started with Python(16)
Getting started with Ubuntu
Getting Started with Python-4: Classes and Objects
Getting started with python-2: functions and dictionaries
04. Conditional Statements for Getting Started with Python
Getting started python learning steps
How to get started quickly with Python
python requests.get with header
Play WeChat with Python
Web Scraping with Python
Implementing student management system with python
Centos6.7 comes with python upgrade to
Played with stocks and learned Python
Gray-level co-occurrence matrix (with python code)
Speed up Python code with Cython
How to make a globe with Python
Automatically generate data analysis report with Python
Create dynamic color QR code with Python
Python | Quickly test your Python code with Hypothesis
How to process excel table with python