Day05 common data type built-in method

 

 

1. Digital Type

(1) .int int

Uses: integer number of data, such as: age, number, etc.

Defined method:

= 18 is A   # is essentially int = Age (18 is) 
Print (A) 
B = int ( ' 18 is ' )
 Print (B) 
C = int ( ' 1.8 ' )
 Print (C)

Note that int only integer numbers Strings of digits or a integer to integer data, floating point data can not be converted

 

Decimal conversion

Print (bin (12 is))   # decimal converted to binary 
Print (int ( ' 1100 is ' , 2))   # Binary converted to decimal 
Print (OCT (12 is))   # decimal conversion octal 
Print (int ( ' 14 ' ,. 8) )   # octal conversion to decimal 
Print (hex (12 is))   # decimal converted to hexadecimal 
Print (int ( ' C ' , 16))   # hexadecimal to decimal conversion

Note: 0b is representative of a binary, octal behalf of 0o, 0x represents hexadecimal

The type of summary:

Save a value

Only one value, say there is no order-disorder

Immutable

 

(2) .float float

Purpose: definition of decimal data type, such as height, weight, salary, etc.

Defined method:

= 1.8 A   # is essentially a float = A (1.8) 
Print (A) 
B = a float ( ' 1.8 ' )   # can be converted character string data stored fractional 
Print (B)

Note: float data type can be converted to decimal string stored

 

2. String type

str string type

Action: storing descriptive data

Defined manner: single quotes '', double quotation marks "" and more quotes '' '' '' "" ""

a = 'sxc'  # 本质是 a = str('sxc')
print(a)
b = str([1,2,3,4])
print(b)
c = str({'name':'sxc','age': 18})
print(c)

注意:字符串可以把列表,字典类型转化为字符串类型

 

优先掌握的操作

1、按索引取值(正向取+反向取) :只能取

a = 'hello world'
print(a[1])  # 正向取
print(a[-1])  # 反向取
a[1] = 'x'  # 报错

 

 2、切片(顾头不顾尾,步长)

a = 'hello world'
print(a[0:8])
print(a[0:10:2])  # 步长为2,即隔一个取一个
print(a)  # 没有改变原值
print(a[8:0:-2])  # 可以反向取值
print(a[5::2])  # 空代表取到最后一个值

 

3、长度len

a = 'abcdefg'
print(len(a))

 

4、成员运算in和not in: 判断一个子字符串是否存在于一个大的字符串中

a ='I have  a apple , i have a pen ,boom , apple pen'
print('apple' in a)
print('ap' in a)
print('sss' in a)
print('sss' not in a)

 注意:结果是一个布尔类型的值

 

 5、去掉字符串左右两边的字符strip,不管中间的(lstrip,rstrip)

user = '              sxc               '
print(user)
print(user.strip(' '))
user1 = '********s*x*c*********'
print(user1.strip('*'))  # 需要注意strip只能去除左右两边连续的字符,不能去除中间的字符
print(user1.lstrip('*'))  # 去掉左边的*
print(user1.rstrip('*'))  # 去掉右边的*
username = input('请输入用户名>>>:').strip(' ')
print(username)

注意:strip只能去除左右两边连续的字符,不能去除中间的字符

 

6、切分split:针对按照某种分隔符组织的字符串,可以用split将其切分成列表,进而进行取值

a = '123/456/789'
print(a.split('/'))  # 切分出来的是一个列表
print(a.split('/')[1])  # 可以用列表的取值方式取值
print(a.split('/',1))  # 只去掉第一个分隔符
print(a.rsplit('/',1))  # 从右往左去掉分隔符
first_pwd,second_pwd,third_pwd = a.split('/')  # 可以用变量一一对应列表中的值
print(first_pwd)
print(second_pwd)
print(third_pwd)

注意:切分出来的是列表,可以用列表的一切方法操作

 

7、循环

a = 'hello world'
for i in a:
    print(i)

 

 需要掌握的操作

1、lower,upper(全部改为大小写的操作)

a ='aBcDeFg'
print(a.lower())
print(a)  # 原值不变
print(a.upper())

 

2、startswith,endswith判断字符在不在开头或者结尾

a = 'hello world'
print(a.startswith('h'))
print(a.startswith('xxx'))
print(a.startswith('hello world'))  # 只要是开头,可以是多个字符
print(a.endswith('d'))
print(a.endswith('world'))

 

3、format的三种玩法(Python推荐使用)

# 第一种,按位置占位,原理跟%s一样
a = 'my name is %s,my age is %s,my hobby is %s.'%('sxc',18,'sing')  # 不能增加超过占位符数量的值
print(a)
a = 'my name is {},my age is {},my hobby is {}.'.format('sxc',18,'sing','jump')  # 可以增加超过占位数量的值,但是只会选取优先对应的值
print(a)
# 第二种,按索引占位
a = 'my name is {2},my age is {0},my hobby is {3}.'.format('sxc',18,'sing','jump')
print(a)
# 第三种,指名道姓占位,按照关键字传参数(关键字传参)
a = 'my name is {name},my age is {age},my hobby is {hobby}.'.format(name ='sxc',age = 18,hobby = 'sing')
print(a)

 

4、replace,替代
a = 'my name is sxc ,sxc is a good boy'
print(a.replace('sxc','sss'))
print(a.replace('sxc','xxx',1))
print(a)  # 不改变原值

 

5、isdigit # 判断字符串中包含的是否为纯数字

while True:
    age = input('请输入你的年龄>>>:')
    if age.isdigit():
        age = int(age)
        if age <= 30:
            print('美女你好')
        else:
            print('阿姨好')
    else:
        print('请输入数字')

判断输入的年龄是不是整数类型

 

需要了解的内置方法

1、find,rfind,index,rindex,count

a = '1,2,3,sxc,xxx,$'
print(a.find('sxc'))  # 查找sxc在字符串中的索引
print(a.find('sxc',1,10))  # 查找sxc在索引为0-9中的位置
print(a.find('sxc',1,3))  # 查找sxc在索引为1-2中的位置,找不到时返回-1,不会报错
print(a.rfind('$',10,15))  # 从右向左查找$在索引为10-13中的位置

print(a.index('xxx'))  # 查找xxx在字符串中的索引
print(a.index('$',1,15))  # 查找$在索引为1-14中的位置
# print(a.index('xxx',1,3))  # 查找xxx在索引为1-2中的位置,找不到时会报错
print(a.rindex('xxx',8,14))  # 从右想做查找xxx在索引为8-13中的位置

print(a.count('x'))  # 统计x在字符串中的个数
print(a.count('x',0,9))  # 统计x在字符串索引为0-8中的个数

 

2、center,ljust,rjust,zfill

print('sxc'.center(10,'$'))  # 以sxc为中心,两边分别补$,直至总共10位为止
print('sxc'.ljust(10,''))  # 以sxc为最左边,后边补★,直至总共10位为止
print('sxc'.rjust(10,''))  # 以sxc为最右边,前面补☆,直至总共10位为止
print('158655'.zfill(10))  # 将158655补成10位数字,不足用0填充

 

3、expandtabs

print('a\tb'.expandtabs(5))  # 空5格

 

4、captalize,swapcase,title

print('heLLo WoRlD'.capitalize())  # 第一个字母大写,其他全部小写
print('heLLo WoRlD'.swapcase())  # 大小写转换
print('heLLo WoRlD'.title())  # 每个单词的首字母大写,其余小写

 

5、is数字系列,isnumeric(),isdecimal(),isdigit()

num1=b'4' #bytes
num2=u'4' #unicode,python3中无需加u就是unicode
num3='' #中文数字
num4='' #罗马数字

# isnumeric(): 只能判断unicode,中文数字,罗马数字
print(num2.isnumeric())
print(num3.isnumeric())
print(num4.isnumeric())

# isdecimal(): 只能判断unicode
print(num2.isdecimal())
print(num3.isdecimal())
print(num4.isdecimal())

# isdigit() : 只能判断bytes,unicode
print(num1.isdigit())
print(num2.isdigit())
print(num3.isdigit())
print(num4.isdigit())

 

6、is其他

print('zzj 123'.isalpha())  # 是否只存在字母,空格也不行
print('zzjsxc'.isalpha())  # 是否只存在字母

print('1356sss唱'.isalnum())  # 是否是字母,数字或者中文字符
print('1356sss唱*'.isalnum())  # 符号不行

 

3 列表类型

list列表类型

作用:存储多个数据,可以按照索引取值

定义方式:在[]内用逗号分开各个值

优先掌握的操作

1、按索引存取值(正向存取+反向存取):即可存也可以取

a = [1,2,3,4,5,6]
print(a)
print(a[1])
a[1] = 99  # 修改列表中的值
print(a)
print(a[-1])  # 反向取值

 

2、切片(顾头不顾尾,步长)

a = [1,2,3,4,5,6]
print(a[0:6:2])  # 在索引为0-5中,步长为2取值,即隔一个取一个
print(a[::1])  # 都为空时全部都取
print(a[6:0:-1])  # 反向取

注意:顾头不顾尾

 

3、长度len

a = [1,2,3,4,5,6]
print(len(a))

 

4、成员运算in和not in

a = [1,222,3,4,'*','sxc']
print(1 in a)
print('*' in a)
print('sxc' not in a)
print('xxx' not in a)

 

5、往列表中添加元素(******)

(1)追加
a = [11,22,33,44,55]
print(a)
a.append(66)
a.append(77)
print(a)


(2)任意位置添加元素
a = [11,22,33,44,55]
print(a)
a.insert(2,999)
print(a)

 

(3)一次性添加多个元素
a = [11,22,33,44,55]
b = [66,77,88,99]
print('a列表是:',a)
print('b列表是:',b)
a.extend(b)  # 本质是for循环b列表中的值,并一个个添加到a列表中
print('ab列表合起来:',a)  # 新列表还是a列表

 

6、删除

方式1:del单纯的删除
a = [11,22,33,44,55]
print(a)
del a[1]  # 通用删除,可以删除任意值
print(a)
del a  # 甚至可以删除整个列表
print(a)

因为a列表已删除,所以最后打印操作会报错

 

  方式2:remove指定要删除的值

a = [11,22,33,44,55]
print(a)
print(a.remove(55))  #删除,返回的是none
print(a)

删除操作返回的是none值

 

  方式3: pop按照索引从列表中取走一个值(默认从末尾取值)

a = [11,22,33,44,55]
print(a)
print(a.pop(3))  # 按照索引取走值,返回的是取走的值
print(a.pop(1))  # 按照索引取走值,返回的是取走的值
print(a)

pop与del和remove的不同是取走一个值,这个取走的值后面还是可以用的

 

8、循环

a = [11,22,33,44,55]
for i in a:  # 循环取值
    print(i)

循环打印列表中的每个值

Guess you like

Origin www.cnblogs.com/sxchen/p/11128525.html