In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "the use of if judgment sentence in python". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "the use of if judgment sentence in python".
Judge (if) statement goal
Application scenarios in development
If statement experience
If statement advance
Comprehensive application
01. Application scenarios in development
Judgment is almost ubiquitous in life, and we make all kinds of choices every day, if so? In that case?.
Judgment in the program
If pays salary today: repay credit card money first if has surplus: can happy again, O (∩ _ ∩) O ~ else: Oh, no. Waiting for 30 days else: the definition of looking forward to payroll judgment
Only if the conditions are met can you do something.
If the conditions are not met, do something else, or do nothing.
It is because of the judgment that the program world is rich and colorful and full of changes!
Judgment sentence is also called "branch statement". It is because of judgment that the program has many branches.
02. If statement experience 2.1 basic syntax of if judgment statement
In Python, the if statement is used to make judgments in the following format:
The condition that if wants to judge: what to do when the condition is established.
Note: the code is indented to a tab key, or 4 spaces-spaces are recommended
In Python development, don't mix Tab and spaces!
We can think of the whole if statement as a complete block of code
The application scenario of elif is to judge multiple conditions at the same time, and all the conditions are equal.
In development, if is used for conditional judgment. If you want to add conditional judgment to the execution statement where the condition is established, you can use the nesting of if.
The nested application scenario of if is to add additional judgment on the premise that the previous conditions are met.
The nested syntax format of if is no different from the previous one except for indentation.
The syntax format is as follows:
If condition 1: condition 1 satisfies the executed code. Condition 2 based on condition 1 of if: the code executed when condition 2 is satisfied. # processing else that condition 2 does not meet: code executed when condition 2 is not satisfied # processing else that condition 1 is not satisfied: code executed when condition 1 is not satisfied. Nesting exercise of if-- Railway Station Security check
Demand
Define the Boolean variable has_ticket to indicate whether there is a ticket or not
Defines the integer variable knife_length to represent the length of the knife, in centimeters
First check whether there are tickets, and if so, security checks are allowed.
During the security check, you need to check the length of the knife to determine whether it is more than 20 centimeters.
If it is more than 20 cm, the length of the knife is indicated, and boarding is not allowed.
If it is not more than 20cm, the security check will pass.
If there is no ticket, you are not allowed to enter the door.
# define Boolean variable has_ticket to indicate whether there is a ticket has_ticket = True# define an integer variable knife_length to denote the length of the knife, unit: cm knife_length = 2cm first check whether there is a ticket, if so, allow the security check if has_ticket: print ("if you have a ticket, you can start the security check.") # when you check the length of the knife Judge whether it is more than 20cm # if it exceeds 20cm, prompt the length of the knife, if knife_length > = 20: print ("do not allow to carry a knife with a length of% d cm"% knife_length) # if it is not more than 20cm, the security check will pass else: print ("pass the security check, have a nice trip …") If you don't have a ticket, you are not allowed to enter else: print ("Brother, you have to buy a ticket first") 05. Comprehensive application-- rock scissors paper
target
Strengthen the logical operation of multiple conditions
Experience the use of import import module ("tool kit")
Demand
Enter the punch from the console-stone (1) / scissors (2) / paper (3)
The computer punches randomly-- first assume that the computer can only produce stones to complete the overall code function.
Compare victory and defeat
Serial number rule 1 rock over scissors 2 scissors over paper 3 paper over stone 5.1 basic code implementation
Let's assume that the computer can only produce stones to complete the whole code function.
# enter the punch from the console-- rock (1) / scissors (2) / paper (3) player = int (input ("Please punch rock (1) / scissors (2) / paper (3):") # computer randomly punches-assuming that the computer always gives rock computer = "win or lose" # if the content of conditional judgment is too long You can add a pair of curly braces to the outermost condition # and between each condition, using enter, PyCharm can automatically increase 8 spaces if ((player = = 1 and computer = = 2) or (player = = 2 and computer = = 3) or (player = = 3 and computer = = 1)): print ("Oh yeah!! The computer sucks! ") Elif player = = computer: print ("see eye to eye, one more!") Else: print ("No, I'll fight you till dawn!") 5.2 processing of random numbers
In Python, to use random numbers, you first need to import the module of random numbers-- "Toolkit".
Import random
After importing the module, you can type one directly after the module name. Then press Tab and you will be prompted for all the functions contained in the module
Random.randint (a, b), which returns an integer between [a, b], including an and b
For example:
Random number generated by random.randint (12,20) # n: 12
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.