从 Jupyter Notebook 角度看
a=3
print("hello")
hello
print('world')
world
print(a)
3
import os
print(os.path.abspath('.'))
C:\Users\hanzhuan
import os
print(os.path.abspath('.'))
E:\data\ProjectData\JupyteNotebook\01-hello
import this
The Zen of Python, by Tim Peters
Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!
1+1
2+2
4
print(1+1)
2+2
2
4
10/3
3.3333333333333335
10%3
1
2**3
8
a=3
a
3
type (a)
int
f = 1.5
type(f)
float
int(f)
1
b = int(f)
type(b)
int
c=float(b)
type(c)
float
a_str="123"
float(a_str)
123.0
a_str="123a"
float(a_str)
ValueError Traceback (most recent call last)
Input In [28], in() |
----> 1 float(a_str)ValueError: could not convert string to float: ‘123a’
b = true
b
True
type(b)
bool
3>5
False
x=3
1<x<6
True
abs(-2.6)
2.6
round(2.6)
3
min(4,5,2,6,1)
1
max(4,5,2,6,1)
6
100000e-4
10.0
10e100
1e+101
2**10
1024
hza_str="hello python "
print(hza_str)
hello python
hza_str='hello '+'python '
hza_str
'hello python ’
hza_str*3
'hello python hello python hello python ’
hza='hello'
len(hza)
5
tang='1 2 3 4 5'
tang.split()
[‘1’, ‘2’, ‘3’, ‘4’, ‘5’]
tang='1,2,3,4,5'
tang=tang.split(',')
tang
[‘1’, ‘2’, ‘3’, ‘4’, ‘5’]
tang_str= ' '
tang_str.join(tang)
‘1 2 3 4 5’
hza='hello python'
hza.replace('python','world')
‘hello world’
hza
# 并未改变hza变量本身
‘hello python’
hza2=hza.replace('python','world')
#新赋值给变量才行
hza2
‘hello world’
hza2.upper()
‘HELLO WORLD’
hza2='HELLO WORLD'
hza2.lower()
‘hello world’
hza=" hello python "
hza.strip()
‘hello python’
hza
’ hello python ’
hza.lstrip()
'hello python ’
hza.rstrip()
’ hello python’
'{} {} {}'.format('han','zhu','an')
‘han zhu an’
'{2} {1} {0}'.format('han','zhu','an')
‘an zhu han’
'{han},{zhu},{an}'.format(han=10,zhu=20,an=30)
# 参数形式传入
‘10,20,30’
han='han zhu an:'
b=123.5
c=985
result = '%s %f %d' % (han,b,c)
result
# 就是print格式化输出的那个语法
‘han zhu an: 123.500000 985’
hza='han zhu an'
print(hza[0])
print(hza[4])
hza[8]
h
z
‘a’
hza[-1]
#从后往前 第一个
‘n’
hza[-6]
‘z’
hza[10]
# 索引也不能越界
IndexError Traceback (most recent call last)
Input In [10], in() |
----> 1 hza[10]IndexError: string index out of range
[m:n] 表示从索引m到n-1
仍然是: 左闭右开的区间
hza[0:3]
# [0,3) 还是左闭右开 =》[0,2]
‘han’
hza[4:7]
‘zhu’
hza
‘han zhu an’
hza[4:]
# 索引4(0开始)开始后面所有
‘zhu an’
hza[:7]
# 开始直到下标6 (还是不包括右)
‘han zhu’
hza[2:-1]
# 开头第2个取到倒数第1个
‘n zhu a’
hza[-2:]
# 倒数第2个开始 一直往后取
‘an’
hza
‘han zhu an’
hza[:-3]
# 从开始一直取到倒数第4个
‘han zhu’
hza[:]
# 取所有
‘han zhu an’
hza[::2]
# 每隔2个取一个值 也即:0 2 4 6 8
‘hnzua’
hza=[]
type(hza)
list
hza=[1,2,3,4]
hza
[1, 2, 3, 4]
hza=['1','2','3','4']
hza
[‘1’, ‘2’, ‘3’, ‘4’]
hza=[1,'hza',3.5]
hza
[1, ‘hza’, 3.5]
hza=list()
print(type(hza))
# 其他建立List的方式
hza=list([1,2,3])
hza
# 其他建立List的方式
#不建议 还是怎么方便怎么来就行了
[1, 2, 3]
hza=[1,2,3]
len(hza)
# 长度
3
a= [123,567]
b = ['han','zhu','an']
a+b
# list 拼接
[123, 567, ‘han’, ‘zhu’, ‘an’]
a*2
# list乘整数 : 复制
[123, 567, 123, 567]
a
[123, 567]
print(a[0])
print(a[1])
print(a[-1])
# 索引的使用还是一样
123
567
567
print(a[2])
# 索引也不能越界
IndexError Traceback (most recent call last)
Input In [16], in() |
----> 1 print(a[2])IndexError: list index out of range
a
[123, 567]
a[0]=456
a
[456, 567]
a[:]=['han','zhu']
a
# 整体替换
[‘han’, ‘zhu’]
a=[0,1,2,3,4,5,6,7]
a[2:5]
# 和数组一样 取任意部分
[2, 3, 4]
del a[0]
a
# 真的删除a中某个元素 再执行一次又会把元素1删除
[1, 2, 3, 4, 5, 6, 7]
del a[4:]
# 删一堆 索引4开始一直往后所有元素全部删除
a
[1, 2, 3, 4]
a=[1,2,3,4,5,6,7,8,9]
8 in a
# 8是否在a中
# 判断元素是否在list中 in
True
10 in a
False
10 not in a
True
hza=['han','zhu','an']
'han' in hza
#元素换成字符串 一样的
True
hza={}
type(hza)
dict
hza=dict()
type(hza)
dict
hza
{}
key - value
hza={}
hza['first']=123
hza
# 先声明字典 再使用字典
{‘first’: 123}
hza['python']=456
hza
{‘first’: 123, ‘python’: 456}
hza['python']
456
hza['python']=789
hza
{‘first’: 123, ‘python’: 789}
hza={'han':123, 'zhu':456, 'an':789}
hza
{‘han’: 123, ‘zhu’: 456, ‘an’: 789}
hza[0]
# 有自己的key 不能用普通数字索引访问了
KeyError Traceback (most recent call last)
Input In [16], in() |
----> 1 hza[0]KeyError: 0
hza_value=[1,2,3]
hza={}
hza['zhuan']=hza_value
hza['zhuan2']=3
hza['zhuan3']='4'
hza
# 字典value可以是任意类型
{‘zhuan’: [1, 2, 3], ‘zhuan2’: 3, ‘zhuan3’: ‘4’}
hza = {}
d1 = {'tang':123, 'yudi':456}
d2={'tang2':456, 'yudi2':789}
hza['test1']=d1
hza['test2']=d2
hza
# value可以是嵌套的字典
{‘test1’: {‘tang’: 123, ‘yudi’: 456}, ‘test2’: {‘tang2’: 456, ‘yudi2’: 789}}
hza=dict([('han',123),('zhuan',456)])
hza
# (key,value)形式直接给字典赋值
{‘han’: 123, ‘zhuan’: 456}
hza['han'] += 1
hza
{‘han’: 124, ‘zhuan’: 456}
hza.get('han')
# 和javaMap一样的取也行
124
hza['an']
# 没有的key报错
KeyError Traceback (most recent call
last) Input In [8], in() |
----> 1 hza[‘an’] KeyError: ‘an’
hza.get('an')
# 没有的key不报错
hza.get('an','hanzhu')
# 直接返回value 原来的字典没有加入新值
‘hanzhu’
hza
{‘han’: 124, ‘zhuan’: 456}
hza.pop('han')
# 直接当栈来用 或者说:删除元素
124
hza
{‘zhuan’: 456}
del hza['zhuan']
hza
# 删除元素 key为参数即可
{}
hza={'han':123, 'zhuan':456}
hza2={'han':789,'python':888}
hza.update(hza2)
# 用另一个字典hza2更新本字典hza hza本来有的key更新value hza原来没有的key直接加入
hza
{‘han’: 789, ‘zhuan’: 456, ‘python’: 888}
'han' in hza
True
'hello' in hza
False
hza
{‘han’: 789, ‘zhuan’: 456, ‘python’: 888}
hza.keys()
dict_keys([‘han’, ‘zhuan’, ‘python’])
hza.values()
dict_values([789, 456, 888])
hza.items()
dict_items([(‘han’, 789), (‘zhuan’, 456), (‘python’, 888)])
# for循环直接方便地遍历字典
for key,value in hza.items():
print(key,value)
han 789
zhuan 456
python 888
# 可以直接返回多个值
def max_min(*arr):
a=max(arr)
b=min(arr)
return a,b
a,b = max_min(8,9,0,12,3)
print(a,b)
12 0
hza=[123,123,456,456,789]
hza=set(hza)
hza
{123, 456, 789}
hza=set()
type(hza)
set
# list转集合
hza=set([123,123,456,456,456,789,789])
hza
{123, 456, 789}
# 真直接构造集合 (可以看成字典的key)
hza={123,456,789,123,456,788}
hza
{123, 456, 788, 789}
a={1,2,3,4}
b={2,3,4,5}
a.union(b)
# a并b
{1, 2, 3, 4, 5}
a | b
{1, 2, 3, 4, 5}
a.intersection(b)
{2, 3, 4}
a & b
{2, 3, 4}
a.difference(b)
{1}
a-b
{1}
b.difference(a)
{5}
b-a
{5}
a={1,2,3,4,5,6}
b={2,3,4}
b.issubset(a)
# b是否为a的子集
True
a.issubset(b)
# a是否为b的子集
False
b<=a
True
b>a
False
a<=a
# 自己是自己的子集
True
a<a
# 自己不是自己的真子集
False
a={1,2,3}
a
{1, 2, 3}
a.add(4)
a
{1, 2, 3, 4}
# a.add({5,6})
# a.add(5,6)
# a.add([5,6])
# 均报错 批量添加用update
a.update([2,3,4,5,6])
a
# 批量添加元素
{1, 2, 3, 4, 5, 6}
a.remove(1)
a
{2, 3, 4, 5, 6}
a.pop()
a
{3, 4, 5, 6}
# a.remove(7)
# 删除不存在的元素 报错
# a.remove([4,5])
# 无法如此批量删除
hza = 123
zhuan = hza
# 同一块内存 id相同 就是一个东西
id(hza)
2040543729840
id(zhuan)
2040543729840
hza is zhuan
True
zhuan = 456789
id(zhuan)
# zhuan重新赋其他值后就是一个新的变量了 id值就变了
2040629319056
hza is zhuan
False
han=12345
zhuan=12345
print(id(han))
id(zhuan)
2040629319376
2040629318736
a=1
b=1
print(id(a))
id(b)
2040543537456
2040543537456