Introducción a Python - tipos de datos básicos y construido en el método

tipo int digital y flotador

definiciones

# 1、定义:
# 1.1 整型int的定义
age=10  # 本质age = int(10)

# 1.2 浮点型float的定义
salary=3000.3  # 本质salary=float(3000.3)

# 注意:名字+括号的意思就是调用某个功能,比如
# print(...)调用打印功能
# int(...)调用创建整型数据的功能
# float(...)调用创建浮点型数据的功能

tipo de conversión

res = int('100111') # 纯数字的字符串转成int
print(res,type(res))

# 了解
'''
十进制 -> 二进制
11 -> 1011
1011 -> 8+2+1
'''
# 十进制——>二进制
print(bin(11)) # 0b1011  0b指的是二进制

# 十进制——>八进制
print(oct(11)) # 0o13 0o指的是八进制

# 十进制——>十六进制
print(hex(11)) # 0xb  0x指的是十六进制

# 二进制——>十进制
print(int('0b1011', 2))

# 八进制——>十进制
print(int('0o13', 8))

# 十六进制——>十进制
print(int('0xb', 16))

# float
salary=3.1 # salary=float(3.1)
print(salary, type(salary))

uso

  • int y float no necesitan haber incorporado en el método
  • Su uso es relativamente operaciones matemáticas +

cadena

definiciones

msg = 'hello' # msg = str('hello')
print(type(msg))

tipo de conversión

# str可以把任意类型转化为字符串
res = str({'a':1})
print(res, type(res))

