python基础3(Python foundation 3)

基础数据类型初识

  • int:数字,能进行数学运算

int:数字,能进行数学运算

  • str:字符串,储存少量文字数据,能进行 +,*,切片以及其他方法

str:字符串,储存少量文字数据,能进行 +,*,切片以及其他方法

  • bool:布尔值,判断真假

bool:布尔值,判断真假

  • list:列表,储存大量不同类型的数据,可以改变

list:列表,储存大量不同类型的数据,可以改变

  • tuple:元组,储存大量不同类型的数据,一旦生成,不可变

tuple:元组,储存大量不同类型的数据,一旦生成,不可变

  • dict:字典,储存特定的类型的数据,可变

dict:字典,储存特定的类型的数据,可变

  • set:集合,储存特定的数据类型,可变的

set:集合,储存特定的数据类型,可变的

int

  • 十进制于二进制之间的转换

    十进制转二进制:

    二进制转十进制:

    b = 0010 1010
    s = 1*2**5+1*2**3+1*2**1
    s1 = 32+8+2

    八四二一,163264128

    bit_length():获取十进制数的有效二进制位数

    i = 1
    print(i.bit_length())
    # 1 0000 0001
    i = 42
    print(i.bit_length())
    #6 0010 1010

  • 十进制转二进制:
  • 二进制转十进制:

    b = 0010 1010
    s = 1*2**5+1*2**3+1*2**1
    s1 = 32+8+2

    八四二一,163264128

  • b = 0010 1010
    s = 1*2**5+1*2**3+1*2**1
    s1 = 32+8+2

  • 八四二一,163264128
  • bit_length():获取十进制数的有效二进制位数

    i = 1
    print(i.bit_length())
    # 1 0000 0001
    i = 42
    print(i.bit_length())
    #6 0010 1010

  • i = 1
    print(i.bit_length())
    # 1 0000 0001
    i = 42
    print(i.bit_length())
    #6 0010 1010

bool

  • bool <-->int

    True –> 1
    False –> 0
    非零即为真
    0 –> False

  • True –> 1
  • False –> 0
  • 非零即为真
  • 0 –> False
  • bool <---> str

    True — > ‘True’【毫无意义】
    False –> ‘False
    非空即为真
    ”空字符串 –> False

  • True — > ‘True’【毫无意义】
  • False –> ‘False
  • 非空即为真
  • ”空字符串 –> False
  • str <--> int

    str全为数字方能转为int
    int百无禁忌,都能转为str

  • str全为数字方能转为int
  • int百无禁忌,都能转为str

str

  • 索引

    索引写法:str[index]

    字符串是有序的,从左到右,以零开始。从右到左,以负1开始

    索引存在超界问题,即索引不能超过长

索引

  • 索引写法:str[index]
  • 字符串是有序的,从左到右,以零开始。从右到左,以负1开始

字符串是有序的,从左到右,以零开始。从右到左,以负1开始

  • 索引存在超界问题,即索引不能超过长

索引存在超界问题,即索引不能超过长

  • 切片

    切片写法:str[startindex:endindex:step]

    切片不存在超界问题,不管正负index

    关于倒叙切step必须是负数

    s = ‘CaiXueMing’
    print(s[-1:-100:-1])

    前包后不包:不取到endindex

切片

  • 切片写法:str[startindex:endindex:step]

切片写法:str[startindex:endindex:step]

  • 切片不存在超界问题,不管正负index
  • 关于倒叙切step必须是负数

    s = ‘CaiXueMing’
    print(s[-1:-100:-1])

  • s = ‘CaiXueMing’
    print(s[-1:-100:-1])

  • 前包后不包:不取到endindex
  • 各种操作方法:以下操作方法都不会改变原字符串,只会生成新的字符串

    str.upper():将字符串变成全大写

    str.lower():将字符串变成全小写

    str.startswith(str,startindex,endindex):判断字符串是否以特定的str开头,index可以不写,默认从字符串最左端开始,写了就按区间的最前端开始

    str.endswith(str,startindex,endindex):同上,只不过是以什么结尾

    str.strip():默认是从开头和末尾两端去除空白字符【空白字符包括回车换行空格等字符】,如果括号内填写了一些字符,就会在字符串两端一一去除匹配的字符

    str.split():默认是按空格将字符串分割,并将分割的元素放到一个列表内,如果括号内有字符,就按该字符进行分割,还可以定义分割的次数

    str.replace(‘oldstr’,’newstr’,count):将字符串内oldstr替换成newstr,可以控制替换次数

    str.join(iterable):将一个全是字符串的可迭代对象,用str拼接起来

    str.count(‘str’):统计特定字符出现的次数

    str.format():格式化输出

    is系列

    str.isalnum():判断是否为数字和字母组成

    str.isalpha():判断是否只由字母组成

    str.isdecimal():判断是否全为十进制数字组成

各种操作方法:以下操作方法都不会改变原字符串,只会生成新的字符串

  • str.upper():将字符串变成全大写

str.upper():将字符串变成全大写

  • str.lower():将字符串变成全小写

