-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbasic
More file actions
299 lines (208 loc) · 6.96 KB
/
basic
File metadata and controls
299 lines (208 loc) · 6.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
0.GUI (graphical user interface) such as file finders
eg sed 's/dude/Colt/g' report.txt > report_new.txt //sed:search and pop it to report_new
Be familiar and confortable with command line
file structure is a tree, all relative to a base ROOT director
root/
each additional level down adds a "/"
absolute path /Users/colt // abs path always start with a / --which stands for the root
by default is at home directory and represent as "~"
pwd : print working directory
cd : change directory
type python3 to go into python3, otherwise is python --2
use touch to create a new .py file
print("hello world")
//don't need the semi colon at end
1.variables must stary with a letter or underscore_
CASE sensitive!!
2. ** power
% remainder
/ 不是整数除法,就是正常的除 (in python 3) python2 和 c++一样还是整数除法
x = 1 + 2**3/4*5
print(x) //1+8/4 *5 =1+2*5 =11.0
1+1.0 // 2.0 - to float automatically
// --- Interger Division
5//3 #1
10//3 #3 (always round down)
1/2 #0.5 python always get float
2**3 #8
49**0.5 #7
13%2 #1
3.constans:fixed vlues such as numbers, letters and strings are constants
string constants use single quotes ('') or double quotes ("")
4. try/except -- traceback
try 里面任意一个die了直接走except,不会继续走下面的了--利用try来尝试危险,catch errors
astr = ‘hello bob'
try:
ister = int (astr)
except:
ister = -1 // usualy provide hints for where is wrong, can add a quit here
5.
name = input('who are u?) // the input() will return a string
print ('welcome', name) // the comma will creat a space
6. use # for comments in python
7. python use indent to distinguish block, which is 4 spaces, it is better to use space than tab, to avoid difference between different editors
8. python 里的print() 有自动换行功效
use --type(9)
will show the type of it, can be a variable name
9.
python documentation ---docs.python.org
Give you information about python stuff--make sure to check the python3 documents
''' for python multiple line comments
#the naming convention of python is s_n_a_k_e
#you don't need to declare the type of variables
nums_of_casts = 99
nums_of_casts * 2 #nums_of_cats doesn't change
all, at, once = 5, 10, 15
#you can always assign and reassign variables
10. naming_conventions in python
most variables should be lower case,
CAPITAL_NAME usually is constants
UpperCamelCase : refer to a class
"dunder" for double underscore ***
11.Some data types:
bool (True, False, the first case has to be capital)
int
str (a sequence of Unicode chars, “ ” or ‘’)
list(an ordered sequence of values of other data types [1,2,3])
dict ( a collection of key: values e.g. {"first_name":"Le", "last_name": "Lei"}
12. Dynamic typing
you can reassign variables to different types
dynamic typing vs statically-typed(variables are stuck with their originally-assigned types)
13. None (case sensitive!)
(python version of NULL)
can set new stuff with None if we haven't assign it
(want a container, but nothing here, and the type of None is Nonetype--only itself)
14.Declaring strings
doesn't matter if single or double, but better to stay it consistant
msg = "he said "hello there !"" --- error, python read it "XX" +YY""
msg2 = "he said 'hello there !'" --- good
msg3 = 'he said "hello there !"' --- good
15.escape sequence
str_sth = "he said \"ha ha\"" --- good #str is never a good variable name, which you could..
16. string concateration
with “ +”
user = "blue Boy"
print("hello there" + user) # hello therebuleBoy ---不会自动给你加空格
8 + "hello" #error!
guess = 9
print("your guess is" + guess) #error!
name = "col"
name += "S" # name now = colS #also works for numbers
17.string format
interpolate #插入
f-strings (python 3.6+)
x = 10
formatted = f"I told you {x} times!" #不可以忘了这个f!
#you can also do math in the {}
# in python 2 .format() --play ground only support it
print("the {} is ).format(sth)
first = "Colt"
last = "Steele"
formatted = "First Name: {}, Last Name: {}". format(first, last) #可以直接在string 后加
18. every stirng is indexed, started with 0
"lol"[0] #l
name = "Chuck"
name[5] # error! max index is 4 ; not counting '\0' (if there is one in python)
name[-1] #k
name[-2] #c 倒着数的嗷
19. convert data types
in string interpolation, data type are implicitly converted into string form
you can explicitly convert type by var2 = type (var1)
deci = 12.213
interger = int(deci) #12
my_list = [1,2,3]
my_list_as_string = string(my_list) # "[1,2,3]"
1/3 ----0.3333
int(99.99) #99 only truncate not around
4/2 ----2.0
round(thing to round, how many decimal points)
20.for input function, 不会自动换行
data = input("what is your name?") # what is your name?your_input (also no space!)
print("You said" +data)
#onther way looks nicer
print("what is your name?")
data = input() # what is your name? -another line- your_input
print("You said" +data)
21. conditional statements
if name == "tony stark :
use indent to distinguish
print("you are Iron Man")
elif name == "Peter Parker": #you can have multiple elif, but only one else
print("you are Spider Man")
elif name == "Natasha":
print("you are Black Widom")
elif name == "Thor":
print("you are Thor")
else :
print("you are Norm Man")
------
#这种没有之后的内容(indent的话会报错)
if name == "tony stark :
print("you are Iron Man")
22. Truthiness
things are natually false: empty objects, empty strings, None, and zero
animal = input("say sth)
if animal: #没有input时自动不会做这些
print("you said sth)
== test if they have the same value
"A" < "a" # yep
logical operators:
and, or(binary), not(unary)
age > 2 and age < 8
if not isWeekend():
do th
if not x or y: #先判断not X, 再看 or
do th
23.is vs ==
is check with the memory and == checks value
is--checking equality
a = 1
a == 1 #True
a is 1 #True
a = [1,2,3]
b = [1,2,3]
a == b #True
a is b #Flase, they are different objects
c = b
b is c #True, b and c are pointing to the same stuff
a = 13
b = 13
a == b #True
a is b #True
24. loop
-iterable objects
[40,30,435]
"hello'
range(1,10) # generate 1-9 包左不包右
for item in iterable_object:
do sth with item
eg:
for char in "hello":
print(char *10) #每个char 会print 10 次
for x in rang(1,10):
print x # print 1-9, BTW, python 的print会自动换行
25. ranges (commonly use for for loops)
range (7) # 0-6
range(1,8) #1-7 永远最右侧的不包
range(1,10,2) # give odd 1,3,5,7,9 - the last one is 间距
range(7,0,-1) #7-1
list(range(10)) #ok
tricky:
>>> nums = range(1,5)
>>> print(nums)
#range(1, 5)
26. while loop
while bool_expression:
do sth
for num in range(1,11):
print(num)
<=>
num = 1
while num < 11:
num += 1
print(num)
27.break
to exit out of a loop immediatly
28. style for python:
PEP8(style Guide for python code!)
autopep 8 --inplace ugly_code.py (need to install the package)