Home > Backend Development > Python Tutorial > Introduction to ternary operator

Introduction to ternary operator

巴扎黑
Release: 2017-07-20 16:44:40
Original
2929 people have browsed it

     

1. What is ternary arithmetic: (Boolean expression? Value 0:Value 1;)

5>3 ?alert('5 big'):alert('3 big');

that is if(5>3){alert('5 big')}else{alert('3 big')};

Note: The difference between ternary operation and if(){}else{} is that ternary operation has a return value

For example:

var max = a>b? a:b;

2. How to write multi-condition ternary operation:

Example: Determine the four levels of ABCD based on student scores

var result = (sc<0 || sc>100) ?("Invalid score"):

sc>=90?("A"):

sc>=80?("B"):

sc>=60?("C"):("D");

Note: Priority issues need to be considered when calculating, add "()' to avoid wrong results!

3. Advantages of ternary operation: concise and clear

name = value 1 if condition else value 2

##    Deep copy and shallow copy

str creates a value that cannot be modified. If modified, create a


For int and str

Assignment, deep copy, shallow copy address is unchanged

For dict, list, tup

Shallow copy only copies the outermost layer

Deep The copy is to copy everything except the innermost layer

  

   Function

Dynamic parameters

Universal parameters

*arges

**kwarges

Local variables and global variables

Global

Uppercase

Modification, global

Local

Lowercase, only executed in code

The above is the detailed content of Introduction to ternary operator. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template