zoukankan      html  css  js  c++  java
  • 数字类型、字符串、列表类型的基本用法

    数字类型

    一:整型int

      1.用途:记录名字、性别等描述性质   

      2.定义方法

    age=18#age=int(18)
    数据类型转换:int可以将纯数字的字符串转换成整型
    n=int('123456')
    print(n,type(n))

      3.常用操作+内置的方法 

        数学运算符&比较运算

    总结

      存一个值 不可变(1.可变:值变,id不变。可变==不可变:值变,id就变不可变==可hash)

    x=10print(id(x)x+=1print(id(x))

    二:浮点类型float

      1.用途:记录薪资、身高、体重等小数相关   

      2.定义方式

    salary=3.1#salary=float(3.1)
    数据类型转换:int可以将纯数字的字符串转换成整型
    sal=float('3.1')
    print(sal,type(sal))

      3.常用操作+内置的方法

        数学运算符&比较运算

    总结

      存一个值 不可变(1.可变:值变,id不变。可变==不可变:值变,id就变不可变==可hash)

    x=3.1
    print(id(x))
    x+=1.1
    print(id(x))

    了解

    #复数x=1+2j
    print(x.real)
    print(x.imag)
    #十进制==>>二进制
    print(bin(11))
    print(oct(11))
    print(hex(11))

    字符串类型   

      1.用途:记录姓名、性别等描述性质   

      2.定义方式:在单引号、双引号、三引号内包含一系列的字符

    name='tulipa' #name=str('tulipa')
    #数据类型转换:str可以将任意类型都转成字符串类型
    n=str([1,2,3]) #[1,2,3]
    print(type(n))

      3.常用操作+内置的方法   

      3.1优先掌握的操作   

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

    msg='hello world'
    print(msg[0])
    print(msg[-1])
    msg[0]='A'

        2.切片(顾头不顾尾,步长):从一个大的字符串中切出一个小字符串

    msg='hello world'
    print(msg[0:5:1])
    print(msg[0:5:2]) #0 2 4
    
    info='yyy 18'
    print(info[0:4])

    了解

    msg='hello world'
    print(msg[-1:-12:-1]) #-1 -2 -3 -4
    print(msg[-1::-1]) #-1 -2 -3 -4
    print(msg[::-1]) #-1 -2 -3 -4

        3.长度len

    print(len('ad你好'))

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

    msg='name age sex'
    print('sex' in msg)
    print('sex' not in msg)

        5.移除空白strip:移除字符串左右两边的字符

    msg='*****egon************'
    res=msg.strip('*')
    print(res)
    msg='+-*&^%egon-=)^(#'
    print(msg.strip('-+*&^%(#='))

    ​name
    =input('name>>>: ').strip() pwd=input('pwd>>>:').strip() if name == 'egon' and pwd == '123': print('login successful') else: print('user or pwd error')

        6.切分splist:把一个字符串按照某种分隔符切成一个列表,从而方便取值

    info='yyy:18:male'
    res=info.split(':')
    print(res[0])
    print(res)
    info1=res[0]+':'+res[1]+':'+res[2]
    info1=':'.join(res) #res中所有的元素必须是str类型print(info1)​
    
    cmd='get|a.txt|131123123'print(cmd.split('|'))

        7.循环

    msg='hello'
    for item in msg:
            print(item)    

      3.2需要掌握的操作   

        1.strip,lstrip,rstrip

    print('egon***'.strip('*'))
    print('****egon***'.lstrip('*'))
    print('****egon***'.rstrip('*'))

        2.lower,upper

    print('AdaEs1'.lower())
    print('AdaEs1'.upper())

        3.startswith,endswith

    print('alex is dsb'.startswith('alex'))
    print('alex is dsb'.startswith('al'))
    print('alex is dsb'.endswith('sb'))

        4.format的三种玩法

    msg='my name is %s my age is %s' %('egon',18)
    print(msg)
    msg='my name is {name} my age is {age}'.format(age=18,name='egon')
    print(msg)

    了解

    msg='my name is {} my age is {}'.format('egon',18)
    print(msg)
    
    msg='my name is {1}{0}{0}{0} my age is {1}{1}'.format('egon',18)
    print(msg)

        5.split.rsplit

    info='egon:18:male'
    print(info.split(':',1))
    print(info.rsplit(':',1))

        6.replace

    msg='kevin is dsb kevin'
    res=msg.replace('kevin','dsb',1)
    print(res)

        7.isdigit:当字符串是由纯数字组成时返回True

    print('123123'.isdigit())
    
    age_of_db=18
    inp_age=input('>>: ').strip()
    if inp_age.isdigit():
            inp_age=int(inp_age)
            print(inp_age == age_of_db)
    else:
            print('年龄必须是数字')        

      3.3了解的操作   

        1.find,rfind,index,rindex,count

    #find: 查找子字符串在大字符串中的起始位置
    msg='kevin is kevin xxxx sb'
    print(msg.find('kevin'))
    print(msg.index('kevin'))
    print(msg.find('asdf'))
    print(msg.index('asdf'))
    
    print(msg.rfind('kevin'))
    
    #count:统计子字符串在大字符串中出现的次数
    print('alex kevin alex kevin kevin'.count('kevin'))

        2.center,ljust,rjust,zfill

    username=input('>>>: ').strip()
    print(username.center(50,'*'))​
    print('egon'.ljust(50,'#'))
    print('egon'.rjust(50,'#'))​
    print('egon'.rjust(50,'0'))
    print('egon'.zfill(50))

        3.captalize,swapcase,title

    print('abdef adsfasdf'.capitalize())
    print('AbC'.swapcase())
    print('my name is egon'.title())

        4.is数字系列

    print('123213'.isdigit())
    print(''.isnumeric())
    print(''.isnumeric())

        5.is其他

    name='My Nmae'
    print(name.isalnum()) #字符串由字母或数字组成
    print(name.isalpha()) #字符串只由字母组成
    
    print(name.isidentifier())
    print(name.islower())
    print(name.isupper())
    print(name.isspace())
    print(name.istitle())

    总结

      存一个值 不可变(1、可变:值变,id不变。可变==不可hash 2、不可变:值变,id就变。不可变==可hash)

    x='a'
    print(id(x)
    x+='b'
    print(x)
    print(id(x))

    列表类型   

      1.用途:记录多个同种属性的值   

      2.定义方式:在[]内用逗号分隔开多个任意类型的值

    li=[1,'a',[1,2]] #li=list([1,'a',[1,2]])
    #数据类型转换:list
    res=list('hello')
    print(res)
    
    for k in {'a':1,'b':2}:
    print(k)
    
    res=list({'a':1,'b':2})
    print(res)

      3.常用操作+内置的方法   

      3.1优先掌握的操作:

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

    li=['a','b','c','d','e']
    print(li[-1])
    print(id(li))
    li[0]='A'
    print(li)
    print(id(li))
    li[5]=11111 # 错误,索引不存在

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

    li=['a','b','c','d','e']
    print(li[0:3])

        3.长度

    li=['a','b','c','d','e']
    print(len(li))

        4.成员变量in和not in

    li=['a','b','c','d','e']
    print('xxx' not in li)

        5.追加append&插入insert

    li=['a','b','c','d','e']
    li.append('aaaaa')
    li.append('bbbb')
    print(li)
    
    li.insert(1,'aaaa')
    print(li)

        6.删除

    li=['a','b','c','d','e']
    #单纯的删除,没有返回值
    del li[0]
    print(li)
    
    res=li.remove('a') #没有返回值
    print(li)
    print(res)
    #从列表中取走一个值
    res=li.pop(0)
    print(li)
    print(res)

        7.循环

    li=['a','b','c','d','e']
    for item in li:
    print(item)

      3.2 需要掌握的操作

    li=['a','b','a','c','d','e']
    print(li.count('a'))
    li.extend([1,2,3])
    
    li.reverse()
    li.sort()
    print(li)
    nums=[-3,2,3,9,]
    nums.sort(reverse=True)
    print(nums)

    总结

      存多个值 可变(1、可变:值变,id不变。可变==不可hash 2、不可变:值变,id就变。不可变==可hash)

    1.用列表模拟队列:先进先出

    l=[]
    #入队
    l.append('first')
    l.append('second')
    l.append('third')
    print(l)
    
    #出队
    print(l.pop(0))
    print(l.pop(0))
    print(l.pop(0))

    2.用列表模拟堆栈:后进先出

    #入栈
    l.append('first')
    l.append('second')
    l.append('third')
    print(l)
    
    #出栈
    print(l.pop(-1))
    print(l.pop(-1))
    print(l.pop(-1))

     

  • 相关阅读:
    图片滤镜高斯模糊
    context.Request.Files为NULL问题 在实现图片上传功能的时候出现在ashx等处理页面出现context.Request.Files为NULL异常,有几点需要注意:
    C#中使用代码动态改变配置文件信息
    缓存
    使用iframe实现图片上传预览效果
    loading 加载
    sql 查询每月的销售金额
    GridView数据格式化
    把图片转换成二进制--把二进制转换成图片
    asp.net js 倒计时总秒数量 和 排序
  • 原文地址:https://www.cnblogs.com/ShenJunHui6/p/10214784.html
Copyright © 2011-2022 走看看