print('hello,world')
hello,world
print(10 ** (1 / 3))
2.154434690031884
import math
math.floor(9.1)
9
import math
math.ceil(9.1)
10
apple_price = 15
grape_price = 25
"苹果的花费:{};葡萄的花费:{}".format(apple_price, grape_price)
'苹果的花费:15;葡萄的花费:25'
number = 10
number += 10
# 乘方
math.pow(3,10)
3 ** 10
59049
min(10,3,19)
3
True,False
(True, False)
True == 1
True
| 操作符 || 解释 |
| — || — |
| < || 小于 |
line = "hello world"
line
'hello world'
line_1 = "ni hao,"
line_2 = "xiaojiejie"
line_1 + line_2
'ni hao,xiaojiejie'
line = "nihao "
line * 10
'nihao nihao nihao nihao nihao nihao nihao nihao nihao nihao '
len(line)
6
line
'buhao'
# id函数,返回一个身份标识符,可以理解为一个变量的内存地址
id(line)
2580246325232
line = "buhao"
id(line)
2580246337584
line = "huan ying da jia lai wan"
# 取0-7 字符
line[0:7]
'huan yi'
line[0:10:2]
'ha ig'
line[-9:]
'a lai wan'
line[::-1]
'naw ial aij ad gniy nauh'
line.capitalize()
'Huan ying da jia lai wan'
line = "ABCDEFG"
line.capitalize()
'Abcdefg'
line.center(33)
' ABCDEFG '
line.center(33, '%')
'%%%%%%%%%%%%%ABCDEFG%%%%%%%%%%%%%'
# 计数
line.count('A')
1
line.endswith('FG')
True
# 当字符不存在 -1
line.find('G')
6
# 当字符不存在时会报错
line.index('G')
6
line = "ASDASDsdsadQWEWQE"
line.upper()
'ASDASDSDSADQWEWQE'
line.lower()
'asdasdsdsadqwewqe'
line.istitle()
False
line.title()
'Asdasdsdsadqwewqe'
line = " lskdk \n \t"
line.strip()
'lskdk'
#空列表
varibals = []
varibals = list()
varibals = [1,2,3,'ss',[]]
varibals
[1, 2, 3, 'ss', []]
varibals =[]
varibals.append(1)
varibals.append(1)
varibals.append(1)
varibals
[1, 1, 1]
type(varibals)
list
varibals = [1,2,324,2414,[1,2]]
new_varibals = varibals.copy()
new_varibals [0] = 9999
new_varibals , varibals
([9999, 2, 324, 2414, [1, 2]], [1, 2, 324, 2414, [1, 2]])
new_varibals[-1][0] = 9999
new_varibals , varibals
([9999, 2, 324, 2414, [9999, 2]], [1, 2, 324, 2414, [9999, 2]])
id(new_varibals),id(varibals),id(new_varibals[-1][0]),id(varibals[-1][0])
(1364208363016, 1364197217544, 1364202819536, 1364202819536)
a = [1,2]
b = [3,4]
a + b
[1, 2, 3, 4]
# 在末尾插入
a.extend(b)
a
[1, 2, 3, 4]
# 在某个位置插入
a.insert(0,100)
a
[100, 1, 2, 3, 4]
# 返回最后一个元素
a.pop()
4
# 移除第一个字符等于2的字符
a.remove('2')
# 排序
a = [1,4,3,6]
a.sort()
a
[1, 3, 4, 6]
a.sort(reverse = True)
a
[6, 4, 3, 1]
4 in a
True
元组:不可变类型的列表
var = tuple()
var = ()
type(var)
tuple
解决映射问题
var = {}
var = dict()
type(var)
dict
var = {
'中':100,
'左':200
}
var ['中']
100
words = ['中','左']
location = [100,200]
location[words.index('中')]
100
new_var = list(zip(words,location))
dict(new_var)
{'中': 100, '左': 200}
list(zip([1,2,3],[22,34,41],[242,452]))
[(1, 22, 242), (2, 34, 452)]
students = ['wong','li','e','aaa']
money = dict.fromkeys(students ,10)
money['wong']
10
a = money.get('ww',100)
a
100
money.keys()
dict_keys(['wong', 'li', 'e', 'aaa'])
money.values()
dict_values([10, 10, 10, 10])
# 删除
money.pop('wong')
10
money
{'li': 10, 'e': 10, 'aaa': 10}
# key 存在 则返回其 value, 不存在 返回 赋值value
money.setdefault('hah',100)
100