uso

  • operación de control de prioridad

    # 1.按索引取值(正向取+反向取):只能取
    msg = 'hello world'
    # 正向取
    print(msg[0])
    # 反向去
    print(msg[-1])
    # 只能取
    msg[0]='H' # 报错
    
    # 2.切片:索引的拓展应用,从一个大字符串中拷贝出一个子字符串
    msg = 'hello world'
    # 顾头不顾尾
    res = msg[0:5]
    print(res)
    # 步长:默认步长为1
    res = msg[0:5:2] # 0,2,4
    print(res) # hlo
    # 反向步长
    res = msg[5:0:-1]
    print(res) # " olle"
    
    res = msg[:] # res=msg[0:11]
    print(res)
    
    res = msg[::-1] # 把字符串倒过来
    print(res)
    
    # 3.长度len
    msg = 'hello world'
    print(len(msg))
    
    # 4.成员运算in和not in
    # 判断子字符串是否存在于一个大字符串中
    print('yumi' in 'yumi is superman')
    print('yumi' not in 'yumi is superman')
    print(not 'yumi' in 'yumi is superman') # 不推荐使用
    
    # 5.移除字符串左右两侧的符号strip()
    # 了解:strip只去两边字符,不去中间
    msg = '**yu*mi**'
    res = msg.strip('*') # 默认去掉左右两侧空格
    print(msg) # 不会改变原值
    print(res) # 是产生了新值
    
    # 应用
    '''
    name = input('name:').strip()
    pwd = input('password:').strip()
    if name == 'yumi' and pwd == '123':
        print('登录成功')
    else:
        print('登录失败')
    '''
    
    # 6.切分split():把一个字符串按照某种分隔符进行切分,得到一个列表
    # 默认按照空格分隔
    info = 'egon 18 male'
    res = info.split()
    print(res)
    # 指定分隔符
    info = 'egon:18:male'
    res = info.split(':')
    print(res)
    # 指定分隔次数(了解)
    info = 'egon:18:male'
    res = info.split(':',1)
    print(res) # ['egon', '18:male']
    
    # 7.循环
    info = 'egon:18:male'
    for i in info.split(':'):
        print(i)
  • Usted necesita tener una operación

    # 1.strip,lstrip,rstrip
    msg = '**yu*mi**'
    res = msg.strip('*') # yu*mi
    res = msg.lstrip('*') # yu*mi**
    res = msg.rstrip('*') # **yu*mi
    
    # 2.lower,upper
    msg = 'AbbCCC'
    print(msg.lower()) # abbccc
    print(msg.upper()) # ABBCCC
    # 3.startswith,endswith
    msg = 'yumi is me'
    print(msg.startswith('yumi')) # True
    print(msg.endswith('me')) # True
    
    # 4.format
    # 5.split,rsplit
    info = 'egon:18:male'
    print(info.split(':', 1)) # ['egon', '18:male']
    print(info.rsplit(':', 1)) # ['egon:18', 'male']
    
    # 6.join:把列表拼接成字符串
    l=['egon','18','male']
    # res=l[0]+':'+l[1]+':'+l[2]
    res = ':'.join(l) # 按照某个分隔符号,把元素全为字符串的列表拼接成一个字符串
    print(res)
    
    # 7.replace('原字符串','新字符串',替换次数)
    # 默认全部替换
    msg = 'you can you up no can no bb'
    print(msg.replace('you','You',))
    print(msg.replace('you','You',1))
    
    # 8.isdigit():判断字符串是否为纯数字
    age = input("age:")
    if age.isdigit():
        print(age)
    else:
        print('必须输入数字')
  • aprender la operación

    # 1.find,rfind,index,rindex,count
    '''
    msg='hello egon hahaha'
    # 找到返回起始索引
    print(msg.find('e')) # 返回要查找的字符串在大字符串中的起始索引
    print(msg.find('egon'))
    print(msg.index('e'))
    print(msg.index('egon'))
    # 找不到
    print(msg.find('xxx')) # 返回-1,代表找不到
    print(msg.index('xxx')) # 抛出异常
    
    msg='hello egon hahaha egon、 egon'
    print(msg.count('egon'))
    '''
    
    # 2.center,ljust,rjust,zfill
    '''
    print('egon'.center(50,'*'))
    print('egon'.ljust(50,'*'))
    print('egon'.rjust(50,'*'))
    print('egon'.zfill(10))
    '''
    
    # 3.expandtabs
    '''
    msg='hello\tworld'
    print(msg.expandtabs(2)) # 设置制表符代表的空格数为2
    '''
    
    # 4.captalize,swapcase,title
    '''
    print("hello world egon".capitalize()) # Hello world egon
    print("Hello WorLd EGon".swapcase()) # hELLO wORlD egON
    print("hello world egon".title()) # Hello World Egon
    '''
    
    # 5.is数字系列
    '''
    num1=b'4' #bytes
    num2=u'4' #unicode,python3中无需加u就是unicode
    num3='四' #中文数字
    num4='Ⅳ' #罗马数字
    
    # isdigit只能识别:num1、num2
    print(num1.isdigit()) # True
    print(num2.isdigit()) # True
    print(num3.isdigit()) # False
    print(num4.isdigit()) # False
    
    
    
    # isnumberic可以识别:num2、num3、num4
    print(num2.isnumeric()) # True
    print(num3.isnumeric()) # True
    print(num4.isnumeric()) # True
    
    # isdecimal只能识别:num2
    print(num2.isdecimal()) # True
    print(num3.isdecimal()) # False
    print(num4.isdecimal()) # False
    '''
    
    # 6.is其他
    '''
    print('abc'.islower())
    print('ABC'.isupper())
    print('Hello World'.istitle())
    print('123123aadsf'.isalnum()) # 字符串由字母或数字组成结果为True
    print('ad'.isalpha()) # 字符串由由字母组成结果为True
    print('     '.isspace()) # 字符串由空格组成结果为True
    print('print'.isidentifier()) # 判断命名是否规范,关键字其结果全为True
    print('age_of_egon'.isidentifier()) # True
    print('1age_of_egon'.isidentifier()) # False
    '''
    

lista

definiciones

tipo de conversión

uso

  • operación de control de prioridad
  • aprender la operación

tupla

efecto

  • Del mismo modo una lista de tuplas, pero también se puede almacenar una pluralidad de elementos de cualquier tipo, excepto que los elementos de la tupla no se pueden modificar, es decir, no se permite una tupla correspondiente a una lista de inmutable, para el registro de una pluralidad de valores fijos de modificar, simplemente para la toma de

definiciones

tipo de conversión

uso

diccionario

definiciones

tipo de conversión

uso

  • operación de control de prioridad
  • Usted necesita tener una operación

conjunto

efecto

  • Establecer, lista, tupla, dict se puede almacenar como una pluralidad de valores, pero principalmente para la colección: deduplicación , los operadores relacionales

definiciones

tipo de conversión

uso

  • Los operadores relacionales
  • deduplicación
  • otras operaciones

Supongo que te gusta

Origin www.cnblogs.com/guanxiying/p/12455204.html
Recomendado
Clasificación