单行注释:以#开头
多行注释:三个双引号"""this is a commit"""
或’’’
message = 'The price of this %s laptop is %d Yuan and the exchange rate is \
%4.2f Yuan to 1 Pound' % ( brand, 4980, exchange_rate )
'The price of this Xiaomi laptop is 4980 Yuan and the exchange rate is 8.69
Yuan to 1 Pound'
message = 'The price of this {0:s} laptop is {1:d} Yuan and the exchange rate \
is {2:4.2f} Yuan to 1 Pound'.format( brand, 4980, exchange_rate )
'The price of this Xiaomi laptop is 4980 Yuan and the exchange rate is 8.69 Yuan to
1 Pound'
int()
str()
float()
user_age=[18,19,20,21,22]
使用下标访问
从前往后,第一个下标为0,例如user_age[0]=18
从后往前,第一个下标为-1,例如user_age[-1]=22
user_age[2:4]
“包前不包后”,即user_age[4]不包含在内
user_age[ 0:5:2 ]
步长为2,[18,20,22]
user_age[ :2]
[18,19]
user_age[1:]
[19,20,21,22]
增:user_age.append(25) / user_age+=25
删:del user_age[5]
小括号(圆括号)
不能更改值
使用下标访问
大括号(花括号)
键值对(Key-value),所有的名字(name/key)必须不同,如果出现两个相同的name,将会被后一个覆盖。
获取输入:
my_name = input( 'Please enter your name: ' )
my_age = input( 'Please enter your age: ' )
print( 'Hello, my name is', my_name, 'and I am', my_age, 'years old.' )
.items() method returns iterable which contains ( key, value ) tuples
def <function name> ( <parameters> ):
<code line(s) indented 4 spaces>
return <expression>
A local variable can only be seen in some part of the program
A global variable can be seen in the whole of the program
If a local variable has the same name as a global, only the local variable can be seen in that
local scope
def some_function( a, b, c=1, d=2, e=3 ):
...
def add_numbers( *num ):
sum = 0
for i in num:
sum = sum + 1
print( sum )
def print_member_age( **age ):
for i, j in age.items():
print( 'Name = ', i, ', Age = ', j, sep='' )
# Note use of sep=''.
*args表示任何多个无名参数,它是一个tuple;**kwargs表示关键字参数,它是一个dict。并且同时使用*args和**kwargs时,必须*args参数列要在**kwargs前
fargs formal (i.e. normal) arguments
*args non-keyworded variable length argument list
**kwargs keyworded variable-length argument list
If you want several of these in the same function, they must be in the order:
1. fargs
2. *args
3. **kwargs
f = open( 'myfile.txt', 'r' )
’r’ is the Mode for reading
w 以写方式打开,
W 文件若存在,首先要清空,然后(重新)创建
a 以追加模式打开 (从 EOF 开始, 必要时创建新文件)
r+ 以读写模式打开
w+ 以读写模式打开 (参见 w )
a+ 以读写模式打开 (参见 a )
rb 以二进制读模式打开
wb 以二进制写模式打开 (参见 w )
ab 以二进制追加模式打开 (参见 a )
rb+ 以二进制读写模式打开 (参见 r+ )
wb+ 以二进制读写模式打开 (参见 w+ )
ab+ 以二进制读写模式打开 (参见 a+ )
def safe_print_file( filename ):
try:
f = open( filename, 'r' )
for line in f:
print( line, end = '' )
f.close()
# Always close files when finished with
except:
print( filename, 'could not be opened.' )
def copy_binary_file( source_file, target_file, buffer_size ):
try:
sf = open( source_file, 'rb' )
tf = open( target_file, 'wb' )
msg = sf.read( buffer_size )
# Read buffer_size bytes at a time
while len( msg ):
tf.write( msg )
msg = sf.read( buffer_size )
sf.close()
tf.close()
# Always close files when finished with
except:
print( source_file, 'could not be opened.' )
We can delete a file:
import os
os.remove( 'test_file.txt' )
We can rename a file:
import os
os.rename( 'test_file2.txt', 'test_file3.txt' )
We can enter numbers in different
bases.
0b… Binary
0o… Octal
0x… Hexadecimal
int( <num_string>, <base> )
十进制转二进制:bin(
十进制转八进制:oct(
十进制转十六进制:hex(
x & y Bit AND x and y
x | y Bit OR x and y
x ^ y Bit XOR x and y
x << y Shift Left x by y bits 左移一位
x >> y Shift Right x by y bits 右移一位
chr( ) gives character
with given ASCII code
ord( ) gives ASCII code of
given character
• ASCII (codes 0-127) plus
• Characters for a particular language (codes 128-255)
Major disadvantage of ISO 8859-1 - ISO 8859-10 is that it does not support CJKV
languages: Chinese, Japanese, Korean, Vietnamese.
6,763 Simplified Chinese characters
Extension of GB2312 and backward compatible with it
Not strictly an adopted standard, but widely used
Compatible with both GB2312 and GBK
A computing standard aiming to encode text in all the world’s languages
The idea is to solve the character encoding problem