Only use 3 days to get the basic concept of Python (Chinese Version | Data Scientist Direction)
为【零基础】的人设计,带你串一遍Python的基本操作。若你已经get了Python的基础知识,请直接去第二天的内容 (传送门-->第二天)
前言:
首先,非常感谢Jiang老师将其分享出来!本课件非常经典!
经过笔者亲测,竟然确实只要三天,便可管中窥豹洞见Python及主要库的应用。实属难得诚意之作!
其次,只是鉴于Jiang老师提供的原始课件用英文写成,而我作为Python的爱好者计算机英文又不太熟练,讲义看起来比较慢。
为了提高自学课件的效率,故我花了点时间将其翻译成中文,以便将来自己快速复习用。
该版仅用于个人学习之用。
再次,译者因工作中需要用到数据分析、风险可视化与管理,因此学习python,翻译水平有限,请谅解。
在征得原作者Yupeng Jiang老师的同意后,现在我将中文版本分享给大家。
------------------------------以下为英文原文-------------------------------------
为【零基础】的人设计,带你串一遍Python的基本操作。若你已经get了Python的基础知识,请直接去第二天的内容 (传送门-->第二天)
Python的背景
Python基础知识
控制结构
功能
读/写文件
实验部分
为什么使用Python ( Why Python?)
安装Anaconda并更新它以便拥有 所有包的最新版本。 我们将使用Python 3.4(而不是以前的Python 2.7版本)。
通过键入以下命令启动Spyder并计算76:
x = 7 ** 6 打印(x)
启动IPython Notebook并计算7的6次方
Name | Notation | Declaration e.g. |
---|---|---|
Integers | int | a = 10 |
Floating | float | b = 3.14 |
Complex | complex | c = 1 + 2j |
String | str | d = 'Python' |
Name | Notation | Examples |
---|---|---|
Addition | + | a + b |
Subtraction | - | c - b |
Multiplication | * | x*y |
Division | / | x/z |
Modulus | % | x%a |
Exponent | ** | a**x |
import decimal
# import the libray "decimal"
# display 2 decimal precision
print (round (3*1415 , 2)) # result 3. 14
print (round (9 .995 , 2)) # result 9. 99
#call function "Decimal " from lib "decimal"
print (decimal.Decimal (9.995))
The last "print" returns
9.9949999999999992184029906638897955417633056640625,
which is exactly how 9.995 is stored in the hardware.
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.find('in')) # index of 'i' in 'in'
print(t.find('Python')) # find sth not in
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
He is a string. who are you?
['He', 'is', 'a', 'string.', 'Who', 'are', 'you?']
11
-1
He i
He|is|a|string.|Who|are|you?
www.google.com
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']) |
l = [1, 2, 3.14, 'data'] #list
print (type(l))l.append ([4, 3])
print(l)l.extend (['delta' ,5 ,6] ) #add a list
print(l)l.insert(3, 'beta') #insert before index 3
print(l)l.remove ('data') #delete an elementprint(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]
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])
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 !!!
for...in ... :statement A
是循环中最常用的语句,通常与range(start,end,step)一起使用,start为起始值,end为结束值,step为步长。 例如,
range(0,8,1) 给出[0,1,2,3,4,5,6,7]
2/
while ...:statement A
将会执行A语句,直到满足while的条件。
# for和range的例子 example of for and range
# 初始值默认值为default start of range is O
# 步长默认值为default step of range is 1
for i in range(2, 10, 3):
print(i)
l= i**2
print(l)
# white to sum up 1 to 100
a = 0
sumup = O
while a < 100 :
a + 1
sumup += a
print ( sumup)
# search the first
# be divided by 17
for i in range(300, 351):
if i % 17 == O:
print (i)
break
else :
cantinue
for i in range (10):
print (i)
for j in range (5):
print (j)
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)
def MaxOfTwo(xl, x2 = 1): ...
def f (x1, x2, x3, ...):
......
return(y1, y2, y3, ...)
a1,b1,c1 = f(...)
file_object = open(filename, mode)
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()
输出将是
我是为这个课程而诞生的
你怎么样?你的考试如何
* ------以下是英文原文--------------------
Output would be:
I am created for the course
How about you? How is your exam?
file = open('newfile.txt', 'a')
file.write('\nI am back again. \n')
file.write('Do you miss me?\n')
file.clase()
with open(“humpty.txt”) as f:
open ('/etc/gimp/2.O/gtkrc')
open('C:\Users\user\Documents\file.txt')
您的老板要求您编写一段代码,以便对于具有float或int类型元素的任何长度列表(list),您的函数可以将它们按照从大到小排序。
例如 如果你的老板给一个list
[8, 2, 4, 6, 1, 9, 0, 3, 5, 7]
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
有三种不同的排序算法:
-----以下为英文原文-----
There are three different algorithms of sorting:
代码功能
使用派生词的定义,对其数字导数函数进行编码。
找到数值解
通过二等分法。
通过Newton-Raphson方法再次解决,该方法是“根搜索”的另一种数值方法。 它是由...给出的
衍生物可以近似于
-----以下为英文原文-----
The derivative can be appraximated by
Bisection is implemented as:
斐波那契序列如下
其中一个方法是递归地使用一个函数。
其中一个方法是利用append()函数。
-----以下为英文原文-----
Fibonacci sequence is given like
ao = 1, a1 = 1,
an = an-1 + an-2 for n> 2
One of the method is to recursively use a function.* One of the method is to take advantage from append() function.
第一天的课程到此结束,辛苦了