基础&运算符

今日概要

  1. 循环
  2. 字符串格式化
  3. 运算符
  4. 编码

内容回顾 & 补充

内容回顾

  1. 计算机基础
  2. 解释器python 2 和 python 3
  3. 语法
    • print
    • 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 浮点数占位符
%% 输出一个%

  1. %s
# 字符串格式化存在的意义
name = input('姓名:')
do = input('在干什么:')
template = "%s在教室,%s。" %(name,do,)
print(template)
# 直接做占位符
template = "我是%s,年龄%s, 职业%s。" %("glacier",18,'学Python',)
print(template)
  1. %d(仅用于数字)
template = "我是%s,年龄%d, 职业%s。" %("alex",73,'讲鸡汤',)
print(template)
  1. %%
name = 'glacier'
template = "%s现在手机的电量是100%%" %(name,)
print(template)
  1. 练习
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. 算数运算

    # 练习题: 1 ~ 100 之间所有的数相加。
    total = 0
    count = 1
    while count <=100:
     total = total + count
     count = count + 1
    print(total)
  2. 赋值运算

    count = 1
    while count <= 100:
        print(count)
        count += 1 # count = count + 1
  3. 逻辑运算

    • 一般情况,用于做判断。

      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的更多相关文章

  1. Python学习02 列表 List

    Python学习02 列表 List Python列表 List Python中的列表(List)用逗号分隔,方括号包围(comma-separated values (items) between ...

  2. Python网络02 Python服务器进化

    原文:Python网络02 Python服务器进化 作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! **注意,在Python 3. ...

  3. python进阶02 特殊方法与特殊属性

    python进阶02 特殊方法与特殊属性 一.初始化.析构 1.初始化 # python中有很多双下划线开头且以下划线结尾的固定方法,它们会在特定的时机被触发执行,这便是特殊方法 # 在实例化的时候就 ...

  4. Python模块02/序列化/os模块/sys模块/haslib加密/collections

    Python模块02/序列化/os模块/sys模块/haslib加密/collections 内容大纲 1.序列化 2.os模块 3.sys模块 4.haslib加密 5.collections 1. ...

  5. Python函数02/函数的动态参数/函数的注释/名称空间/函数的嵌套/global以及nolocal的用法

    Python函数02/函数的动态参数/函数的注释/名称空间/函数的嵌套/global以及nolocal的用法 目录 Python函数02/函数的动态参数/函数的注释/名称空间/函数的嵌套/global ...

  6. Python面向对象02/类的空间问题、类与对象之间的关系、类与类之间的关系

    Python面向对象02/类的空间问题.类与对象之间的关系.类与类之间的关系 目录 Python面向对象02/类的空间问题.类与对象之间的关系.类与类之间的关系 1. 类的空间问题 2. 类与对象之间 ...

  7. (python函数02)列表生成式

    (python函数02)列表生成式 示例代码  num = [i for i in range(1, 10)] print(num) ​ num = [i for i in range(1, 10) ...

  8. 极简python教程02:基础变量,删繁就简

    python极简教程已经开赛,如果错过说明可以回翻: 极简python教程:赛前说明 借这个机会,我再讲讲我的教程和其他网上的教程的区别: 1 我分享的内容,是我在工作中会高频使用的语法,是精华内容 ...

  9. Python学习--02输入和输出

    命令行输入 x = input("Please input x:") y = raw_input("Please input x:") 使用input和raw_ ...

  10. python基础02 基本数据类型

    摘要:简单的数据类型以及赋值 变量不需要声明 python的变量不需要声明,你可以直接输入: >>>a = 10 那么你的内存里就有了一个变量a, 它的值是10,它的类型是integ ...

随机推荐

  1. nginx匹配规则说明以及匹配的优先级

    location 匹配规则语法规则 location [=|~|~*|^~] /uri/ { … } 模式    含义location = /uri    = 表示精确匹配,只有完全匹配上才能生效lo ...

  2. spring batch (一) 常见的基本的概念介绍

    SpringBatch的基本概念介绍 内容来自<Spring Batch 批处理框架>,作者:刘相. 一.配置文件 在项目中使用spring batch 需要在配置文件中声明: 事务管理器 ...

  3. Oracle数据库查询所有关键字

    管理员账户登录后,执行以下命令:  select * from v$reserved_words

  4. 我的FPGA之旅4---led流水灯

    [1]输入端口不能使用reg数据类型,因为reg类型对应的FPGA内部的寄存器.这样理解:reg寄存器具有记忆功能;而wire类型数据就相当于一根连线.input输入信号用wire连线进来就好:out ...

  5. Keras RetinaNet github项目安装

    在存储库目录/keras-retinanet/中,执行pip install . --user 后,出现错误: D:\>cd D:\JupyterWorkSpace\keras-retinane ...

  6. 20.C# 创建自己的泛型类型

    1.定义泛型类 可以使用以下语法创建泛型类,T可以是任意符合C#标识符命名规范的任意标识符 class MyGenericClass<T> { //.... } 泛型类可以包含任意多个类型 ...

  7. Cocos Creator 橡皮差(刮刮卡)功能(转)

    实现一个刮刮卡的效果,于是在论坛里搜集了一些资料并且看了一下CCMask的源码,做出来一套可用的教程,分享给大家.(WEBGL和Native端测试可用) maskNode是详细设置如下 我们在 scr ...

  8. php----------const 定义的常量和define()定义的常量的区别?

    用法一:const用于类成员变量,一经定义不可修改,define用于全局常量,不可用于类成员变量的定义,const可在类中使用也可以在类外面使用,define不能. 定义:const 常量名=值; 没 ...

  9. Object:所有类的超类

    Java中每个类都是由Object类扩展而来 1.equals方法 在Object类中,这个方法用于判断两个对象是否具有相同的引用,然而对于大多数类来说,经常需要检测两个对象状态的相等性. publi ...

  10. StrictRedis

    StrictRedis对象⽅法 通过init创建对象,指定参数host.port与指定的服务器和端⼝连接,host默认为localhost,port默认为6379,db默认为0 sr = Strict ...