一、大纲

如下,chapter4 是个概览,之后才是具体讲解。

[Python] 01 - Number and Matrix

 

二、 编译过程

Ref: http://www.dsf.unica.it/~fiore/LearningPython.pdf

[Python] 01 - Number and Matrix

 

 



 

 

 

 

 

 

三、 四个概念

145/1594

Python programs can be decomposed into modules, statements, expressions, and objects, as follows:

1. Programs are composed of modules.

2. Modules contain statements.

3. Statements contain expressions.

4. Expressions create and process objects.

 

四、变量类型

Python’s Core Data Types

[Python] 01 - Number and Matrix

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

五、枚举类

From: 使用枚举类

使用默认值

from enum import Enum
# 定义 Month
= Enum('Month', ('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'))
# 遍历
for name, member in Month.__members__.items(): print(name, '=>', member, ',', member.value)

 

自定义值

@unique装饰器可以帮助我们检查保证没有重复值,这里使用了“类”。

from enum import Enum, unique

@unique
class Weekday(Enum):
    Sun = 0 # Sun的value被设定为0
    Mon = 1
    Tue = 2
    Wed = 3
    Thu = 4
    Fri = 5
    Sat = 6

 

六、举些栗子

  1 # coding: utf-8
  2 
  3 # <h1 align="center">Built-ins</h1>
  4 
  5 # ## Types and classes
  6 
  7 # In[1]:
  8 
  9 
 10 type(True), type(1), type (1.), type('1'), type([]), type(()), type({}), type({1})
 11 
 12 
 13 # In[2]:
 14 
 15 
 16 x = 1.
 17 type(x)
 18 
 19 
 20 # In[3]:
 21 
 22 
 23 type(bool), type(int), type(float), type(str), type(list), type(type)
 24 
 25 
 26 # In[4]:
 27 
 28 
 29 isinstance(1, int), isinstance(1, float), isinstance({2}, set), isinstance({}, set)
 30 
 31 
 32 # In[5]:
 33 
 34 
 35 issubclass(bool, int), issubclass(int, float)
 36 
 37 
 38 # ## Literal representations
 39 
 40 # In[6]:
 41 
 42 
 43 bool(True), bool(-17.8), bool('17'), bool([]), bool({}), bool(None), bool('')
 44 
 45 
 46 # In[7]:
 47 
 48 
 49 int('17'), int('10001', 2), int('21', 8), int('15', 12), int('11', 16), int('h', 22)
 50 
 51 
 52 # In[8]:
 53 
 54 
 55 int(True), int(17), int(-17), int(0b10001), int(0o21), int(0x11), int(17.8)
 56 
 57 
 58 # In[9]:
 59 
 60 
 61 bin(True), bin(17), bin(-17), bin(0b10001), bin(0o21), bin(0x11)
 62 
 63 
 64 # In[10]:
 65 
 66 
 67 oct(True), oct(17), oct(-17), oct(0b10001), oct(0o21), oct(0x11)
 68 
 69 
 70 # In[11]:
 71 
 72 
 73 hex(True), hex(17), hex(-17), hex(0b10001), hex(0o21), hex(0x11)
 74 
 75 
 76 # In[12]:
 77 
 78 
 79 float(False), float(17), float(17.), float(17e-1), float(0.17E2), float('170E-1') 
 80 
 81 
 82 # In[13]:
 83 
 84 
 85 print(complex(0), complex(1.5), complex(4.5, -7.5))
 86 print(complex(0).conjugate(), complex(1.5).conjugate(), complex(4.5, -7.5).conjugate())
 87 print(complex(0).real, complex(1.5).real, complex(4.5, -7.5).real)
 88 print(complex(0).imag, complex(1.5).imag, complex(4.5, -7.5).imag)
 89 
 90 
 91 # ## Operations on numbers
 92 
 93 # In[14]:
 94 
 95 
 96 abs(-3.8), abs(-2), abs(3.8)
 97 
 98 
 99 # In[15]:
