Python (Day4) collection, Boolean type

Source: Internet
Author: User

Function one of the set: relational operation
The function of the set two: to go heavy


To define a collection:
The elements within the set must be unique;
The elements in the set must be hash, and also the immutable type;
The collection is unordered

123456 s={‘egon‘,123,‘egon‘,‘1‘}   #s=set({‘egon‘,123,‘egon‘,‘1‘} ) print(s,type(s)) s={‘1‘,1,[1,2]} s={‘1‘,1,(1,2),‘a‘,‘b‘} print(s)

Cycle

123 s = { ' 1 ' 1 1 2 ' a ' ' B ' } for   in  s:      print (i)

  

Relational operations

1234567891011121314151617181920 python_s={‘egon‘,‘alex‘,‘钢蛋‘,‘老王‘}linux_s={‘alex‘,‘钢蛋‘,‘欧德博爱‘,‘艾里科四‘}#取共同部分:交集 print(python_s & linux_s) #取老男孩所有报名学习的学生:并集 print(python_s | linux_s) #取只报名了python课程的学生:差集 print(python_s -linux_s)#取只报名了linux课程的学生:差集 print(linux_s -python_s)#取没有同时报名python和linux课程的学:对称差集 print(linux_s ^ python_s)

Collection method

123456789101112131415161718 #集合方法 python_s={‘egon‘,‘alex‘,‘钢蛋‘,‘老王‘} linux_s={‘alex‘,‘钢蛋‘,‘欧德博爱‘,‘艾里科四‘} print(python_s.intersection(linux_s))# 交集:python_s & linux_s print(python_s.union(linux_s))#并集:| print(python_s.difference(linux_s)) #python_s-linux_s print(python_s.symmetric_difference(linux_s))# 对称差集, python_s ^ linux_s python_s={‘egon‘,‘alex‘,‘钢蛋‘,‘老王‘} linux_s={‘alex‘,‘钢蛋‘,‘欧德博爱‘,‘艾里科四‘} python_s.difference_update(linux_s) print(python_s)

Collection operation type

1234567891011121314 s1={‘a‘,1}s1.add(1)    #追加一项print(s1)s1.discard(‘a‘)     #如果在 set “s”中存在元素 x, 则删除s1.discard(‘b‘)print(s1)s1.remove(‘bbbbbbb‘)    #移除print(s1)s1={‘a‘,1,‘b‘,‘c‘,‘d‘}print(s1.pop())    #随机删除

  

Summarize

+ View Code

Boolean type:

True and False
Print (Type (True))


count=10
Print (BOOL (Count > 10))

#所有的数据类型自带布尔值, only 0,none, empty Boolean value is False

1234567 l=[]iflen(l) ==0:    print(‘列表为空‘)print(bool(l))ifnotl: #bool(l)    print(‘列表为空‘)

  

String

Definition: It is a collection of ordered characters used to store and represent basic textual information, "' or" "or" ' "" that contains what is called a string
Characteristics:
1. Only one value can be stored
2. Non-volatile
3. Define the character set in left-to-right order, the subscript is accessed sequentially from 0, ordered
Add:
1. Both single and double quotation marks cannot suppress the meaning of special characters, if you want all the characters in quotation marks to nonspacing special meaning, precede the quotation marks with R, such as Name=r ' L\THF '

Python (Day4) collection, Boolean type

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.