0802 study, python advanced built-in method of numeric types and string types built-in method

Learn finishing 0802

Outline basic computer knowledge base of ~ Python

A few days before school finished content python fundamental part of the foundation - computer, gathering information on the first outline of the knowledge base to learn today

Computer Basics of programming

What is the programming language

What is programming

Why Programming

Composed of five computer

cpu

Controller

Operator

RAM

External memory

input device

Output Device

IO device

Application process starts

Multi-core CPU

32 and 64

Mechanical hard disk works

SSD

The basic computer operating system

What is the file

What is the application procedure

The role of the operating system

The three main components of a computer system

The operating system boot process

Classification of programming languages

Machine language

Assembly language

High-level language

Interpreted language

Compiled language

Network bottleneck effect

python basis

Python implementation of the program in two ways

Interactive

Command line

variable

What is variable

Compositional variables

Naming the variable name

Both formats defined variable names

constant

What is constant

Define constants way

python variable memory management

Reference count

python garbage collection

Small integer pool

Fancy assignment

Chain assignment

Cross-type assignment

Note

Single-line comments

Multi-line comments

Basic data types

Digital Type

String type

List Type

Dictionary Type

Boolean

unzip

User interaction with python

Three ways formatted output

Placeholder

format format

f-String formatting

Basic operators

Arithmetic Operators

Comparison Operators

Logical Operators

Assignment Operators

Identity operator

Operator Precedence

determining if the flow control

Single-branch structure

Two-branch structure

Multi-branch structure

if nested

The while loop flow control

while+break

while+continue

while+else

while nesting

The for loop flow control

for+break

for+continue

for+else

for nesting

Ordered or unordered

Variable or invariable

The outline also write a play so many characters, followed by consolidation of new knowledge today, start today to learn advanced knowledge python, and more burning brain, ha ha

A data type built-in method

Summary template:

  • effect

  • Defined way

  • Built-in method

  • A plurality of stored value or values

  • Ordered or disordered (order: that is indexed and disorderly: no index)

  • Variable or non-variable (Key)

    Variable: Variable value change id unchanged

    Immutable: variable id variable value becomes

    id constant value of the variable, i.e., modified on the basis of the original value, for the variable data type; id value becomes variable, i.e., to re-apply a new value into the space, compared with immutable data types.

1.1 numeric type built-in method

1.1.1 Integer

  • Role: that age, number, grade, etc.

  • Is defined by: may be used int (), the purely digital string to decimal integer

    x = 10
    x = int('10')
    x = int(10.1)
    print(x)         # 10
    x = int('10.1')  # 报错,int()里10.1本来就是字符串了,不需要加引号
    print(x)
  • Built: There is no built-in method, only arithmetic + - * / and compare operations> <> = <=

  • A plurality of stored value or values: a stored value

  • Ordered or disordered: Integer only one value, this argument is not

  • Variable or non-variable: immutable

    x=10
    print(id(x))   # id为1925279488
    x=11
    print(id(x))   # id为1925279520

    Variable id value becomes, the int immutable

1.1.2 Float

  • Role: to represent salary, height, weight, etc.

  • It is defined by: can float () method of the string to a purely digital floating-point number

    x = float('111')
    print(x)        #  输出为111.0
    print(type(x))  #  <class 'float'>
  • Built: There is no built-in method, only arithmetic + - * / and compare operations> <> = <=

  • A plurality of stored value or values: one

  • Orderly or disorderly: not this statement

  • Variable or non-variable (Key): immutable

Numeric types are not variable

1.2 string type built-in method

  • Role: description of the nature of things, such as the person's name, a single loving, address, country, etc.

  • Defined: Use '', '', '' '' '' "" "" "" wrapped string of characters

    s = 'sdfklsdjfk'
    s = b'sdkfljl' # 打印出来的bytes类型,二进制类型,010101010100110100  # 有这种二进制的定义方式,其实一点用都没有
    print(s)
    
    # \n 换行
    s  = 'a\na'  # 碰到斜杠了,计算机就清楚下一个字符和\拼接在一起会有特殊的意义
    print(s)  
    
    # \t 缩进4个空格
    s = 'a\t\ta'
    print(s)   # 输出结果为a    a
    
    # \r 回退上一个打印结果,覆盖上一个打印结果
    print('\ra',end='')   # 
    print('\\ra',end='')  # 加一个\让后面的\变得无意义
    
    
    # s = 'a\\na'
    # print(s)
    # s = r'\ra\t\na'  # raw
    # print(s)
  • Built-in method:

s='nick handsome'
# 一、优先掌握:
# 1、按索引取值(只可取,不可改变)
print(s[1])  # 输出为i

# 2、切片(顾头不顾尾,步长)
# 索引切片
msg = 'hello nick'
#      0123456789  # 索引序号

