标签:ascii码 占用 优化 ges 标准 执行 usr gbk wan
一 python第一个程序
print(‘hello world!‘) # python3.x print ‘hello world!‘ # python2.x
二 变量
2.1 变量名称规则
[‘and‘, ‘as‘, ‘assert‘, ‘break‘, ‘class‘, ‘continue‘, ‘def‘, ‘del‘, ‘elif‘, ‘else‘, ‘except‘, ‘exec‘, ‘finally‘, ‘for‘, ‘from‘, ‘global‘, ‘if‘, ‘import‘, ‘in‘, ‘is‘, ‘lambda‘, ‘not‘, ‘or‘, ‘pass‘, ‘print‘, ‘raise‘, ‘return‘, ‘try‘, ‘while‘, ‘with‘, ‘yield‘]
2.2 案例
_name = ‘sam‘ # 正确 1name = ‘yang‘ # 错误 name_it_1 = ‘yuyu‘ # 正确
2.3 代码案例
#!/usr/bin/env python3 # encoding: utf-8 # Author: Sam Gao # print("Hello world") name = "Sam Gao" # name -----> "Sam Gao" name2 = name # name -----> "Sam Gao" and name2 -----> "Sam Gao" print(‘My name is‘, name, name2) # 输出: My name is Sam Gao Sam Gao name = ‘pao che ge‘ # name -----> "pao che ge" and name2 -----> "Sam Gao" print(name, name2) # 输出: pao che ge Sam Gao
三 字符编码
3.1 ascii
ASCII(American Standard Code for Information Interchange,美国标准信息交换代码)是基于拉丁字母的一套电脑编码系统,主要用于显示现代英语和其他西欧语言,其最多只能用 8 位来表示(一个字节),即:2**8 = 256-1,所以,ASCII码最多只能表示 255 个符号。注:python2.x 默认使用ascii编码
3.2 中文字符集
GB2312
一共收录了7445个字符,包括6763个汉字和682个其它符号。汉字区的内码范围高字节从B0-F7,低字节从A1-FE,占用的码位是72*94=6768。其中有5个空位是D7FA-D7FE。
GBK
GB2312 支持的汉字太少。1995年的汉字扩展规范GBK1.0收录了21886个符号,它分为汉字区和图形符号区。汉字区包括21003个字符。
GB18030
2000年的 GB18030是取代GBK1.0的正式国家标准。该标准收录了27484个汉字,同时还收录了藏文、蒙文、维吾尔文等主要的少数民族文字。现在的PC平台必须支持GB18030,对嵌入式产品暂不作要求。所以手机、MP3一般只支持GB2312。
从ASCII、GB2312、GBK 到GB18030,这些编码方法是向下兼容的,即同一个字符在这些方案中总是有相同的编码,后面的标准支持更多的字符。在这些编码中,英文和中文可以统一地处理。区分中文编码的方法是高字节的最高位不为0。
按照程序员的称呼,GB2312、GBK到GB18030都属于双字节字符集 (DBCS)。繁体中文,big5。
GB2312 GBK GB18030 占两个字节,并且能够向下兼容 GB18030 --> GBK --> GB2312 --> ASCILL
3.3 unicode
Unicode(统一码、万国码、单一码)是一种在计算机上使用的字符编码。Unicode 是为了解决传统的字符编码方案的局限而产生的,它为每种语言中的每个字符设定了统一并且唯一的二进制编码,规定虽有的字符和符号最少由 16 位来表示(2个字节)
注:python3.x 默认使用unicode编码
3.4 utf-8
UTF-8,是对Unicode编码的压缩和优化,他不再使用最少使用2个字节,而是将所有的字符和符号进行分类:ascii码中的内容用1个字节保存、欧洲的字符用2个字节保存,东亚的字符用3个字节保存...
3.5 案例
#!/usr/bin/env python # encoding: utf-8 # 告诉解释器用utf-8编码 # Author: Sam Gao name = ‘高绍阳‘ # python2.x print name
#!/usr/bin/env python3 # Author: Sam Gao name = ‘高绍阳‘ # 默认用unicode编码 print(name)
四 input 输入
4.1 简单输入
#!/usr/bin/env python3 # encoding: utf-8 # Author: Sam Gao import getpass _username = ‘sam‘ _password = ‘123456‘ username = input(‘username:‘) passwd = getpass.getpass(‘password:‘) # 在pycharm里面用不了,只能在交互式或者 在linux里以 python3 passwd.py 执行,作用:不会显示输入内容 if _username == username and _password == passwd: print(‘welcome user {name} login‘.format(name=username)) else: print(‘Invaild username or password‘)
4.2 案例 猜年龄游戏
#!/usr/bin/env python3 # encoding: utf-8 # Author: Sam Gao age_of_sam = 28 while True: guess_age = int(input(‘guess age:‘)) if age_of_sam == guess_age: print(‘yes, you got it‘) elif age_of_sam > guess_age: print(‘think big‘) else: print(‘think small‘)
五 while for循环 和 if判断
5.1 while True:
# 参考 4.2 案例 猜年龄游戏 当不知道循环到哪里的时候,就使用
5.2 案例
知识点:1. 有while......else.....的形式; 2. break和continus的用法;if 和 if.....elif...else 和 if...else
#!/usr/bin/env python3 # encoding: utf-8 # Author: Sam Gao age_of_sam = 28 count = 0 ‘‘‘ 猜三次,如果猜正确,则退出 ‘‘‘ # while True: # if count == 3: # break # # guess_age = int(input(‘guess age:‘)) # # if age_of_sam == guess_age: # print(‘yes, you got it‘) # break # # elif age_of_sam > guess_age: # print(‘think big‘) # else: # print(‘think small‘) # # count += 1 ##################################################### # while count < 3: # guess_age = int(input(‘guess age:‘)) # # if age_of_sam == guess_age: # print(‘yes, you got it‘) # break # # elif age_of_sam > guess_age: # print(‘think big‘) # else: # print(‘think small‘) # # count += 1 # if count == 3: # print(‘you guess too many times, fuck you !‘) ###################################################### # 或者 while count < 3: guess_age = int(input(‘guess age:‘)) if age_of_sam == guess_age: print(‘yes, you got it‘) break elif age_of_sam > guess_age: print(‘think big‘) else: print(‘think small‘) count += 1 else: print(‘you guess too many times, fuck you !‘)
5.3 使用for只玩3次
#!/usr/bin/env python3 # encoding: utf-8 # Author: Sam Gao age_of_sam = 28 for i in range(3): guess_age = int(input(‘guess age:‘)) if age_of_sam == guess_age: print(‘yes, you got it‘) break elif age_of_sam > guess_age: print(‘think big‘) else: print(‘think small‘) else: print(‘you guess too many times, fuck you !‘) for i in range(1, 12, 3): # 3代表步长 print(‘loop:‘, i)
5.4 任性玩5.2的游戏
#!/usr/bin/env python3 # encoding: utf-8 # Author: Sam Gao age_of_sam = 28 count = 0 while count < 3: # while True: guess_age = int(input(‘guess age:‘)) if age_of_sam == guess_age: print(‘yes, you got it‘) break # break 结束整个循环,不结束父循环 elif age_of_sam > guess_age: print(‘think big‘) else: print(‘think small‘) count += 1 if count == 3: while True: continue_play = input(‘Do you want to play:[yes/no]:‘) if continue_play == ‘yes‘: count = 0 break elif continue_play == ‘no‘: break else: print(‘pls input valid string‘) continue # continus 跳出当前循环 else: print(‘you guess too many times, fuck you !‘)
六 字符串格式化
案例
#!/usr/bin/env python3 # encoding: utf-8 # Author: Sam Gao # username = input(‘usrname:‘) # password = input(‘password:‘) # # print(‘username:‘, username) # print(‘password:‘, password) name = input(‘name:‘) # ===== python2 raw_input python3 eval(input) ===== python2 input age = int(input(‘age:‘)) # 默认输出的是字符串 job = input(‘job:‘) salary = int(input(‘salary:‘)) print(‘name:‘, type(name), ‘age‘, type(age)) info = ‘‘‘ ----------------info of %s--------------- Name: %s Age: %d Job: %s Salary: %d ‘‘‘ % (name, name, age, job, salary) # %s 字符串 %d 整数 %f 浮点数 info2 = ‘‘‘ ----------------info of {_name}--------------- Name: {_name} Age: {_age} Job: {_job} Salary: {_salary} ‘‘‘.format(_name=name, _age=age, _job=job, _salary=salary) info3 = ‘‘‘ ----------------info of {0}--------------- Name: {0} Age: {1} Job: {2} Salary: {3} ‘‘‘.format(name, age, job, salary) print(info) print(info2) print(info3)
标签:ascii码 占用 优化 ges 标准 执行 usr gbk wan
原文地址:http://www.cnblogs.com/gaosy-math/p/7342680.html