Keywords: Python

# integer

## Definition mode

`age = 18    # age = int(18)`

## Common methods: mathematical calculation, base conversion

```print(bin(100))     # Decimal to binary
print(oct(100))     # Decimal to octal
print(int('1100100', 2))    # Decimal conversion of any type, int('Decimal number to convert',type)```

# float

## Definition method:

`height = 1.0    # height = float(1.0)`

# Character string

## Definition method:

```# 1,Single quotation mark
s1 = 'Elephant'
# 2,Double quotation marks
s2 = "Elephant 2"
# 3,Three single quotation marks/Double quotation marks
s3 = '''Elephant 3'''
s4 = """Elephant 4"""
print(s1)
print(s2)
print(s3)
print(s4)
# There is no difference among the above three methods, but they cannot be mixed
# It can be nested, such as inner single outer double, inner double outer single
s5 = 'I like it"Elephant"'
print(s5)
# String preceded by a lowercase r，For escape, escape as string
s6 = r"I like it'Elephant'1.234"
print(type(s6))

//Print results:
//Elephant
//Elephant 2
//Elephant 3
//Elephant 4
//I like it"Elephant"
<class 'str'>```

# Priority in knowledge***

## 1. Index value (forward fetching and reverse fetching), which can only be fetched but not saved

```s1 = 'hello world'
print(s1)    # Forward letter e
print(s1[-10])  # Reverse letter e

//Print results:
e
e```

## 2. Index slice: intercepts a small segment of string

```s1 = 'hello world'
print(s1[2:5])
print(s1[4:])
print(s1[:5])
print(s1[0:-2:2])
print(s1[::-1])

//Print results:
llo
o world
hello
hlowr
dlrow olleh```

## 3. Member operation: in, not in

```s1 = 'hello world'
print('o'in s1)
print('o' not in s1)

//Print results:
True
False```

## input no matter what type is received, string must be returned

```a1 = '******kang******'     # Just want to take out. kang
print(a1.strip("*"))
# strip()The symbols in can be customized

//Print results:
kang```

## Function: to segment a string, you can specify the separator of segmentation, and return a list

```a1 = 'kang|18|male'
print(a1.split('|'))

//Print results:
['kang', '18', 'male']```

## 6. len: get the number of elements in the current data

```a = 'hello'
print(len(a))
for i in a:
print(i)

//Print results:
5
h
e
l
l
o```

## lestrip: remove spaces / characters on the left

```a1 = '*******kang*******'
print(a1.rstrip('*'))
print(a1.lstrip('*'))

//Print results:
*******kang
kang*******```

## upper: change all characters to uppercase

```a1 = 'Kang'
print(a1.lower())
print(a1.upper())

//Print results:
kang
KANG```

## 9. Startswitch / endswitch: judge whether the current string is... Start with, or with.. At the end, it must be a Boolean value

```s1 = 'Hello world'
print(s1.startswith("He"))
print(s1.endswith('ld'))

//Print results:
True
True```

## 10,.format/f-string

```#Your name is: kang, your age is: 18
name = 'kang'
age = 21
print("your name is: {name}, your age is: {age}, gender is: {gender}".format(age=age, name=name, gender='male '))
# f-string:
#To receive variables through braces, you must add a lowercase f,,, before the string, which will be available only after Python 3.6

Print results:

## 11,split，rsplit

```s1 = 'name,age,gender'
print(s1.split(' , ', 1))   # Number of segmentation can be specified

//Print results:
['name,age,gender']```

## 12. join: splices the iteratable objects (each element in the l1 list is spliced according to the separator in the previous string)

```l1 = ['kang', '21', 'male']
print('|'.join(l1))

//Print results:
kang|21|male```

## 13. Replace: replace the elements in the string. The old value of the parameter is first followed by the new value

```s1 = 'kang,21'
print(s1.replace('kang', 'zhao'))

//Print results:
zhao,21```

## 14. isdigit: judge whether the data in the current string is a number (can judge bytes,unicode)

```score = input('Please enter a score:')
if score.isdigit():
score = int(score)
if score >= 90:
print('excellent')
else:
print('you tmd Can you lose well')```

# Knowledge points learned

## find / rfind / index / rindex / count

```# find: Find the location of an element in the current string, return the index, and return if not found-1
s1 = 'Did you eat today?'
print(s1.find("？", ))
# index  # Find the location of an element in the current string, return index, no return exception found
print(s1.index("？"))
# count  # Count the number of an element in the current string
print(s1.count("eat"))

//Print results:
8
8
2```

## center \ ljust \ rjust \ zfill

```print("Welcome".center(8,"-"))
print("Welcome".ljust(30,"-"))
print("Welcome".rjust(30,"-"))
print("Welcome".zfill(50))
s1 = """
kang\t18\tmale\t
zhao\t18\tfemale\t
"""
print(s1.expandtabs())
print(s1.expandtabs(20))

//Print results:
--Welcome--
//Welcome--------------------------
--------------------------Welcome
0000000000000000000000000000000000000000000000 Welcome

kang    18      male
zhao    18      female

kang                18                  male
zhao                18                  female  ```

## is series

```a = b'10'
b = '10'
c = 'Ten'
d = 'IV'
print(type(a))
print(b.isnumeric())
print(c.isnumeric())
print(d.isnumeric())
# isdigit: unicode,bytes
print(a.isdigit())
print(b.isdigit())

//Print results:
<class 'bytes'>
True
True
False
True
True```

# list

## 1. Index value (both positive and negative can be obtained, not only desirable, but also saved)

```l1 = [1, 2, 3, 4, 5, 6, 7]
print(l1)

Print results:
3```

## 2. Index slice

```l1 = [1, 2, 3, 4, 5, 6, 7]
print(l1[2:5])
print(l1[4:])
print(l1[:5])
print(l1[0:-2:2])
print(l1[::-1])

//Print results:
[3, 4, 5]
[5, 6, 7]
[1, 2, 3, 4, 5]
[1, 3, 5]
[7, 6, 5, 4, 3, 2, 1]```

## 3. Append: append value. It can only be added to the last part of the list, and only one value can be added at a time***

```l1 = [1, 2, 3, 4, 5, 6, 7]
l1.append(1000)
print(l1)

Print results:
[1, 2, 3, 4, 5, 6, 7, 1000]```

## 4. Insert: insert value. Specify the insertion position through index

```l1 = [1, 2, 3, 4, 5, 6, 7]
l1.insert(3, 999)
print(l1)

Print results:
[1, 2, 3, 999, 4, 5, 6, 7]```

## 5. extend: the append must be an iterative object. At the end of the

`l1 = [1, 2, 3, 4, 5, 6, 7]`
```l1.extend([8, 9])
print(l1)

Print results:
[1, 2, 3, 4, 5, 6, 7, 8, 9]```

## 6. remove: delete the specified value thoroughly

```l1 = [1, 2, 3, 4, 5, 6, 7]
l1.remove(1)
print(l1)

Print results:
[2, 3, 4, 5, 6, 7]```

## 7. pop: don't transfer value. Delete from the last by default. Specify index delete. pop has return value

```l1 = [1, 2, 3, 4, 5, 6, 7]
val = l1.pop(2)
print(l1)
print(val)

//Print results:
[1, 2, 4, 5, 6, 7]
3```

Posted by mjlogan on Tue, 05 Nov 2019 04:44:02 -0800