str.lower():将字符串变成全小写

  • str.startswith(str,startindex,endindex):判断字符串是否以特定的str开头,index可以不写,默认从字符串最左端开始,写了就按区间的最前端开始

str.startswith(str,startindex,endindex):判断字符串是否以特定的str开头,index可以不写,默认从字符串最左端开始,写了就按区间的最前端开始

  • str.endswith(str,startindex,endindex):同上,只不过是以什么结尾

str.endswith(str,startindex,endindex):同上,只不过是以什么结尾

  • str.strip():默认是从开头和末尾两端去除空白字符【空白字符包括回车换行空格等字符】,如果括号内填写了一些字符,就会在字符串两端一一去除匹配的字符

str.strip():默认是从开头和末尾两端去除空白字符【空白字符包括回车换行空格等字符】,如果括号内填写了一些字符,就会在字符串两端一一去除匹配的字符

  • str.split():默认是按空格将字符串分割,并将分割的元素放到一个列表内,如果括号内有字符,就按该字符进行分割,还可以定义分割的次数

str.split():默认是按空格将字符串分割,并将分割的元素放到一个列表内,如果括号内有字符,就按该字符进行分割,还可以定义分割的次数

  • str.replace(‘oldstr’,’newstr’,count):将字符串内oldstr替换成newstr,可以控制替换次数

str.replace(‘oldstr’,’newstr’,count):将字符串内oldstr替换成newstr,可以控制替换次数

  • str.join(iterable):将一个全是字符串的可迭代对象,用str拼接起来

str.join(iterable):将一个全是字符串的可迭代对象,用str拼接起来

  • str.count(‘str’):统计特定字符出现的次数

str.count(‘str’):统计特定字符出现的次数

  • str.format():格式化输出

str.format():格式化输出

  • is系列

    str.isalnum():判断是否为数字和字母组成

    str.isalpha():判断是否只由字母组成

    str.isdecimal():判断是否全为十进制数字组成

is系列

  • str.isalnum():判断是否为数字和字母组成

str.isalnum():判断是否为数字和字母组成

  • str.isalpha():判断是否只由字母组成

str.isalpha():判断是否只由字母组成

  • str.isdecimal():判断是否全为十进制数字组成

str.isdecimal():判断是否全为十进制数字组成

————————

Initial knowledge of basic data types

  • Int: number, capable of mathematical operation

Int: number, capable of mathematical operation

  • STR: string, which stores a small amount of text data, and can be used for +, *, slicing and other methods

STR: string, which stores a small amount of text data, and can be used for +, *, slicing and other methods

  • Bool: Boolean value to judge whether it is true or false

Bool: Boolean value to judge whether it is true or false

  • List: a list that stores a large number of different types of data and can be changed

List: a list that stores a large number of different types of data and can be changed

  • Tuple: tuple, which stores a large number of different types of data. Once generated, it is immutable

Tuple: tuple, which stores a large number of different types of data. Once generated, it is immutable

  • Dict: dictionary, storing specific types of data, variable

Dict: dictionary, storing specific types of data, variable

  • Set: a set that stores specific data types and is variable

Set: a set that stores specific data types and is variable

int

  • Conversion from decimal to binary
    Decimal to binary:
    Binary to decimal:
    b = 0010 1010
    s = 1*2**5+1*2**3+1*2**1
    s1 = 32+8+2
    8421, 16326418
    bit_ Length(): get the valid binary digits of decimal number
    i = 1
    print(i.bit_length())
    # 1 0000 0001
    i = 42
    print(i.bit_length())
    #6 0010 1010
  • Decimal to binary:
  • Binary to decimal:
    b = 0010 1010
    s = 1*2**5+1*2**3+1*2**1
    s1 = 32+8+2
    8421, 16326418
  • b = 0010 1010
    s = 1*2**5+1*2**3+1*2**1
    s1 = 32+8+2
  • 8421, 16326418
  • bit_length():获取十进制数的有效二进制位数

    i = 1
    print(i.bit_length())
    # 1 0000 0001
    i = 42
    print(i.bit_length())
    #6 0010 1010

  • i = 1
    print(i.bit_length())
    # 1 0000 0001
    i = 42
    print(i.bit_length())
    #6 0010 1010

bool

  • bool <-->int

    True –> 1
    False –> 0
    非零即为真
    0 –> False

  • True –> 1
  • False –> 0
  • Non zero is true
  • 0 –> False
  • bool <---> str
    True — > ‘true’ [meaningless]
    False –> ‘False
    If it is not empty, it is true
    ” empty string — > false
  • True — > ‘true’ [meaningless]
  • False –> ‘False
  • If it is not empty, it is true
  • ” empty string — > false
  • str <--> int
    STR can be converted to int only if it is all numbers
    Int can be converted to STR without taboos
  • STR can be converted to int only if it is all numbers
  • Int can be converted to STR without taboos

str

  • Indexes
    Index writing method: STR [index]
    Strings are ordered, from left to right, starting with zero. From right to left, start with minus 1
    The index is out of bounds, that is, the index cannot exceed the length

