一. python简介(回顾)
1. python是一门解释型弱类型高级语言
2. python的解释器
CPython, PyPy, JPython, IronPython, Ipython
3. print(内容1, 内容2)
4. 变量
程序运行过程中产生的中间值, 暂时存储在内存中.供后面的程序使用
命名规范:
1. 由字母, 数字, 下户线组成
2. 不能是数字开头, 更不能是纯数字
3. 不能使用关键字
4. 不要太长
5. 要有意义
6. 区分大小写
7. 不要用中文
8. 推荐使用驼峰或下划线
5. 类型
1. int整数,
2. str字符串, ',",''',"""
3. bool布尔. True, False
6. input() 用户交互, 获取的内容是字符串类型
获取的内容是字符串类型
7.
二、今日内容
1、循环
while条件:
代码块while条件:
代码块else: #当条件不成立时,执行else语句
代码块
死循环 、 输出5遍
1 a=2
2 while a==2:3 print("你真二")4
5 whileTrue:6 print("你好!")7
8 a=09 while a<5:10 print("喷死你")11 a=a+1
View Code
break 和 continue
1 #让用户一直输入内容并打印,直到内容为 q时终止程序
2 whileTrue:3 content = input("请输入内容(输入内容为q时退出程序):")4 if content=='q':5 break #打断 跳出循环
6 print(content)7
8 #让用户一直输入内容并打印,直到内容为q时,打印q并终止程序
9 flag=True10 whileflag:11 content=input("请输入内容(输入内容为q时退出程序):")12 if content=='q':13 flag=False #打断 终止当前本层循环 不影响后面程序的运行
14 print(content)15
16 #continue
17 whileTrue:18 content = input("请输入内容(输入内容为q时退出程序):")19 if content=='q':20 continue #停止本次循环(不执行循环中下面的程序) 继续执行下次循环
21 print(content)22
23 #break是彻底的停止当前循环体
24 #continue是停止本次循环,继续下次循环(用于排除一些东西)
25
26 #输出1-10 排除4
27 count=1
28 while count<=10:29 if count==4:30 count=count+1 #不写则为死循环
31 continue
32 print(count)33 count=count+1
View Code
while else 作为一个整体(循环体) 只有while中条件不成立时才执行else中的语句
1 count=1
2 while count<=20:3 print(count)4 count=count+1
5 else:6 print("数完了")7
8 #while else作为一个整体
9 count=1
10 while count<=20:11 if count==10:12 break #跳出真个循环 同时也不再执行else中的语句
13 print(count)14 else: #只有条件不成立时才会执行
15 print("数完了")
View Code
2 、格式化输出
%s 占位符,占的是字符串,同时也能够接受数字的(全能型)%d 占位符,占的是数字,
用法: 字符串后可直接%(,,,) 其中有顺序"%s岁的%s在%s喜欢%s" % (age, name, location, hobby)
1 name="alex"
2 age=26
3 location="湖边"
4 hobby="唱歌"
5 s="%s岁的%s喜欢在%s站着%s"%(age,name,location,hobby)6 print(s)7
8 #输出 26岁的alex喜欢在湖边站着唱歌
View Code
1 name = input("请输入名字:")2 age = input("请输入年龄:")3 job = input("请输入你的工作:")4 hobby = input("请输入你的爱好:")5 s = '''------------ info of %s -----------6 Name : %s7 Age : %s8 job : %s9 Hobbie: %s10 ------------- end -----------------''' %(name, name, age, job, hobby)11 print(s)
View Code
当字符串中有%s占位符后,后面想输出%时,需要写两个%,即(%%).
name = 'sylar'
#如果你的字符串中出现了%s这样的格式化的内容. 后面的%都认为是格式化.如果想要使用%. 需要转义 %%
print("我叫%s, 我已经学习了2%%的python了" %(name))#输出 我叫sylar, 我已经学习了2%的python了
print("我叫周润发. 我已经活了50%了")#输出 我叫周润发. 我已经活了50%了
View Code
3、运算符
1) 算数运算符
+ - * /
% 取余 (10/3 1 )// 取整 (10/3 3 )** 幂次方 (2**3 2的3次方)
2) 比较运算符
==等于!=不等于<>不等于>大于=大于等于<= 小于等于
3) 赋值运算符
= c=a+b (赋值)
+= c+=a 为 c=c+a
-=
*=
/=
%=
**=
//=
4) 逻辑运算
and且 同真则真or或 同假则假not非 非真即假,非假即真
优先级: ()、not、and、orXorY : X为非零时,值为X
X为零时,值为Y
XandY : 与or相反
其中: True看做非零,False看做零
练习:
print(3>4 or 4<3 and 1==1) #False
print(1 < 2 and 3 < 4 or 1>2 ) #T
print(2 > 1 and 3 < 4 or 4 > 5 and 2 < 1) #T
print(1 > 2 and 3 < 4 or 4 > 5 and 2 > 1 or 9 < 8) #F
print(1 > 1 and 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6)print(not 2 > 1 and 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6) #F
#x or y 如果x==0 那么就是y, 否则是x
print(1 or 2) #1
print(2 or 3) #2
print(0 or 3) #3
print(0 or 4) #4
print(0 or 1 or 3 or 0 or 5)print(1 and 2) #2
print(2 and 0) #0
print(0 and 3) #0
print(0 and 4) #0
print(2 > 3 and 3) #false相当于0
View Code
4、编码
1.ASCII
最早的编码,至今还在使用,8位一个字节(字符)2.GBK
国标码,16位2个字节.3.unicode
万国码,32位4个字节.4.UTF-8可变长度的unicode
英文,8位.1个字节
欧洲文字,16位.2个字节
汉子,24位.3个字节
8bit=1byte
1024byte=1KB
1024KB=1MB
1024MB=1GB
1024GB=1TB
5、in 和 not in
可以判断xxxx字符串是否存在于xxxxxxxxxx字符串中.
1 content=input("请输入内容:")2 if "马化腾"in content or "马云" incontent:3 print("您输入的内容不合法!")4 else:5 print("您输入的内容合法!")