本文内容来源于Yupeng Jiang(伦敦大学数学系)课件,本人认为非常经典,原文为纯英文,网上也已经有翻译的版本,但是自认为还是有些凌乱,看起来不是很舒服,故再次整理发布于此。其一,是作为自己巩固复习之用;其二,资源共享
课程大纲:
为什么使用python?
1.python环境
2. Anaconda中包含的库:
3.常用的数据类型:
Name | Notation | Declaration e.g. |
---|---|---|
Integers | int | a = 10 |
Floating | float | b = 3.14 |
Complex | complex | c = 1 + 2j |
String | str | d = 'Python' |
4.算数运算符:
Name | Notation | Examples |
---|---|---|
Addition | + | a + b |
Subtraction | - | c - b |
Multiplication | * | x*y |
Division | / | x/z |
Modulus | % | x%a |
Exponent | ** | a**x |
练习:执行下列代码
浮点数的精度:
import decimal print (round (3*1415,2)) #3. 14 print (round (9.995,2)) #9. 99 print (decimal.Decimal (9.995)) #9.9949999999999992184029906638897955417633056640625
练习:字符串str的一些方法
t = 'He is a string. Who are you?' print(t.capitalize()) # Cap first letter print(t.split()) # split by words print(t.find('i')) # return index of 'i' print(t[0:4]) # returu from index 0 to 3 print(t.replace(' ','|')) # replace by '|' w = 'http://www.google.com' print(w.strip('http://')) #delete sth
5.python基本数据结构
Name | Nation | Declaration e.g. |
---|---|---|
Tuple | tuple | b = (1,2.5, 'data') |
List | list | c = [1,2.5,'data'] |
Dictionary | dict | d = {'Name': 'Kobe', 'Country':'US'} |
Set | set | e = set(['u','d','ud','d','du']) |
列表(list)的一些方法:
l = [1,2,3.14,'data']
print (type(l))
l.append ([4,3])
print(l)
l.extend (['delta',5,6] )
print(l)
l.insert(3,'beta')
print(l)
l.remove ('data')
print(l)
<class 'list'> [1, 2, 3.14,'data',[4,3]] [1, 2, 3.14,'data',[4,3],'delta',5,6] [1, 2, 3.14,'beta','data',[4, 3],'delta', 5,6] [1, 2, 3.14,'beta',[4, 3],'delta',5,6]
python参考对象:
x = [1,2,3,4] y = x y[0] = 5 print(x) x = [1,2,3,4] z = x.copy() z[0] = 5 print (x)
[5,2,3,4] [1,2,3,4]
多维列表:试一试
a = [[1,2,3,4],[1,2,3,4],[1,2,3]] print(a) print(a[0][3])请注意: 多维列表不是矩阵。 数学运算符可能会导致您不想看到的结果。 对于矩阵计算,我们将在明天花费大量的时间
6.条件语句
Name | Notation |
---|---|
larger | > |
smaller | < |
equal | == |
larger or equal | >= |
sma |
a = [24,16,54] b = [] if a[0]< a[1] and a[0]< a[2] : b.append(a[0]) if a[1] < a[2]: b.append(a[1]) b.append(a[2]) else: b.append(a[2]) b.append(a[1]) # This piece of code is not done yet. # Please complete it !!!
7.循环语句:
循环语句有很多不同的样式代码,我们举例说明两种,for循环和while循环
for...in ... :
statement A
是循环中最常用的语句,通常与range(start,end,step)一起使用,start为起始值,end为结束值,step为步长。
while ...:
statement A
将会执行A语句,直到满足while的条件。
例子:
for i in range(2, 10, 3): print(i) l= i**2 print(l) a = 0 sumup = O while a < 100 : a + 1 sumup += a print(sumup)
break和continue
for i in range(300, 351): if i % 17 == O: print(i) break else : continue
循环嵌套:
for i in range(10): print(i) for j in range(5): print(j)
8.方法(函数)的定义
def TheNameOfFunction(paral, para2): ... return Outcome
举例:求两个变量最大值的函数
def MaxOfTwo (x1, x2): if x1 >= x2: return x1 else: return x2 a = l b = 2 c = MaxOfTwo(a, b) print(c)
9.读取/写入文件
file_object = open(filename, mode)
这个mode参数可以是(The mode can be)
例子:
写入
file = open('newfile.txt', 'w') file.write('I am created for the course. \n') file.write('How about you? ') file.write('How is your exam?') file.close()
读取
file = open('newfile.txt', 'r') #show whole efile print(file.read()) #show first ten characterrs print(file.read(10)) #view by line print(file.readline()) #view all by line print(file.readlines()) file.close()
循环读取
file = open('newfile.txt', 'r') for line in file: print (line) file.close()
文件添加内容
file = open('newfile.txt', 'a') file.write('\nI am back again. \n') file.write('Do you miss me?\n') file.close()
with open("humpty.txt") as f:
...
第二天内容请进传送门>>>三天搞定python基础-day2