windows要加环境变量。
linux若升级版本不一致,
#!/usr/bin/env python 调用环境变量中的python
#!/usr/bin/python 调用系统中默认的python
变量定义规则:
1.变量名只能为字母、数字或下划线的任意组合
2.变量名的第一个字符不能是数字
3.有部分系统已经使用的关键字不能声明为变量名:"and" "as" "break" "def" "with" etc.
4.变量名尽量达意,以_或大写字母表达(GF_of_sb 或 GFOfSb),常量(pai 3.1415926就是一个常量)通常以大写字母来命名。
===============================================================================
字符编码:
二进制到数字的转换:x位0与1 可以表示的最大数字为2**x -1
字符编码
ASSCII(美国标准信息交换代码):将数字与字符相联系,从而二进制表示出数字转化为字符。其最多只能用8byte位来表示一个字节。即2**8-1=255,故其最多只能表示255个字符。
其中0-127先使用了,后边预留了128-255,1980年中国取了一个数字范围作为中文的索引,引入GB2312,共7000多个汉字加字符,1995年扩展到GBK1.0。2000年GBK18030收录了27484个汉字,现在的pc平台必须支持GBK18030,对嵌入式产品暂不做要求,故手机、MP3一般只支持GB2312。
ASSCII ---> GB2312 ---> GBK ---> GBK18030都是向下兼容的。
之后由于多个国家都进行重编,字符混乱。
Unicode(统一码),为每种语言每个字符设置了统一并唯一的二进制编码 。每个字符占两个字节,即16byte位。缺点:所占用的空间大。
utf-8在Unicode基础上做了改进,存英文字符占用1个字节,存其他语言占用两个字节。即 00000000 10001110 有一个字节的8位都是0,就省略。
-----------------------------------------------------
python3字符编码默认utf-8
python2字符默认ASCII,固要想在py2中使用中文需要先声明字符集为utf-8,即 # -*- coding:utf-8 -*-
eg: 2,3不同点
name = "你好,世界!"
print (name)
注释:
1.单行注释 #
2.多行注释‘‘‘ 多行 ‘‘‘ 三个单/双引号
3.打印多行 ‘‘‘ 多行 ‘‘‘ 三个单/双引号
python中单/双引号用法完全一致,唯一要注意单双引号互套的情况 "i‘m a girl"
eg:
name = "你好,世界!"
‘‘‘
print (name)
print (name)
print (name)
‘‘‘
msg = ‘‘‘
print (name)
print (name)
‘‘‘
#print (name)
print (msg)
print (name)
执行结果:
print (name)
print (name)
你好,世界!
脚本等待用户输入执行,如同shell的read -p:
username = input(‘usrname:‘)
print (username)
格式化输出:字符串拼接 4种方法‘‘‘str‘‘‘ 与%s
注:%s 代表此处接受字符串
%d 代表此处接受整数
%f 代表此处接受浮点数
要注意对应的变量的type,在接受变量时可强制转化
eg:当引入变量 %d 将字符串转化为数字 age =int(input("age:"))
py2 的raw_input = py3 的input
eg:
name = input("name:")
age = input("age:")
job = input("job:")
salary = input("salary:")
#法一:
info = ‘‘‘
----------------- info of ‘‘‘ +name+ ‘‘‘--------------------
Name:‘‘‘ +name+ ‘‘‘
Age:‘‘‘ +age+ ‘‘‘
Job:‘‘‘ +job+ ‘‘‘
Salary:‘‘‘ + salary+ ‘‘‘
‘‘‘
print (info)
#法二:
info = ‘‘‘
----------------- info of %s -----------------
Name:%s
Age:%s
Job:%s
Salary:%s
‘‘‘ % (name,name,age,salary)
#法三:
info = ‘‘‘
----------------- info of {_name}} -----------------
Name:{_name}
Age:{_age}
Job:{_job}
Salary:{_salary}
‘‘‘ .format(_name=name,
_age=age,
_job=job,
_salary=salary)
#法四:
info = ‘‘‘
----------------- info of {0}} -----------------
Name:{0}
Age:{1}
Job:{2}
Salary:{3}
‘‘‘ .format(name,age,job,salary)
print (info)
执行结果:
----------------- info of yh--------------------
Name:yh
Age:22
Job:it
Salary:00
密文输入(在pycharm中不好使)
import getpass
username = input("username:")
password = getpass.getpass("password:")
print(username,password)
if:
--------------------------
_username = ‘yh‘
_password = ‘yanghuan‘
username = input("username:")
password = input("password:")
print(username,password)
if _username == username and _password == password:
print("welcome user {name} login...".format(name = username))
else:
print("invalid username or password!")
执行结果:
username:yh
password:yanghuan
yh yanghuan
welcome user yh login...
--------------------------
猜年龄脚本:(if-elif-else while错了猜三次)
age_of_oldboy = 56
count = 0
while True:
if count == 3:
break
guess_age = int(input("guess age:"))
if guess_age == age_of_oldboy:
print("yes,u got it!")
break
elif guess_age > age_of_oldboy:
print("think smaller")
else:
print("think bigger")
count +=1
if count == 3:
print("you have tried too many times..")
优化:
while True:
if count == 3:
break
改为:
while count<3:
------------------
if count == 3:
print("you have tried too many times..")
改为:
else:
print("you have tried too many times..")
---------------------------------------------
注:while-else 中的else代表while不成立走else
猜三次不对自动退出:
age_of_oldboy = 56
count = 0
while count<3:
guess_age = int(input("guess age:"))
if guess_age == age_of_oldboy:
print("yes,u got it!")
break
elif guess_age > age_of_oldboy:
print("think smaller")
else:
print("think bigger")
count +=1
else:
print("you have tried too many times..")
猜三次input n 退出,任意键继续:
age_of_oldboy = 56
count = 0
while count<3:
guess_age = int(input("guess age:"))
if guess_age == age_of_oldboy:
print("yes,u got it!")
break
elif guess_age > age_of_oldboy:
print("think smaller")
else:
print("think bigger")
count +=1
if count == 3:
continue_guess = input("do u want to keep guess?")
if continue_guess != "n":
count = 0
--------------------------------------------------
for循环:
for-else:
当for正常执行完毕,执行else;
当for break非正常退出,不执行else。
for-break:
break:跳出break所在的整个for循环,包括else。
for-contine:
contine:跳出此次循环,进行下次循环,包括else。
age_of_oldboy = 56
for i in range(3):
guess_age = int(input("guess age:"))
if guess_age == age_of_oldboy:
print("yes,u got it!")
break
elif guess_age > age_of_oldboy:
print("think smaller")
else:
print("think bigger")
else:
print("you have tried too many times..")
------------------------------------------
for i in range(1,5):
if i <3:
print("loop",i)
else:
continue
print("hehe...",i)
执行结果:
loop 1
hehe... 1
loop 2
hehe... 2
------------------------------------------
for i in range(0,10,2):#从0开始,隔一个数打印一次
print("loop",i)