标签:shell脚本 利用 字符 令行 定义 繁体中文 标准 年龄 inf
一、py2和py3的区别
最大的区别在于,py3对Unicode的支持
官方将在2020年停止对py2.7的支持
One popular module that don‘t yet support Python 3 is Twisted (for networking and other applications).后续将会支持
二、Hello World程序
在linux 下创建一个文件叫hello.py,并输入
1 print("Hello World!")
然后执行命令:python hello.py ,输出
1 localhost:~ jieli$ vim hello.py 2 3 localhost:~ jieli$ python hello.py 4 5 Hello World!
指定解释器
上一步中执行 python hello.py 时,明确的指出 hello.py 脚本由 python 解释器来执行。
如果想要类似于执行shell脚本一样执行python脚本,例: ./hello.py
,那么就需要在 hello.py 文件的头部指定解释器,如下:
1 #!/usr/bin/env python 2 3 4 5 print "hello,world"
如此一来,执行: ./hello.py
即可。
ps:执行前需给予 hello.py 执行权限,chmod 755 hello.py
三、变量
Variables are used to store information to be referenced and manipulated in a computer program. They also provide a way of labeling data with a descriptive name, so our programs can be understood more clearly by the reader and ourselves. It is helpful to think of variables as containers that hold information. Their sole purpose is to label and store data in memory. This data can then be used throughout your program.
变量定义的规则:
变量赋值:
1 name = "oldboy" 2 name2 = name 3 name = "newboy" 4 print("my name is",name,name2)
运行结果是:
my name is newboy oldboy
原因是:name和name2指向同一个字符串“oldboy”,将name重新赋值后,name指向“newboy”,而name2指向不变,仍然是“oldboy”
四、字符编码
常见的编码类型有:ASCII编码(适用于现代英语和其他西欧语言);简体中文的GB2312(7000+个汉字)、GBK1.0(21000+个汉字)、GBK18030(27000+个汉字);繁体中文的big5
ASCII(American Standard Code for Information Interchange,美国标准信息交换代码)是基于拉丁字母的一套电脑编码系统,主要用于显示现代英语和其他西欧语言,其最多只能用 8 位来表示(一个字节),即:2**8 = 256-1,所以,ASCII码最多只能表示 255 个符号。
显然ASCII码无法将世界上的各种文字和符号全部表示,所以,就需要新出一种可以代表所有字符和符号的编码,即:Unicode
Unicode(统一码、万国码、单一码)是一种在计算机上使用的字符编码。Unicode 是为了解决传统的字符编码方案的局限而产生的,它为每种语言中的每个字符设定了统一并且唯一的二进制编码,规定虽有的字符和符号最少由 16 位来表示(2个字节),即:2 **16 = 65536, 注:此处说的的是最少2个字节,可能更多
UTF-8,是对Unicode编码的压缩和优化,他不再使用最少使用2个字节,而是将所有的字符和符号进行分类:ascii码中的内容用1个字节保存、欧洲的字符用2个字节保存,东亚的字符用3个字节保存...
代码注释
单行注视:# 被注释内容
多行注释:""" 被注释内容 """
五、输入与字符格式化
使用input输入,有多种字符串格式化方法:
1 name = input("name:") 2 age = input("age:") 3 job = input("job:") 4 salary = input("salary:") 5 6 info = """-------info of {_name}-------- 7 name:{_name} 8 age:{_age} 9 job:{_job} 10 salary:{_salary} 11 """.format(_name=name,_age=age,_job=job,_salary=salary) 12 13 info2 = """-------info of %s-------- 14 name:%s 15 age:%d 16 job:%s 17 salary:%d 18 """%(name,name,int(age),job,int(salary)) 19 20 info3 = """-------info of {0}-------- 21 name:{0} 22 age:{1} 23 job:{2} 24 salary:{3} 25 """.format(name,int(age),job,int(salary)) 26 27 print(info) 28 print(info2) 29 print(info3)
对于密码输入,如果想要不可见,需要利用getpass 模块中的 getpass方法,即:
1 import getpass 2 3 # 将用户输入的内容赋值给 name 变量 4 5 pwd = getpass.getpass("请输入密码:") 6 7 # 打印输入的内容 8 9 print(pwd)
在pyCharm下使用有问题,可以在命令行下执行,看getpass效果
六、if...else表达式
场景一: 通过条件判断用户输入的账号密码是否正确,正确就弹出欢迎词
1 # 提示输入用户名和密码 2 3 4 5 # 验证用户名和密码 6 7 # 如果错误,则输出用户名或密码错误 8 9 # 如果成功,则输出 欢迎,XXX! 10 11 12 13 14 15 #!/usr/bin/env python 16 17 # -*- coding: encoding -*- 18 19 20 import getpass 21 22 23 24 name = raw_input(‘请输入用户名:‘) 25 26 pwd = getpass.getpass(‘请输入密码:‘) 27 28 29 30 if name == "alex" and pwd == "cmd": 31 32 print("欢迎,alex!") 33 34 else: 35 36 print("用户名和密码错误")
场景二:在程序里设定好你的年龄,然后启动程序让用户猜测,用户输入后,根据他的输入提示用户输入的是否正确,如果错误,提示是猜大了还是小了
1 guess_age = int(input("guessage:")) 2 if guess_age == my_age: 3 print("yes,you got it!") 4 break 5 elif guess_age > my_age: 6 print("guess smaller...") 7 else: 8 print("guess bigger!")
七、while循环
通过while循环优化猜年龄的程序,程序设置猜3次,不对就退出,注意循环中对else的使用:
1 my_age = 34 2 count = 0 3 while count < 3: 4 guess_age = int(input("guessage:")) 5 if guess_age == my_age: 6 print("yes,you got it!") 7 break 8 elif guess_age > my_age: 9 print("guess smaller...") 10 else: 11 print("guess bigger!") 12 count += 1 13 else:#注意这里,针对while循环可以使用else语句,让程序更简单精炼 14 print("too many times,byebye!")
八、for循环
使用for循环,实现while相同的效果,也使用了else语句:
1 my_age = 34 2 for i in range(3): 3 guess_age = int(input("guessage:")) 4 if guess_age == my_age: 5 print("yes,you got it!") 6 break 7 elif guess_age > my_age: 8 print("guess smaller...") 9 else: 10 print("guess bigger!") 11 else: 12 print("too many times,byebye!")
最终,再次优化,实现用户可以根据自己的需要,一直猜下去:
1 my_age = 34 2 count =0 3 while count <3: 4 guess_age = int(input("guessage:")) 5 if guess_age == my_age: 6 print("yes,you got it!") 7 break 8 elif guess_age > my_age: 9 print("guess smaller...") 10 else: 11 print("guess bigger!") 12 count +=1 13 if count ==3: 14 continue_confime = input("try again?") 15 if continue_confime != "n": 16 count = 0
注意循环中break和continue的区别:
break是跳出当前这层循环的整个循环
1 for i in range(4): 2 print("-----------",i) 3 for j in range(4): 4 print("&&&&&&",j) 5 if j <2: 6 print("***",j) 7 else: 8 break
执行效果:
----------- 0
&&&&&& 0
*** 0
&&&&&& 1
*** 1
&&&&&& 2
----------- 1
&&&&&& 0
*** 0
&&&&&& 1
*** 1
&&&&&& 2
----------- 2
&&&&&& 0
*** 0
&&&&&& 1
*** 1
&&&&&& 2
----------- 3
&&&&&& 0
*** 0
&&&&&& 1
*** 1
&&&&&& 2
continue是跳出当次循环
1 for i in range(4): 2 print("-----------",i) 3 for j in range(4): 4 print("&&&&&&",j) 5 if j <2: 6 print("***",j) 7 else: 8 continue
执行结果:
----------- 0
&&&&&& 0
*** 0
&&&&&& 1
*** 1
&&&&&& 2
&&&&&& 3
----------- 1
&&&&&& 0
*** 0
&&&&&& 1
*** 1
&&&&&& 2
&&&&&& 3
----------- 2
&&&&&& 0
*** 0
&&&&&& 1
*** 1
&&&&&& 2
&&&&&& 3
----------- 3
&&&&&& 0
*** 0
&&&&&& 1
*** 1
&&&&&& 2
&&&&&& 3
标签:shell脚本 利用 字符 令行 定义 繁体中文 标准 年龄 inf
原文地址:http://www.cnblogs.com/zilin-2017/p/6886484.html