Python day 02
基础&运算符
今日概要
- 循环
- 字符串格式化
- 运算符
- 编码
内容回顾 & 补充
内容回顾
- 计算机基础
- 解释器python 2 和 python 3
- 语法
- input
- if / elif / else
- 数据类型
- int 整数 / str 字符串 / bool 布尔值
'''评分规则:
A >=90
B >=80
C >=70
D 其他
用户输入成绩,根据成绩的不同显示不同的级别。
'''
score = input('请输入你的成绩:')
score_int = int(score)
if score_int >= 90 and score_int <= 100:
print('你的成绩为', score_int, ',', '评分为:A')
elif score_int >= 80 and score_int < 90:
print('你的成绩为', score_int, ',', '评分为:B')
elif score_int >= 70 and score_int < 80:
print('你的成绩为', score_int, ',', '评分为:C')
elif score_int < 70 and score_int >= 0:
print('你的成绩为', score_int, ',', '评分为:D')
else:
print('你的输入有误!')
补充
# if条件的嵌套
message = '''欢迎致电10086
1.话费查询;
2.流量服务;
3.业务办理;
4.人工服务'''
print(message)
index = input('请输入您要选择的服务:')
index = int(index)
if index == 1:
print('话费查询')
elif index == 2:
print('流量服务')
elif index == 3:
content = '''业务办理
1.修改密码;
2.更改套餐;
3.停机'''
print(content)
value = input('请输入要办理的业务:')
value = int(value)
if value == 1:
print('修改密码')
elif value == 2:
print('更改套餐')
elif value == 3:
print('停机')
else:
print('输入有误')
elif index == 4:
print('人工服务')
else:
print('输入有误')
今日内容
1.循环语句
while True:
print('人生苦短,我用Python。')
while True: # 死循环,炫迈效果。
# 请通过循环,1 2 3 4 5 6 8 9 10.
count = 1
while count <= 10:
print(count)
count = count + 1
while count == 7 :
count = count +1
count = 1
while count <=10:
if count != 7:
print(count)
count += 1
2.关键字:break
while True:
print(666)
break # 终止当前循环
print('结束')
# 练习:
# 通过break实现 1 ~ 10
count = 1
while True:
print(count)
if count == 10:
break
count = count + 1
print('结束')
# break是终止当前循环
while True:
print('你好')
while True:
print(666)
break
break
3.关键字:continue
如果遇到continue,则不再继续往下走,而是回到while条件判断位置
count = 1
while count <=10:
print(count)
continue # 本次循环如果遇到continue,则不在继续往下走,而是回到while条件位置。
count = count + 1
# 示例:1234568910
count = 1
while count <=10:
if count == 7:
count = count + 1
continue
print(count)
count = count + 1
while补充
- while else
count = 1
while count < 10:
print(count)
count = count + 1
else: # 不再满足while后的条件时,触发。 或 条件=False
print('ELSE代码块')
print('结束')
count = 1
while True:
print(count)
if count == 10:
break
count = count + 1
else: # 不再满足while后的条件时,触发。 或 条件=False
print('ELSE代码块')
print('结束')
4.字符串格式化:占位符
%s 字符串占位符
%d 整数占位符
%e 浮点数占位符
%% 输出一个%
- %s
# 字符串格式化存在的意义
name = input('姓名:')
do = input('在干什么:')
template = "%s在教室,%s。" %(name,do,)
print(template)
# 直接做占位符
template = "我是%s,年龄%s, 职业%s。" %("glacier",18,'学Python',)
print(template)
- %d(仅用于数字)
template = "我是%s,年龄%d, 职业%s。" %("alex",73,'讲鸡汤',)
print(template)
- %%
name = 'glacier'
template = "%s现在手机的电量是100%%" %(name,)
print(template)
- 练习
name = input('请输入姓名:')
age = input('请输入年龄:')
job = input('请输入职业:')
hobby = input('请输入爱好:')
msg = '''
------------ info of Alex Li ----------
Name : %s
Age : %s
job : %s
Hobbie: %s
------------- end ----------------'''
data = msg %(name,age,job,hobby,)
print(data)
5.运算符
算数运算
# 练习题: 1 ~ 100 之间所有的数相加。 total = 0 count = 1 while count <=100: total = total + count count = count + 1 print(total)赋值运算
count = 1 while count <= 100: print(count) count += 1 # count = count + 1逻辑运算
一般情况,用于做判断。
if 1 > 0 and 1 > 2: print('666')二般情况,用于做取值。
or
""" 对于 or,如果有遇到 value= 1 or 9 第一个值如果是转换成布尔值如果是真,则value=第一值。 第一个值如果是转换成布尔值如果是假,则value=第二值。 如果有多个or条件,则从左到右依次进行上述流程。 示例: v1 = 0 or 1 v2 = 8 or 10 v3 = 0 or 9 or 8 """and
""" 对于and,如果遇到 value= 1 and 9 这种情况 如果第一个值转换成布尔值是True,则value=第二个值。 如果第一个值转换成布尔值是False,则value=第一个值。 如果有多个and条件,则从左到右依次进行上述流程。 示例: v1 = 1 and 9 v2 = 1 and 0 v3 = 0 and 7 v4 = 0 and "" v5 = 1 and 0 and 9 """结合
# 先看and再看or v1 = 1 and 9 or 0 and 6 print(v1)其他
优先级 在没有()的情况下not 优先级高于 and,and优先级高于or,即优先级关系为(
)>not>and>or,同一优先级从左往右计算。字符串和整数 转换为布尔值的时候,只有''和 0 被转换为 False ,其他的都是 True
6.单位
8bit = 1byte
1024byte = 1KB
1024KB = 1MB
1024MB = 1GB
1024GB = 1TB
1024TB = 1PB
1024TB = 1EB
1024EB = 1ZB
1024ZB = 1YB
1024YB = 1NB
1024NB = 1DB
常用到TB就够了
今日注释
pycharm 快速多行加减注释 Ctrl + /
pycharm 快速复制 鼠标位置Ctrl + D 快速复制到下一行
Python day 02的更多相关文章
- Python学习02 列表 List
Python学习02 列表 List Python列表 List Python中的列表(List)用逗号分隔,方括号包围(comma-separated values (items) between ...
- Python网络02 Python服务器进化
原文:Python网络02 Python服务器进化 作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! **注意,在Python 3. ...
- python进阶02 特殊方法与特殊属性
python进阶02 特殊方法与特殊属性 一.初始化.析构 1.初始化 # python中有很多双下划线开头且以下划线结尾的固定方法,它们会在特定的时机被触发执行,这便是特殊方法 # 在实例化的时候就 ...
- Python模块02/序列化/os模块/sys模块/haslib加密/collections
Python模块02/序列化/os模块/sys模块/haslib加密/collections 内容大纲 1.序列化 2.os模块 3.sys模块 4.haslib加密 5.collections 1. ...
- Python函数02/函数的动态参数/函数的注释/名称空间/函数的嵌套/global以及nolocal的用法
Python函数02/函数的动态参数/函数的注释/名称空间/函数的嵌套/global以及nolocal的用法 目录 Python函数02/函数的动态参数/函数的注释/名称空间/函数的嵌套/global ...
- Python面向对象02/类的空间问题、类与对象之间的关系、类与类之间的关系
Python面向对象02/类的空间问题.类与对象之间的关系.类与类之间的关系 目录 Python面向对象02/类的空间问题.类与对象之间的关系.类与类之间的关系 1. 类的空间问题 2. 类与对象之间 ...
- (python函数02)列表生成式
(python函数02)列表生成式 示例代码 num = [i for i in range(1, 10)] print(num) num = [i for i in range(1, 10) ...
- 极简python教程02:基础变量,删繁就简
python极简教程已经开赛,如果错过说明可以回翻: 极简python教程:赛前说明 借这个机会,我再讲讲我的教程和其他网上的教程的区别: 1 我分享的内容,是我在工作中会高频使用的语法,是精华内容 ...
- Python学习--02输入和输出
命令行输入 x = input("Please input x:") y = raw_input("Please input x:") 使用input和raw_ ...
- python基础02 基本数据类型
摘要:简单的数据类型以及赋值 变量不需要声明 python的变量不需要声明,你可以直接输入: >>>a = 10 那么你的内存里就有了一个变量a, 它的值是10,它的类型是integ ...
随机推荐
- numpy(一)
np.zeros(10,dtype=int) #创建全为0的一位数组 np.ones((3,5),dtype=float) #创建3*5的二维全为1的数组 np.full((3,5),3.14) #创 ...
- WIN7远程桌面连接--“发生身份验证错误。要求的函数不受支持”
故障现象:WIN7发现远程桌面无法连接了,报“发生身份验证错误.要求的函数不受支持”的错误: 解决办法:开始菜单->运行gpedit.msc 打开配置项:计算机配置>管理模板>系统& ...
- FreeSwitch 终端命令详细介绍
FreeSwitch版本:1.6.9 以下为部分终端命令 alias 语法: alias [add|stickyadd] <alias> <command> | del [&l ...
- 使用easyui搭建网页架子
使用踩坑: 一.弹出框上datagrid第二次加载数据,必须在显示状态,datagrid加载数据才会渲染,否则是空白 $('#xq_selKs').window('open').window('cen ...
- table-cell width:1% 深入理解
问题描述 今天在使用Bootstrap给页面添加底部导航栏时,需要在手机下也使导航栏呈现水平排列的效果.最后在网上查找解决方法是,看到这样一个解决方法: .nav-justified > li ...
- ln -s软链接文件算文件吗
场景: 开发A在windows环境下完成了开发,配置管理员cm搭建jenkins在centos环境下编译,cm编译失败,但是开发A在他的windows环境下可以编译过,最后发现是某几个so文件的软链接 ...
- JavaScript 当前URL取参返回字典
getParam : function(){ return (key, strURL = window.location.search) => new RegExp("(^|\\?|& ...
- git本地分支与远程分支
github上已经有master分支 和dev分支 在本地 git checkout -b dev 新建并切换到本地dev分支 git pull origin dev 本地分支与远程分支相关联 在本地 ...
- Back up and restore information in Firefox profiles
Click the menu button , click Help and select Troubleshooting Information. The Troubleshooting Infor ...
- spring-boot 参考链接
http://blog.csdn.net/jsu_9207/article/details/66472096 http://blog.csdn.net/lu1005287365/article/det ...