python基本语法(二)

基础&运算符

1. 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('输入错误')

2. pycharm变更解释器

3. 循环语句

1. 循环打印 “人生苦短,我用Python。”

while True: 
    print('人生苦短,我用Python。')

2. while后加入条件

while 1>0 and 2>1:
    print('人生苦短,我用Python。')

3. 数字相加

"""
count = 1
value = count + 1
print(value)
"""
"""
count = 1
count = count + 1
print(count)
"""

4.请通过循环,让count每次循环都 + 1 .

"""
count = 1
while True:
    print(count)
    count = count + 1
"""
# 练习
"""
while True:
    count = 1
    print(count)
    count = count + 1
"""

5. 请通过循环,1 2 3 .. 10.

count = 1
while count <= 10:
    print(count)
    count = count + 1
print('结束')

6. 请通过循环,1 2 3 4 5 6 8 9 10.

"""
# 错误示例
count = 1
while count <= 10 and count != 7 :
    print(count)
    count = count + 1
"""
"""
# 正确
count = 1
while count <= 6:
    print(count)
    count = count + 1
count = 8
while count <= 10:
    print(count)
    count = count + 1
"""
"""
# 正确
count = 1
while count <= 10:
    if count != 7:
    print(count)
    count = count + 1
"""
"""
# 正确
count = 1
while count <= 10:
if count == 7:
    pass
else:
    print(count)
    count = count + 1
"""

7. 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
"""

8. continue

"""
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

9. 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('结束')
"""

10. 其他

  • 快速注释 ctrl+?

  • pycharm断点

4. 字符串格式化

1. %s

# 字符串格式化存在的意义
name = input('姓名:')
do = input('在干什么:')
template = "%s在教室,%s。" %(name,do,)
print(template)
# 直接做占位符
# template = "我是%s,年龄%s, 职业%s。" %("alex",73,'讲鸡汤',)
# print(template)

2. %d

# template = "我是%s,年龄%d, 职业%s。" %("alex",73,'讲鸡汤',)
# print(template)

3. %%

# name = 'alex'
# template = "%s现在手机的电量是100%%" %(name,)
# print(template)

4. 练习

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)
# 练习题:打印 1 ~ 100 之间的奇数。
count = 1
while count <= 100:
    val = count % 2
    if val == 1:
        print(count)
    count = count + 1

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,同一优先级从左往右计算。

      • 数据类型转换

        ## 小知识
        # - int
        # - str
        # - bool
        # 数字转字符串
        # v1 = 666
        # v2 = str(v1)
        # 字符串转数字
        # v1 = "666"
        # v2 = int(v1)
        # 数字转布尔值
        # v1 = 0
        # v2 = bool(v1)
        # print(v2)
        # 字符串转布尔值
        # v1 = ""
        # v2 = bool(v1)
        # print(v2)
        # 布尔值转换其他
        # v1 = True
        # v2 = str(v1)
        # print(v2)
        # 需要掌握的转换知识点
        """
        - 字符串转数字
        - 数字转字符串
        - "" / 0 转换布尔之后是False
        """

6. 编码

  • 编码扩展

    • ascii

    • unicode

      • ecs2 在以前用两个字节表示一个字符

      • ecs4 现在大部分用四个字节表示一个字符

    • utf-8,中文用3字节。

    • utf-16

    • gbk,中文用2字节。

    • gb2312,中文用2字节。

  • 单位

    8bit = 1byte
    1024byte = 1KB
    1024KB = 1MB
    1024MB = 1GB
    1024GB = 1TB
    1024TB = 1PB
    1024TB = 1EB
    1024EB = 1ZB
    1024ZB = 1YB
    1024YB = 1NB
    1024NB = 1DB
    常⽤到TB就够了
每天学习新的知识,会让自己更加充实
原文地址:https://www.cnblogs.com/fengpiaoluoye/p/15119945.html