print(f'切片3-最后: {msg[3:]}')  # 从索引3的位置切片到最后一位,输出为lo nick
print(f'切片3-8: {msg[3:8]}')    # 输出为lo ni
print(f'切片3-8,步长为2: {msg[3:8:2]}')  # 输出为l i
print(f'切片3-最后,步长为2: {msg[3::2]}')  # 输出为l ik

# 了解,步长为正从左到右;步长为负从右到左
print('\n**了解知识点**')             # **了解知识点**
print(f'切片所有: {msg[:]}')          # 切片所有: hello nick
print(f'反转所有: {msg[::-1]}')       # 反转所有: kcin olleh
print(f'切片-5--2: {msg[-5:-2:1]}')   # 切片-5--2:  ni
print(f'切片-2--5: {msg[-2:-5:-1]}')  # 切片-2--5: cin

# 3、for循环
s = 'nick handsome'
for i in s:
    print(i)  # 逐行打印s中的每个字符
   
# 4、strip()  移除空白
s1 = '      nick handsome         '
print(s1.strip())  # 去两端的空白

s2 = '***!!!!!nick handsome----***'
print(s2.strip('-*!'))  # 指定多个字符一起去掉,只要strip里面有的字符就全部干掉
print(s2.strip('nick'))  # 指定多个字符一起去掉,只要strip里面有的字符就全部干掉
# s2.strip('*-!')  # 首先判断字符串s的两端字符,为*,再去strip里找有没有*,有就去掉,再去判断字符串s的两端字符,!-,再从strip里面找,有去掉,没有停止去掉

# 5、split() 切割
s2 = '***!!!!!nick handsome----***'
print(s2.split())  # 默认以空格切割字符串
print(s2.split('!'))  # 以!切割
print(s2.split('!', 2))  # 以!切割,最大切割次数为2,也就是从找到第一个!开始切割,找到第二个!切割后就结束切割

# 6、in 或 not in  返回结果就是True或False
s2 = '***!!!!!nick handsome----***'
print('*' in s2)  # True
print('$' not in s2)  # True

# 7. 长度len
s2 = 'nick handsome'
print(len(s2))  # 求字符串的长度


# 二、需要掌握:
# 1. lstrip() 和 rstrip()  左移除和右移除
s2 = '***!!!!!nick handsome----***'
print(s2.lstrip('*'))  # !!!!!nick handsome----***
print(s2.rstrip('*'))  # ***!!!!!nick handsome----

# 2. rsplit() 从右开始切割
info = 'nick:male:19'
print(f"info.rsplit(':', 1): {info.rsplit(':', 1)}")  # 从右开始切割,1表示只切割一次,输出为info.rsplit(':', 1): ['nick:male', '19']

# lower&upper   把大写变小写和把小写变大写
s3 = 'aaabbJ'
print(s3.lower())  # 把字符串中所有大写变成小写,aaabbj
print(s3.upper())  # 把字符串中所有小写变成大写,AAABBJ

# startswith&endswith  以什么开头和以什么结尾,返回结果为True或False
s3 = 'aaabbJ'
print(s3.startswith('b'))  # False
print(s3.endswith('J'))    # True


# join(用的比较多)一般和split联用
s3 = ' '
print(s3.join(['234', '234', '234']))  # 以s3也就是以空格为间隔符,拼接列表里的每一个元素,输出为234 234 234

s = '辣条/薯片/汽水/泡面/火腿肠/枸杞/当归/鹿茸'
s1 = s.split('/')  # 先把s以/切分
print(s1)   #  ['辣条', '薯片', '汽水', '泡面', '火腿肠', '枸杞', '当归', '鹿茸']
print('*'.join(s1))  # 再把切分后的每一个元素间用*连接
# 输出结果为:辣条*薯片*汽水*泡面*火腿肠*枸杞*当归*鹿茸


# replace  替换
s2 = 'yongjiu handsome'
print(s2.replace('yongjiu', 'gebilaowang')) # 用gebilaowang替换掉yongjiu


## isdigit(纯数字)/isalpha(纯字母)  返回结果为True或False
s2 = '12312'
print(s2.isdigit())   # True

s3 = 'aaac1c'
print(s3.isalpha())  # False


# 3、做了解即可
# find|rfind|index|rindex|count    一般常用find
s2 = '**23423***ni234234ck $$ hand223423some******'
print(s2.find('$'))  # 从左找,找到第一个停止,找不到返回-1
print(s2.rfind('$'))  # 从右找,找到就停止,找不到返回-1

print(s2.index('$'))  # 找不到报错
print(s2.rindex('$'))  # 找不到报错
  • A plurality of stored value or values: one
  • Ordered or disordered: orderly, as long as there is an index, are ordered, so the string is ordered
  • Variable or non-variable (Key): immutable

Built-in string methods much Hutchison multi-purpose, multi-play exercises the code, in order to skillfully use

Guess you like

Origin www.cnblogs.com/zhuangyl23/p/11291584.html