python是动态语言,强类型语言
1.不用事先声明类型,随时可以赋值为其他类型
2. 编程时不知道是什么类型,很难推断(编译时无法检查,只有运行时才能检查)
基础语法
基础数据类型
注释
由符号#标注的文字
数字
- 整数 不区分短整型和长整型
- 进制 :二进制(0b01),0o10,十六进制(0xa)
- 布尔型 : True 、False
- 浮点数
- 2、3.1415、-0.12、146e9等价于1.46*10 9次方
字符串
- 使用’ "单双引号引用的字符的序列
- ‘’'和”””单双三引号
- 字符串前加R或r则该字符串不做特殊处理(转义字符无效)
- 支持切片访问
num = "1" # string type
num = "Let's go" # string type
num = "He's \"old\"" # string type
mail = "Xiaoyi: \n hello \n I am you!"
mail = """Xiaoyi:
hello
I am you!
""" # special string format
string = 'xiaoyi' # get value by index
copy = string[0] + string[1] + string[2:6] # note: [2:6] means [2 5] or[2 6)
copy = string[:4] # start from 1
copy = string[2:] # to end
copy = string[::1] # step is 1, from start to end
copy = string[::2] # step is 2
copy = string[-1] # means 'i', the last one
列表
列表list用[]来定义。里面的元素可以修改。List是一个类,支持很多该类定义的方法,这些方法可以用来对list进行操作。
userList = ["xiaoyi", 25, "male"]
name = userList[0]
age = userList[1]
gender = userList[2]
userList[3] = 88888 # error! access out of range
userList.append(8888) # add new elements
"male" in userList # search
userList[2] = 'female' # can modify the element (the memory address not change)
userList.remove(8888) # remove element
userList.remove(userList[2]) # remove element
元组
元组tuple用()来定义。相当于一个可以存储不同类型数据的一个数组。可以用索引来访问,但需要注意的一点是,里面的元素不能被修改。
user = ("xiaoyi", 25, "male")
name = user[0]
age = user[1]
gender = user[2]
t1 = () # empty tuple
t2 = (2, ) # when tuple has only one element, we should add a extra comma
user[1] = 26 # error!! the elements can not be changed
name, age, gender = user # can get three element respectively
字典
字典dictionary用{}来定义。它的优点是定义像key-value这种键值对的结构,就像struct结构体的功能一样。它也支持字典类支持的方法进行创建和操作。
item = ['name', 'age', 'gender']
value = ['xiaoyi', '25', 'male']
zip(item, value) # zip() will produce a new list:
# [('name', 'xiaoyi'), ('age', '25'), ('gender', 'male')]
# but we can not define their corresponding relationship
# and we can define this relationship use dictionary type
# This can be defined as a key-value manner
# dic = {key1: value1, key2: value2, ...}, key and value can be any type
dic = {'name': 'xiaoyi', 'age': 25, 'gender': 'male'}
dic = {1: 'zou', 'age':25, 'gender': 'male'}
# and we access it like this: dic[key1], the key as a index
print dic['name']
print dic[1]
# another methods create dictionary
fdict = dict(['x', 1], ['y', 2]) # factory mode
ddict = {}.fromkeys(('x', 'y'), -1) # built-in mode, default value is the same which is none
# access by for circle
for key in dic
print key
print dic[key]
# add key or elements to dictionary, because dictionary is out of sequence,
# so we can directly and a key-value pair like this:
dic['tel'] = 88888
# update or delete the elements
del dic[1] # delete this key
dic.pop('tel') # show and delete this key
dic.clear() # clear the dictionary
del dic # delete the dictionary
dic.get(1) # get the value of key
dic.get(1, 'error') # return a user-define message if the dictionary do not contain the key
dic.keys()
dic.values()
dic.has_key(key)
转义字符
- \ ,\t,\r,\n,’,"
- tab:制表符,用在对齐打印,一般为了补足4个字符或者8个字符
- \n:下一行
缩进
- 舍弃了C等语言的花括号,用缩进代替层次关系
- 约定使用4个空格缩进
运算符
算术运算符
运算符 | 含义 | 例子 |
---|---|---|
+ | 加 | 1 + 2 = 3 |
- | 减 | 2 - 1 = 1 |
* | 乘 | 3 * 3 = 9 |
/ | 除 | 9 / 3 = 3.0 |
% | 取余数 | 4 % 3 = 1 |
** | 幂指运算 | 2 ** 3 = 8 |
位运算符
- &(与):二进制的按位的相乘
- |(或):按位相加(按位相加不进位)
- ~:按位取反
- ^:按位异或
运算符优先级
单目运算符>双目运算符
算数运算符>位运算符>身份运算符>成员运算符>逻辑运算符
长表达式,多用括号,易读性
表达式
- 由数字,符号,括号,变量等的组合
- 算数表达式
- 逻辑表达式
- 赋值表达式
Python中,赋值即定义,如果一个变量已经定义,赋值相当于重新定义