Python Core programming Basics Python operators, operator precedence, expressions introduction--20150717

Source: Internet
Author: User
Tags bitwise true true

Python Core programming Basics Python operators, operator precedence, expressions Introduction

1.Python Operators and Expressions:

(1) Understanding Pyhton Operator 1: What is an operator

In Python operations, sometimes we need to operate on one or more numbers or one or more strings, *,+


(2) Understanding Pyhton Operator 2: What operators are and how operators are used

+: Add

-: Minus

*: Multiply

/: Except

* *: Power

<: Less than

>: Greater Than

! =: Not equal to

: The integer portion of the divide

%: Divide the remainder part

&: Bitwise AND

| : Bitwise OR

^: Bitwise XOR OR

~: Take Back

>>: Move Right

<<: Move Left

<=: Less than or equal to

>=: greater than or equal to

= =: equals sign, an equal sign is called an assignment

Not: Logical non-

And: Logic and

Or: Logical OR


#实例:

1) # "+": two objects added

#两个数字相加

A = 7+8print A

#输出结果:

15


2) #两个字符串相加

b = "good" + "job!" Print B

#输出结果:

Good job!


3) # "-": Take the opposite number of a number or subtract two numbers

A = -7print ab =-( -8) Print B

#输出结果:

-7

8

c = 19-1print C

#输出结果:

18


4) # "*": the number of times a number is multiplied by two or a string repeats

A = 4*7print ab = "Hello" *7print b

#输出结果:

28

Hello

Hello

Hello

Hello

Hello

Hello

Hello


5) # "/": Divide two numbers

A = 7/2print a #除数和被除数是整数, then the result is also the integer b = 7.0/2c = 7/2.0 #除数和被除数其中有一个是浮点数, then the result will also have a decimal print bprint C

#输出结果:

3

3.5

3.5


6) # "* *": exponentiation operation

A = 2**3 #相当于2的3次幂, that is 2*2*2print a

#输出结果:

8


7) # "<": less than symbol, returns a bool value

A = 3<7print AB = 3<3print b

#输出结果:

Ture

False


8) # ">": Greater than symbol, returns a bool value

A = 3>7print AB = 3>1print b

#输出结果:

False

Ture


9) # "! =": Not equal to the symbol, also returns a bool value

A = 2!=3print AB = 2!=2print b

#输出结果:

Ture

False


10) # "//": division operation, then returns the integer portion of its quotient, and the remainder is removed

A=10//3print A

#输出结果:

3


11) # "%": division operation, and then return to its quotient of the remainder part of the drop-off quotient

A=10%3print A

#输出结果:

1

b=10%1 #没有余数的时候返回什么? Print B

#输出结果:

0

A=10//3 #a为商的整数部分b =10%3 #b为c =3*a+b #c为除数乘以商的整数部分加上余数, the value of C should be the divisor print C

#输出结果:

10


# "&": Bitwise AND operation, the so-called bitwise-and refers to a number converted to binary, then these binary numbers are bitwise to perform with the operation, the same 1 is 1

A=7&18 #执行一下, why 7 and 18 participants get 2?? Print a

#输出结果:

2

"First we open the calculator, then we convert 7 to binary, get 7 binary value is: 111, auto-complete 8-bit, that is 00000111

Then we convert 18 to binary, the value of 182 is 10010, the same complement is 8 bits, or 00010010

And then, we'll be 00000111

, followed by a 00010010 bitwise AND operation,

The result is: 00000010, then we convert 00000010 to decimal

Get the number two, so 7 with 18 bitwise with the result is the binary 10, that is, the decimal 2


‘‘‘


13) # "|": Bitwise OR operation, also we want to convert the number to binary after the bitwise OR operation, 1 is 1

A=7|18print A

#输出结果:

23

"' Let's analyze, as well as our 7 binary form is the binary form of the 00000111,18 is 00010010

We will be 00000111

With 00010010 bitwise OR operation,

The result is 00010111, and then we convert 00010111 to decimal.

Get the number 23, so 7 with 18 bitwise OR the result is binary 10111, which is the decimal 23

‘‘‘


14) # "^" bitwise XOR, XOR refers to the difference is 1, the same is 0

A=7^18print A

#输出结果:

21st

‘‘‘

First, XOR means that the difference is 1, and the same is 0.

Let's analyze the same as our 7 binary form is the binary form of the 00000111,18 is 00010010

We will be 00000111

With +00010010 bitwise XOR OR operation,

The result is 00010101, and then we convert 00010101 to decimal.

Get the number 21, so 7 with 18 bitwise XOR result is binary 10101, which is the decimal 21

‘‘‘


15) # "~": Bitwise Rollover ~X =-(x+1)

A=~18 # ~18=-(18+1) =-19print A

#输出结果:

-19


# "<<": Shift left

‘‘‘

For example, 18 left is to move his binary form 00100100 to the left, that is, to move to 00100100, that is, to become 36, left a unit equivalent to multiply 2, left to move two units

The equivalent of multiplying by 4, moving the left 3 units equal to 8, moving the n units to the left is equivalent to multiplying by 2 n power.

‘‘‘

A=18<<1print Ab=3<<3print b

#输出结果:

36

24


# "<<": Move Right

‘‘‘

The right shift is the inverse of the left shift, moving the corresponding binary number to the right, a unit to the right by dividing by 2, moving the right two units equal to dividing by 4, the right shift of 3 units equivalent

