Python study notes third (Chapter 3) basic data type operations

Course Contents:

Operation of basic data types: operation of numbers, operation of strings, operation of Boolean data, judgment of data types

Focus:

Two, string processing operations

(1) Store a string sentence as a string list s.split()

s = "I like British very much"
s1 = s.split(" ")

['I', 'like', 'British', 'very', 'much']

type(s1)
list

(2) Delete the spaces on the left of a grid of strings, delete the spaces on the right of a grid of strings, and delete the spaces on both sides of a grid of strings s.strip(), s.lstrip(), s.rstrip()

s = "    USA British English     "
s1 = s.strip(" ")
s2 = s.lstrip(" ")
s3 = s.rstrip(" ")

s1
'USA British English'
s2
'USA British English     '
s3
'    USA British English'

(3) Count the number of certain letters, for example, how many times "an" appears in the string "sadhfjl an sdiedhan adskj", s.count('apple') , count how many'apple 's in the string s word. How to count the repeated words in a list

s = 'apple is a fruit , apple music'
print(s.count('apple'))

运行结果:
2

(4) The character is converted into unicode code, ord() function, such as'l' is converted into its corresponding unicode code (a number),

print(ord('特'))
print(ord('斯'))
print(ord('拉'))

运行结果

29305
26031
25289

(5) Unicode code , a code that can be recognized by the computer, is converted into a character chr, using the chr() function,

print(chr(666))
print(chr(777))
print(chr(888))

运行结果

ʚ
̉
͸

(6) Aggregate strings, such as'abcde' and ['a','b','c','d','e'.'f'], generally strings can be aggregated

【1】
e = "12345"
r = '*'.join(e)
r

运行结果:
'1*2*3*4*5'

【2】
r = 12345
e = '@'.join(r)
print(e)

运行结果:
TypeError                                 Traceback (most recent call last)
<ipython-input-35-754abb58bd0e> in <module>
      1 r = 12345
----> 2 e = '@'.join(r)
      3 print(e)

TypeError: can only join an iterable

【3】
e = 'abcdef'
w = '*'.join(e)
w
运行结果:
'a*b*c*d*e*f'

(7) String replacement s.replace("python",'py'): replace the character python in the string s with py

s = 'Python is a good langguage'
s1 = s.replace('Python','py')
print(s1)

py is a good langguage

print(s)
Python is a good langguage

(8) Make all the letters of the string into lowercase and make the string all uppercase. Use the s.title() function to capitalize the first letter

s = 'QweQwr'
s1 = s.lower()
s2 = s.upper()
print(s1,s2)

qweqwr QWEQWR

Three, Boolean data operation

(1) all() function, any() function

All() function, all elements are non-zero, the answer is non-zero;

any() function, one element is non-zero, the answer is non-zero

s =all([True,True,False,True])
r =any([False,False,False,True,False])
print(s)
print(r)

False
True

(2) How to act as a mask

import numpy as np
r = np.array([[11,22,33,44]])
print(r>22)

运行结果:
[[False False  True  True]]


代码
r[r>22]
运行结果:
array([33, 44])

Four, type discrimination and type conversion

(1) Determine whether a variable is a function of a certain class isinstance() 

s = 123
r = 'abc'

print(isinstance(r,int))
print(isinstance(r,object))
print(isinstance(s,int))

运行结果:
False
True
True

(2) The detection method of the string, whether there are only numbers, whether there are only letters, whether there are only numbers and letters. The answer is True, False

s.isdigit()

s.isalpha ()

s,isalnum()

(3) Type conversion str(), int(), float(), eval()

Experiments show that only string numbers can use these functions

summary

(1) This chapter mainly introduces the usage of some functions, which can be summarized into four categories, namely functions for processing numbers, functions for processing strings, functions for processing Boolean types, functions for judging variable types and converting variable types;

(2) The functions for processing strings are: divide a sentence (string) into words according to spaces and store them in the list s.split(" ") function; remove the strings on the left, right, and left of a string Function lstrip(), rstrip(), strip() function; function to determine whether a subset is in the complete set if'an' in'adsd an falfjan dhahankd' function; function to count the number of a subset in a string .count('XX'); A function s.replace("python", "py") that replaces all the characters'python' in a string s with'py'; capitalizes all letters and all Lowercase letters, uppercase first letters s.upper(), s.lower(), s.title().; Determine whether a string is all letters, all numbers, all numbers and letters s.isalpha(), s.isdigist(), s.alnum() function; judge whether a variable is an instance of the class isinstance(s, int)

Functions for processing Boolean data: any ([X1, X2, X3, X4]) if one is correct, the final answer is correct, all ([X1, X2, X3, X4]) if all elements are correct, the final answer is correct; an array X, X> 22, then X[ X> 22] will get a new array, all elements in the array are greater than 22; 

Guess you like

Origin blog.csdn.net/dqefd2e4f1/article/details/112922071