zoukankan      html  css  js  c++  java
  • python3-day2-python基础2

    一、for循环

    for循环是我们编程中非常常用的一种循环,以下就是for循环在python中的一些应用实例:

    1、单层for循环

    #!/usr/bin/env python3
    #-*- coding: utf-8 -*-
    #user:slim_liu

    for i in range(10):#循环10次
      print(i)#打印0-9
      if i == 5:#如果打印到5
      break#退出循环
    else:
      print("循环顺利结束")#正常循环结束

    2、for循环嵌套for循环

    for i in range(1,10,2):#从1-10,步长为2
      for j in range(10):#打印0-9
        if j <6:
        continue
    print(i,j)

    二、数据类型

    1、数字,数字有多种表示方法,8进制oct(10)  16进制hex(10) 二进制bin(10) 

      1.1整型:int
      1.2长整型:python3去掉了
      1.3布尔:true 和false bool(0)
      1.4浮点型:0.4
      1.5复数:4+5i

    2、字符串的操作
      1.1pritn(r'aaa asdf') r取消特殊含义
      1.2center(20) 居中显示
      1.3count('a',4,7) #统计某个元素出现的次数,可以指定起始位置和结束位置,从4开始,7结束count('a',4,-1)
      1.4endswith('d') 判断结尾真假
      1.5expandtabs(10) 指定table键长度
      1.6find('d')查找字符
      1.7format格式化字符串 print('{}{}'.format('name','age'))
      1.8index#显示索引值
      1.9inalnum#字母或者数字为真
      2.0isalpha#只能是字母为真
      2.1isdecimal#十进制为真
      2.2indigit#整型为真
      2.3isnumeric#数字为真
      2.4isidentifier#判断是否包含系统保留关键字
      2.5lstrip strip #删除左右空格,strip指定关键字可以去除指定关键字
      2.6just ljust just #左右填充,可以指定填充字符,指定总长度
      2.7zfill #左填充 指定总长度 000000000000abc 填充的字符只能是0
      2.8 name='abacd'
      ta=str.maketrans('a','1') #指定将a替换成1
      print(name.translate(ta))

    3、常用字符串的操作: 

      1)strip 移除空格
      2)len 查看长度
      3)index 索引
      4)a[1:4]切片

    4、运算符:

      1)算数运算 + - * / % // **
      2)比较运算 == != < > >= <=
      3)赋值运算 = += -+ *= /= %= //=
      4)位运算 & | ^ << >>
      5)逻辑运算 and or not
      6)成员运算 in 、not in 判断一个元素是否存在于字符串中
      7)身份运算 is 、not is
      8)布尔值:非零数字及非空字符串自带的布尔值都为True

    三、while循环

    count=0
    oldage=56

    while count <3:
      age=input("age>>:")
      if age.isdigit():
        age=int(age)
        if age == oldage:
          print("猜对了")
          break
        elif age < oldage:
          print("猜小了")
        else:
          print("猜大了")
     else:
      count += 1
      continue

    四、列表

      1)定义一个列表:names =  []列表用中括号表示

      2)列表常用的操作: 

        增:
          1.names.append("liuhailong") #追加
          2.names.insert(2,"zhangsan") #插入
        删:
          1.names.remove("liuhailong")
          2.del names[3]
          3.print(names.pop(3))#删除并打印删除的内容 不加条件,删除最后一个
        改:
          1.names[1]="刘海龙"
          2.names.clear()#清空
          3.names.extend(n2) #将n2和names合并
        查:
          1.names[1]#通过下标
          2.names[0::2]#切片打印,names[-3:]#最后三个元素   
          3.names.index("alex")#取元素下标
          4.names.count("liuhailong")#统计目标个数
          5.names.reverse()#反转列表
          6.names.sort#对列表进行排序***python3中不能对int和str进行综合排序
          7.names.copy()#复制列表

  • 相关阅读:
    centos8 将SSSD配置为使用LDAP并要求TLS身份验证
    Centos8 搭建 kafka2.8 .net5 简单使用kafka
    .net core 3.1 ActionFilter 拦截器 偶然 OnActionExecuting 中HttpContext.Session.Id 为空字符串 的问题
    Springboot根据不同环境加载对应的配置
    VMware Workstation12 安装 Centos8.3
    .net core json配置文件小结
    springboot mybatisplus createtime和updatetime自动填充
    .net core autofac依赖注入简洁版
    .Net Core 使用 redis 存储 session
    .Net Core 接入 RocketMQ
  • 原文地址:https://www.cnblogs.com/slim-liu/p/6009676.html
Copyright © 2011-2022 走看看