Python learning notes (11) Python statements (1), learning notes python statements

Source: Internet
Author: User

Python learning notes (11) Python statements (1), learning notes python statements

Operators and condition statements

Arithmetic Operators

Operator Description Instance
+ Add-add two objects A + B output result 30
- Minus-get a negative number or a number minus another number A-B output result-10
* Multiply by two numbers or return a string that has been repeated for several times. A * B output 200
/ Divide By-x by y B/a output result 2
% Modulo-return the remainder of the Division B % a output result 0
** Power-returns the y Power of x. A ** B is the 10th power, and the output result is 100000000000000000000.
// Returns the integer portion of the operator. 9 // 2 output result 4, 9.0 // 2.0 output result 4.0

Comparison Operators

Operator Description Instance
= Equal to-compare whether the object is equal (A = B) returns False.
! = Not equal to-compare whether two objects are not equal (! = B) returns true.
<> Not equal to-compare whether two objects are not equal (A <> B) returns true. This operator is similar! =.
> Greater than-returns whether x is greater than y (A> B) return False.
< Less than-returns whether x is less than y. All comparison operators return 1 to indicate true, and 0 to indicate false. This is equivalent to the special variables True and False. (A <B) returns true.
> = -Returns whether x is greater than or equal to y. (A> = B) returns False.
<= Less than or equal to-returns whether x is less than or equal to y. (A <= B) returns true.
1 >>> a = 2 2 >>> B = 2 3 >>> a = B # judge whether two objects a and B are equal, and return true, the objects referenced by the two variables are 4 True 5 >>> a is B 6 True 7 >>> id (a), id (B) # The same object referenced by a and B 8 (54354784L, 54354784L) 9 >>> da = {"lang": ["python", c], "teacher ": "cc"} 10 >>> import copy11 >>> db = copy. deepcopy (da) # Deep copy 12 >>> db13 {'lang ': ['python', set ([0, 1, 3, 5, 6])], 'teacher': 'cc'} 14 >>> id (da), id (db) # is two different objects 15 (54489976L, 64755912L) 16 >>> da is db # Determine whether the object is the same 17 False18 >>> da = db # two objects are equal 19 True20 >>> da! = Db # two objects are not equal 21 False22 >>> a, B = 2,323 >>>! = B 24 True25 >>> a <> b26 True27 >>> 321> 123428 False29 >>> "321"> "1234" 30 True31 >>>

Logical operators

 

Operator Logical expression Description Instance
And X and y Boolean "and"-If x is False, x and y returns False, otherwise it returns the calculated value of y. (A and B) returns 20.
Or X or y Boolean "or"-If x is not 0, it returns the value of x; otherwise, it returns the calculated value of y. (A or B) returns 10.
Not Not x Boolean "not"-If x is True, False is returned. Returns True if x is False. Not (a and B) returns False

Bool () determines whether an object is true or false.

A and B if A = true: return bool (B) if A = false: return false; execute A first. if A is true, return the result of B. If A is false, false is returned directly.

A or B: if A = true: return true else: return bool (B) First executes A. if A is true, true is returned. Otherwise, the bool value of B is returned.

1 >>> a = "python" 2 >>> B = [] 3 >>> bool (a) 4 True 5 >>> bool (B) 6 False 7 >>> bool (0) 8 False 9 >>> bool (1) 10 True11> 4> 3 and 4 <912 True13> 4 <3 and 4> 914 False15> 4> 3 or 4> 916 True17> 4 <3 or 4 <918 True19 >>> not (4> 3) 20 False21> 4> 3 and 4> 9 or 4 <5 # Run 4> 3 first to true, then run and on the right, or 4> 9 to false, execute 4 <5 is true, and the right side of and is true. Therefore, true22 True23 is returned.>

Condition Statement

Review statement

Print, import, and value assignment statements

1 >>> print "hello world" # print Statement 2 hello world3 >>> import math # import Statement 4 >>> a = 2 # assign a value statement

Condition Statement

1 import random # introduce random number module 2 a = random. randint (0,100) # random integer 3 from 0 to 100 if a> 50: 4 print a # Note: four spaces before print 5 print "a is bigger than 50" 6 elif a = 50: 7 print a 8 print "a is 50" 9 else: 10 print a11 print "a is smaller than 50"

Ternary Operators

A = Y if X else Z indicates A = Y. Assign the object Y to A. if X is true, assign Y to, otherwise, assign Z to.

>>> X = 2 >>> y = 8 >>> a = "python" if x> y else "web" if x> y assigns python to, otherwise, the web is assigned to a. >>> a 'web'>

 

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.