By 8, the right shift n units is equal to the power of N to divide by 2.

‘‘‘

A=18>>1print ab=18>>2# from 00010010 to 00000100, which results in 4, equivalent to 18//(2**2) Print B

#输出结果:

9

4


# "<=": Less than equals sign, comparison operation, less than or equal to, returns a bool value

A=3<=3b=4<=3print Aprint b

#输出结果:

Ture

False


# ">=": greater than or equal, comparison operation, return bool value

A=1>=3print Ab=4>=3print b

#输出结果:

False

Ture


20) # "= =": Compares two objects for equality, returns a bool value

A = 12==13print ab= "Hello" = = "Hello" Print b

#输出结果:

False

Ture


) #not: Logical non, BOOL value not operation

A=trueb = not aprint bc=falseprint not C

#输出结果:

False

Ture


#and: Logical AND, bool value and operation, intersect, True

‘‘‘

True and True equals True

True and False equals false

False and True equals false

‘‘‘

Print true and True

#输出结果:

Ture


#or: Logical OR, bool value or operation, phase, true True

‘‘‘

True and True equals True

True and False equals True

False and False equals false

‘‘‘

Print True or False

#输出结果:

Ture


2.Python Operation Priority

(1) What is the Python priority level:

The execution of a program or operator of a python operation is sequential, if a, and B are present at the same time, if a can take precedence over B, then the priority of a is higher than b,b priority is lower than a, where a, B can be an operator, or the program can be made.

Classification:

Inter-Program Prioritization

Precedence between operators

#实例:

1) #优先级的作用:

A = 2+7*8 #乘号优先级高于加减print ab=9>7print b

#输出结果:

58

ture# description > Precedence higher than equals sign =


(2) Python priority use combat

#优先级实例:

#优先级排行榜第1名--function call, address, subscript


#优先级排行榜第2名-Power Operation * *

A=4*2**3print A

#输出结果:

32


#优先级排行榜第3名--Rollover operation ~

# "~": Bitwise Rollover ~X =-(x+1)

A=~18 # ~18=-(18+1) =-19print A

#输出结果:

-19


#优先级排行榜第4名--The sign differs from the plus minus symbol

Print 2+4*-2 #我们可以看, the use of the sign is next to the operand, otherwise it will be wrong, this #说明正负号优先于加减乘除运算, plus minus is followed by the number

#输出结果:

-6

#优先级排行榜第5名--*,/,%

Print 2+4*2/4

#输出结果:

4


#优先级排行榜第6名--+,-

Print 3<<2+1

#输出结果:

24

#00000011变成00011000, which is 24


#优先级排行榜第7名--<<, >>


#优先级排行榜第8名--Bitwise &, ^, |, in fact, these three are also in the priority order, but they are at the same level, so do not subdivide


#优先级排行榜第9名--comparison operator <,>,=.>=,<=,==

A=2*3+5<=5+1*2print A

#输出结果:

False#11<=7


#优先级排行榜第10名--not, and, or of logic


#优先级排行榜第11名--lambda expression, deferred execution


3. Basic rules and characteristics of operator precedence


(1) Python common operator precedence usage rule

#优先级使用规律


#1, usually left-bound, left to right

Print 4+6+5*6+6

#输出结果:

46


#2, when the assignment is usually right-bound, from right to left

A=8+91print A

#输出结果:

99


(2) Python Priority rules memory heart

function addressing subscript one

Power Operation Small Two grinning

The third position of the whole unit # such as the flip operation ~, the sign (followed by the operand), an operator of an operator, single, one, Yuan, operand;

Multiplication 4,000 miles

Multiplication is over. No (v) plus minus

Six dolls like to move left and right

Old Seven bitwise OR heel with

Eight Immortals Crossing strength (comparison operation)

The first logical or non-reciprocal

#lambda表达式在刚开始很少遇到, it is easy to put it to the lowest priority, so in order to facilitate the memory by default it can be


(3) Python priority usage tips

#优先级使用小技巧

#如果实在分不清哪个优先级高哪个优先级低也没关系, just use () to change the priority level

a=2+3*7b= (2+3) *7c= ((2+3) +5) *7# parentheses from inside out, first internal print aprint bprint C

Output Result:

23

35

70


Introduction to 4.Python expressions

(1) What is a Python expression

As Python becomes, we combine the values, variables, and operators together as an expression, such as: "OK", a= "Hello, My Girl", a=5, and so on.

#什么是表达式

#1

"Hello" #字符串表达式

#2

25+7# Calculation Expressions

#3

a=67# an equation expression

a# variable

#4

A= "Hello"


(2) Use of Python expressions

Characteristics:

1) Single-line command: (enter single-line command lines programming mode)

A) Value expression: Value expression result is still value

>>> 8#enter8b) variable expression >>>a= "OK" >>>a>>> "OK" c) evaluates the expression: The result is calculated >>>7+815d) string expression >>> ok ' OK '


2) Multi-line command:

F5 Run No results


(3) Differences between expression execution and print execution results

Python's expression execution results differ from print: String output is different, expression output has single quotes, print has no quotation marks

>>> "QQ" #Enter ' QQ ' >>>print "QQ" Qq>>>a=7>>>a7>>>print A7


This article is from the "8626774" blog, please be sure to keep this source http://8636774.blog.51cto.com/8626774/1675616

Python Core programming Basics Python operators, operator precedence, expressions introduction--20150717

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.