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

s={' Egon ', 123, ' Egon ', ' 1 '}   #s =set ({' Egon ', 123, ' Egon ', ' 1 '}) print (S,type (s)) s={' 1 ', 1,[1,2]} s={' 1 ', 1, (), ' a ' , ' B '} print (s)

Cycle

s={' 1 ', 1, (), ' A ', ' B '}for i in S:    print (i)

  

Relational operations

python_s={' Egon ', ' Alex ', ' Steel egg ', ' Lao Wang '}linux_s={' Alex ', ' Steel egg ', ' old Man fraternity ', ' Eric Four ' #取共同部分: Intersection print (python_s & linux_s) # Take the old boy all enrolled students: python_s | linux_s #取只报名了python课程的学生: Set Print (python_s-linux_s) #取只报名了linux课程的学生: Difference print ( linux_s-python_s) #取没有同时报名python和linux课程的学: Symmetric difference print (linux_s ^ python_s)

Collection method

#集合方法 python_s={' Egon ', ' Alex ', ' Steel egg ', ' Lao Wang '} linux_s={' Alex ', ' Steel egg ', ' old Man fraternity ', ' Eric Four '} print (Python_s.intersection (linux_s) ) # Intersection: 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)) # symmetric difference set, python_s ^ linux_s python_s={' Egon ', ' Alex ', ' Steel egg ', ' Lao Wang '} linux_s ={' Alex ', ' Steel egg ', ' The love of the Brotherhood ', ' Eric Four '} python_s.difference_update (linux_s) print (python_s)

Collection operation type

S1={' A ', 1} s1.add (1)    #追加一项 print (S1) s1.discard (' a ')     #如果在 set "s" exists in element x, delete s1.discard (' B ') print (S1) S1.remove (' bbbbbbb ')    #移除 print (S1) s1={' A ', 1, ' B ', ' C ', ' d '} print (S1.pop ())    #随机删除

  

Summarize

S.update (t)   s |= t   returns the  set  "s" after adding the elements in the  set  "T"      s.intersection_update (t)   s &= t   returns only the elements containing the  set  "T" are reserved  set  "S"     s.difference_update (t)   s -= t   return deleted  set  "T" contains elements after  set  "s"     s.symmetric_difference_update (t)    s ^= t   returns  set  "s" containing elements in  set  "T" or  set  "s", not both     s.add (x)      add elements to  set  "s"  x     s.remove (x)      remove element from  set  "s"  x,  throw if not present  KeyError     s.discard (x)      if there are elements in  set  "s"  x,  Delete      s.pop ()      Delete and return an indeterminate element in  set  "s",  if empty,  keyerror is thrown     s.clear ()      Delete all elements in  set  "s"    

Boolean type:

True and False
Print (Type (True))


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

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

L=[] If Len (l) = = 0: Print     (' list is empty ') print (bool (l)) if not L: #bool (L)     print (' list is empty ')

  

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.