一、流程控制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))

04-Python入门学习-流程控制的更多相关文章

  1. 04 Python入门学习-流程控制(if else elif while for)

    一:流程控制if 语法一: if 条件: code1 code2 code3 ... age = 20 height = 170 weight = 60 sex = 'female' is_beaut ...

  2. Python入门6 —— 流程控制 - if判断

    代码块: 1.代码块指的是同一级别的代码,在python中用缩进相同的空格数(除了顶级代码块无任何缩进之外,其余代码块都是在原有的基础上缩进4个空格)来标识同一级的代码块 2.同一级别的代码块会按照自 ...

  3. python入门之流程控制

    if else 格式: if 条件 command1 command2elif 条件: command3    command4 else: command3 command4 注意条件后和else后 ...

  4. python语法入门之流程控制

    python语法入门之流程控制 流程控制是指控制流程,具体指控制程序执行的流程. 流程控制分为三种: 1.顺序结构 程序从上而下的正常执行(正常执行的代码就是顺序结构) 2.分支结构 赋予程序人的思维 ...

  5. python入门学习:6.用户输入和while循环

    python入门学习:6.用户输入和while循环 关键点:输入.while循环 6.1 函数input()工作原理6.2 while循环简介6.3 使用while循环处理字典和列表 6.1 函数in ...

  6. Python3.7.4入门-2流程控制工具

    2 流程控制工具 记得在语句后加冒号 2.1 while # Fibonacci series: # the sum of two elements defines the next a, b = 0 ...

  7. Python 入门之流程控制语句

    Python 入门之流程控制语句 1.if判断 (1) 单 if if –-如果 if 条件: 缩进 结果 (官方推荐4个空格,或者一个tab 不能空格和tab混合使用) money = 10 pri ...

  8. python中的流程控制

    目录 引言 流程控制的分类 分支结构 单if结构 if与else结构 if与elif与else结构 if分支的嵌套 循环结构 while循环 while + break循环 while + conti ...

  9. python入门学习:9.文件和异常

    python入门学习:9.文件和异常 关键点:文件.异常 9.1 从文件中读取数据9.2 写入文件9.3 异常9.4 存储数据 9.1 从文件中读取数据 9.1.1 读取整个文件  首先创建一个pi_ ...

随机推荐

  1. C#图片操作公共库

    存一下,以后找起来方便 包括图片加载.压缩.base64等 public static class ImageFun { #region 图片 public static EncoderParamet ...

  2. python中的装饰器迭代器生成器

    装饰器: 定义:本质是函数(装饰其它函数) 为其它函数添加附加功能 原则: 1 不能修改被装饰函数源代码    2 不修改被装饰函数调用方式 实现装饰器知识储备: 1 函数即‘’变量‘’ 2 高阶函数 ...

  3. python Django cookie和session

    在一个会话的多个请求中共享数据,这就是会话跟踪技术.例如在一个会话中的请求如下:  请求银行主页: 请求登录(请求参数是用户名和密码): 请求转账(请求参数与转账相关的数据): 请求信誉卡还款(请求参 ...

  4. [再寄小读者之数学篇](2014-06-23 二阶导数估计 [中国科学技术大学2013年高等数学B 考研试题])

    设 $f(x)$ 二阶连续可导, $f(0)=f(1)=0$, $\dps{\max_{0\leq x\leq 1}f(x)=2}$. 证明: $$\bex \min_{0\leq x\leq 1}f ...

  5. [物理学与PDEs]第1章习题8 磁场分布 $\ra$ 电流分布

    设在真空中有一圆柱形磁场 $$\bex B(P)=\sedd{\ba{ll} \cfrac{2I}{Cr},&r\geq R,\\ \cfrac{2I}{CR^2}r,&r<R, ...

  6. 集合各个实现类的底层实现原理 ----- 原文地址:https://blog.csdn.net/qq_25868207/article/details/55259978

    ArrayList实现原理要点概括 参考文献: http://zhangshixi.iteye.com/blog/674856l https://www.cnblogs.com/leesf456/p/ ...

  7. Oracle 表操作

    表操作 复制表结构和数据 CREATE TABLE table_name AS SELECT * FROM old_table_name; 复制表结构 CREATE TABLE table_name ...

  8. vim配置(使用Vundle)

    1.前言 Vim的配置文件位于~/.vimrc,文件使用VimScript语法来编写. 2. vim插件管理 Vundle是一个全自动的插件管理器,让我们通过维护插件列表的方式管理插件.它为安装.更新 ...

  9. centos7.6编译安装php7.2.11及redis/memcached/rabbitmq/openssl/curl等常见扩展

    centos7.6编译安装php7..11及redis/memcached/rabbitmq/openssl/curl等常见扩展 获取Php的编译参数方法: [root@eus-api-cms-bac ...

  10. PHP 必知的 16 个编程法则

    以双下划线(__)开头的方法称为魔术方法 -__construct():类的构造方法: -__destruct():类的析构方法: -__call($funName, $arguments):当访问未 ...