Functions of tuples

  • Here’s the code
  • The following functions are basically applicable to List
Len (t = (1,2,3,4,5))
5



Print (Max (t)) print(min(t))
5
1


T = tuple(l) print(t) print(t) print(t)
() 1. What are you talking about? 2.

Functions of tuples

  • It’s basically the same as list
T = (2,1,2,3,45,1,1, 1,1,2,) print(t.count(2)) # index: Print (t.Indexes (45)) # print(t.Indexes (1)) # print(t.Indexes (1))
3, 4, 1

Tuple variable exchange method

  • Two variables exchange values
Print (a) print(b) print("*" * 20) print("*" * 20) C = a a b b = c print(a) print(b) print("*" * 20)
1 3 * * * * * * * * * * * * * * * * * * * * 3 1 * * * * * * * * * * * * * * * * * * * * 1 3

Collection – set

  • Set is a concept in high school mathematics
  • An unordered set of unique data, each of which becomes an element
Dict (" dict (")) print(" dict (") ") print(" dict (") ") print(" dict (") ") print(" dict (") ") print(" dict (")"
<class 'set'>
set()
<class 'set'>
{1, 2, 3, 4, 5, 6, 7}


Dict (" dict ") print(" dict ") print(" dict ") print(" dict ")
<class 'dict'>
{}

Characteristic of a set

  • The data of the collection is out of order, that is, indexing and sharding cannot be used
  • The data elements within the collection are unique and can be used to exclude duplicate data
  • The data in the collection, STR, int, float, tuple, frozen collection, etc., that is, can only be placed inside the hashing data

Set sequence operation

} print(s) if "love" in s: print("Yes") if "haha" not in s: print("Yes")
{'you', 4, 5, 'love', 'i'}
Yes
Yes

Collection traversal operation

} for I in s: print(I)} for I in s: print(I)
you
4
5
love
i


# with a collection of tuples traversal s = {(1, 2, 3,), (" I ", "love" and "you"), (4 and 6)} for k, m, n in s: print (k, "-", m, "--", n) for k in s: print(k)
i -- love -- you
4 -- 5 -- 6
1 -- 2 -- 3
('i', 'love', 'you')
(4, 5, 6)
(1, 2, 3)

Connotation of set

# # general collection connotation under the set automatically filter out after initialization repeating element s = {23223233,2,4,5,6,3,4,1,5,3} print (s) # general collection connotation ss = {I} for I in s print (ss)
{1, 2, 3, 4, 5, 6, 233, 23, 223}
{1, 2, 3, 4, 5, 6, 233, 23, 223}


SSS = {I for I in s if I % 2 == 0} print(SSS)
{2, 4, 6}


S1 = {1,2,3,4} s2 = {" I ", "love", "you"} s = {m*n for m in s2 for n in s1} print(s) s = {m*n for m in s2 for n in s1 if n == 2} print(s)
{'you', 'youyou', 'love', 'lovelovelovelove', 'lovelovelove', 'lovelove', 'iii', 'youyouyouyou', 'ii', 'i', 'iiii', 'youyouyou'}
{'lovelove', 'youyou', 'ii'}

Set functions/functions about sets

Print (len(s)) print(Max (s)) print(min(s)) print(len(s)) print(min(s)) print(min(s))
8, 543 1
# set: generate a collection of l = {1,2,3,4,5,4,3,2,1} s = set (l) print (s)
{1, 2, 3, 4, 5}


Print (s) print(s) print(s) print(s)
{1, 3}


# clear s = {1,2,3,4,5} print(id(s)) s () print(id(s)
1370773843528, 1370773843528,
If the value you want to remove does not exist, use # discard: Remove the value specified in the collection is the same as remove, but if you want to remove it, Print (s) print(s) print("*" * 20) s.card (100) print(s) print("*" * 20) s.card (100) print(s) s.remove(100) print(s)
{1, 2, 3, 5, 23} {2, 3, 5, 23} * * * * * * * * * * * * * * * * * * * * {2, 3, 5, 23} --------------------------------------------------------------------------- KeyError Traceback (most recent call last) <ipython-input-35-0113522ad176> in <module> 12 print(s) 13 ---> 14 s.remove(100) 15 print(s) KeyError: 100
D = s.p () print(d) print(s)
1
{2, 3, 4, 5, 6, 7}


