If/else statementsConditional statements actually in the previous article we have used several times, here we need to re-grand to introduce it, conditional statements are judged by one or more statements of the return result (true or false) to choose to perform the next command, the more commonly used is the IF/ELSE statement, It can be expressed in code like this (pay attention to the direct indentation relationship of the statement OH):
:print ' The number is positive 'Elif num print ' The number is negative 'Elseprint ' The number is zero '_______________________________ The = = Budget character Determines whether 2 objects are equal, and is to determine whether the two are equivalent (the same object)An Assert statement is available if you need to make sure that a condition in the program must be true for the program to function4. CirculationWhile loopWhile xPrint XX=x+1For loopwords=[' This ', ' was ', ' an ', ' ex ', ' Par
') - Account_login () - Else: + Print('incorrect password, please re-enter') - Account_login () + A Account_login () at - Print(password_list) - #You can view the latest list contentAfter running the program will be prompted to enter a password, if you enter "# #", this will enter the password reset link, will prompt "Please enter a new password", enter a new password, the end of the password_list will be inserted in the new password, the password entered correctly, will show "logi
If Else form [Email protected] ~]$ VI testoperator.py#!/bin/python#operator testingnumber1=111number2=222if (NUMBER1>NUMBER2): Print ("num1>num2") else:print ("Num2>num1") str1= "AAA" str2= "BBB" if (STR1==STR2): Print ("STR1=STR2") Else: Print ("Str1!=str2") a=3b=2if (a>b): Print ("A>b") elif (a==b): Print ("A=b") else:print ("A The judgment condition of the IF statement can be expressed by > (greater than), If judgment condition: Execute statement
1. Conditional judgmentage = 3if age >= 18: print(‘adult‘)elif age >= 6: print(‘teenager‘)else: print(‘kid‘)Elif is an else if abbreviation and can have more than one elif.2. CycleThere are two types of Python loops,One is the for...in loop, which sequentially iterates through each element of the list or tuple.names = ["geg","gege","geege"];for name in names: print(name)If you want to calculate
range (1): = sqrt ( N)if root = = Int (root): Print N Break Else : Print " didn ' t dind it! "Results>>>Didn ' t dind it!
List derivation--lightweight loops
A list derivation is a way to create a new list with other lists, such as>>> [(x, Y) for x in range (3) for Y in range (3)][(0, 0), (0, 1), (0, 2), (1, 0), (1, 1), (1, 2), (2, 0), (2, 1)]>>> girls = [' Alice ', ' Bernice ', ' Clarice ']>>> boys = [' Chris ', ' Arnold ', ' Bob ']>>> [B + ' + ' +g for
(' Little Violet ') running results: [[1, 2, 3, 4, 5, 6], [' Name ', ' age ', ' sex ', ' haha ', [' xiaoming ', ' Little black ', ' Little white ', ' Little Violet '], 890]my[1][2]= ' sex ' running results: [[1, 2, 3, 4, 5, 6], [' Name ', ' age ', ' gender ', ' haha ', [' xiaoming ', ' Little black ', ' Little White '], 890]Iii. Conditions of JudgmentPython conditional judgments are basically if-elif-else, examples:If score>=90: Print (' You are ex
# Conditional JudgmentThe role of Elif:else ifNote: "Colon"BMI =w/(h*h)If bmiPrint (' lighter ')Elif bmiPrint (' normal ')else BIM:Print (' obese ')# loopsFor x in XS a bit like the C # foreachRange () generates an integer sequence, range (101) generates an integer sequence of 0~100While loopFind differentOnen = one while n>10: n-1 print(n)Twon = ten while n >: = n-1 print('1', N)The difference between one and two,,,#dict和setDict Dictionar
Conditional statement: If ... else elifLoop statement: while forExit: Break continue return exit passApplication Scenario: Level Three menu Enter City serial number enter the menu input b return input Q exit#!/usr/bin/env Python3ImportOSImportSYS" "make a level three menu Shandong Rizhao East Haven Lanshan Wulian" "defShowcd (dir):" ":p Aram dir: The directory to go: return: Returns the list under the directory to go" " if(Os.path.exists (dir)): Di
Conditional judgmentPython's indentation rules, if the IF statement is true, the code is executed, you can also add an else statement, meaning, if the judgment is false, do not execute if the content, to the else executedNum=num >=: (num) (): (num) ()Of course, the above judgment is rough, can be used elif to make more detailed judgmentsNum=num >=: (num) () num >=: (): ()Elif is the else if abbreviation, can have more than one elif, so the IF statemen
Condition to judge a computer can do a lot of automated tasks, because it can make their own conditions to judge. For example, enter a user's age, print different content according to age, in a Python program, with an IF statement: age = 20if ages >= 18: print (' Your is ', Age) print (' adult ') according to the Python indentation rules, if the IF statement evaluates to True, the indented two lines of th
= + (age-2)print ("Human years:", Human)###input (' press return> ')Save the above script in the dog.py file and execute the script:python dog.pyAge of the dog:1About human yearsThe following are the operations operators commonly used in the IF:operator Description to> Greater than>= greater than or equal to= = equals, compares objects for equality! = does not equal# The program demonstrates the = = operator# Use digital print (5 = = 6)# using Variablesx = 5y = 8print (x = = y) The result of
1. Condition Judgment:There is only oneIf***Elif: #多个条件加elif, want to add a few plus a few****Else****Python is indented to represent the statement block, see the colon, the downside is to indent2. CirculationTwo types, for and whileFor I in Rangle (): #想循环几次, write a few****While * * *: #while循环必须有计数器****3. String formattingUsername = input (' Please enter your name: ')123Example 1. username+ ', welcome, ' + ' time is: ' +timeExample 2. ' %s Hello '%
Conditional statements in Python, the IF statement is used for conditional judgment, in the following format:If you want to judge the condition:things to do when the conditions are set...It is important to indent in Python, to indent 4 spaces in the run content of the IF condition statement Walkthrough-Judging age Requ
Basic data types such as strings, numbers, etc.1 #!/usr/bin/env Python 32 #-*-coding:utf-8-*-3 4 #1, string (the contents of the preceding quotation mark are strings)5 #a basic form of a string6 " "7 name = ' abc '8 name = "ABC"9 name =" "Abc" "Ten " " One #addition of String B A " " - n1 = ' abc ' - b2 = ' de ' the B3 = ' FG ' - N4 = n1 + n2 +n3 - then N4 = ' ABCDEFG ' - " " + #multiplication of C-strings - " "
This article is original loneliness, this article belongs to the Spring and Autumn Original award program, without permission to reprintThis article source:I spring and Autumn college I spring and Autumn (ichunqiu.com) network security platform can provide professional network security technology, network infiltration technology, network security attack and defense technology, but also to provide network penetration training, network security engineer Training, is an online network security edu
elif statements(equivalent to the else if in C language)When we need to have more judgment, we tend to use a different syntactic expression, namely, using elif:Python code
if judgment condition 1:
Execute Statement 1
elif judgment Condition 2:
Execute Statement 2
elif Judgment Condition 3:
Execute Statement 3
......
Else:
EXECUTE statement N
Now we have a task to complete, assuming Now there's a score, You have his rank as follows: 90 points and above belong to A; 80 89 po
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.