"Python programming from the beginning to the practice" _ Chapter Fifth _IF statements

Source: Internet
Author: User

Condition test

The core of each if statement is an expression with a value of ture or false, which is known as a condition test. Python determines whether to execute the code in the If statement based on the value of the condition test as ture or false. If the value of the condition test is Ture,python, the code immediately following the IF statement is executed, and if it is False,python, the code is ignored.

string comparison

string checking is case-sensitiveCheck for equality
" BMW " " BMW " True " Audi " " BMW " False

Check for any unequal

" BMW " " BMW " False " Audi " True

Compare numbers

==

is equal
!= is not equal
> Greater than
< Less than
>= Greater than or equal
<= Less than or equal

>>> age = 18>>> Age = = +true>>> Age! *true>>> age <Tru E>>> Age >false>>> age <= +True>>> age >=False 

Check multiple conditions

True if the and multiple bars are set, otherwise false

 and  and age >True and and age <False

True if more than one condition is set, false if all are not true

or or age >Trueoror age >False

in to determine whether a particular value is in the included list

>>> name = ['Frank','Alex','Bob ' ]'Frank' in nametrue' May ' inch Namefalse

&NBSP; < Span style= "COLOR: #ff0000" >not in  to determine that a specific value is not in the included list

>>> name = ['Frank','Alex','Bob ' ]'Frank' not in namefalse'  May '  not inch nametrue

Boolean expression

The result is either True or FALSE, true or false.

If-n a ELIF-ELSE structure

This is the longest case if the structure is the shortest case, just use if on the line
# election age =if age >=:    print("enough to vote! " )# Run results enough to vote!

Two choice cases, If-else

# election age =if age >=:    print("your is oldenough to vote! " )else:    print("sorry,you is too young to vote! " )# running results sorry,you is too young to vote!

In many cases if-elif-else,elif can have multiple

 #   amusement park Selling Tickets  age = 12 if  Age < 4:  print  ( " you admission cost is $.   " )  elif  Age < 18 :  print  ( " your admission cost is $.   " )  else  :  print  ( " your admission cost is $.   ") 
Python does not require the IF-ELIF structure to have an else code block, else is an all-encompassing statement, as long as the non-satisfied will execute else under the statement, may cause invalid or even malicious data, if you know the final test conditions, you should consider the elif instead of else! No matter if-n a elif-else, as long as one satisfies, it will only execute the condition that evaluates to true and will not perform the other.

Python returns True when the list contains at least one element, and returns false! when the list is empty

Name = []ifName:Print("Name in")Else:    Print("No name")#Run ResultsNo NameName= ["Frank"]ifName:Print("Name in")Else:    Print("No name")#Run ResultsNameinch

Python programming from Get started to practice _ Chapter Fifth _IF statements

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.