In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/02 Report--
Blog structure
If statement of Python
For cycle of Python
Understand Python installation and operation
I. Python condition judgment
Conditional statement refers to making the program flow to different code blocks according to the different calculation results of conditional expressions. Python statements are if statements and if else statements.
If conditional statement
The if statement is used to determine whether a condition is true. If so, execute the program in the statement, otherwise skip the following content of the if statement. The syntax format is as follows:
If (condition):
Sentence block
The examples are as follows:
Click New script, and then run
Money=100\\ defines a variable if (money > 90):\\ output if greater than 90: ending print "aaa" print "bbb"\\ automatically indents 4 spaces\\ output result aaabbb > >
Case study:
Print 'Please enter students' test scores' score = input () print score > = 60if score > = 60:\\ the end of the colon, omitting parentheses, if greater than 60, output qualified print 'pass'\\ output results as follows: enter students' test scores 80True > conditional expression and relation operator
The syntax is as follows:
Operand relation operator Operand
Operation result > print 10==9False > print 10 conditional statement 9True > print 10 > 9True > print 10 > > print 10 > > if--else conditional statement
If sentence can also add else keyword, according to the condition judgment, decide to execute different statement blocks, when the condition is True, execute statement block 1, when the condition is False, execute statement block 2. The syntax is as follows:
If (condition):
Statement block 1
Else:
Statement Block 2
Case study:
Print 'Please enter students' exam scores' score=input () if score > = 60:\\ if it is greater than or equal to 60, it will output qualified print 'qualified' else: print 'come on'\ except for 60 or more All are refueling\\ output results are as follows > > Please enter students' examination scores 80\\ execute the first pass input 80 pass enter students' examination scores 50\\ execute the second input 50 refueling > multiple if statements
Use if-else to determine the condition so that the program can have two execution paths, but sometimes more paths are needed for processing, so you can add elif keyword processing. The syntax is as follows:
If (condition 1): statement block 1elif (condition 2): statement block 2elif (condition 3): statement block 3else: statement block when condition 1 is true, the statement block corresponding to condition 1 is executed, when condition 2.3 is true, the corresponding statement blocks are executed respectively, when the previous conditions are unsuccessful Execute the statement block corresponding to else.
Case study:
Print 'Please enter student grades' score=input () if score > = 90: print 'excellent' elif score > = 70: print 'good' elif score > = 60: print 'qualified' else: print 'effort'
\ the output is as follows
Please enter the student examination score 80 pass > > Please enter the student examination score 50 come on > > Please enter the student score 100 excellent > > Please enter the student score 80 good > > Please enter the student score 50 efforts > > Please enter the student score 60 pass > Common questions of the if sentence
(1) logical errors are easy to occur when using if-elif-else statements, because the condition is judged from top to bottom, and if the condition is true, the following condition judgment will no longer be executed.
(2) it is easy to make mistakes that the statement blocks are not indented in if statements.
(3) for programmers who use other languages to convert to Python, because they are used to forgetting the colons after expressions, they need to practice familiar with the syntax.
II. Python loops
When writing a program, there is often code that needs to be run repeatedly, and Python provides while and for for loop operations.
While cycle
The while loop can decide whether or not to loop the statement block according to the condition. The syntax is as follows: while loop condition: while loop condition:
The examples are as follows:
Initialize cycle Calculator isum=0\\ initialize the total score variable sumwhile I > >
Nesting use j=1prompt = 'Please enter the student name:' while j > for loop
For loops are another way to control loops. While executes loops using conditional judgment, while for iterates through elements.
Several ways of 1.for cycle
The grammatical structure is as follows:
For variable in collection: statement block
(1) the for loop can traverse the string and get each character of the string one by one
For letter in 'python':\\ output result according to the letter of the word print' current letter:%s'% letter\\ output result is as follows: current letter:pcurrent letter:ycurrent letter:tcurrent letter:hcurrent letter:ocurrent letter:n >
(2) the for loop can traverse lists and tuples
Fruits = ["watermelon", "apple", "grape"] for fruit in fruits: print fruit\\ output results are as follows: watermelon, apple grape >
(3) when you need to loop the same content, you can use for loop and range () function together. Let's first look at the function of the range () function.
The output result of print range (0JEI 5) print range (0BI 5JI 2)\\ is as follows: [0,1,2,3,4] [0,2,4] > >
Range (0. 5) outputs a list that starts with the first parameter 0, adds 1 by default, and ends when it is greater than or equal to the second parameter, so the second parameter value is not included in the list. Range (0.5) added a third parameter 2. The effect is to add 2 at a time. The final list value is [0quoi 2pr 4]. So the purpose of the range () function is to create a list of numbers that range from the beginning to the end of the number. The for loop can traverse the list. So you can traverse the results of the range () function.
For i in range (0Pol 5): print 'Welcome to Beijing'\\ output results are as follows: welcome to Beijing >
Range (0,5) is a list of zero four, the loop is executed five times, the output statement is executed five times, and the value of the variable I is the element value of each traversal list.
Example of 2.for Loop
Subjects = ('python','mysql','linux') sum=0for i in subjects: print' Please enter% s exam score:'% I score=input () sum+=scoreavg = sum / len (subjects) print 'Zhang San's average score is% d'% avg\ the output result is as follows: enter python score: 44 Please enter mysql score: 55 Please enter linux score: 66 sheets three average score is 55 > >
3. Logical operator
There is a logical expression in any language, which is an expression connected by logical operators and variables, as shown in the figure:
The method of use is as follows:
> print (not True) False > print (True and False) False > print (True or False) True >
4.for loop nesting
Like while loops, for loops can also be nested
The examples are as follows:
Students = ['Zhang San','Li Si'] subjects = ('python','mysql','linux') for student in students: sum = 0 for subject in subjects: print' Please enter% s test scores:'% (student,subject) score = input () sum + = score avg = sum / len (subjects) print's average score is% d'% (student) Avg)\\ output results are as follows: enter Zhang San's python test score: 20 Please enter Zhang San's mysql test score: 20 please enter Zhang San's linux test score: 20 Zhang San's average score is 20, please enter Li Si's python test score: 30 Please enter Li Si's mysql test score: 30 Please enter Li Si's linux test score: 30 Li Si's average score is 30 > cycle control
When using while and for to do loop operations, it may be necessary to change the normal execution order of the loop, so it needs to be implemented by loop control statements, such as break and continue.
1.break
You can jump out of the loop by using the break statement in the statement block of the loop. The following changes are made to the code for outputting the GPA. When the grade is invalid, break is used to exit the loop. The sample code is as follows:
Students = ['Zhang San', 'Xiao Wu'] subjects = ('python','mysql','linux') for student in students:\\ layer 1 cycle sum=0 for subject in subjects:\\ layer 2 cycle print' Please enter% s test score:'% (student) Subject) score = input () if (score100): the score entered by print 'needs to be greater than 0 or less than 100 Loop out of 'break Sum + = score avg = sum / len (subjects) print's average score is% d'% (student,avg)\\ the output results are as follows: > > Please enter Zhang San's python test score: 150 input scores need to be greater than 0 or less than 100, cycle out
2.continue
The role of continue is different from that of break. Instead of ending the entire loop, it skips the remaining statements in the body of the current loop, retests the loop state, and prepares to move on to the next loop. The sample code is as follows:
Students = ['Zhang San', 'Xiao Wu'] subjects = ('python','mysql','linux') for student in students: sum = 0 I = 0 while (I < len (subjects)): print' Please enter% s test score:'% (student) Subjects [I]) score = input () if (score100): the score entered by print 'needs to be greater than 0 or less than 100 Re-enter 'continue Sum + = score I = len 1 avg = sum / len (subjects) print's average score is% d'% (student,avg)\\ the output results are as follows: enter Zhang San's python test score: 20 Please enter Zhang San's mysql test score: 400The input score needs to be greater than 0 or less than 100. Re-enter\\ you can see that you must re-enter if it is greater than 100. please enter Zhang San's mysql score: 60
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.