# issuperset # issuperset # issuperset # issuperset # issuperset # issuperset # issuperset # issuperset # issuperset Intersection s1 = {1,2,3,4,5,6} s2 = {5,6,7,8,9} s2 = s1.intersection s2 = s1.difference(s2) print(s_2) s_3 = s1.issubset(s2) print(s_3) s_4 = s1.issuperset(s2) print(s_4)
{5, 6}
{1, 2, 3, 4}
False
False


Print (S_1) s_2 = s1 + s2 print(S_2) s_2 = s1 + s2 print(S_2) s_2 = s1 + s2 print(S_2) s_2 = s1 + s2 print(S_2)
{1, 2, 3, 4}



---------------------------------------------------------------------------

TypeError                                 Traceback (most recent call last)

<ipython-input-45-fac787d752ea> in <module>
      7 print(s_1)
      8 
----> 9 s_2 = s1 + s2
     10 print(s_2)


TypeError: unsupported operand type(s) for +: 'set' and 'set'

B: Frozen set

  • Freezing is an operation that cannot be modified
  • Frozenset is a special set
Print (type(s) print(type(s) print(s) print(type(s) print(s) print(type(s) print(s)
<class 'frozenset'>
frozenset()

Dict dictionary

  • A dictionary is a kind of composite data, unordered composite data, data in the form of key-value pairs
Dict () dict() dict() dict() dict() dict() dict() dict() dict() dict() D = {"one":1, "two":2, "three":3} print(d) # dict({"one":1, "two":2,) Dict (one=1, two=2, three=3) print(d) # d = dict([("one",1)) ("two",2), ("three",3)]) print(d)
<class 'dict'>
{}
{}
{'one': 1, 'two': 2, 'three': 3}
{'one': 1, 'two': 2, 'three': 3}
{'one': 1, 'two': 2, 'three': 3}
{'one': 1, 'two': 2, 'three': 3}


Characteristics of dictionaries

  • A dictionary is a sequence type, but it is an unordered sequence, so there is no sharding or index
  • Each of the data in the dictionary is made up of key-value pairs, KV pairs

    • Key: must be a hash value, such as int, string, float, tuple, but list, set, and dict are not acceptable
    • Value: Any value

Common Dictionary Operations

D = {"one":1, "two":2, Print (d["one"]) print(d["one"]) print(d["one"]) print(d)
1
{'one': 'eins', 'two': 2, 'three': 3}
{'two': 2, 'three': 3}


D = {"one":1, "two":2, "three":3} if 2 in d: print("value") if "two" in d: print("key") if ("two,2") in d: print("kv")
key


D = {"one":1, "two":2, "three":3} Print (k, d[k]) for k in d.keys(): print(k, d[k]) for v in d.keys(): print(k, d[k]) Print (v) # for k,v in d. ems(): print(k, "--", v) # for k,v in d. ems():
one 1
two 2
three 3
one 1
two 2
three 3
1
2
3
one -- 1
two -- 2
three -- 3

Dictionary generation

d = {"one":1, "two":2, Dd = {k:v for k,v in d. ems()} print(dd) = {k:v for k,v in d. ems() if v % 2 == 0} print(dd)
{'one': 1, 'two': 2, 'three': 3}
{'two': 2}

Dictionary correlation function

Dict = {"one":1, "two":2, "three":3} print(STR (d)) print(STR (d))
{'one': 1, 'two': 2, 'three': 3}


D = {"one":1, "two":2, "three":3} I = d. ems() print(type(I)) print(I) print(I)
<class 'dict_items'>
dict_items([('one', 1), ('two', 2), ('three', 3)])


K = d.keys() print(type(k)) print(k)
<class 'dict_keys'>
dict_keys(['one', 'two', 'three'])


Print (type(v)) print(v) print(v)
<class 'dict_values'>
dict_values([1, 2, 3])


# get: D = {"one":1, "two":2, "three":3} print(d.get("oner")) # get None, Print (d. set ("one", 100)) print(d. set ("one33", 100)) print(d['on333']) print(d. set ("one33", 100))
None
1
100



---------------------------------------------------------------------------

KeyError                                  Traceback (most recent call last)

<ipython-input-86-f8c01a58018e> in <module>
      8 print(d.get("one33", 100))
      9 
---> 10 print(d['on333'])


KeyError: 'on333'


# fromkeys: Using the specified sequence as the key, Use a value as the value for all keys in the dictionary l = ["eins", "zwei", "dree"] # Note the type of the two arguments from fromKeys # Note the body of the call from fromKeys D = dict. "hahahahaha") print(d)
{'eins': 'hahahahaha', 'zwei': 'hahahahaha', 'dree': 'hahahahaha'}