給我十分鐘!我就能帶你入門Python!超級詳細的基礎教程!

給我十分鐘!我就能帶你入門Python!超級詳細的基礎教程!

Python

>>> help(5)

Help on int object:

(etc etc)

>>> dir(5)

['__abs__', '__add__', ...]

>>> abs.__doc__

'abs(number) -> number

Return the absolute value of the argument.'

語法

給我十分鐘!我就能帶你入門Python!超級詳細的基礎教程!

數據類型

Python具有列表(list)、元組(tuple)和字典(dictionaries)三種基本的數據結構,而集合(sets)則包含在集合庫中(但從Python2.5版本開始正式成為Python內建類型)。列表的特點跟一維數組類似(當然你也可以創建類似多維數組的“列表的列表”),字典則是具有關聯關係的數組(通常也叫做哈希表),而元組則是不可變的一維數組(Python中“數組”可以包含任何類型的元素,這樣你就可以使用混合元素,例如整數、字符串或是嵌套包含列表、字典或元組)。數組中第一個元素索引值(下標)為0,使用負數索引值能夠從後向前訪問數組元素,-1表示最後一個元素。數組元素還能指向函數。來看下面的用法:

給我十分鐘!我就能帶你入門Python!超級詳細的基礎教程!

給我十分鐘!我就能帶你入門Python!超級詳細的基礎教程!

字符串

Python中的字符串使用單引號(‘)或是雙引號(“)來進行標示,並且你還能夠在通過某一種標示的字符串中使用另外一種標示符(例如 “He said ‘hello’.”)。而多行字符串可以通過三個連續的單引號(”’)或是雙引號(“””)來進行標示。Python可以通過u”This is a unicode string”這樣的語法使用Unicode字符串。如果想通過變量來填充字符串,那麼可以使用取模運算符(%)和一個元組。使用方式是在目標字符串中從左至右使用%s來指代變量的位置,或者使用字典來代替,示例如下:

給我十分鐘!我就能帶你入門Python!超級詳細的基礎教程!

流程控制

Python

給我十分鐘!我就能帶你入門Python!超級詳細的基礎教程!

函數

給我十分鐘!我就能帶你入門Python!超級詳細的基礎教程!

Python

class MyClass(object):

common = 10

def __init__(self):

self.myvariable = 3

def myfunction(self, arg1, arg2):

return self.myvariable

# This is the class instantiation

>>> classinstance = MyClass()

>>> classinstance.myfunction(1, 2)

3

# This variable is shared by all classes.

>>> classinstance2 = MyClass()

>>> classinstance.common

10

>>> classinstance2.common

10

# Note how we use the class name

# instead of the instance.

>>> MyClass.common = 30

>>> classinstance.common

30

>>> classinstance2.common

30

# This will not update the variable on the class,

# instead it will bind a new object to the old

# variable name.

>>> classinstance.common = 10

>>> classinstance.common

10

>>> classinstance2.common

30

>>> MyClass.common = 50

# This has not changed, because "common" is

# now an instance variable.

>>> classinstance.common

10

>>> classinstance2.common

50

# This class inherits from MyClass. The example

# class above inherits from "object", which makes

# it what's called a "new-style class".

# Multiple inheritance is declared as:

# class OtherClass(MyClass1, MyClass2, MyClassN)

class OtherClass(MyClass):

# The "self" argument is passed automatically

# and refers to the class instance, so you can set

# instance variables as above, but from inside the class.

def __init__(self, arg1):

self.myvariable = 3

print arg1

>>> classinstance = OtherClass("hello")

hello

>>> classinstance.myfunction(1, 2)

3

# This class doesn't have a .test member, but

# we can add one to the instance anyway. Note

# that this will only be a member of classinstance.

>>> classinstance.test = 10

>>> classinstance.test

10

給我十分鐘!我就能帶你入門Python!超級詳細的基礎教程!

導入

外部庫可以使用 import [libname] 關鍵字來導入。同時,你還可以用 from [libname] import [funcname] 來導入所需要的函數。例如:

Python

import random

from time import clock

randomint = random.randint(1, 100)

>>> print randomint

64

文件I / O

Python針對文件的處理有很多內建的函數庫可以調用。例如,這裡演示瞭如何序列化文件(使用pickle庫將數據結構轉換為字符串):

給我十分鐘!我就能帶你入門Python!超級詳細的基礎教程!

其它雜項

  • 數值判斷可以鏈接使用,例如 1
給我十分鐘!我就能帶你入門Python!超級詳細的基礎教程!

給我十分鐘!我就能帶你入門Python!超級詳細的基礎教程!

小結

本教程並未涵蓋Python語言的全部內容(甚至連一小部分都稱不上)。Python有非常多的庫以及很多的功能特點需要學習,所以要想學好Python你必須在此教程之外通過其它方式,例如閱讀Dive into Python。我希望這個教程能給你一個很好的入門指導。如果你覺得本文還有什麼地方值得改進或添加,或是你希望能夠了解Python的哪方面內容,請留言。

給我十分鐘!我就能帶你入門Python!超級詳細的基礎教程!


分享到:


相關文章: