数据类型和变量
- 除法
# 1.除法/
>>> 10 / 3
3.3333333333333335
# 2.地板除//
>>> 10 // 3
3
字符串和编码
格式化
- 在Python中,采用的格式化方式和C语言是一致的,用
%
实现,举例如下:
>>> 'Hello, %s' % 'world'
'Hello, world'
>>> 'Hi, %s, you have $%d.' % ('Michael', 1000000)
'Hi, Michael, you have $1000000.'
#有些时候,字符串里面的%是一个普通字符,这个时候就需要转义,用%%来表示一个%:
>>> 'growth rate: %d %%' % 7
'growth rate: 7 %'
- format()
format()
方法,它会用传入的参数依次替换字符串内的占位符{0}
、{1}
……
>>> 'Hello, {0}, 成绩提升了 {1:.1f}%'.format('小明', 17.125)
'Hello, 小明, 成绩提升了 17.1%'
- f-string
使用以f
开头的字符串,称之为f-string
,它和普通字符串不同之处在于,字符串如果包含{xxx}
,就会以对应的变量替换:
>>> r = 2.5
>>> s = 3.14 * r ** 2
>>> print(f'The area of a circle with radius {r} is {s:.2f}')
The area of a circle with radius 2.5 is 19.62
使用list和tuple
list
Python内置的一种数据类型是列表:list。list是一种有序的集合,可以随时添加和删除其中的元素。
classmates = ['Michael', 'Bob', 'Tracy'];
print(classmates); # ['Michael', 'Bob', 'Tracy']
# 用len()函数可以获得list元素的个数
print(len(classmates)); # 3
# 索引
classmates[0]; # Michael
# 取最后一个元素
print(classmates[-1]); #Tracy
# 取倒数第二个元素
print(classmates[-2]); #Bob
# 插入元素
# 1.追加元素到末尾
classmates.append('Adam')
print(classmates); # ['Michael', 'Bob', 'Tracy', 'Adam']
# 2.插入到指定位置
classmates.insert(1, 'Jack')
print(classmates); # ['Michael', 'Jack', 'Bob', 'Tracy', 'Adam']
# 删除元素
# 1. 删除末尾元素
classmates.pop();
print(classmates); #['Michael', 'Jack', 'Bob', 'Tracy']
# 2. 删除指定位置元素
classmates.pop(1);
print(classmates); #['Michael', 'Bob', 'Tracy']
# 要把某个元素替换成别的元素,可以直接赋值给对应的索引位置:
classmates[1] = 'Sarah';
# 多维
s = ['python', 'java', ['asp', 'php'], 'scheme']
print(s[2][1]); # php
tuple
另一种有序列表叫元组:tuple。tuple和list非常类似,但是tuple一旦初始化就不能修改,比如同样是列出同学的名字:
>>> classmates = ('Michael', 'Bob', 'Tracy')
classmates这个tuple不能变了,它也没有append(),insert()这样的方法。其他获取元素的方法和list是一样的。
不可变的tuple有什么意义?因为tuple不可变,所以代码更安全。如果可能,能用tuple代替list就尽量用tuple。
要定义一个只有1个元素的tuple,如果你这么定义:
t = (1)
定义的不是tuple,是1
这个数!这是因为括号()
既可以表示tuple,又可以表示数学公式中的小括号,这就产生了歧义,因此,Python规定,这种情况下,按小括号进行计算,计算结果自然是1
。
只有1个元素的tuple定义时必须加一个逗号,
,来消除歧义:
t = (1,)
条件判断
age = 23;
if age >= 18:
print('adult')
elif age >= 6:
print('teenager')
else:
print('kid')
s = input('birth:')
birth = int(s)
if birth < 2000:
print('00前')
else:
print('00后')
循环
Python的循环有两种,一种是for…in循环,依次把list或tuple中的每个元素迭代出来,第二种循环是while循环。
# 循环
# 1.for..in
names = ['Michael', 'Bob', 'Tracy'];
for name in names:
print(name);
# 1-100求和
sum = 0;
for x in range(101):
sum = sum + x;
print(sum)
# 2.while
sum = 0
n = 99
while n > 0:
sum = sum + n
n = n - 2
print(sum)
还有break,continue
使用dict和set
dict
- Python内置了字典:dict的支持,dict全称dictionary,在其他语言中也称为map,使用键-值(key-value)存储,具有极快的查找速度。
>>> d = {'Michael': 95, 'Bob': 75, 'Tracy': 85}
>>> d['Michael']
- 要避免key不存在的错误,有两种办法,一是通过
in
判断key是否存在;二是通过dict提供的get()
方法,如果key不存在,可以返回None
,或者自己指定的value。
# 避免key不存在的错误
print('Thimas' in d); # Flase
print(d.get('Thimas')); # None
print(d.get('Thimas', -1)); # -1
- 要删除一个key,用
pop(key)
方法,对应的value也会从dict中删除:
>>> d.pop('Bob')
75
>>> d
{'Michael': 95, 'Tracy': 85}
set
- set和dict类似,也是一组key的集合,但不存储value。由于key不能重复,所以,在set中,没有重复的key。
- 要创建一个set,需要提供一个list作为输入集合:
>>> s = set([1, 2, 3])
>>> s
{1, 2, 3}
- 通过
add(key)
方法可以添加元素到set中,可以重复添加,但不会有效果
>>> s.add(4)
>>> s
{1, 2, 3, 4}
- 通过
remove(key)
方法可以删除元素:
>>> s.remove(4)
>>> s
{1, 2, 3}
- set可以看成数学意义上的无序和无重复元素的集合,因此,两个set可以做数学意义上的交集、并集等操作:
>>> s1 = set([1, 2, 3])
>>> s2 = set([2, 3, 4])
>>> s1 & s2
{2, 3}
>>> s1 | s2
{1, 2, 3, 4}