Python初学者笔记@contents
1.1 Python 初学者指南
以下笔记是在有一定C语言基础的情况下写成,参考了kaggle上面的Alexis Cook’s Titanic Tutorial(非常推荐这个教程),收录了一些重要且实用的syntaxs和tips,非商业使用,纯属个人学习。
2.1 变量和数字
Operator | Name | Description |
---|---|---|
a + b | Addition | Sum of a and b |
a - b | Subtraction | Difference of a and b |
a * b | Multiplication | Product of a and b |
a / b | True division | Quotient of a and b |
a // b | Floor division | Quotient of a and b, removing fractional parts |
a % b | Modulus | Integer remainder after division of a by b |
a ** b | Exponentiation | a raised to the power of b |
-a | Negation | The negative of a |
2.2 函数
1.获取帮助
help(function)
输出:1.function( )的参数 2.function( )的描述
Attention: help( )可以查询很多东西,还包括输出,一个列表,元素.调用函数等等。
2.定义函数
def function_name():
return # 可以没有
Python不能直接将我们新定义的函数转换为很贴切的描述,但我们在定义函数的时候可以做注释(docstring),这样就能用help( )函数来查询该函数的信息。
def function_name():
"""
description
>>> example
"""
return # 可以没有
help(function_name)的输出就是"“” 和"""之间的内容。
2.3 布尔数和条件判断
Operation | Description | Operation | Description |
---|---|---|---|
a == b | a equal to b | a != b | a not equal to b |
a < b | a less than b | a > b | a greater than b |
a <= b | a less than or equal to b | a >= b | a greater than or equal to b |
Attention: 3.0 == 3, ‘3’ != 3
3.1 List 列表
一些形式:
List = [1, 2, 3]
planets = ['Mercury', 'Venus', 'Earth', 'Mars', 'Jupiter', 'Saturn', 'Uranus', 'Neptune']
hands = [['J', 'Q', 'K'], ['2', '2', '2'], ['6', 'A', 'K']]
my_favourite_things = [32, 'raindrops on roses', help]
一些引用方式:以planets为例,
planets[0] == 'Mercury'
planets[-1] == 'Neptune'
planets[0:3] == ['Mercury', 'Venus', 'Earth']
planets[:3] == ['Mercury', 'Venus', 'Earth']
planets[3:] == ['Mars', 'Jupiter', 'Saturn', 'Uranus', 'Neptune']
planets[1:-1] == ['Venus', 'Earth', 'Mars', 'Jupiter', 'Saturn', 'Uranus']
planets[-3:] == ['Saturn', 'Uranus', 'Neptune']
一些常用函数:
len(list) # 求列表长度
sum(list) # 求列表数之和
sorted(list) # 将列表元素以字母顺序排列(in alphabetical order)
max(list) # 求列表最大数
list.index(elem) # 求某元素索引
一些疑问:
a = [1, 2, 3]
b = [1, [2, 3]]
c = []
d = [1, 2, 3][1:] # ????
# Put your predictions in the list below. Lengths should contain 4 numbers, the
# first being the length of a, the second being the length of b and so on.
lengths = [3, 2, 0, 2]
3.2 Tuple 元组
一些形式:
t = (1, 2, 3) # 等效于 t = 1, 2, 3
Attention: 元组不能通过t[0] = 4来修改值
一些用法:
元组常用于函数有多个返回值的情况
3.3 循环和列表理解(List Comprehensions)
Loops
for循环:
planets = ['Mercury', 'Venus', 'Earth', 'Mars', 'Jupiter', 'Saturn', 'Uranus', 'Neptune']
for planet in planets:
print(planet, end=' ') # print all on same line
Outputs[]: Mercury Venus Earth Mars Jupiter Saturn Uranus Neptune
一个循环需要:要使用的变量名称和循环的集 ,用 "in "这个词把它们连接起来。
这个可循环的集可以是列表、元组,甚至字符串。
for i in range(n_runs):
表示循环n_runs次。
while 循环:
while i < n:
如果不能达到判断条件就终止循环。
List Comprehensions
List 可以由循环和条件来定义。
short_planets = [planet for planet in planets if len(planet) < 6]
short_planets
Out[]:['Venus', 'Earth', 'Mars']
3.4 字符串
一些形式:
x = 'Pluto is a planet'
y = "Pluto is a planet"
x == y
Out[]: True
双引号和单引号具有同样效力,根据字符串中原本有没有引号决定。
如果要在一个字符串中用一种引号,则需要转义字符。
'Pluto\'s a planet!'
Out[]: "Pluto's a planet!"
print之后自带换行符。
字符串可以类似于列表地直接访问其中字母。
一些函数:
str.split( ) 可以将字符串以空格来分割成单元,以列表形式输出。如果在括号里加入其他符号,可以按其他符号来分割。
‘symbol’.join(list) 可以将列表形式的单元以各种符号连接形成字符串。
‘+’ 加号可以连接字符串格式的元素组成新字符串。
str.format( ) 可以将str中{ }的部分替换成format( )内的元素,{: .n%}表示替换成保留n位有效数字的数值,{ }内可编号。
3.5 字典
dictionary = { 'elem1' : num1, 'elem2' : num2, ...} # num为elem的索引(key)
dictionary['elem1'] == num1
4 扩展库
一些形式:
import math # 引用math库
import math as mt # 引用math库并命名为mt
from math import * # *使你可以直接访问整个库的所有变量(不带任何前缀),有时会出问题
from math import log, pi # 可以直接引用要用的部分,以便直接访问
初学者笔记到此结束,关于python的学习仍在继续,其中有很多可以扩展讲述的部分仍待展开……