100 
101 
102 print(round(-3.6), round(-3.5), round(-3.4), round(3.4), round(3.5), round(3.6))
103 print(round(-3.1235, 3), round(-3.123456, 4), round(3.123456, 4), round(3.1235, 3))
104 
105 
106 # In[16]:
107 
108 
109 divmod(13, 5), divmod(-13., 5), divmod(13., -5.), divmod(-13, -5.), divmod(3.5, 2)
110 
111 
112 # In[17]:
113 
114 
115 print(pow(2, 3), pow(-2., -1), pow(4, -0.5), pow(-1, 0.5), pow(-3.8, 0), pow(1j, 1j))
116 print(pow(-2, 3, 3), pow(-2, 3, 5), pow(2, 3, 3), pow(2, 3, 5))
117 
118 
119 # ## Strings
120 
121 # In[18]:
122 
123 
124 print(ord('c'), ord('\xf7'), ord(''), ord('\u2603'))
125 print(chr(99), chr(247), chr(8226), chr(9731))
126 
127 
128 # In[19]:
129 
130 
131 ascii('Ça me tient ∞ment à cœur\t')
132 
133 
134 # In[20]:
135 
136 
137 repr('A string'), str('A string')
138 
139 
140 # In[21]:
141 
142 
143 a = 2; b = 4.5
144 eval('(a + 3.5) * (b + 5.)')
145 
146 
147 # In[22]:
148 
149 
150 message = input('Input your message: ')
151 print('Your message is:', message)
152 
153 
154 # ## Creating and processing iteratables
155 
156 # In[23]:
157 
158 
159 print(tuple(range(4)), tuple(range(4, 8)), tuple(range(4, 16, 3)))
160 print(tuple(range(-8)), tuple(range(-8, -4)), tuple(range(-16, -8, -3)))
161 print()
162 
163 print(range(2, 8, 2).count(4), range(2, 8, 2).count(5), range(2, 8, 2).index(4))
164 
165 
166 # In[24]:
167 
168 
169 print(list(enumerate({10, 15, 25, 40})))
170 print(dict(enumerate((10, 15, 25, 40), 3)))
171 
172 
173 # In[25]:
174 
175 
176 print(list(zip([1, 2, 3, 4], [11, 12, 13], [21, 22, 23, 24, 25], [31, 32, 33])))
177 print(list(zip(*zip([1, 2, 3, 4], [11, 12, 13], [21, 22, 23, 24, 25], [31, 32, 33]))))
178 
179 
180 # In[26]:
181 
182 
183 print(list(map(sorted,
184                [(1, 2, 3), (7, 5, 4, 6, 8), (10, 9), (11,)])))
185 print(set(map(int.__add__,
186               [1, 2, 3, 4, 5], [11, 12, 13])))
187 print(dict(map(lambda x: (x, 2 * x),
188                (0, 1, 2, 3, 4, 5))))
189 print(tuple(map(lambda x, y, z: len({x, y, z}) == 2,
190                 [1, 20, 30, -4, 5, 60], [-1, 20, 31, 4, 5], [1, 20, 32, 4, -5, 61, 70])))
191 
192 
193 # In[27]:
194 
195 
196 print(list(filter(str.isupper,
197                  {'A': 1, 'b': 2, 'c': 3, 'D': 4, 'E': 5})))
198 print(tuple(filter(lambda x: x < 10,
199                 [-1, 20, -3, -4, 50, 60])))
200 
201 
202 # In[28]:
203 
204 
205 print(sum([3, 1, 7, 5]))
206 print(sum({3: 1, 1: 1, 7: 2, 5: 3}))
207 
208 
209 # In[29]:
210 
211 
212 print(min((3, 1, 7, 5)))
213 # min() accepts also an arbitrary number of arguments
214 print(min(3, 1, 7, 5))
215 
216 
217 # In[30]:
218 
219 
220 print(max({3 : 10, 1: 10, 7: 10, 5: 10}))
221 # max() accepts also an arbitrary number of arguments
222 print(max(3, 1, 7, 5))
223 
224 
225 # In[31]:
226 
227 
228 sorted([2, 1, 3, 4, 0])
229 
230 
231 # In[32]:
232 
233 
234 list(reversed((2, 1, 3, 4, 0)))
235 
236 
237 # In[34]:
238 
239 
240 print(any([0, 0, 0, 0, 0]))
241 print(any((1, 0, 0, 1, 0)))
242 
243 
244 # In[35]:
245 
246 
247 print(all({5, 2, 3, 1, 4}))
248 print(all({5: 1, 2: 1, 3: 1, 0: 1, 4: 4}))
View Code

相关文章:

  • 2022-12-23
  • 2021-11-18
  • 2021-09-10
  • 2021-12-10
  • 2021-12-15
  • 2022-12-23
  • 2022-12-23
  • 2021-12-09
猜你喜欢
  • 2021-08-15
  • 2021-11-24
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案