Conditions and loops of the Python Foundation

Source: Internet
Author: User

I. If statement 1.1 functions

Computers are also called computers, meaning that computers can react differently to changes in ambient conditions (i.e., expession), like the human brain, which is code execution.

The IF statement is to control the computer to achieve this function

1.2 Syntax 1.2.1: Single-branch, single-weight condition judgment
1 if expression: 2 3     expr_true_suite45 Note: Expession is a true code execution Expr_true_suite
1.2.2: Single branch, multiple criteria judgment
1 if  not  or over_time >=:23     print('warning:service Is dead')45     warn_tag+=1
1.2.3:if+else
1 if expression: 2 3     expr_true_suite   45Else:67     Expr_false_suite
1.2.4: Multi-branch If+elif+else
1 ifExpession1:2 3 Expr1_true_suite4 5 elifexpression2:6 7 Expr2_true_suite8 9 elifExpession3:Ten  One Expr3_true_suite A  - Else: -  theNone_of_the_above_suite
Summary of 1.2.5:IF statements
    1. If the expression returns a value of True then it executes its child code block, then the IF statement to this end, otherwise into the next branch judgment, until one of the branches is satisfied, after executing the IF
    2. Expression can introduce an operator: Not,and,or,is,is not
    3. Multiple expression for enhanced readability most useful parentheses contain
    4. A consistent representation of if and else indentation level is a pair
    5. Elif and else are optional
    6. An if judge has at most one else but can have multiple elif
    7. else represents the end of if judgment
    8. Expession can be the form of an expression that returns a Boolean value (example x>1,x is not None) or a single Standard object (example X=1;if x:print (' OK '))
    9. All standard objects are available for Boolean testing and can be compared between objects of the same type. Each object inherently has a Boolean True or False value. A null object, any number with a value of zero, or a Boolean value of None for the null object is False.

The Boolean value of the following object is False

1.3 Cases
1 #!/usr/bin/env python2 #_*_coding:utf-8_*_3 4 " "5 prompt to enter user name and password6 7 Verify user name and password8 if error, the output user name or password is incorrect9 If successful, the output is welcome, xxx!Ten " " One  A ImportGetpass -  -Name=input ('User name:') thePasswd=getpass.getpass ('Password:') -  - ifName = ='Alex'  andpasswd = ='123': -     Print('Local tyrants inside please') + Else: -     Print('Earth Turtle, please go away .') +  AUser Login Verification
User Login Case
1 #!/usr/bin/env python2 #_*_coding:utf-8_*_3 4 " "5 print their permissions based on what the user has entered6 7 Alex--Super Admin8 Eric--General Administrator9 Tony,rain--- business executivesTen other--Ordinary users One " " AName = input ('Please enter user name:') -  -  the ifName = ="Alex": -     Print("Super Admin") - elifName = ="Eric": -     Print("General Administrator") + elifName = ="Tony" orName = ="Rain": -     Print("Business Supervisor") + Else: A     Print("Normal User") at  -Output permissions based on user input memory
output permissions based on user input memory1.4 Three-dimensional expression

Grammar:

Expr_true_suite if expession else expr_false_suite

Case one:

1 >>> active=12print('service is active'if Else Print ('service is inactive') 3  is active

Case TWO:

1 >>> x=12 >>> y=23ifelse  y4 > >> smaller5 1
Two. While Statement 2.1 features

The essence of the while loop is to allow the computer to repeat the same thing (that is, the while loop is a conditional loop, which includes: 1. Condition count cycle, 2 conditional infinite loop)

This condition means: conditional expression

The same thing means: The block of code that the while loop body contains

Repeat things such as: from 1 to 10000, ask for all the odd numbers within 1-10000, the service waits for a connection

2.2 Syntax 2.2.1: Basic syntax
1  while expression: 2 3     suite_to_repeat45 Note: Repeat suite_to_repeat until expression is no longer true
2.2.2: Counting Cycle
1 count=02 while (Count < 9):3     Print (' Theloop is%s' %count)4     
1 tag=True2 count=03 while tag:4      If count = = 9:5         tag=False6     print(' The loop is%s' %count)7     count+=1
2.2.4:while and Break,continue,else
 1  count=0  2  while  (Count < 9 3  count+=14  if  count = = 3:  5  print  ( "  jump out of this layer loop , which completely ends this one/layer while loop   "  6  break  7  print  ( " the loop is %s   "%count) 
Break jumps out of this layer loop
1Count=02  while(Count < 9):3Count+=14     ifCount = = 3:5         Print('jump out of this loop, that is, after the loop continue code is no longer executed, into the next loop')6         Continue7     Print('The loop is%s'%count)8 9Continue jump out of this cycle
continue jump out of this cycle

Conditions and loops of the Python Foundation

Related Article

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.