02-python--基础二

一、格式化输出

# 1、格式化输出 %:占位符 s:字符串 d:数字 %%:转义
name = input("your name:  ")
age = int(input("your age:  "))
height = int(input("your height:  "))
msg = "name: %s, age: %d, height: %d" %(name, age, height)
print(msg)

name = input("your name:  ")
age = int(input("your age:  "))
job = input("your job:  ")
hobbie = input("your hobbie:  ")
msg = '''------------- index of %s -------------
name = %s
age = %d
job = %s
hobbie = %s
------------- end of %s -------------
''' % (name, name, age, job, hobbie, name)
print(msg)

name = input("your name:  ")
age = int(input("your age:  "))
height = int(input("your height:  "))
msg = "name: %s, age: %d, height: %d,xx:  3%%" % (name, age, height)    # %用于转义
print(msg)

# 2、while-else 如果while没有被break打断则会执行else中的语句,否则不会
count = 0
while count <= 5:
    count += 1
    if count == 3:
        break
else:
    print("over")
View Code

二、运算符

  算数运算符:+ - * / % **

  比较运算符:== >= <= != < >

  赋值运算符:= += -= *= /=

  逻辑运算符:and or not

  * 判断结果不多赘述,切记计算机很懒,能确定结果就不会继续进行计算即可。

三、编码

  8bit = 1byte 1024byte = 1KB 1024KB = 1MB 1024MB = 1GB 1024GB = 1TB 1024TB = 1PB 1024TB = 1EB 1024EB = 1ZB 1024ZB = 1YB 1024YB = 1NB 1024NB = 1DB

  gbk:包含英文字母、数字、特殊字符和中文

    英文一个字节表示

    中文两个字节表示

  unicode:万国码

    英文两个字节表示

    中文四个字节表示

  utf-8:

    英文一个字节表示

    欧洲两个字节表示

    中文三个字节表示

  ascii:只包含英文字母、数字、特殊字符

    一个字节

四、转换
 
# str
# s1 = 'taiBAi'
# print(s1.capitalize())
# print(s1.swapcase())
# msg = 'tai-bai-dfl'
# print(msg.title())
# s2 = 'barry'
# print(s1.center(20, '*'))

# find:通过元素找索引,找不到返回-1
# index:通过元素找索引,找不到报错
# print(s2.find('r'))
# print(s2.index('y'))

# tuple:如果只有一个元素并且没有逗号,那么它不是元祖。数据类型为元素的数据类型一致
# tu1 = (2)
# print(tu1, type(tu1))

# tu1 = (1, 2, 3, 3, 2, 4, 1, 3, 3, 3)
# print(tu1.count(3))
# print(tu1.index(4))

# list
# li = ['d', 'f', 1, 3]
# print(li.index('f'))

# l1 = [3,4,5,6,12,3,5,7,2,1]
# l1.sort()
# l1.sort(reverse=True)
# l1.reverse()
# print(l1)

# l1 = [1, 2, 3]
# l2 = [1, 'db', '123', '321']
# print(l1 + l2)

# l1 = [1, 2, 3]
# print(l1 * 3)
#
# for i in range(5, 0, -1):
# print(i)
#
# l1 = [11, 22, 33, 44, 55]
# 索引单数删除元素
# l2 = []
# for index in range(len(l1)):
# if index % 2 == 0:
# l2.append(l1[index])
# l1 = l2
# print(l1)

# del l1[1::2]
# print(l1)

# for index in range(len(l1) - 1, -1, -1):
# if index % 2 == 1:
# l1.pop(index)
# print(l1)

# dict
dic1 = {'name': 'dfg', 'age': 18}
dic2 = {'name': 'dfl', 'hobby': 'sport'}
# dic.update(hobbt='sport', hight='178')
# print(dic)
# dic.update(name='dfggggggg')
# print(dic)
# dic.update([(1, 'a'), (2, 'b')])
# print(dic)
# dic1.update(dic2) # 有则更新,没有则添加
# print(dic1)

# fromkeys
# dic3 = dict.fromkeys('asdb', 100)
# dic4 = dict.fromkeys([1, 2, 3], 200)
# print(dic3)
# print(dic4)

# dic5 = dict.fromkeys([1, 2, 3], [])
# dic5[1].append(666)
# print(dic5)

# 循环一个字典时,改变字典大小会报错
dic6 = {'k1': 1, 'k2': 2, 'k3': 3, 'age': 18}
l6 = []
# 报错的
# for index in dic6.keys():
# if 'k' in index:
# dic6.pop(index)

# for index in dic6.keys():
# if 'k' in index:
# l6.append(index)
# for i in l6:
# dic6.pop(i)
# print(dic6)

# for index in tuple(dic6.keys()):
# for index in list(dic6.keys()):
# if 'k' in index:
# dic6.pop(index)
# print(dic6)
原文地址:https://www.cnblogs.com/Daspig/p/12591432.html