In [8]:
print('hello, "world')
In [7]:
print("hello, 'world")
In [347]:
import this
In [9]:
5 + 100
Out[9]:
In [12]:
100 / 10
Out[12]:
In [13]:
100 * 10
Out[13]:
In [14]:
100 - 99
Out[14]:
In [15]:
10 % 3
Out[15]:
In [16]:
10 ** 3
Out[16]:
In [18]:
10 ** (1/3)
Out[18]:
In [19]:
import math
In [20]:
math.pi
Out[20]:
In [21]:
math.sin(math.pi/2)
Out[21]:
In [22]:
math.floor(9.23432)
Out[22]:
In [23]:
math.ceil(9.234)
Out[23]:
In [26]:
# 苹果的花费
print(5*2)
# 葡萄的花费
print(15*1.5)
# 总花费
print((5*2) + (15*1.5))
In [32]:
apple_price = 5
apple_weight = 2
apple_cost = apple_price * apple_weight
grape_price = 15
grape_weight = 1.5
grape_cost = grape_price * grape_weight
total_cost = apple_cost + grape_cost
print(apple_cost, total_cost)
In [33]:
"苹果的花费为:{};葡萄的花费为:{};总花费为:{}".format(apple_cost, grape_cost, total_cost)
Out[33]:
In [34]:
number + 2
In [35]:
a = 10
b = 20
a, b = b, a
In [36]:
print("a is {}, b is {}".format(a, b))
1、标识符的第一个字符必须是字母表中的字母(大写或小写)或者一个下划线
2、标识符名称的其他部分可以由字母(大写或小写)、下划线(‘_’)或数字(0-9)组成。
3、标识符名称是对大小写敏感的。
In [40]:
n = 10
N = 10
In [45]:
# 错误: 首字符为数字
3thidnf = 10
In [49]:
# 字母中间有空格,为非法字符
my name = 100
In [48]:
# -是变量命名中的非法字符
my-name = 10
In [54]:
round(100/3, 3)
Out[54]:
1、字符串 str
2、数字 int float complex ..
3、列表 list
4、元组 tuple
5、字典 dict
In [56]:
number = 10
number = number + 10
number += 10
In [57]:
number -= 10
number *= 10
number /= 10
In [58]:
import math
In [59]:
# 乘方、开放
math.pow(3, 10)
Out[59]:
In [61]:
# 推荐是用
3 ** 10
Out[61]:
In [62]:
math.floor(2.23242)
Out[62]:
In [63]:
math.ceil(2.234234)
Out[63]:
In [65]:
# 度的转换
math.radians(180)
Out[65]:
In [66]:
math.sin(math.pi/2)
Out[66]:
In [67]:
min(10, 12, 234, 100, 1)
Out[67]:
In [68]:
max(10, 12, 234, 100, 1)
Out[68]:
In [70]:
sum([10, 12, 234, 100, 1])
Out[70]:
In [72]:
divmod(10, 3)
Out[72]:
In [73]:
True, False
Out[73]:
In [74]:
True == 1
Out[74]:
In [75]:
False == 0
Out[75]:
In [91]:
# 不建议这样写,没有意义, 有一个特例,后面会讲到
True + 10
Out[91]:
In [78]:
100 > 10
Out[78]:
In [81]:
# 与运算,同为真则为真
True and False
Out[81]:
In [86]:
# 或运算, 只要一个为真则为真
True or False
Out[86]:
In [89]:
# 非运算,取反
not False
Out[89]:
操作符 | 解释 |
---|---|
< | 小于 |
<= | 小于等于 |
> | 大于 |
>= | 大于等于 |
== | 等于 |
!= | 不等于 |
is | 是相同对象 |
In [98]:
# 字符串可以用双引号,也可以用单引号。通过单、双引号的恰当使用,可以避免不必要的字符转义(escape),也就是说,可以避免使用 \ (转义字符)
line = "hello world"
line = "hello world\""
line = 'hello \'world'
In [101]:
# 字符串的加法操作
line_1 = "ni hao, "
line_2 = 'xiaojiejie'
line_1 + line_2
Out[101]:
In [104]:
# 字符串的乘法操作
line = 'nihao '
line * 10
Out[104]:
In [107]:
# 字符串是不可变类型的变量
line
Out[107]:
In [109]:
line = 'buhao '
line
Out[109]:
In [105]:
# 返回字符串的长度
len(line)
Out[105]:
In [119]:
line = 'nihao '
line_copy = line
# id函数,返回一个身份识别符,可以理解为一个变量的内存地址
id(line), id(line_copy)
Out[119]:
In [120]:
line = 'buhao '
id(line), id(line_copy)
Out[120]:
In [116]:
23 + 10
Out[116]:
序号 | 名称 | 年龄 |
---|---|---|
1 | wong | 18 |
2 | sun xing zhe | 500 |
In [ ]:
line = 'huan ying da jia lai wan men shang ke'
In [123]:
line[0:10]
Out[123]:
In [186]:
line[0:20:3]
Out[186]:
In [128]:
line[-10:]
Out[128]:
In [129]:
line[::-1]
Out[129]:
In [134]:
line[-1] = 'E'
In [136]:
line.capitalize()
Out[136]:
In [199]:
line = "ASDFASDFEWFSDF"
line.capitalize()
Out[199]:
In [145]:
# 居中
line.center(20)
Out[145]:
In [147]:
# 计数
line.count('Z')
Out[147]:
In [151]:
line.endswith('FEWFSDD')
Out[151]:
In [152]:
line.startswith('ASDFA')
Out[152]:
In [201]:
line
Out[201]:
In [204]:
line.find('A', 2)
Out[204]:
In [167]:
# 当字符不存在时,报错
line.index('Z')
In [175]:
line = 'Aslkdfjsldkf'
In [170]:
line.upper()
Out[170]:
In [172]:
line.lower()
Out[172]:
In [176]:
line.istitle()
Out[176]:
In [177]:
line.isupper()
Out[177]:
In [178]:
line.islower()
Out[178]:
In [180]:
line = ' lskdas k \n\t '
In [181]:
line.strip()
Out[181]:
In [182]:
line.rstrip()
Out[182]:
In [183]:
line.lstrip()
Out[183]:
In [184]:
line = 'Aslkdfjsldkf'
In [185]:
line.swapcase()
Out[185]:
In [188]:
line = "ni hao"
id(line)
Out[188]:
In [189]:
new_line = line.upper()
id(line), id(new_line)
Out[189]:
In [190]:
# 空列表
varibals = []
varibals = list()
In [191]:
varibals = [1, 2, 3, 'ni hao', 'hello, python', [], [100, 100]]
In [194]:
varibals = []
varibals.append(1)
varibals.append(2)
varibals.append('ni hao')
varibals
Out[194]:
In [197]:
varibals[0] = 10
varibals
Out[197]:
In [207]:
var = 10
type(var)
var = 'str'
type(var)
Out[207]:
In [211]:
varibals[-2:]
Out[211]:
In [212]:
varibals + [1,23]
Out[212]:
In [213]:
varibals * 4
Out[213]:
In [214]:
len(varibals)
Out[214]:
In [217]:
# 没有返回值,而是修改了我们列表对象本身
varibals.append(1)
varibals
Out[217]:
In [218]:
# 清空
varibals.clear()
In [246]:
varibals = [1,12,23,4234, [1,2]]
In [247]:
new_varibals = varibals.copy()
In [248]:
new_varibals[-1][0] = 99999
new_varibals
Out[248]:
In [252]:
varibals
Out[252]:
In [253]:
id(new_varibals[-1]), id(varibals[-1])
Out[253]:
In [255]:
a = [1,2]
b = [3,4]
a + b
Out[255]:
In [257]:
a.extend(b)
a
Out[257]:
In [258]:
a
Out[258]:
In [259]:
a.insert(0, 100)
In [263]:
a
Out[263]:
In [264]:
a.pop(0)
Out[264]:
In [265]:
a
Out[265]:
In [271]:
a.remove('z')
In [267]:
a
Out[267]:
In [270]:
a.remove?
In [269]:
a
Out[269]:
In [275]:
a.sort(reverse=True)
In [277]:
a
Out[277]:
In [279]:
5 in a
Out[279]:
In [282]:
var = tuple()
var = ()
type(var)
Out[282]:
In [286]:
var = (1,2, 1, 3,4,5, [23,34,43])
In [287]:
var.count(1)
Out[287]:
In [289]:
var.index(5)
Out[289]:
In [290]:
a, b = 10, 20
In [293]:
a = 10, 20
In [294]:
a
Out[294]:
元组变量 | 字符串变量 | 列表变量 |
---|---|---|
t_1 = [1,2,3,4,5] | s_1 = 'ni hao' | l_1 = [1,2,3,4,5] |
t_2 = [5,6,7,8,9] | s_2 = 'how are you' | l_2 = [6,7,8,9,10] |
函数 | 元组 | 实例 | 字符串 | 实例 | 列表 | 实例 | |
---|---|---|---|---|---|---|---|
+ | ✅ | t_1 + t_2 | ✅ | s_1 + s_2 | ✅ | l_1 + l_2 | |
* | ✅ | t_1 * 2 | ✅ | s_1 * 2 | ✅ | l_1 * 2 | |
> < | ✅ | t_1 > t_2 | ✅ | s_1 > s_2 | ✅ | l_1 > l2 | |
[index] | ✅ | t_1[0] | ✅ | s_1[0] | ✅ | l_1[0] | 列表可以索引赋值,字符串、元组不可以 |
[::] | ✅ | t_1[::] | ✅ | s_1[0:10:1] | ✅ | l_1[0:10:2] | 列表可以切片赋值,字符串、元组不可以 |
len | ✅ | len(t_1) | ✅ | len(s_1) | ✅ | len(l_1) | |
bool | ✅ | bool(t_1) | ✅ | bool(s_1) | ✅ | bool(l_1) | 空字符串、空列表、空元组转换为布尔型为False |
count | ✅ | t_1.count(1) | ✅ | s_1.count('n') | ✅ | l_1.count(1) | |
index | ✅ | t_1.index(3) | ✅ | s_1.index('n') | ✅ | l_1.index(1) | |
replace | ✅ | s_1.replace('n', 'N') | 字符串replace函数返回一个新字符串,原来的变量不变 | ||||
sort | ✅ | l_1.sort() | |||||
reverse | ✅ | l_1.reverse() | 字符串不可更改,只能通过生成一个新的字符串来翻转 | ||||
append | ✅ | l_1.append(100) | |||||
extend | ✅ | l_1.extend(l_2) | |||||
remove | ✅ | l_1.remove(1) | |||||
pop | ✅ | l_1.pop() |
In [296]:
var = {}
var = dict()
type(var)
Out[296]:
In [298]:
var = {
'中': 100,
'左': 200
}
In [299]:
var['中']
Out[299]:
In [300]:
words = ['中', '左']
location = [100, 200]
location[words.index('中')]
Out[300]:
In [303]:
new_var = list(zip(words, location))
In [311]:
new_var
Out[311]:
In [304]:
dict(new_var)
Out[304]:
In [310]:
list(zip([1,2], [3,4, 5], [4,5,5]))
Out[310]:
In [313]:
students = ['wong', 'li', 'sun', 'zhao', 'qian']
In [315]:
money = dict.fromkeys(students, 10)
In [318]:
money['wong']
Out[318]:
In [324]:
a = money.get('ww', '100')
In [325]:
print(a)
In [326]:
money.keys()
Out[326]:
In [327]:
money.values()
Out[327]:
In [328]:
money.items()
Out[328]:
In [331]:
# 删除操作
money.pop('wong')
In [332]:
money
Out[332]:
In [340]:
money['nihao'] = 100
In [341]:
money
Out[341]:
In [343]:
money.setdefault('haha', 1000)
Out[343]:
In [344]:
money
Out[344]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]: