Python-characteristics of for loop, while loop, string

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(步长)

Insert picture description here
Example 1:
Question: Find the sum of all even numbers from 1 to 100
Insert picture description here
Example 2:
Topic: Factorial
Insert picture description here

2. Statement Controller

(1) break: jump out of the entire loop, will not recycle the following content
Insert picture description here
Insert picture description here
(2) continue: Exit this loop, the code behind continue will not be executed, but the loop will continue.
Insert picture description here
Insert picture description here
(3) exit: End the loop operation
Insert picture description here
Insert picture description here
(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
Insert picture description here
final result is a total of 24 three-digit numbers that satisfy the conditions.
Insert picture description here
Insert picture description here

<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
Insert picture description here
Insert picture description here
operation results are as follows:
Insert picture description here
<3> Question stem: Use a for loop to achieve the effect of a command line.
Insert picture description here
Insert picture description here
The results are as follows:
Insert picture description here

3.while loop

while loop syntax

while  条件:
    条件满足时,做的事情1
    条件满足时,做的事情2
     ...

Example 1:
Insert picture description here
Example 2:
Insert picture description here
while loop nesting
Example:

Output in the console:
Insert picture description here

Problem solving steps: The
Insert picture description here
results are as follows:
Insert picture description here

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:
Insert picture description here
<2> Question stem: The above for loop exercise question 2 user login using a while loop to achieve the
Insert picture description here
Insert picture description here
operation results:
Insert picture description here
<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:
Insert picture description here
Insert picture description here
running results:
Insert picture description here

4. String

Definition of string
Insert picture description here
Insert picture description here
operation result:
Insert picture description here
Characteristic of string

<1> Index

一个字符 S = 'hello'
则:S[0]=h、S[1]=e、S[2]=l、S[3]=l、S[4]=o

Insert picture description here
Operation result:
Insert picture description here
<2>Cut
Insert picture description here
The results are as follows:
Insert picture description here
<3>Repeat
Insert picture description here
<4> Link
Insert picture description here
<5> Member operator
Insert picture description here
<6> String removal function
Insert picture description here
Insert picture description here

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.

Insert picture description here
Operation result:
Insert picture description here
<2> Questions:

Determine what type is in the string

(1) Judgment number
Insert picture description here
(2) Judgment title
Insert picture description here
(3) Judgment of upper and lower case and letters
Insert picture description here
Operation result:
Insert picture description here
(4) Conversion of upper and lower case letters
Insert picture description here
(5) Judgment of what starts and ends with
Insert picture description here
Insert picture description here
<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
Insert picture description here
Insert picture description here
results are as follows:
Insert picture description here

Published 41 original articles · praised 0 · visits 1694

Guess you like

Origin blog.csdn.net/qq_44749796/article/details/105619261