大家一起學python
環境安裝
Debian版本的安装命令如下:
su – aptitude install ipython python-setuptools
Fedora版本的安装命令如下:
su – yum install ipython python-setuptools-devel
Gentoo版本的安装命令如下:
su – emerge ipython
Ubuntu版本的安装命令如下:
sudo apt-get install ipython python-setuptools
從軟體源安裝
來源:https://github.com/ipython/ipython/downloads
解壓縮:tar xzf ipython-<version>.tar.gz
使用git:git clone https://github.com/ipython/ipython.git
進入ipython目錄:cd ipython
安裝指令:sudo setup.py install
宣告變數
my_int = 7
my_float = 1.23
my_bool = True
my_string = “Hello!”
Whitespace
def spam():
eggs = 12
return eggs
print spam()
Vim 縮排設定:vim ~/.vimrc
set sw=4
set ts=4
filetype indent on
autocmd FileType python setlocal et sta sw=4 sts=4
參考:
http://linux-wiki.cn/wiki/Vim%E4%BB%A3%E7%A0%81%E7%BC%A9%E8%BF%9B%E8%AE%BE%E7%BD%AE
Comments
#Hello
""""Hellowbyebye"""
String
my_string = “Hello”
String_len = len(my_string) // 5
my_string.lower() //”hello”
my_string. upper() //”HELLO”
str(10) // “10”
+
"Let's not go to %s. 'Tis a silly %s." % (str1, str2)
輸入
my_input = int(raw_input(“input message:"))
Conditionals
EX:
if answer > 5:
return 1
elif answer < 5:
return -1
else:
return 0
function
Def function_name (argument[, argument]):
#Do
Lists
EX:
numbers = [5, 6, 7, 8]
numbers[0] = 5
len(numbers) // 4
numbers[start: end: step]
numbers.append(9)
Dictionaries
EX:
d = {'key1' : 1, 'key2' : 2, 'key3' : 3}
d['key1' ] = 1
d['key4’ ] = 4
d.remove('key3' )
Loop
EX:
while condition:
#Do
else:
#
for variable in list_name: for i in range():
# Do #Do
Class
class CLASS_NAME(object):
def __init__(self, a1, a2, a3)
self.a1 = a1
self.a2 = a2
self.a3 = a3
My_class = CLASS_NAME(a1,a2,a3)

大家一起學Python