The second section of the Python base variables, operators, if statements, while and for loop statements

Source: Internet
Author: User
Tags arithmetic operators bitwise bitwise operators

   In our previous section, we found that when we use string to do some function processing, we all write down the whole string, such as "Jasonhy". Startwith ("J"), If we use "Jasonhy" in many places in the program, each of them is written like this, The code looks very low, and you can sometimes write wrong, another is, if the demand changes now, is no longer "jasonhy" into the "Jasonhy", then this time you have to change every place? This workload can be imagined. To facilitate the readability and maintainability of our code, we have introduced variables and operators.

Variables can only consist of words, numbers, and underscores, and cannot start with a number, cannot use the keyword in Python, and it is best not to duplicate the built-in functions in Python.

Operators, including:

Arithmetic operators

Comparison operators

Assignment operators

logical operators

Bitwise operators

Member operators

Identity operator

We first look at the arithmetic operators, simply say we are familiar with the subtraction: +-x/, in addition to these, Python also has a modulo operation (%), power operation (XX), Fetch quotient operation (//), here only the following three are demonstrated:

Modulo (%): the remainder

Print (10%3)
The output is:
1

   Power operation (XX): That's what we call the second party

Print (2**3)
The output is:
8

   Provider (//):

Print (10//3)
The output is:
3

   The comparison operator, which is our common greater than (or greater than or equal to) (>/>=), is less than (or less than or equal to) (</<=), not equal to (!). =), equal to (= =), the return result is true and false, demonstrating a = =:

Print (10 = = 3)
The output is:
        False

   The assignment operator, the simple assignment operator is =, there is also: addition Assignment (+ =), subtraction Assignment (-=), multiplication assignment (x=), Division Assignment (/=), modulus Assignment (%=), Power Assignment (xx=), Fetch quotient Assignment (//=), = then directly assigns the right value to the left variable, For example, var = 18, 18 is assigned to Var, the result is as follows:

Print (VAR)
The result of the output is;
18

Other assignment operators, the left operation first, and then the = operation, such as var = 18,var + = 1 (first 18 + 1 arithmetic operation, then = operation), the result is as follows:

var = 1+ + +   print(VAR)
The output is:
19

A logical operator, with an operation (and), or an operation (or), a non-operation (not), evaluates to a true and False,and operation, and all is true to return the true,or operation as long as there is a true return True,not operation negation. Examples are as follows:

VA =print and vb = = +)
The output is:
True
Print and vb = = 19)
The output is:
False
VA = += printor vb = = +)
The output is:
True
 Print or vb = = +)
The output is:
True
 Print or vb = = 1)
The output is:
False

Bitwise operator, which is the operation of numbers in binary order:

Bit with (&): Two corresponding bits are 1, the result is 1, otherwise 0

Bit or (|): Two corresponding bits as long as one is 1, the result is 1, otherwise 0

Bit xor (^): two corresponding bits as long as the result is 1, otherwise 0

Bitwise Inverse (-): is to reverse each for, 1 becomes 0, 0 becomes 1

Left movement (<<): Each bits to the left to move several bits, after moving, high drop, low 0

Right Move (>>): Each bits moves several bits to the right

The member operator is used to test whether a series of members are included, such as a string, a list (later), a tuple (later):

In: Returns true if found in the specified sequence

Not in: Reverse fetch

    Case:

" Jasonhy " Print ("J" in vstr) Print ("J" in Vstr)
The output is:
①true
②false

identity operator to compare whether two objects are the same:

is: To determine if two identifiers are references to the same object, yes, returns true

Was not: Reverse fetch

    Case

" Jasonhy "  "jasonhy"printis"jasonhy  "  print is VSTRB)  
The output is:
①true
②false

if statement, the format is as follows:

The first type:

If condition:

code block

Else

code block

The second type:

If condition:

code block

Elif Conditions:

code block

Else

code block

   Case:

" Jasonhy "  "jasonhy"if is nameb):      Print( " same name, same person. "   Else:      print(" not the same name, not the same person ")  
Namea ="Jasonhy"Nameb="Jasonhy"Namec="Jasonhy"    if(Namea isnameb):Print("Nameb and Namea are the same person .")elif(Namea isNamec):Print("Namec and Namea are the same person .")Else:Print("They all look the same, but the names are different, not the same person .")

   While Loop statement:

Format:

While condition:

code block

   Case:

Time = 5  while time > 0:   print(" start countdown ==>%d"% Time )    -= 1 print(" Welcome to Jasonhy's Python blog ")

   For Loop statement:

Format:

For child element in sequence:

code block

   Case:

Name ="Abc123jasonhy"Count= 1 forVninchName:if("2"==vn):Print("you finally guessed it, it was 2.") Break        Else:Print("you've guessed it%d times."%count) Count+ = 1Print("Game Over")

Note: In a looping statement, break represents a stop loop, continue means that the loop is skipped and the next loop is executed

The second section of the Python base variables, operators, if statements, while and for loop statements

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.