Python程序员半栈工程师

《三天搞定Python基础概念之第一天》中文版

2017-09-05  本文已影响1040人  MurphyWan
前言:
首先,非常感谢Jiang老师将其分享出来!本课件非常经典!
经过笔者亲测,竟然确实只要三天,便可管中窥豹洞见Python及主要库的应用。实属难得诚意之作!
其次,只是鉴于Jiang老师提供的原始课件用英文写成,而我作为Python的爱好者计算机英文又不太熟练,讲义看起来比较慢,为了提高自学课件的效率,故我花了点时间将其翻译成中文,以便将来自己快速复习用。
该版仅用于个人学习之用。
再次,译者因工作中需要用到数据分析、风险可视化与管理,因此学习python,翻译水平有限,请谅解。
在征得原作者Yupeng Jiang老师的同意后,现在我将中文版本分享给大家。

作者:Dr.Yupeng Jiang

翻译:Murphy Wan

大纲( Outline)

------------------------------以下为英文原文-------------------------------------

第一天的主要内容

为什么使用Python ( Why Python?)

使用Python 2或3?

一些在线资源 (Some online resources)

Python环境 (Python environment)

Anaconda中包括的库(Libararies)

管理你的Anaconda

你可以在本课程中学到什么?

今天的安排

练习1

缩进(Indentation)

编程中的一些好习惯(Some good habit in programming)

关于Python的基础知识(Basic knowlegde on Python)

常用的数字数据类型(Common used numeric data types)

Name Notation Declaration e.g.
Integers int a = 10
Floating float b = 3.14
Complex complex c = 1 + 2j
String str d = 'Python'

算数运算符(Arithmetic operators)

Name Notation Examples
Addition + a + b
Subtraction - c - b
Multiplication * x*y
Division / x/z
Modulus % x%a
Exponent ** a**x

练习2(Exercise 2)

浮点数的精度(Precision of float)

例子(Example)

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.

练习3:字符串str的一些功能(Exercise3:Some functions for 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.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    

Python功能:索引(Python features: Indexing)

基本的数据结构(Basic data structures)

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'])

列表的一些有用功能(Some useful functions for list)


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]    

Python功能:参考对象(Python features: Refer for object)

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]

多维列表(Multidimensional list)

a  =  [[1,2 , 3 ,4],[1,2 ,3,4],[1,2 ,3]] 
print(a)
print(a[0][3])

条件语句(Conditions)

Name Notation
larger >
smaller <
equal ==
larger or equal >=
sma

示例和练习:条件语句 (Example & exercise: conditions)

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 !!!

循环语句 (Loops)

     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的条件。

举例:循环语言(Example: loops)

# 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)

break和continue.

# search the first
# be divided by 17
for i in range(300, 351):
    if i % 17 == O:
        print (i)
        break
    else :
        cantinue

循环语句嵌套(Loop inside the loop)

for  i  in range (10):
     print (i)     
     for j in range (5):
         print (j)

练习(Exercises)


功能 (Functions)


功能(方法or函数)的声明 (Function declaration)

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(...)

读取/写入文件 (Reading/ writing files)


内建open()方法 [Built-in open() function]

file_object = open(filename, mode)

创建一个文件 (Creating new file)

   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()

读取一个文件 (Reading a file)


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()


循环读取一个文件 (Looping over a file object)


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?


增加一个文件 (Adding in a file)

file = open('newfile.txt', 'a')
file.write('\nI am back again. \n')
file.write('Do  you miss me?\n')
file.clase()

with语句 (The with statement)


with open(“humpty.txt”) as f:



文件路径 (File paths)

open ('/etc/gimp/2.O/gtkrc')
open('C:\Users\user\Documents\file.txt')


实验部分 (Lab Session)



目标1 (Target 1)

               [8, 2, 4, 6, 1, 9, 0, 3, 5, 7]

               [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]



目标1提示: (Hint for Target 1:)

-----以下为英文原文-----

There are three different algorithms of sorting:



目标2 (Target 2)

通过二等分法。



目标2的提示 (Hint for Target 2:)

-----以下为英文原文-----



目标3 (Target 3)




目标3提示: (Hint for Target 3:)

-----以下为英文原文-----


第一天的课程到此结束,辛苦了

上一篇下一篇

猜你喜欢

热点阅读