# day8 - sets and strings

#### aggregate

1. A collection is a container data type; Take {} as the flag of the container, and multiple elements are separated by commas: {element 1, element 2...} the set is variable and unordered

Element: immutable and unique

Empty set: x = {} is a dictionary

​ y = set() is an empty set

The collection has its own de duplication function

```set3 = {1, 2, 3, 1, 2, 2, 4}
print(set3)  # {1, 2, 3, 4}
names = ['Zhang San', 'Li Si', 'tom', 'Zhang San']
names = set(names)
print(names)
```

The collection is out of order

```print({1, 2} == {2, 1})  # True
```
1. Addition, deletion, modification and query of set

1. Search traversal

```print("=====")
set4 = {10, 20, 70, 90}
for x in set4:
print(x)
```
2. increase

```set4 = {10, 20, 70, 90}
print(set4)  # {100, 70, 10, 20, 90}
# b. Assemble Update - adds all the elements in the sequence to the collection
set4.update('abc')
print(set4)  # {100, 70, 'c', 10, 'a', 'b', 20, 90}
```
3. Delete

```# Assemble Remove (element) - deletes the specified element. If the element does not exist, an error will be reported
set4 = {10, 20, 70, 90}
set4.remove(70)
print(set4)  # {10, 20, 30}

# Assemble Discard (element) - delete the specified element (no error will be reported if the element does not exist)
set4 = {10, 20, 70, 90}
print(set4)
```
4. Change - cannot modify element

2. Mathematical set operation: python set supports mathematical set operation: subset (>, <, < =, > =), Union (|), intersection (&), difference (-), symmetric difference (^)

1. subset

```print({200, 300, 600} > {1, 2})  # False (not compare size)
# a. Set 1 > Set 2 - determines whether set 2 is a true subset of set 1
print({10, 20, 30} > {10, 20})  # True
print({10, 20} > {10, 20})  # False

# b. Set 1 > = set 2 - determines whether set 2 is a subset of set 1
print({10, 20, 30} >= {10, 20})  # True
print({10, 20} >= {10, 20})  # True
```
2. Union

```set1 = {1, 2, 3, 4, 5, 6}
set2 = {4, 5, 6, 7, 8}
print(set1 | set2)  # {1, 2, 3, 4, 5, 6, 7, 8}
```
3. intersection

```set1 = {1, 2, 3, 4, 5, 6}
set2 = {4, 5, 6, 7, 8}
print(set1 & set2)  # {4, 5, 6}
```
4. Difference set

```print(set1 - set2)  # {1, 2, 3}
print(set2 - set1)  # {8, 7}
```
5. Symmetric difference set

```print(set1 ^ set2)  # {1, 2, 3, 7, 8}
```

#### Strings and characters

1. String is a container data type; Take 'or' 'or' 'as the container flag, which has multiple characters

Immutable (addition, deletion and modification are not supported); Orderly (subscript operation is supported)

Element of string: each basic unit and character of string (any symbol can be used)

```# character string
str1 = 'abc'
str2 = "abc123"
str3 = '''abc123'''
str4 = """abc123"""

# The string led by '' '' and '' '' can be typed directly to wrap the line
str5 = '''
hello
123
jdsk
'''
```
1. character

python has no character corresponding type, only the concept of character

1) Ordinary characters: include all symbols, such as numbers, letters, Chinese and special symbols

2) Escape character: a character with special functions after being combined with \ is an escape character

• \n - line feed

• \t - a horizontal tab

• \'- a single quotation mark

• \"- a double quotation mark

• \- a backslash character

Note: the length of any escape character is 1

```# Ordinary character
str1 = '123'
str2 = 'ahsj'
str3 = '#›@@*'

# Escape character
str1 = '21\nabc'
print(str1)

str2 = '12\'k'
print(str2)

str5 = '\t123abc'
print(str5)

str6 = '123\u4effsd'
print(str6)
```

1. Prevent escape

Adding r/R to the front of the character in python can make the function of all escape characters in this string disappear

```str7 = '\tabc\n123\\aaa'
print(str7)
str8 = R'\tabc\n123\\aaa'
print(str8, len(str8))  # \tabc\n123\\aaa 15
```

