Skip to content

Commit 168c4bf

Browse files
committed
add python basic
1 parent 0eaa8ea commit 168c4bf

File tree

2 files changed

+167
-0
lines changed

2 files changed

+167
-0
lines changed

189.py

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#==================================================
2+
#==> Title:
3+
#==> Author: Zhang zhen
4+
#==> Email: hustmatnoble.gmail.com
5+
#==> GitHub: https://github.com/MatNoble
6+
#==> Date:
7+
#==================================================
8+
9+
10+
nums = list(range(1, 8))
11+
k = 3 + len(nums)
12+
13+
# nums = [-1,-100,3,99]
14+
# k = 2
15+
k %= len(nums)
16+
print(k)
17+
18+
print(nums[-k:] + nums[:-k])

python basic/type_of_data.md

+149
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
# 数据类型
2+
## 变量 Variable
3+
- 不可变:Number, String, Tuple
4+
- 可变:Bool, List, Set, Dictionary
5+
6+
### 布尔 Bool
7+
True or False
8+
9+
| and | True | False |
10+
| --- | --- | --- |
11+
|True | True | False |
12+
|False | False | False |
13+
14+
| or | True | False |
15+
| --- | --- | --- |
16+
|True | True | True |
17+
|False | True | False |
18+
19+
| not | True | False |
20+
| --- | --- | --- |
21+
| | False | True |
22+
23+
### 数字 Number
24+
> int, long, float, complex
25+
26+
- `//` **向下取整**:返回商的整数部分
27+
```
28+
365 // 10 # 36
29+
5 // 3 # 1
30+
-5 // 3 # -2
31+
```
32+
33+
- 负数零取整 int()
34+
```
35+
int(-5//3) # -1
36+
```
37+
38+
- `%` 取模 - 返回除法的余数
39+
```
40+
365 % 10 # 5 --> (365 - (365//10)*10 = 5)
41+
5 % 3 # 2 --> (5 - (5//3)*3 = 2)
42+
-5 % 3 # 1 --> (-5 - (-5//3)*3 = 1)
43+
5 % -3 # -1 --> (5 - (5//-3)*(-3) = -1)
44+
# 奇数
45+
num % 2 != 0
46+
# 偶数
47+
num % 2 == 0
48+
49+
# 移位操作
50+
nums % 2^n = nums & (2^n - 1)
51+
nums % 2 = nums & 1
52+
```
53+
54+
### 字符串 String
55+
56+
- f -> format
57+
```
58+
first_name = "Ross"
59+
last_name = "MatNoble"
60+
full_name = f"{first_name} {last_name}" # Ross MatNoble
61+
```
62+
- 特殊符号
63+
64+
|操作|说明|
65+
|-|-|
66+
| \ | 转义符|
67+
|\n| 换行符|
68+
|\t|制表符|
69+
70+
`r` 避免转义
71+
`print(r"\\\t\\") # \\\t\\`
72+
73+
|操作|说明|
74+
|-|-|
75+
|.title()|首字母大写|
76+
|.upper()|大写|
77+
|.lower()|小写|
78+
|.strip()|消除空格|
79+
80+
### 列表 List[]
81+
|操作|说明|
82+
|-|-|
83+
|.insert(index, value)||
84+
|.append(value)||
85+
|.remove(value)||
86+
|del list[index]||
87+
|.pop()||
88+
89+
#### 直接赋值 & 浅拷贝(copy) & 深拷贝(deepcopy)
90+
91+
- **直接赋值**:其实就是对象的引用(别名)。
92+
- **浅拷贝(copy)**:拷贝父对象,不会拷贝对象的内部的子对象。
93+
- **深拷贝(deepcopy)**: copy 模块的 deepcopy 方法,完全拷贝了父对象及其子对象。
94+
95+
<img src="https://cdn.jsdelivr.net/gh/MatNoble/Images/copy.png" width="600"/>
96+
97+
```
98+
import copy
99+
a = [1, 2, 3, 4, ['a', 'b']] #原始对象
100+
101+
b = a #赋值,传对象的引用
102+
c = copy.copy(a) #对象拷贝,浅拷贝
103+
d = copy.deepcopy(a) #对象拷贝,深拷贝
104+
105+
a.append(5) #修改对象a
106+
a[4].append('c') #修改对象a中的['a', 'b']数组对象
107+
108+
print( 'a = ', a )
109+
print( 'b = ', b )
110+
print( 'c = ', c )
111+
print( 'd = ', d )
112+
113+
>>> ('a = ', [1, 2, 3, 4, ['a', 'b', 'c'], 5])
114+
>>> ('b = ', [1, 2, 3, 4, ['a', 'b', 'c'], 5])
115+
>>> ('c = ', [1, 2, 3, 4, ['a', 'b', 'c']])
116+
>>> ('d = ', [1, 2, 3, 4, ['a', 'b']])
117+
```
118+
119+
#### 其他函数
120+
121+
|操作|说明|
122+
|-|-|
123+
|.sort()|排序|
124+
|.reverse()|逆序|
125+
126+
### 元组 Tuple()
127+
128+
**不可修改**
129+
130+
### 字典 Dictionary{:}
131+
`dict = {key1:value1, key2:value2, key3:value3}`
132+
133+
-
134+
`dict['key'] = value`
135+
-
136+
`del dict['key']`
137+
138+
[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/drive/1TUA3v4K_zdZzSUjCtoO-0tbA_9sAvFQe?usp=sharing)
139+
140+
### 集合 Set{}
141+
142+
**唯一性**
143+
144+
|操作|说明|
145+
|-|-|
146+
|.add(value)||
147+
|.update(value)|增, 列表、元组、字典|
148+
|.remove(value)||
149+
|.discard(value)|删,不报错|

0 commit comments

Comments
 (0)