zoukankan      html  css  js  c++  java
  • python自动化--语言基础二运算符、格式化输出、条件语句、循环语句、列表、元组

    运算符包括:算术运算符、比较运算符、赋值运算符、逻辑运算符、成员运算符、身份运算符。

    算术运算符

      %   取模(余数)
      //  取相除的整数部分
      /   (5/2=2.5)

    比较运算符

      ==  等于
      !=  不等于
      <   小于
      >   大于
      <=  小于等于
      >=  大于等于

    1 if a==b:
    2     print(1)
    3 else:
    4     print(2)

    赋值运算符

      a+=b   等于 a=a+b
      a-=b          a=a-b
      a*=b          a=a*b
      a/=b          a=a/b
      a%=b        a=a%b
      a**=b       a=a**b
      a//=b         a=a//b

    逻辑运算符

      逻辑运算执行顺序:not  >  and  >  or
      非0和非空(null)值为true,0或者null为false

    1 a = 0
    2 b = 10
    3 if not(a and b):  #有里往外看循环
    4     print(1)
    5 else:
    6     print(2)

    成员运算符

      in        not in

     1 a = 1
     2 b = 20
     3 c = [1,2,3,4,5]
     4 if a in c:
     5     print("a在c列表中")
     6 else:
     7     print("a在c列表中")
     8 
     9 if b not in c:
    10     print("b不在c列表中")
    11 else:
    12     print("b在c列表中")

    身份运算符

      ==  判断变量的值是否相等
      is  判断某个变量身后的id是否一样

    1 if a is b:
    2     print("a和b的id一样")
    3 else:
    4     print("a和b的id不一样")
    5     print(id(a))
    6     print(id(b))

    格式化输出

    1 print("a = %.2f"%(a*3))  #a = 3.00
    2 print("xiaoxiao
    ")
    3 print("我的名字是%s,今年%d"%('小小',27))

    条件语句

    1 flag = False
    2 name = "xiao"
    3 if name == "qia":
    4     flag = True
    5     print("欢迎来到python全栈测试工程师课堂")
    6 else:
    7     print(name)
    1 num = 9
    2 if num>=0 and num <=10:
    3     print("very well")

    循环语句

      循环语句:while,for,嵌套
      循环控制语句:break跳出并终止循环,continue跳出本次循环执行下一次循环

    while语句

    1 count = 0
    2 while count<9:
    3     print("count=",count)
    4     count+=1
    5 print("while循环结束")

    break及continue语句

     1 i = 1
     2 while i<10:
     3     i+=1
     4     if i%2 > 0:
     5         continue
     6     print(i)
     7 print("==============================")
     8 i = 1
     9 while True:
    10     print(i)
    11     i+=1
    12     if i>10:
    13         break

    if ... else...  和   while ... else ... 是一样的。

    for语句

    1 for index in range(0,3):  #等同于range(32     print(index)          #打印0,12
    1 index = 0
    2 starnames = ['今年','明年','后年']
    3 for index in range(len(starnames)):
    4     print(starnames[index])

    range(5)       #01234
    range(1,5,2)  #13
    range(1,5)    #1234

    练习:

    1、计算练习

    x = 1+2.0+3
    x=17,x*=3
    x=17,x/=3
    x=15,x/3
    x=15.0,x/3
    x=17,x//3
    x=17,x%3
    x=3,x**3

     1 x = 1+2.0+3
     2 print(x)
     3 x=17
     4 x*=3
     5 print(x)
     6 x=17
     7 x/=3
     8 print(x)
     9 x=15
    10 x = x/3
    11 print(x)
    12 x=15.0
    13 x = x/3
    14 print(x)
    15 x=17
    16 x =x//3
    17 print(x)
    18 x=17
    19 x = x%3
    20 print(x)
    21 x=3
    22 x = x**3
    23 print(x)

    2、list列表练习

     1 LI = ["快乐大本营",2016,"我是歌手"]
     2 print(LI[1])
     3 LI.append("非诚勿扰")
     4 print(LI)
     5 LI.extend("男生女生")
     6 print(LI)
     7 LIS=[1,2,3]
     8 LI.extend(LIS)
     9 print(LI)
    10 
    11 LI.insert(1,2017)    #插入的下标位置和具体的值
    12 LI.insert(-1,2017)   #实际上是插入在倒数第二位13 
    14 LI.pop(1)            #删除下标是1的元素
    15 16 LI.remove(2016)      #移除元素2016
    17 print(LI)
    18 
    19 print(LI.index("快乐大本营"))  #获取下标
    20 print(LI.count(""))         #统计出现的次数
     1 正向和反向排序
     2 list_sort = [2,3,1,4]
     3 
     4 list_sort.sort()       #sort方法是重新排序列表不生成新的列表
     5 print(list_sort)
     6 
     7 list_sorted = sorted(list_sort)  #sorted排序列表并生成新的列表
     8 print(list_sorted)
     9 
    10 
    11 list_sort.reverse()    #倒排
    12 print(list_sort)

    3、以下两行输出一个空的列表
    list = ['a', 'b', 'c', 'd', 'e']
    print(list[10:])

    4、输出长度为5,append只增加一个元素,把[5,6,7,8]当做一个元素追加在列表中
    numbers=[1,2,3,4]
    numbers.append([5,6,7,8])
    print(len(numbers))

  • 相关阅读:
    IntelliJ IDEA 快捷键终极大全,速度收藏!
    49式!Python初级到高级招式都全了
    字节跳动三轮技术面_后端研发提前批
    这样让你的 IDEA 好用到飞起来!
    你 多久没有跳槽了?
    推荐几款能提升代码效率的笔记应用
    推荐 15 款编程游戏,从此谁都可以学编程!
    StackOverflow 创始人关于如何高效编程的清单
    11 条编程经验分享
    InnoDB和MyISAM存储引擎的区别
  • 原文地址:https://www.cnblogs.com/hanxiaobei/p/6475826.html
Copyright © 2011-2022 走看看