zoukankan      html  css  js  c++  java
  • DAY04-Python入门学习-流程控制

    一、流程控制if

    语法1:

     if 条件:
       code1
       code2
       code3
       ....

     age=180
     height=163
     weight=75
     sex='female'
     is_beautiful=True
    
     if age > 16 and age < 30 and height > 158 and weight < 100 and sex == 'female' and is_beautiful:
     print('表白。。。')
    
     print('=====>other code')


    语法2:

     if 条件:
       code1
       code2
       code3
       ....
     else:
       code1
       code2
       code3
       ....

     age=180
     height=163
     weight=75
     sex='female'
     is_beautiful=True
    
     if age > 16 and age < 30 and height > 158 and weight < 100 and sex == 'female' and is_beautiful:
     print('表白。。。')
     else:
     print('阿姨好')


    语法3:多分枝

     强调:if的多分枝=但凡有一个条件成立,就不会再往下判断其他条件了
     if 条件1:
       code1
       code2
       code3
       ....
     elif 条件2:
       code1
       code2
       code3
       ....
     elif 条件3:
       code1
       code2
       code3
       ....
     ........
     else:
       code1
       code2
       code3
       ....


     练习

     如果:成绩>=90,那么:优秀

     如果成绩>=80且<90,那么:良好

     如果成绩>=70且<80,那么:普通

     其他情况:很差

     score = input('>>: ')
     score=int(score)
     if score >= 90:
       print('优秀')
     elif score >= 80:
       print('良好')
     elif score >= 70:
       print('普通')
     else:
       print('很差')

    语法4:if嵌套

     age=18
     height=163
     weight=75
     sex='female'
     is_beautiful=True
    
     is_successfull=True
    
     if age > 16 and age < 30 and height > 158 and weight < 100 and sex == 'female' and is_beautiful:
       print('表白。。。')
     if is_successfull:
       print('在一起')
     else:
       print('我逗你玩呢')
     else:
       print('阿姨好')
    
    
     print('other code....')


     如果:今天是Monday,那么:上班
     如果:今天是Tuesday,那么:上班
     如果:今天是Wednesday,那么:上班
     如果:今天是Thursday,那么:上班
     如果:今天是Friday,那么:上班
     如果:今天是Saturday,那么:出去浪
     如果:今天是Sunday,那么:出去浪# today=input('>>: ')

     if today == 'Monday':
       print('上班')
     elif today == 'Tuesday':
       print('上班')
     elif today == 'Wednesday':
       print('上班')
     elif today == 'Thursday':
       print('上班')
     elif today == 'Friday':
       print('上班')
     elif today == 'Saturday':
       print('出去浪')
     elif today == 'Sunday':
       print('出去浪')
     else:
       print('''必须输入其中一种:
       Monday
       Tuesday
       Wednesday
       Thursday
       Friday
       Saturday
       Sunday
       ''')
    today=input('>>: ')
    if today == 'Monday' or today == 'Tuesday' or today == 'Wednesday' or today == 'Thursday' or today == 'Friday':
      print('上班')
    elif today == 'Saturday' or today == 'Sunday':
      print('出去浪')
    else:
      print('''必须输入其中一种:
      Monday
      Tuesday
      Wednesday
      Thursday
      Friday
      Saturday
      Sunday
      ''')


    二、流程控制while

    1 什么是循环

        循环就是一个重复的过程

    2 为何要有循环

        人可以重复的去做某一件事

        程序中必须有一种机制能够控制计算机像人一样重复地去做某一件事

    3 如何用循环

     语法

     while 条件:

         code1

         code2

         code3

         ...

     user_from_db='egon'
    
     pwd_from_db='123'
    
     
    
     while True:
    
         inp_user=input('please input your username: ')
    
         inp_pwd=input('please input your password: ')
    
         if inp_user == user_from_db and inp_pwd == pwd_from_db:
    
             print('login successfull')
    
         else:
    
             print('user or password err')

     while + break: break代表结束本层循环

     user_from_db='egon'
    
     pwd_from_db='123'
    
     
    
     while True:
    
         inp_user=input('please input your username: ')
    
         inp_pwd=input('please input your password: ')
    
         if inp_user == user_from_db and inp_pwd == pwd_from_db:
    
             print('login successfull')
    
             break
    
         else:
    
             print('user or password err')

     

     while+continue:

    continue代表结束本次循环(本次循环continue之后的代码不在运行),直接进入下一次循环

     强调:continue一定不要作为循环体的最后一步代码

     start=1

     while start < 8: 5 < 8
    
         if start == 4:
    
             start += 1 start=5
    
             continue
    
         print(start)
    
         start+=1

    while循环的嵌套

     user_from_db='egon'
    
     pwd_from_db='123'
    
     
    
     while True:
    
         inp_user=input('please input your username: ')
    
         inp_pwd=input('please input your password: ')
    
         if inp_user == user_from_db and inp_pwd == pwd_from_db:
    
             print('login successfull')
    
             while True:
    
                 cmd=input('>>>: ')  cmd='quit'
    
                 if cmd == 'quit':
    
                     break
    
                 print('%s run......' %cmd)
    
             break
    
         else:
    
             print('user or password err')

     while + else

     else的代码会在while循环没有break打断的情况下最后运行

     n=1
    
     while n < 5:
    
         if n == 4:
    
             break
    
         print(n)
    
         n+=1
    
     else:
    
         print('=====》')


    三、流程控制for

     for循环:可以不依赖索引而取指

     names=['egon','alex_dsb','lxx_sb','yxx_dsb']
     for item in names: item='lxx_sb'
        print(item)
     dic={'x':1,'y':2,'z':3}
     for k in dic: k='x'
        print(k,dic[k])

     for vs while

     for可以不依赖于索引取指,是一种通用的循环取指方式

     for的循环次数是由被循环对象包含值的个数决定的,而while的循环次数是由条件决定的

    names=['egon','alex_dsb','lxx_sb','yxx_dsb']
      for i in range(0,len(names)):
        print(names[i])

    四、数据类型int,float

    一:基本使用:int

     1 用途:

    记录年龄、等级、号码

     2 定义方式

     age=18 age=int(18)
     print(type(age))
     int('abadf') #报错
     int('10.1') #报错
     int('101') #int只能将字符串中包含纯数字转成整型

     进制转换(了解**)

     其他进制转成十进制

     二进制:0,1

     10 1*(2**1) + 0*(2**0)

      十进制:0-9

     371 3*(10**2) + 7*(10**1) + 1*(10**0)

      八进制:0-7

     371 3*(8**2) + 7*(8**1) + 1*(8**0)

      十六进制:0-9 A-F

     371 3*(16**2) + 7*(16**1) + 1*(8**0)

     十进制转成其他进制

     print(bin(12))#二进制
     print(oct(12))#八进制print(hex(16))#十六进制

    二:该类型总结

     1 存一个值

     2 不可变

     x=10
     print(id(x))
     x=11
     print(id(x))

    一:基本使用:float

     1 用途:

    记录身高、体重、薪资

     2 定义方式

     salary=3.1 #salary=float(3.1)
     res=float('3.3')
     print(type(res))

    二:该类型总结

     1 存一个值

     2 不可变

    x=10.3
    print(id(x))
    x=10.4
    print(id(x))
    
     



  • 相关阅读:
    My Eclipse
    那一夜,我被梦中笑醒的事之(数据库)
    KTV项目总结
    欢迎来到,数据库联盟!
    学习手工创建表,表关系以及用exists 来查询
    sql 将Null 值转化成空字符串
    jquery toggle
    推荐一个不错的配色网站
    css之zindex
    关于前后端分离与不分离
  • 原文地址:https://www.cnblogs.com/xvchengqi/p/9356267.html
Copyright © 2011-2022 走看看