流程控制if...else....

name = '疯子'
res = input('你叫什么名字?')
if res == name:
print('帅哥')
else:
print('丑男')

如果:女人的年龄>=18并且<22岁并且身高>170并且体重<100并且是漂亮的,那么:表白,否则:叫阿姨

age_of_girl=18
height=171
weight=99
is_beautiful=True
if age_of_girl >= 18 and age_of_girl < 22 and height > 170 and
weight < 100 and is_beautiful == True:
print('表白...')
else:
print('阿姨好')

逻辑运算符

小练习:

1.定义布尔型变量 has_ticket 表示是否有车票 #2.定义整形变量 knife_length 表示刀的长度,单位:厘米 #3.首先检查是否有车票,如果有,才允许进行安检 #4.安检时,需要检查棍子的长度,判断是否超过20厘米 # 如果超过20厘米,提示棍子的长度,不允许上车 # 如果不超过20厘米,按键通过 #5.如果没有车票,不允许进门

has_ticket = True
stick_length = 22 if has_ticket:
if stick_length > 20:
print('你的棍子太长了',stick_length)
else:
print('请进....')
else:
print('您没有车票,不允许进入车站')

return 语句

return语句[表达式]退出函数,选择性地向调用方返回一个表达式。不带参数值的return语句返回None。之前的例子都没有示范如何返回数值,下例便告诉你怎么做:

# 可写函数说明
def add_sum(arg1, arg2): # 返回2个参数的和."
total = arg1 + arg2
print("函数内 : ", total)
return total # 调用sum函数
total = add_sum(10, 20)

流程控制之while

while 条件: # 循环体

如果条件为真,那么循环体则执行,执行完毕后再次循环,重新判断条件。。。

如果条件为假,那么循环体不执行,循环终止

1、循环取出列表里面的每一个元素

 l1 = ['a','b','c','d','e','f']
count = 0
while count < len(l1):
print(l1[count])
count += 1
0-10之间的偶数
count = 0
while count <= 10:
print(count)
count += 2

2、死循环

 while True:
print(1)

3、while....else.....

 count = 0
while count <= 5 :
count += 1
print("Loop",count)
else:
print("循环正常执行完啦")

4、continue:跳过本次循环

break:跳出循环

pass:占位符

 count = 0
while count <= 5:
count += 1
if count == 2:
continue
print(count)
 count = 0
while count <= 5:
count += 1
if count == 2:
break
print(count)
 count = 0
while count <= 5:
count += 1
if count == 2:
pass
print(count)

猜拳小练习

import random
print('-----欢迎来猜拳-----')
print('1:石头 2:剪刀 3:布 4:退出')
info = {
'win' :0,
'lose' :0,
'draw' :0
}
while True:
hum = input('请出拳:').strip()
com = ['石头', '剪刀', '布']
com1 = random.choice(com)
if hum == '1' and com1== '石头' or hum == '2' and com1 == '剪刀' or hum == '3' and com1 == '布':
print('决战到天亮!')
info['draw'] += 1
elif hum == '2' and com1== '石头' or hum == '3' and com1 == '剪刀' or hum == '1' and com1 == '布':
print('小垃圾!看看我是啥--%s'% com1)
info['lose'] += 1
elif hum == '4':
break
elif hum == '3' and com1== '石头' or hum == '1' and com1 == '剪刀' or hum == '2' and com1 == '布':
print('你牛逼!')
info['win'] += 1
else:
print('你倒是出拳啊!渣渣!!')
print(info)

流程控制之for循环

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

  for i in range(10):

    缩进的代码块

2 break与continue(同while)

 for i in range(0,11,3):#range顾头不顾尾
print(i) 可迭代对象
l1 = [1,2,3]
t1 = (1,2,3)
info = {'name':'蒋介石','age':88}
str1 = 'hello,world'
s1 = {1,2,3} for i in info.items():
print(i)

操作文件

在python3中如何打开一个文件呢?

f = open('lock.txt', 'r',encoding='utf-8')
应用程序向操作系统发起打开文件的操作
print(f.read())
操作系统打开文件得到文件句柄返回给应用程序并赋值给f
f.close()
应用程序向操作系统发起关闭文件的信号,并且回收变量f

注意:操作完文件后一定要记得关闭,因为如果开的文件多了,不关闭,会浪费系统资源,使系统变慢 #但是还是有很多同学忘记关闭文件,因此,可以用下面这个方式

with open('lock.txt', 'r', encoding='utf-8') as f:
print(f.read())

这里要注意字符编码,秉承一个原则就不会出现乱码(用什么方式写的,就用什么方式打开)

r ,#只读模式【默认模式,文件必须存在,不存在则抛出异常】

w,#只写模式【不可读;不存在则创建;存在则清空内容】

a, #之追加写模式【不可读;不存在则创建;存在则只追加内容】 #以下三种跟上面三种一样,只不过是以二进制方式

rb

wb

ab

文本打开的常用模式和操作文件的方法

f.read() #读取所有内容,光标移动到文件末尾
f.readline() #读取一行内容,光标移动到第二行首部
f.readlines() #读取每一行内容,存放于列表中
f.write('1111\n222\n') #针对文本模式的写,需要自己写换行符
f.write('1111\n222\n'.encode('utf-8')) #针对b模式的写,需要自己写换行符
f.writelines(['333\n','444\n']) #文件模式 #了解
f.readable() #文件是否可读
f.writable() #文件是否可读
f.closed #关闭文件
f.flush() #立刻将文件内容从内存刷到硬盘
f.name #查看打开的文件名

移动光标

文件的光标移动

 with open(r'C:\Users\Administrator.USER-20190512NQ\Desktop\a.txt', 'rb') as f:
data1 = f.read(2)
data2 = f.read()
data3 = f.read()
print(data3)
rt模式打开只可以(0,0),(0,1),(0,2)
f.seek(0,0)#后面的0代表把光标移动到开头,前面的数字代表光标的移动
f.seek(0,1)#后面的1代表相对位置,前面的数字代表光标的移动
f.seek(0,2)#后面的2代表把光标移动到末尾,前面的数字代表光标的移动
data1 = f.read(2)
f.seek(2,0)
f.seek(2,1)
f.seek(-6,2)
data2 = f.read()
print(data2.decode('utf-8')) with open(r'C:\Users\Administrator.USER-20190512NQ\Desktop\a.txt', 'r',encoding='utf-8') as f1:
data = f1.read()
new = data.replace('abc','sb250')
with open(r'C:\Users\Administrator.USER-20190512NQ\Desktop\a.txt', 'w', encoding='utf-8') as f2:
f2.write(new) tail -f message | grep '404'
import time
with open(r'C:\Users\Administrator.USER-20190512NQ\Desktop\a.txt', 'rb') as f:
f.seek(0, 2)
while True:
data = f.read()
if b'404' in data:
print(data.decode('utf-8'))
else:
time.sleep(0.2)

最新文章

  1. 原生javascript 固定表头原理与源码
  2. 【Alpha版本】冲刺-Day5
  3. Maven 的插件和生命周期的绑定
  4. 阿里云安装Tomcat
  5. 深入理解Linux修改hostname(转载)
  6. F7控件
  7. PHP 数组(遍历)
  8. C 的 一些写法格式 交流
  9. 解决Windows 7删除执行过的 EXE、Bat文件有延迟的问题
  10. 一秒钟速算(WINFORM)
  11. JavaScript中ActiveXObject操作本地文件夹
  12. [UWP小白日记-6]页面跳转过度动画
  13. HDU 4291 A Short problem(矩阵+循环节)
  14. 人工智能背景下的Office 365现状和发展趋势
  15. 想要学习jQuery却不知从何开始?本文为你精选5个例子帮你快速成为jQuery大师
  16. P4177 [CEOI2008]order 网络流,最小割,最大权闭合子图
  17. 【Android开发经验】android:windowSoftInputMode属性具体解释
  18. Codeforces Round #363 (Div. 1) B. Fix a Tree 树的拆环
  19. Python发送带附件的邮件
  20. easyui combotree模糊查询

热门文章

  1. Windows KMS激活脚本
  2. 关于 Vue 微信客户端 不能播放音乐(报错和不能播放的问题)
  3. JS 全局作用域和局部作用域
  4. Java多线程和并发(八),synchronized底层原理
  5. 51 Nod Bash 游戏v2
  6. 【IOI2018】组合动作
  7. CF1213F Unstable String Sort
  8. AcWing:108. 奇数码问题(归并排序 + 逆序数)
  9. Windows下安装jdk
  10. spring整合之后运行报什么只读错误。Write operations are not allowed in read-only mode (FlushMode.MANUAL): Turn your Session into FlushMode.COMMIT/AUTO or remove &#39;readOnly&#39; marker from transaction definition.