Python Development Basics Day4 Boolean operations, collections

Source: Internet
Author: User
Tags pear true true

Boolean value

True True

False false

All data types have their own Boolean values, and the data is false only when 0,none and empty.

Print (bool ()) print (bool ()) Print (bool (")) Print (bool (")) print (bool (0)) print (bool (None)) Output result Falsefalsefalsetruefalsefalse

Not, OR and and precedence

Priority from high to Low: Not>>and>>or

Example 1: Calculate and, after calculation or----->true and false to False,false or false to false, result output no

If True and False or false:    print (' yes ') Else:    print (' No ')

Output results
No

Example 2: Calculate and, after calculate or------->false and False to False,true and false to true, output Yes

If True or False and false:    print (' yes ') Else:    print (' no ') output Yes

Example 3: Calculate not, then calculate and, and finally calculate or------>not true to False,false and false to False,false or false to output no

If not True and False or false:    print (' yes ') Else:    print (' no ') output result no

Example 4: First evaluates not false to True, evaluates false and true to False, evaluates true or False to true, output Yes

If True or false and not false:    print (' yes ') Else:    print (' no ') output Yes

Collection

A collection is a relational operation that removes duplicate elements, and the elements can only be immutable, and the set and dictionary are unordered.

Create a Collection

S1={1,2,2,3,4}print (S1,type (S1)) S2=set (' Apple ') print (S2,type (S2)) s3=set ([' Apple ', ' apple ', ' pear ') print (S3,type (S3)) S4=set (' Apple ', ' apple ', ' pear ') print (S4,type (S4)) S5=set ({' Apple ': ' apple ', ' pear ': ' pears '}) print (S5,type (S5)) Output: (may be different in order) {1, 2, 3, 4} <class ' Set ' >{' a ', ' P ', ' l ', ' e '} <class ' set ' >{' pear ', ' apple '} <class ' Set ' ; {' pear ', ' apple '} <class ' set ' >{' pear ', ' apple '} <class ' Set ' >

Create an empty collection

S=set () print (type (s))

Intersection

s={1,2, ' C ', 4,5}d={1, ' A ', ' B ', 4,5}print (s.intersection (d)) or print (S & D) output: {1, 4, 5}

and set

s={1,2, ' C ', 4,5}d={1, ' A ', ' B ', 4,5}print (s.union (d)) or print (s | d) output: {' B ', 1, 2, 4, 5, ' A ', ' C '}

Subtraction

s={1,2, ' C ', 4,5}d={1, ' A ', ' B ', 4,5}print (s.difference (d)) or print (s-d) output {' C ', 2}

Symmetric difference Sets

s={1,2, ' C ', 4,5}d={1, ' A ', ' B ', 4,5}print (s.symmetric_difference (d)) or print (s ^ d) output: {' B ', 2, ' A ', ' C '}

Emptying elements within a collection

s={1,2, ' C ', 4,5}s.clear () print (s) output result set ()

Finding the difference set and assigning values

s={1,2, ' C ', 4,5}d={1, ' A ', ' B ', 4,5}s.difference_update (d) Output result {2, ' C '}

Update

S1={' A ', 1}s2={' a ', ' B ', 3,1}s1.update (S2) print (S1) output result {1, 3, ' A ', ' B '}

Add to

S1={' A ', 1}s1.add (3) s1.add (' A ') print (S1) output result {1, 3, ' a '}

Delete

S1={' A ', 1}s1.discard (' a ')     #当没有该元素时候不报错    #s1. Remove (' AA ')    #当没有aa元素时候会报错 #s1.pop ()            #随机删除print ( S1) Output result: {1}

Judge

S1={' A ', 1}s2={' a ', ' B ', 3,1}print (S1.issubset (S2))   #判断s1是否是s2的子集 if S1 equals S2 then each is a subset of print (S2.issuperset (S1)  ) #判断s2是否是s1的超集print (S1.isdisjoint (S2))   #判断有没有集合 If no true output is returned Truetruefalse

Cycle

For i in S2:    print (i) output (unordered) leap

Python Development Basics Day4 Boolean operations, collections

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.