Conditional statements, branch statements, and logical and comparison operators in Python

Source: Internet
Author: User
Tags switch case

if and Else statements in Python perform simple tests such as:


A = 3;b = 5;if a<b:print (' A small b large ') Else:print (' A big B small ')

Answer output:

A small B large

My understanding of conditional judgment if:

The so-called conditional judgment is actually if a<b we do the corresponding treatment otherwise (a greater than B case) we have to a>b the corresponding processing we have to judge a variable or an expression is only true when the corresponding processing when else is optional

Like what:

Name = ' Zhang San ' ismarry = ' true ' #表示某个人是否结婚时 #接下来只对真作出相应的处理 if Ismarry:print (name+ ' married ')

Answer output :

Zhang San married

You can also use the IF condition in PHP to determine the same as in Python:

$name = ' Zhang San '; $isMarry = ' TRUE '; if ($isMarry) {echo $name. " Married "}

Answer output :

Zhang San married

We can also use the logical operator : Boolean with (and) Boolean or (or) Boolean not (not) to establish a Boolean type expression:

And: Boolean with If A is false A and B returns False ( short-circuit operation : If the value of a is false when a logical operation is not performed B is worthwhile ) Otherwise he calculates the value of B Special: It's true, the rest is false.

Or: Boolean or if A is false then B-value calculation if A is true then the same short-circuit operation Characteristics: True or False is False

Not: Boolean non if A=true returns False if A=false returns True feature: True for false false

comparison operator (= =) : Indicates whether two values are equal

Next we create a Boolean-type expression:

#企业判断某个人是否是老员工price = 3200;type = "Dollars" years = "AA" If the salary is greater than 3000 and the wage type is USD and does not fall under the entry 2 years or the salary is greater than 3200, the person is the old employee if price > 3 and type = = "Dollars" and not (years<2 or price>3200):p rint (' This man is an old employee ')

Answer output :

This guy's an old employee.

Logic Operators can also be used to make logical judgments in PHP:

and (Logical AND): general use of symbols && representations ( high priority ) can also use and features: If $a and $b are true to indicate true

or (logical OR): general use of symbols | | Indicate ( high priority ) you can also use the or feature: if $ A and $b have a true return True

Logical non: Use symbols ! Indicates a feature: True when the return false value is false when a true

comparison operator (= =) Same as Python, indicates whether two values are equal

The code is as follows:

$price = 3200; $type = ' dollars '; $year = 5;if ($price >3000 && $type = = ' dollars ' &&! $year <2 | | $price >3200) {echo ' This person is an old employee ';}

Answer output :

This guy's an old employee.


There are no specialized branch statements in python that switch case is used to judge multiple values but can be judged using the Elif statement :

Today = 3;if Today = = 1:str = ' It's Monday '; elif now ==2:str = ' today is Tuesday '; Elif Day ==3:str = ' It's Wednesday '; elif today = = 4:str is Thursday '; else:str = ' Today is Friday ';p rint (str);

Answer output :

Today is Wednesday

In PHP, you can use the Switch branch statement to determine multiple values of a variable:

 $today  = 3;switch ($today) {    case 1:        $str   =  ' Today is Monday ';     break;     case 2:      $str   =  ' Today is Tuesday ';      break;    case 3:      $str   =  ' Today is Wednesday ';      break;    case 4:     $ str  =  ' Today is Thursday ';     break;    case 5:       $str   =  ' Today is Friday ';     break;     default:       $str   =  ' Today is the weekend ';   //default: As long as the value is not equal to 1 , the 2,3,4,5 will enter into this inside}echo  $str; 

Answer output :

Today is Wednesday

In Python, the in operator is usually used to determine whether a value is contained in another object and it also returns TRUE or false

The Boolean data type has a value of two:true means True false represents false

Summarize:


Boolean AND and Boolean or or Boolean non-not

|| Represents logic or ! Represents logical non-

3,python if you want to determine the different values of a variable to make corresponding processing using if:elif:else: php uses switch{case:default:}

4, comparison operator = = Determines whether two values are equal

5, in to determine if an object belongs to

6, Boolean value: true False

This article is from the "Hong Dachun Technical column" blog, please be sure to keep this source http://hongdachun.blog.51cto.com/9586598/1759046

Conditional statements, branch statements, and logical and comparison operators in Python

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.