阿里python,最开始参加阿里天池大赛,看到这个学习目录
https://tianchi.aliyun.com/course/?spm=5176.12282013.0.0.4fdf13de2sqr4R
Python基础
https://tianchi.aliyun.com/course/courseDetail?spm=5176.12282042.0.0.14f92042n1sGu3&courseId=287
python 实战
https://tianchi.aliyun.com/course/courseDetail?spm=5176.12282042.0.0.14f92042n1sGu3&courseId=218
numpy入门
https://tianchi.aliyun.com/notebook-ai/detail?spm=5176.12282042.0.0.14f92042n1sGu3&postId=5977
pandas入门
https://tianchi.aliyun.com/notebook-ai/detail?spm=5176.12282042.0.0.14f92042n1sGu3&postId=6068
pandas实践-2012美国总统竞选赞助数据分析-天池实验室-阿里云天池
https://tianchi.aliyun.com/notebook-ai/detail?spm=5176.12282042.0.0.14f92042n1sGu3&postId=10585
=====================
1. 下划线和圆整round
In interactive mode, the last printed expression is assigned to the variable _
.
下划线在交互模式代表上一个打印出来的表达式。
>>> tax = 12.5 / 100
>>> price = 100.50
>>> price * tax
12.5625
>>> price + _
113.0625
>>> round(_, 2)
113.06
如果你不希望前置了 \
的字符转义成特殊字符,可以使用 原始字符串 方式,在引号前添加 r
即可
>>> print('C:\some\name') # here \n means newline!
C:\some
ame
>>> print(r'C:\some\name') # note the r before the quote
C:\some\name
字符串字面值可以跨行连续输入。一种方式是用三重引号:"""..."""
或 '''...'''
。字符串中的回车换行会自动包含到字符串中,如果不想包含,在行尾添加一个 \
即可。如下例:
print("""\
Usage: thingy [OPTIONS]
-h Display this usage message
-H hostname Hostname to connect to
""")
字符串是可以被 索引 (下标访问)的,第一个字符索引是 0。索引也可以用负数,这种会从右边开始数,
>>> word = 'Python'
>>> word[0] # character in position 0
'P'
>>> word[-1] # last character
'n'
注意 -0 和 0 是一样的,所以负数索引从 -1 开始。
注意切片的开始总是被包括在结果中,而结束不被包括。
>>> word[0:2] # characters from position 0 (included) to 2 (excluded)
'Py'
您也可以这么理解切片:将索引视作指向字符 之间 ,第一个字符的左侧标为0,最后一个字符的右侧标为 n ,其中 n 是字符串长度。例如:
+---+---+---+---+---+---+
| P | y | t | h | o | n |
+---+---+---+---+---+---+
0 1 2 3 4 5 6
-6 -5 -4 -3 -2 -1
======== List =========
https://docs.python.org/zh-cn/3.8/tutorial/datastructures.html
>>>letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g']
>>> len(letters)
7
>>>letters[2:5] = []
>>> letters
['a', 'b', 'f', 'g']
>>> letters[:] = []
>>> letters
[]
也可以嵌套列表 (创建包含其他列表的列表), 比如说:
>>> a = ['a', 'b', 'c']
>>> n = [1, 2, 3]
>>> x = [a, n]
>>> x
[['a', 'b', 'c'], [1, 2, 3]]
>>> x[0]
['a', 'b', 'c']
>>> x[0][1]
'b'
======== print ==========
>>> i = 256*256
>>> print('The value of i is', i)
The value of i is 65536
print("-- Do you have any ", i, "?")
======== While ==========
关键字参数 end 可以用来取消输出后面的换行, 或使用另外一个字符串来结尾:
>>> a, b = 0, 1
>>> while a < 1000:
... print(a, end=',')
... a, b = b, a+b
...
0,1,1,2,3,5,8,13,21,34,55,89,144,233,377,610,987,
======== For ==========
words = ['cat', 'window', 'defenestrate']
for w in words:
print(w, len(w))
cat 3
window 6
defenestrate 12
for i in range(5):
print(i)
给定的终止数值并不在要生成的序列里;
range(5, 10)
5, 6, 7, 8, 9
起始,终止,步长
range(0, 10, 3)
0, 3, 6, 9
range(-10, -100, -30)
-10, -40, -70
a = ['Mary', 'had', 'a', 'little', 'lamb']
# len(a)=5
for i in range(len(a)):
print(i, a[i])
如果你只打印 range,会出现奇怪的结果:
>>> print(range(10))
range(0, 10)
此对象会在你迭代它时基于所希望的序列返回连续的项,但它没有真正生成列表,这样就能节省空间。
我们称这样对象为 iterable,也就是说,适合作为这样的目标对象:函数和结构期望从中获取连续的项直到所提供的项全部耗尽。 我们已经看到 for
语句就是这样一种结构,而接受可迭代对象的函数的一个例子是 sum()
:
>>> sum(range(4)) # 0 + 1 + 2 + 3
6
====== If ======
x = int(input("Please enter an integer: "))
if x < 0:
x = 0
print('Negative changed to zero')
elif x == 0:
print('Zero')
elif x == 1:
print('One')
else:
print('More')
========= Lamba 函数 ============
https://docs.python.org/zh-cn/3.8/tutorial/controlflow.html#lambda-expressions
可以用 lambda
关键字来创建一个小的匿名函数。这个函数返回两个参数的和: lambda a, b: a+b
。Lambda函数可以在需要函数对象的任何地方使用。它们在语法上限于单个表达式。
>>> def make_incrementor(n):
... return lambda x: x + n
...
>>> f = make_incrementor(42)
>>> f(0)
42
>>> f(1)
43
>>> pairs = [(1, 'one'), (2, 'two'), (3, 'three'), (4, 'four')]
>>> pairs.sort(key=lambda pair: pair[1]) # 根据one, two ...首字母升序排列,f-o-th-tw
>>> pairs
[(4, 'four'), (1, 'one'), (3, 'three'), (2, 'two')]
Python »3.8.3 Documentation »Python HOWTOs »Sorting HOW TO
student_objects = [
... Student('john', 'A', 15),
... Student('jane', 'B', 12),
... Student('dave', 'B', 10),
... ]
>>> sorted(student_objects, key=lambda student: student.age) # sort by age
[('dave', 'B', 10), ('jane', 'B', 12), ('john', 'A', 15)]
===========================
https://docs.python.org/zh-cn/3.8/tutorial/controlflow.html#arbitrary-argument-lists
出现在 *args
参数之后的任何形式参数都是 ‘仅限关键字参数’,也就是说它们只能作为关键字参数而不能是位置参数。
>>> def concat(*args, sep="/"):
... return sep.join(args)
...
>>> concat("earth", "mars", "venus")
'earth/mars/venus'
>>> concat("earth", "mars", "venus", sep=".")
'earth.mars.venus'
=========== 解包函数的输入参数 ========
*号及**号
https://docs.python.org/zh-cn/3.8/tutorial/controlflow.html#unpacking-argument-lists
>>> list(range(3, 6)) # normal call with separate arguments
[3, 4, 5]
>>> args = [3, 6]
>>> list(range(*args)) # call with arguments unpacked from a list
[3, 4, 5]
>>> def parrot(voltage, state='a stiff', action='voom'):
... print("-- This parrot wouldn't", action, end=' ')
... print("if you put", voltage, "volts through it.", end=' ')
... print("E's", state, "!")
...
>>> d = {"voltage": "four million", "state": "bleedin' demised", "action": "VOOM"}
>>> parrot(**d)
-- This parrot wouldn't VOOM if you put four million volts through it. E's bleedin' demised !