====================================================================================================================
2019-03-06更新
6、在python脚本开头指定字符编码
#coding=utf-8
或者
#! -*- coding:utf-8 -*-
python2
默认的编码是ASCII
,需要把字符编码修改为utf-8
才支持中文字符;python3
默认的编码是utf-8
,不需要修改字符编码就可以支持中文字符;
====================================================================================================================
5、指定解释器执行脚本
参考链接: https://www.jianshu.com/p/400c612381dd
在.py
脚本文件的开头处写上
第一种方式:
#!/usr/bin/env python3
或者
第二种方式:
#!/usr/bin/python3
作用是该文件由指定路径下的python执行,第一种指定的是系统环境变量中的python,第二种指定的是python的绝对路径。
- 在UNIX like系统中才用这样指定python的方式,可直接执行python脚本,例如:用命令
./test.py
直接运行test.py
; - 在windows系统中执行python的方式为
python test.py
,且不需要开头指定解释器; - 在UNIX like系统里也可以用
python2 test.py
执行,且在该处指定的python2
优先级高于脚本中指定的python3
,所以会以python2
的版本执行test.py
;
====================================================================================================================
2019-02-27更新
4、函数中可变参数
- 如果不用可变参数:
def calc(numbers):
s = 0
for i in numbers:
s = s + i*i
return s
print(calc([1,2,3,4]))
结果显示
30
- 如果用可变参数,调用函数的时候就不用携程列表或元组的形式了:
def calc(*number):
s = 0
for i in number:
s = s + i*i
return s
print(calc(1,2,3,4))
- 调用列表或元组时可用这种形式:
def calc(*number):
s = 0
for i in number:
s = s + i*i
return s
nums = [1,2,3,4]
print(calc(*nums))
====================================================================================================================
2018-10-16更新
3、判断输入的字符串是否为正整数
prompt = "\n请输入年龄:"
prompt +="\n输入'quit'退出程序."
message = ''
while message != 'quit':
message = input(prompt)
if message.isdigit():
age = int(message)
if age < 3:
print('岁数%s,观众免费' % (message))
elif age < 13:
print('岁数%s,票价为10$' % (message))
else:
print('岁数%s,票价为15$' % (message))
elif message == 'quit':
break
else:
print("请输入正确的年龄(正整数).")
====================================================================================================================
2、字典嵌套
citys = {
'beijing':{
'country':'china',
'population':'21,710,000',
'fact':'the capital of China',
},
'wahsington':{
'country':'american',
'population':'694,000',
'fact':'the capital of US'
},
'paris':{
'country':'france',
'population':'2,244,000',
'fact':'the capital of France'
},
}
citys['london'] = {
'country': 'britain',
'population': '8,136,000',
'fact': 'the capital of France'
}
for city,city_info in citys.items():
print('\nCity:' + city.title())
print('\tcountry:' + city_info['country'].title())
print('\tpopulation:' + city_info['population'])
print('\tfact:' + city_info['fact'])
运行结果如下:
City:Paris
country:France
population:2,244,000
fact:the capital of France
City:Beijing
country:China
population:21,710,000
fact:the capital of China
City:Wahsington
country:American
population:694,000
fact:the capital of US
City:London
country:Britain
population:8,136,000
fact:the capital of France
====================================================================================================================
1、打印时引用参数
river = {
'changjiang':'china',
'nile':'egypt',
'amazonas':'brazil'
}
for k,v in river.items():
print("The %s runs through %s." % (k.title(),v.title()))
运行结果如下:
The Amazonas runs through Brazil.
The Nile runs through Egypt.
The Changjiang runs through China.