基本数据类型第二篇

今日内容

  • 内容补充

  • 循环语句

  • 字符串格式化

  • 运算符

  • 编码

  • 单位

内容补充

条件语句补充

  • if条件语句的嵌套
'''需求打印10086服务选项
---------欢迎致电10086---------
1、套餐服务
2、投诉建议
3、业务办理
4、人工服务
让用户选择服务类型,并在业务办理板块加入流量业务及宽带业务让用户选择业务
'''
#!/usr/bin/env python
# -*- coding:utf-8 -*-
print('''						
---------欢迎致电10086---------
1、套餐服务
2、投诉建议
3、业务办理
4、人工服务
''')
template = input('请输入要办理的业务:')		#等待用户输入办理业务
template_int = int(template)
if template_int == 1:					   #判断输入业务
    print('套餐服务')
elif template_int == 2:
    print('投诉建议')
elif template_int == 3:					   #嵌套if重新判断用户输入服务类型
    print('''
---------业务办理 - --------
1、流量业务
2、宽带业务 
    ''' )
    index = input('请输入要办理的服务:')
    index_int = int(index)
    if index == 1:
        print('流量业务')
    else:
        print('宽带业务')
else:
    print('人工服务')

while循环语句

  • 基本while循环
#需求:死循环打印'我是帅哥'
while True:				#循环后跟条件,最后都会转化为布尔类型进行判断
    print('我是帅哥')	 #执行结束,重新判断条件是否满足
  • while循环的嵌套
#需求:循环打印一次我是帅哥,再打印两次才怪
score = 1
while True:
    print('我是帅哥')
    while score < 3:
        print('才怪')
        score += 1
    break
  • break 终止当前循环
#需求:终止死循环我是帅哥
while True:
    print('我是帅哥')
    break
  • contunue 结束本次循环开始下次循环
#需求:打印1 2 3 4 5 6 8 9 10
count = 1
while count <= 10:
    if count == 7:
        count += 1
        continue
    print(count)
    count += 1
  • while...else
#格式
while 条件:
    代码块		  #当代码块中break跳出循环时不执行else中的代码块
else:			#while条件不满足时才会执行
    代码块

字符串格式化

  • 占位符
    • %s
    • %d
  • 含有占位符的字符串想输出%时要多加一个% 即%%
  • 字符串格式化时要在变量最后加一个 ','
#需求让用户输入个人信息并打印个人信息
name = input('请输入姓名:')			#用户输入个人信息
age = input('请输入年龄:')
gender = input('请输入性别:')
index = input('请输入手机电量:')
age = int(age)
index = int(age)
template = """						#字符串格式化
-----个人信息-----
name  :%s
age   :%d
gender:%s
index :%d%%
-----------------
"""
print(template %(name, age, gender, index, )) #字符串格式化在最后要加一个','

运算符

  • 算术运算符 : + - * / %取余 **次方 //取整
  • 比较运算符: == > < >= <= !=不等于 ><不等于
  • 赋值运算符: = += -= *= /= %= **= //=
  • 成员运算符: in not in
  • 逻辑运算符: not and or
  • 面试题
value = not 1 or 9 and 0 	
print(value)			#0

  • 逻辑运算中优先级 () > 算数运算 > 比较运算 > not > and > or

编码

  • ascii
  • unicode
    • ecs2 用16位表示
    • ecs4 用32为表示
  • utf-8 中文用三个字节表示,虽然gbk看似节省空间但utf-8国际通用所以推荐用utf-8编码
  • gbk 中文用两个字节表示
  • gb2312

单位进制

8位 == 1byte

1024byte == 1KB

1024KB == 1MB

1024MB == 1GB

1024GB == 1TB

原文地址:https://www.cnblogs.com/zhushengdong/p/13986719.html