q ### 1.for loop statement
for loop syntax
for 变量 in range():
循环需要执行的代码
range(stop): 0 ~ stop-1
range(start,stop): start ~ stop-1
range(start,stop,step): start ~ stop-1 step(步长)
Example 1:
Question: Find the sum of all even numbers from 1 to 100
Example 2:
Topic: Factorial
2. Statement Controller
(1) break: jump out of the entire loop, will not recycle the following content
(2) continue: Exit this loop, the code behind continue will not be executed, but the loop will continue.
(3) exit: End the loop operation
(4) Practice questions:
<1> Questions:
There are four numbers of 1, 2, 3, and 4, how many three-digit numbers that are different from each other and have no repeated numbers can be generated by these four numbers (cannot contain 122, 133)
Solution: The
final result is a total of 24 three-digit numbers that satisfy the conditions.
<2> Questions:
User login program requirements:
1. Enter the user name and password;
2. Determine whether the user name and password are correct? (Name = 'root'. Passwd = 'westos')
3. In order to prevent brute force cracking, there are only three opportunities to log in. If there are more than three opportunities, an error message will be reported;
Solution: The
operation results are as follows:
<3> Question stem: Use a for loop to achieve the effect of a command line.
The results are as follows:
3.while loop
while loop syntax
while 条件:
条件满足时,做的事情1
条件满足时,做的事情2
...
Example 1:
Example 2:
while loop nesting
Example:
Output in the console:
Problem solving steps: The
results are as follows:
Test questions for while loop
<1> Title:
Calculation: all even numbers between 0-100 and
the counting method in python.
There are two common counting methods, which can be divided into:
natural counting method (starting from 1)-more in line with human custom
program counting method (from 0 Start)-Almost all programming languages choose to start counting from 0.
Therefore, when you write a program, you should try to develop a habit: unless the special requirements of the demand, otherwise the loop count starts from 0.
Loop calculation: In program development, you often encounter the need to use loop to repeat calculations (using the power of the CPU to complete the corresponding complex calculations).
In this situation:
1. Define a variable above the while for storage The final calculation result.
2. Inside the loop body, each loop uses the latest calculation results to update the previously defined variables.
Solve the problem:
<2> Question stem: The above for loop exercise question 2 user login using a while loop to achieve the
operation results:
<3> Question stem:
Number guessing game: 1. The system randomly generates a number from 1 to 100;
2. The user has a total of 5 chances to
guess the number ; 3. If the number guessed by the user is greater than the number given by the system, print "too big";
4. If the number guessed by the user is less than the number given by the system, print "too small";
5. If the number guessed by the user is equal to the number given by the system, print "Congratulations" and exit the loop;
Problem-solving steps:
running results:
4. String
Definition of string
operation result:
Characteristic of string
<1> Index
一个字符 S = 'hello'
则:S[0]=h、S[1]=e、S[2]=l、S[3]=l、S[4]=o
Operation result:
<2>Cut
The results are as follows:
<3>Repeat
<4> Link
<5> Member operator
<6> String removal function
Exercises for strings:
<1> Questions:
Determine whether a number is a palindrome. If yes, output true !!!, not false !!!. The number of palindromes is the same as forward reading and reverse reading. If 121 is the number of palindromes, -121 is not the number of palindromes.
Operation result:
<2> Questions:
Determine what type is in the string
(1) Judgment number
(2) Judgment title
(3) Judgment of upper and lower case and letters
Operation result:
(4) Conversion of upper and lower case letters
(5) Judgment of what starts and ends with
<3>
Whether the variable name is legal:
1. The variable name can be composed of letters, numbers or underscores
2. The variable name can only start with letters or underscores
Let s = 'hello @'
1. Determine whether the first element of the variable name is a letter or underscore s [0]
2. If the first element meets the conditions, judge other elements s [1:] except the first element
Problem solving steps: The
results are as follows: