day02 基础&运算符

1.循环语句

day02 基础&运算符
day02 基础&运算符

  1. if-else

    score = input('请输入成绩:')
    score_int = int(score)
    
    if score_int >= 90:
        print('A')
    elif score_int >= 80:
        print('B')
    elif score_int >= 70:
        print('C')
    else:
        print('D')
    
  2. 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('输入错误')
    
    
  3. while循环

    while True:
        print('人生苦短,我用Python.')
        
    while 1>0 and 2>1:
        print('人生苦短,我用Python。')
    
  4. 请通过循环,将count每次循环都+1

    count = 1
    while True:
        count = count + 2
        print(count)
        
    #练习
    while True:
        count = 1
        print(count)
        count = count + 1
    
  5. 请通过循环,打1,2,3,4,... 10

    count = 1
    while count <= 10:
         print(count)
         count = count + 1
    
  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 <= 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,则不再继续往下走,而是回到循环条件位置,
        count = count + 1
    '''
    
    #示例: 1 2 3 4 5 6 8 9 10
    '''
    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. 总结

  • while基本结构
  • break
  • continue
  • while else

2.字符串格式化

  1. %s
  2. %d
  3. %%
#字符串格式化存在的意义
'''
name = input('姓名:')
do = input('在干什么:')

template = "%s在教室,%s。"%(name,do,)
print(template)
'''

#直接做占位符
# template = '我是%s,年龄%s,职业%s.'%('alex',73,'讲鸡汤',)
# print(template)

# name = 'alex'
# 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)

3.运算符

#算术运算符
# value = 11 % 3
# print(value)

#练习题:通过循环打印1-100中的奇数

# count = 1
# while count <= 100:
#    val = count % 2
#    if val == 1:
#        print(count)
#     count = count + 1

# val = 2 ** 8
# print(val)

# val = 9/2
# print(val)

# val = 9//2
# print(val)

#练习题:打印1~100之间相加
'''
total = 0
count = 1
while count <= 100:
    total = total + count
    count = count + 1
print(total)
'''


total = 0
# total1 = 0
# total2 = 0
'''
count = 1
while count <= 100:
    val = count % 2
    if val == 1:
        total1 = total - count
    else:
        total2 = total + count
    count = count + 1
total = total1 + total2
print(total)
'''

#逻辑运算符 and or not
#一般情况下
# if 1>0 or 1>2:
#     print('666')

# c+=a 等效于c=c+a

#二般情况
##小知识
# - int
# - str
# - bool

# v1 = '666'
# v2 = int(v1)

#数字转布尔值
# v1 = 0
# v2 = bool(v1)
# print(v2)

#字符串转布尔值
# v1 = '0'
# v2 = bool(v1)
# print(v2)

#布尔值转换其他
# v1 = True
# v2 = int(v1)
# print(v2)

# v1 = True
# v2 = str(v1)
# print(v2)

#转换
'''
    - 字符串转数字
    - 数字转字符串
    - “”、0 转换布尔值之后是False,其余是True
'''

#对于or,如果有遇到
'''
    value = 1 or 9
    第一个值如果转换成布尔值是真则等于第一个值
    第一个值如果转换成布尔值是假则等于第二个值
    如果有多个or条件,则从左到右依次进行上述的流程
    示例:
        v1 = 0 or 1
        v2 = 8 or 10
        v3 = 0 or 9 or 8
'''
# value = 0 or 9
# print(value)

# value = 1 or 9
# print(value)

# value = 0 or ''
# print('--->',value,'<-----')

#对于and ,如果遇到
'''
  如果第一个值转换成布尔值是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  
'''
# v1 = 1 and 9
# print(v1)

# v2 = 1 and 0
# print(v1)

# v3 = 0 and 7
# print(v3)

# v4 = 0 and ''
# print(v4)

# v5 = 1 and 0 and 9
# print(v5)

#综合
#先看and再看or
# v1 = 1 and 9 or 0 and 6

4.编码

  • 编码扩展
    • ascii
    • unicode
      • ecs2
      • ecs4
    • utf-8 ,中文用3个字节
    • utf-16
    • gbk,中文用2个字节
    • gb2312,中文用2个字节
  • 单位
    • 8bit = 1byte
    • 1024byte = 1KB
    • 1024KB = 1MB
    • 1024MB = 1KB
    • 1024KB = 1GB
    • 1024GB = 1TB
    • 1024TB = 1PB
上一篇:day02


下一篇:JavaScript_day02