Computer character storage

When a computer stores data, it can only directly store digital data in binary form

1. Character encoding

In order to enable the computer to store characters, we associate each character with a specific number, and store the number corresponding to the character when it is necessary to store characters; The number corresponding to each character is the coded value of the character.

2. Coding table

The table that saves the one-to-one correspondence between characters and numbers is the character coding table. The common character coding tables are ASCII and Unicode coding tables

1. ASCII: one byte is used to encode 128 characters, including common English symbols, numbers and letters. Other numbers are coded in front of letters, uppercase letters are coded in front of lowercase letters, and uppercase and lowercase letters are discontinuous
2. Unicode code table: it is called universal code. The first 128 characters are ASCII characters. Chinese code range: 4e00 ~ 9fa5
3. Coded characters: \ u four digit hexadecimal number (four digit hexadecimal number is the coded value of letters)

```str1 = 'a\u0061'
print(str1)

str2 = '\u4e00\u4e01\u4e05'
print(str2)
```
4. chr function

```# Chr (coded value) - get the character corresponding to the coded value
print(chr(97))  # a

# Exercise: print all Chinese characters
for i in range(0x4e00, 0x4e05):
print(chr(i))
```
5. ord function

```# Ord (character) - gets the encoded value of the character
print(ord('Herding'), ord('odd'))
print(hex(ord('Herding')), hex(ord('odd')))
```

#### String related operations

1. Get string

String gets characters in the same way as list gets elements

​ 1) Get single character

```str1 = 'Life is short, I use it python!'
print(str1[5])

str2 = 'how are\n you!'
print(str2[-4],str2[9])  # y y
```

​ 2) Slice

```message = 'Once there was a sincere love in front of me, which I didn't cherish...'
print(message[1:])  # After having a sincere love in front of me, I didn't cherish it
print(message[2:10:3])  # Have true love
print(message[-10: -2: -1])  # ''
```

​ 3) Traversal

```message2 = 'You think I think that's what you think'
for x in message2:
print(x)

message3 = 'you jump, i jump!'
for x, y in enumerate(message3):
print(x, y)

# Exercise 1: print the coded value of each character in message3
print("===Exercise 1===")
for x in message3:
print(ord(x))

# Exercise 2: count the number of Chinese characters in message4
print("===Exercise 2===")
message4 = 'how are you? I'm fine! what about you?'
count = 0
for x in message4:
if 0x4e00 <= ord(x) <= 0x9fa5:
count += 1
print(count)
```

String 1 + string 2 - combines two strings to produce a new string

```str1 = 'abc' + '123'
print(str1)
```

String * N / N * string - produces n identical strings

```str2 = 'abc' * 3
print(str2)  # abcabcabc
```

3. Comparison operation: >, < > =<=

The two strings compare the size of the encoded value of the first pair of unequal characters
Judge whether a character is lowercase: 'a' < = x '< =' z '
Judge whether A character is A capital letter: 'A' < = x < = 'Z'
Judge whether a character is a letter: 'a' < = x < = 'Z' or 'a' < = x < = 'Z'
Judge whether a character is a numeric character: '0' < = x < = '9'
Judge whether a character is Chinese: '\ u4e00' < = x < = '\ u9fa5'

```print('abc' > 'ABC123')  # True
print('You 123' > 'a123')  # True

# Exercise 3: extract all lowercase letters from the string
print('===Exercise 3===')
str3 = 'ho3w2a however ndHwMPhy key'
new_str = ''
for i in str3:
if 'a' <= i <= 'z':
new_str += i
print(new_str)

# Exercise 4: convert all lowercase letters in a string to uppercase letters
print('===Exercise 4===')
new_str = ''
for i in str3:
if 'a' <= i <= 'z':
new_str += chr(ord(i) - 32)
else:
new_str += i
print(new_str)
```
1. In and not in

```character in character string - Determines whether there are specified characters in the string
String 1 in String 2 - Judge whether string 2 contains string 1

print('a' in 'abc')  # True
print('how' in 'how are you!')  # True
print('how' in 'habco12w')  # False
```

Tags: Python string

Posted by dave_2008 on Wed, 25 May 2022 21:05:39 +0300