1 字符串打印采用单引号、双引号均可,三引号用于打印多行字符串
2 format字符串使用:
age=
3
name="Tom"
print("{0} was {1} years old".format(name, age))
可使用"+"产生同样的输出效果
print
(name+
" was "
+str(age)+
" years old"
)
3 变量命名规则:
第一个字符必须是字母或者下划线,其余字符可以是字母、下划线或者数字
4 采用"#"注释
5 Python语言采用缩进方式规定语法
6 Python数据类型:
6.1 numeric type
int(包含boolean)、folat、complex(复数),可用z.real以及z.imag取得实部和虚部
7 Python中的数据结构
7.1 List使用
若需要打印中文字符,需要在代码最前面添加
# -*- coding:utf-8 -*-
换行\n:
print
(
"what's your name?\nTom"
)
List创建采用[]:
numberList=[
1
,
3
,
5
]
且在Python中支持List中不同类型元素
mixList=[
"aaa"
,
2
,
5
,
"bbb"
]
取List中元素:numberList[1],mixList[3]
List中元素更新:
numberList[
1
]=
30
删除List中元素:
del
numberList[
1
]
7.2 Tuple元组使用
Tuple创建采用():
numberTuple=(
1
,
2
,
3
)
Tuple访问元素与List相似:
numberTuple[1]
Tuple与List较大差别在于Tupel一旦被创建,元素值不能修改,也不能被删除,但可以删除整个Tuple: del numberTuple
在定义只包含一个元素的
Tuple时,需要在后面加",":
oneTuple=(
1
,)
List与Tuple可以相互转换:
numberList=[
1
,
2
,
3
,
4
]
numberTuple=tuple(numberList) numberList=list(
numberTuple
)
List可作为Tuple中的单个元素:
mixTuple=(
1
,
2
,[
3
,
4
,
5
])
且可以改变List元素的值:
mixTuple[
2
][-
2
]=
40
Tuple一次赋多值:v=('a','b','c') (x,y,z)=v