Python Learning Notes (2nd lesson)

Source: Internet
Author: User
Tags arithmetic operators bitwise unsupported

In this section, you will learn about operators, mainly arithmetic and logical operators

1. Arithmetic operators

    • Division operation, Integer/integer = integer, floating-point/integer = floating-point number, integer/float = floating-point:

>>> 17/3
5
>>> 17/3.0
5.666666666666667
>>> 17.0/3
5.666666666666667
>>>

    • Multiplication, integer * integer = integer, floating-point number * integer = floating-point number:

>>> 17*10
170
>>> 17.0*10
170.0
>>> 17.00*10
170.0
>>> 12.3*0.3
3.69

    • Addition operation, Integer + integer = integer, integer + floating point number = floating point

>>> 1+2
3
>>> 1.0+2
3.0
>>> 1.0+2.0
3.0

Note: Sometimes, the value of the addition operation may have some error, for example: 1+1.22 is not equal to 2.22

>>> 1.22+1
2.2199999999999998
>>> 1.23+1
2.23

    • Subtraction, integer-integer = integer, integer-float = floating-point, floating-point number-integer = floating point:

>>> 10-2
8
>>> 10.0-2
8.0
>>> 10-2.0
8.0

Note: Sometimes, the value of the subtraction operation may have a little error, for example: 1.22-0.1 is not equal to 1.12

>>> 1.22-0.1
1.1199999999999999
>>> 1.23-0.1
1.13

    • The% of Python is the modulo operator (integer% integer = remainder):

>>> 5%2
1
>>> 5.4%2
1.4000000000000004
>>> 5%0.2
0.19999999999999973

    • exponentiation Operator: * *

>>> 10**2
100
>>> 10**2.0
100.0

    • The integer divide operator is//and returns the integral part of the quotient:

>>> 10//2
5
>>> 10//3
3
>>> 10.0//3
3.0

2. Logical operators

    • logical operators and, or, non, corresponding Python symbols are: And, or, not

>>> False and True
False
>>> true and True
True
>>> false and False
False
>>> False or True
True
>>> true or True
True
>>> false or False
False

>>> not True
False
>>> not False
True

    • Shift operators << and >>, which indicate that the binary bits of the number are moved left or right several:

>>> 4<<2
16
>>> 4>>2
1
>>> 4>>3
0
>>>
>>> 4>>4
0
>>> 4<<32
17179869184L
>>> 4<<64

Note: The right infinity shift can shift the number to 0, and left shifts can increase the number infinitely. The number of shift operators must be integers, otherwise an error will be

>>> 0.2>>2

Traceback (most recent):
File "<pyshell#53>", line 1, in <module>
0.2>>2
typeerror:unsupported operand type (s) for >>: ' float ' and ' int '
>>> 2>>0.1

Traceback (most recent):
File "<pyshell#54>", line 1, in <module>
2>>0.1
typeerror:unsupported operand type (s) for >>: ' int ' and ' float '

    • bitwise AND, bitwise, or bitwise XOR, bitwise rollover, the corresponding Python notation is:&, |, ^, ~


Examples are as follows:

>>> 8&10
8
>>> 8|10
10
>>> 10^8
2
>>> ~
-11
>>> ~-12
11

Python Learning Notes (2nd lesson)

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.