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 ...
随机推荐
- Autofac之生命周期和事件
Autofac为注册的类型对象提供了一套生命周期事件,覆盖了一个类型从注册到最后“释放”的一套事件.有了这些事件,我们可以相对方便的在类型对象的各个阶段进行AOP操作. builder.Registe ...
- linux和shell的学习记录
1.16条常用的命令 .文件的权限修改:(把文件1.txt的归属改为mysql的,然后ll查看) chown mysql:mysql .txt .增加当前用户的x权限,然后ll查看: chomd u+ ...
- PDM:Training Models of Shape from Sets of Examples
这篇论文介绍了一种创建柔性形状模型(Flexible Shape Models)的方法--点分布模型(Point Distribution Model).该方法使用一系列标记点来表示形状,重要的是根据 ...
- LG3834 可持久化线段树1
题意 给定\(N\)个整数构成的序列,将对于指定的闭区间查询其区间内的第\(K\)小值. $n \leq 2 \times 10^5 $ 思路 在\([l,r]\)区间内的数的个数,可以用\(sum[ ...
- elasticsearch 之编译过程
https://github.com/elastic/elasticsearch/blob/master/CONTRIBUTING.md 不同的版本需要指定JDK 可以下载openJDK版本到服务器上 ...
- PyAutoGUI使用
PyAutoGUI是一个纯Python的GUI自动化工具,其目的是可以用程序自动控制鼠标和键盘操作,利用它可以实现自动化任务,再也不用担心有重复枯燥的任务了. 安装: pip install pyau ...
- Java多线程处理List数据
实例1: 解决问题:如何让n个线程顺序遍历含有n个元素的List集合 import java.util.ArrayList; import java.util.List; import org.apa ...
- css画斜线(用于时间的显示)
- Vue系列之 => MintUI初使用
安装 npm i mint-ui -S main.js import Vue from 'vue' // 1,导入 vue-router包 import vueRouter from 'vue-rou ...
- ajax的网上解析
/* 用XMLHTTPRequest来进行ajax异步数据交交互*/ 主要有几个步骤: //1.创建XMLHTTPRequest对象 //最复杂的一步 if (window.XMLHttpReques ...