zoukankan      html  css  js  c++  java
  • python基础4

    一、for循环

    l=['a','b','c']
    print(len(l))
    i=0
    while i<len(l):
        print(l[i])
        i+=1
    
    l=['a''b''c']
    for itme in l:
        print(itme)
    
    dic={'x':111,'y':222,'z':333}
    for k in dic:
        print(k,dic[k])


    while循环VSfor循环
    1、while循环称之为条件循环,循环次数取决于何时为False
    for循环称之为。。。,循环次数取决于数据包含的元素个数
    2、for循环专门用来取值,在循环取值方面比while还要强大,但凡遇到取值就应该用到for循环。

    l=['a','b','c']
    for i in range(3):
        print(i,l[i])
    
    for+break
    names=['egon','kevin','alex','hulaoshi']
    for name in names:
        if name=='alex':break
        print(name)
    
    for+continue
    names=['egon','kevin','alex','hulaoshi']
    for name in names:
        if name=='alex':continue
        print(name)
    
    for+else
    names=['egon','kevin','alex','hulaoshi']
    for name in names:
        if name=='alex':break
        print(name)
    else:
        print('>>>>:')


    for循环嵌套
    for i in range(3):
        for j in range(2):
            print(i,j)
            外层循环第一次:i=0
        内层循环
            00
            01
    
    外层循环第二次:i=1
        内层循环
            10
            11
    外层循环第三次: i=2
        内层循环
            2,0
            2,1
    print(' ',end='
    ')#自动换行
    print('bbbbb',end='') #不换行
    二、数字类型
    1、整形int
    ======================================基本使用======================================
    # 1、用途:记录年龄、等级、号码等
    
    # 2、定义方式
    age=10 # age=int(10)
    
     类型转换
     print(int(3.1))
    rea=float('111.1')
    print(rea,type(rea))
    
    进制转换(了解)
    十进制转成。。。进制
    print(bin(13))转二进制
    print(oct(13))转8进制
    print(hex(13)转16进制
    
    #3、常用操作+内置的方法
    
    ======================================该类型总结====================================
     存一个值
    
     不可变
    x=10
    print(id(x))
    x=11
    print(id(x))

    2、浮点型float
    ======================================基本使用======================================
    # 1、用途:记录年龄、等级、号码等
    
    # 2、定义方式
    age=10 # age=int(10)
    
     类型转换
     print(int(3.1))
    rea=float('111.1')
    print(rea,type(rea))
    
    进制转换(了解)
    十进制转成。。。进制
    print(bin(13))转二进制
    print(oct(13))转8进制
    print(hex(13)转16进制
    
    #3、常用操作+内置的方法
    
    ======================================该类型总结====================================
     存一个值
    
     不可变
    x=10
    print(id(x))
    x=11
    print(id(x))

    三、字符串类型.
    字符串类型str:
    ======================================基本使用======================================
     1、用途:记录描述性值的状态,比如名字、性别等
    
     2、定义方式
     msg='hello word'# msg=str('hello word')
     类型转换:可以将任意类型转成字符串类型
     res1=str(10)
    res2=str(10.3)
    res3=str([1,2,3])
    res4=str({'x':1}) #res4="{'x':1}"
    
    print(type(res1))
    print(type(res2))
    print(type(res3))
    print(type(res4))
    3、常用操作+内置的方法
    优先掌握的操作:(*****)

    #1、按索引取值(正向取+反向取) :只能取
    msg='hell word'
    print(type(msg[0]))
    print(msg[-1])

    #2、切片(顾头不顾尾,步长)
    msg='hello world'
     print(msg[0]+msg[1]+msg[2])
     print(msg[0:5])
     print(msg[0:5:2]) #0 2 4
     print(msg[0:]) #
     print(msg[:])
     print(msg[-1:-5:-1]) #-1 -2 -3 -4
     print(msg[::-1]) #-1 -2 -3 -4

    #3、长度len:统计的是字符的个数
    msg='h你d'
    print(len(msg))
    msg='h你d'
    print(len(msg))

    #4、成员运算in和not in:判断一个子字符串是否存在与一个大字符串中
    msg='hello world'
    print('ho' in msg)
    print('ho' not in msg)

    #5、移除空白strip:移除字符串左右两边的某些字符
    msg='      hello      '
    
    print(msg.strip(' '))
    print(msg.strip())
    print(msg)
    
    name=input('name>>>: ').strip() #name='egon'
    pwd=input('password>>>: ').strip()
    
    if name == 'egon' and pwd == '123':
        print('login successfull')
    else:
        print('username or password error')
    
    msg='***h**ello**********'
    print(msg.strip('*'))
    
    msg='*-=+h/ello*(_+__'
    print(msg.strip('*-=+/(_'))

    6、切分split: 把有规律的字符串切成列表从而方便取值
    info='egon:18:180:160'
    res=info.split(':'1)
    print(res)
    print(res(1))
    info='egon:18:180:150'
    res=info.split(':')
    print(res)
    
    
    s1=res[0]+':'+res[1]+':'+res[2]+':'+res[3]
    s1=''
    for item in res:
        s1+=item
    print(s1)
    
    
    s1=':'.join(res)
    print(s1)
    
    ':'.join([1,2,3,4,5])

    #7、循环
    for i in 'hello':
        print(i)


    需要掌握的知识(*****)
    #1、strip,lstrip,rstrip
    msg='*****hello word*****'
    print(msg.strip('*'))#去左右两侧
    print(msg.lstrip('*'))#去左侧
    print(msg.rstrip('*'))#去右侧
    #2、lower,upper
     msg='AaBbCc123123123'
     print(msg.lower())#转小写
     print(msg.upper())#转大写
    #3、startswith,endswith
    msg='alex is dsb'
    print(msg.startswith('alex'))#是否在开头
    print(msg.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')#格式化输出
    了解
    msg='my name is {} my age is {}'.format(18,'egon')
    msg='my name is {0}{0} my age is {1}{1}{1}'.format(18,'egon')#多次输出
    print(msg)
    #5、split,rsplit
    cmd='get|a.txt|33333'
    print(cmd.split('|',1))
    print(cmd.rsplit('|',1))
    #6、replace
    msg='kevin is sb kevin kevin'
    print(msg.replace('kevin','sb',2))
    #7、isdigit #当字符串内为纯数字时结果为True
    res='11111'
    print(res.isdigit())
    int(res)
    age_of_bk=18
    inp_age=input('your age: ').strip()
    if inp_age.isdigit():
        inp_age=int(inp_age) #int('asdfasdfadfasdf')
        if inp_age > 18:
            print('too big')
        elif inp_age < 18:
            print('to small')
        else:
            print('you got it')
    else:
        print('必须输入纯数字')
    

      

    
    
    了解(**#1、find,rfind,index,rindex,count
    print('xxxkevin is sb kevin'.find('kevin'))
    print('xxxkevin is sb kevin'.index('kevin'))
    print('xxxkevin is sb kevin'.rfind('kevin'))
    print('xxxkevin is sb kevin'.rindex('kevin'))
    
    
    res='xxxkevin is sb kevin'.find('kevasdfsadfin')
    print(res)
    
    res='xxxkevin is sb kevin'.index('kevasdfsadfin')
    
    
    
    print('kevin is kevin is kevin is sb'.count('kevin'))
    
    #2、center,ljust,rjust,zfill
    print('egon'.center(50,'*'))
    print('egon'.ljust(50,'*'))
    print('egon'.rjust(50,'*'))
    print('egon'.zfill(50))
    
    #3、captalize,swapcase,title
    print('my name is kevin'.capitalize())
    print('AaBbCc'.swapcase())
    print('my name is kevin'.title())
    
    #4、is其他
    name='egon123'
    print(name.isalnum()) #字符串由字母或数字组成
    print(name.isalpha()) #字符串只由字母组成
    
    print(name.islower())
    print(name.isupper())
    name='    '
    print(name.isspace())
    msg='I Am Egon'
    print(msg.istitle())
  • 相关阅读:
    JavaScript观察者模式
    JavaScript装饰者模式
    使用uni-app开发小程序,关于小程序更新后与用户本地不会及时更新解决办法
    6.3 基于二分搜索树、链表的实现的集合Set复杂度分析
    解决uni-app props 传递数组修改后不能使用问题
    6.2 集合和映射--集合Set->底层基于链表实现
    6.1 集合和映射--集合Set->底层基于二叉搜索树实现
    【刷题记录
    【codeforces
    【loj
  • 原文地址:https://www.cnblogs.com/liushen1995/p/9996573.html
Copyright © 2011-2022 走看看