• for循环、数字类型、字符串类型


    一:For循环

    1.迭代式循环:for,语法如下

      for i in range(10):

        缩进的代码块

    2.while循环 VS  for循环

        2.1while循环:称之为条件循环,循环的次数取决于条件何时为False.

        2.2for循环:称之为...循环,循环的次数取决于数据包含的元素的个数

        2.3for循环专门用来取值,在循环取值方面比while循环要强大,以后但凡遇到循环取值的场景,就应该用for循环

    3.for+break

    4.for+continue

    5.for+else

    6.for循环的嵌套:

      for i in range(3):

        for i in range(2):

          print(i,j)

    外层循环执行一次,内层循环要执行全部.

    二:数字类型

    1.整形int

    1.1:用途:用于记录年龄,等级,号码等

    1.2:定义方式

      age=10

    1.3类型转换

      print(int(3.1)

      res=int('11111') print(res,type(res)) 

     res=float('11111.1')
      print('res,type(res)')

    1.4 了解(**)
    十进制转换成...
    print(bin(13))二进制
    print(oct(13))八进制
    print(hex(13))十六进制

    1.5:常用操作+内置方法
    存一个值
    不可变
    可变or不可变:(可变:值可变,id不变.可变==不可hash.
            不可变:值变,id就变,不可变==可hash)

    2.浮点型float
    2.1用途:记录身高,体重,薪资等
    2.2定义方式:salary=10.1
    类型转换:print(float(10))
    print(float(1.1))
    print(float('1.1'))
    2.3常用操作+内置方法
      存一个值
      不可变
    三:字符串类型
    1.用途:用于记录描述性值的状态,比如名字,性别等
    2.定义方式:
      msg='hello world'
    3.类型转换:可以把任意类型转换成字符串类型
    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))

    4.常用操作+内置方法
    优先掌握的操作:
    1.按索引取值(正向取+反向取):只能取
    2.切片(顾头不顾尾,步长)
    3.长度len:统计的是字符的个数
    4.成员运算in和not in:判断一个子字符串是否存在一个大字符串中
    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('*-=+/(_'))
    View Code

        6.切分split:把有规律的字符串切成列表从而方便取值

    info='egon:18:180:150'
    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])
    View Code

        7.循环

      for i in 'hello'

        print(i)

        需要掌握的操作:

      1.strip,lstrip,rstrip

        strip:移除字符串左右两边的某些字符

        lstrip:去掉字符串左边的某些字符

        rstrip:去掉字符串右边的某些字符

      2.lower,upper

        lower:将字符串中的字母全部变成小写

        upper:将字符串中的字母全部变成大写  

      3.startswith,endswith

        startswith:判断字符以...开头

        endswith:判断字符以...结尾

      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(18,'egon')
    msg='my name is {0}{0} my age is {1}{1}{1}'.format(18,'egon')
    print(msg)
    View Code

      5.split,rsplit

        split:切分,把有规律的字符串切成列表从而方便取值

        rsplit:从右往左切

      6.join

        join表示拼接,相当于for循环

      7.replace

        替换

    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('必须输入纯数字')
    View Code

      8.isdigit

        判断字符串里面是否是纯数字,当字符串内为纯数字的时结果为True.



















  • 相关阅读:
    上传代码到github
    AFN多文件进度下载
    NSURLSession各文件关系
    H5动静分离
    iOS设备获取总结
    iOS与JS开发交互总结
    iOS11 push控制器tabbar上移问题
    解决iOS11 UIScrollView下移问题
    关于react16.4——错误边界
    关于react16.4——上下文Context
  • 原文地址:https://www.cnblogs.com/liuxiaolu/p/9995913.html
Copyright © 2020-2023  润新知