Indexes

  • 索引写法:str[index]
  • Strings are ordered, from left to right, starting with zero. From right to left, start with minus 1

Strings are ordered, from left to right, starting with zero. From right to left, start with minus 1

  • The index is out of bounds, that is, the index cannot exceed the length

The index is out of bounds, that is, the index cannot exceed the length

  • 切片

    切片写法:str[startindex:endindex:step]

    切片不存在超界问题,不管正负index

    关于倒叙切step必须是负数

    s = ‘CaiXueMing’
    print(s[-1:-100:-1])

    前包后不包:不取到endindex

section

  • 切片写法:str[startindex:endindex:step]

切片写法:str[startindex:endindex:step]

  • There is no over bound problem for slices, regardless of positive or negative index
  • About flashback, step must be negative
    s = ‘CaiXueMing’
    print(s[-1:-100:-1])
  • s = ‘CaiXueMing’
    print(s[-1:-100:-1])

  • Package before package but not after package: endindex is not obtained
  • Various operation methods: the following operation methods will not change the original string, but only generate a new string
    Str.upper(): change the string to full uppercase
    Str.lower(): change the string to lowercase
    Str.startswitch (STR, StartIndex, endindex): judge whether the string starts with a specific str. the index can not be written. By default, it starts from the leftmost end of the string. When it is written, it starts from the front end of the interval
    Str.endswitch (STR, StartIndex, endindex): same as above, but with what
    Str.strip(): the default is to remove blank characters from the beginning and end [blank characters include carriage return, line feed, space and other characters]. If some characters are filled in parentheses, the matching characters will be removed one by one at both ends of the string
    Str.split(): by default, the string is divided by space, and the divided elements are placed in a list. If there is a character in the bracket, it is divided by that character, and the number of times of division can also be defined
    Str.replace (‘oldstr ‘,’ newstr ‘, count): replace oldstr in the string with newstr, and you can control the number of replacements
    Str.join (iteratable): concatenate an iteratable object that is full of strings with str
    Str.count (‘str ‘): counts the number of occurrences of a specific character
    Str.format(): format output
    Is series
    Str.isalnum(): judge whether it is composed of numbers and letters
    Str.isalpha(): judge whether it is only composed of letters
    Str.isdecimal(): judge whether it is all composed of decimal digits

Various operation methods: the following operation methods will not change the original string, but only generate a new string

  • Str.upper(): change the string to full uppercase

Str.upper(): change the string to full uppercase

  • Str.lower(): change the string to lowercase

Str.lower(): change the string to lowercase

  • Str.startswitch (STR, StartIndex, endindex): judge whether the string starts with a specific str. the index can not be written. By default, it starts from the leftmost end of the string. When it is written, it starts from the front end of the interval

Str.startswitch (STR, StartIndex, endindex): judge whether the string starts with a specific str. the index can not be written. By default, it starts from the leftmost end of the string. When it is written, it starts from the front end of the interval

  • str.endswith(str,startindex,endindex):同上,只不过是以什么结尾

str.endswith(str,startindex,endindex):同上,只不过是以什么结尾

  • Str.strip(): the default is to remove blank characters from the beginning and end [blank characters include carriage return, line feed, space and other characters]. If some characters are filled in parentheses, the matching characters will be removed one by one at both ends of the string

Str.strip(): the default is to remove blank characters from the beginning and end [blank characters include carriage return, line feed, space and other characters]. If some characters are filled in parentheses, the matching characters will be removed one by one at both ends of the string

  • Str.split(): by default, the string is divided by space, and the divided elements are placed in a list. If there is a character in the bracket, it is divided by that character, and the number of times of division can also be defined

Str.split(): by default, the string is divided by space, and the divided elements are placed in a list. If there is a character in the bracket, it is divided by that character, and the number of times of division can also be defined

  • str.replace(‘oldstr’,’newstr’,count):将字符串内oldstr替换成newstr,可以控制替换次数

str.replace(‘oldstr’,’newstr’,count):将字符串内oldstr替换成newstr,可以控制替换次数

  • Str.join (iteratable): concatenate an iteratable object that is full of strings with str

Str.join (iteratable): concatenate an iteratable object that is full of strings with str

  • Str.count (‘str ‘): counts the number of occurrences of a specific character

Str.count (‘str ‘): counts the number of occurrences of a specific character

  • Str.format(): format output

Str.format(): format output

  • Is series
    Str.isalnum(): judge whether it is composed of numbers and letters
    Str.isalpha(): judge whether it is only composed of letters
    Str.isdecimal(): judge whether it is all composed of decimal digits

Is series

  • Str.isalnum(): judge whether it is composed of numbers and letters

Str.isalnum(): judge whether it is composed of numbers and letters

  • Str.isalpha(): judge whether it is only composed of letters

Str.isalpha(): judge whether it is only composed of letters

  • Str.isdecimal(): judge whether it is all composed of decimal digits

Str.isdecimal(): judge whether it is all composed of decimal digits