python基础二
一、循环
1.while 循环 while 条件: 循环体 else: 当上面条件为假,才会执行 执行顺序: 判断条件是否为真. 如果真. 执行循环体. 然后再次判断条件....直到循 环条件为假. 程序退出 #求100内所有整数和 sum=0 i=1 while i <=100: sum=sum+i i+=1 print(sum)
2. break和continue break: 停止当前本层循环 continue: 停止当前本次循环. 继续执行下一次循环 continue:用来排除一些东西 # count = 1 # while count <= 10: # if count == 4: # count = count + 1 # continue # 用来排除一些内容 # print(count) # count = count + 1 break:跳出循环 # count = 1 # while count <= 20: # if count == 10: # break # 不会触发else的执行, while...else...是一个整体. break的时候彻底的停止这个整体 # print(count) # count = count + 1 # else: # 当上面的条件不成立的时候执行这个else中的代码 # print("数完了") #输入一个整数,判断是否为质数 # number=int(input("请输入一个数:")) # count=2 # while count<number: # if number%count==0: # print("这个数不是质数") # break # count = count + 1 # else: # print("这个数是质数") #输入一个数,判断多少位 # number=int(input("请输入数字:")) # count=1 # while True: # if number<(10**count): # print("这个数是"+str(count)+"位数") # break # else: # count=count+1
二、格式化输出和运算符
3. 格式化输出 %s 占位字符串, 全能的. 什么都能接 %d 占位数字 # name="alex" # age = 38 # hobby = "浪" # location = "湖边" # # # print(age+"岁的"+name+"在"+location+"喜欢"+hobby) # # # 格式化 # # %s 占位. 占位的是字符串, 全能的. 什么都能接 # # %d 占位. 占位的是数字 # print("%s岁的%s在%s喜欢%s" % (age, name, location, hobby)) # name = input("请输入名字:") # age = input("请输入年龄:") # job = input("请输入你的工作:") # hobby = input("请输入你的爱好:") # # s = '''------------ info of %s ----------- # Name : %s # Age : %s # job : %s # Hobbie: %s # ------------- end -----------------''' % (name, name, age, job, hobby) # # print(s) # name = 'sylar' # # 如果你的字符串中出现了%s这样的格式化的内容. 后面的%都认为是格式化.如果想要使用%. 需要转义 %% # print("我叫%s, 我已经学习了2%%的python了" % (name)) # # print("我已经完成50%工作了")
4. 运算符 and: 并且, 两端同时为真. 结果才能是真 or: 或者, 有一个是真. 结果就是真 not: 非真既假, 非假既真 顺序: () => not => and => or x or y: 如果x是零, 输出y 如果x是非零, 输出x True: 非零 False: 零 # x or y 如果x是0 返回y, 如果x是非零, 返回x # print(1 or 2) # 1 # print(1 or 0) # 1 # print(0 or 1) # 1 # print(0 or 2) # 2 # print(0 or 1 or 2 or 3) # print(3 or 0 or 1 or 0 or 2) # and和or相反. 不要去总结and. 记住or # print(1 and 2) # 2 # print(0 and 2) # 0 # print(1 and 0) # 0 # print(0 and 1) # 0 # print(1 and 2 or 3) # print(1 or 2 and 3) # False: 0, True: 1(非零) # print(1 and 2>3) # print(2>3 and 1)
三、编码
编码 1. ascii. 最早的编码. 至今还在使用. 8位一个字节(字符) 2. GBK. 国标码. 16位2个字节. 3. unicode. 万国码. 32位4个字节 4. UTF-8. 可变长度的unicode. 英文: 8位. 1个字节 欧洲文字:16位. 2个字节 汉字. 24位. 3个字节 8bit = 1byte 1024byte = 1KB 1024KB = 1MB 1024MB = 1GB 1024GB = 1TB