posted in Python 

Python 之禅

>>>
import this

PEP

PEP (Python Enhancement Proposal) 编写Python改进提案
PEP8 建议:

1. 缩进 4个空格
2. 行长 每行不超过80字符,注释行长不超过72字符(最初计算机终端窗口每行只能容纳79字符)

查看版本安装

#查看版本
python --version
python3 --version
#查看安装位置
type -a python
type -a python3
which python
whereis python

安装ipython

#ipython 语法高亮
pip3 install ipython
#安装完终端输入 ipython 

语法

>>>
## title, upper, lower, rstrip, str
print("hello world");
message = "xuzhihua Hello Python Crash Course reader!   "
print(message.title());
print(message.upper());
print(message.lower());
print(message.rstrip());
print(3*0.1);
message = "Happy " + str(23) + " xuzhihua"
print(message)


##array array[n], append, insert, del, pop, remove, sort, sorted
bicycle = ['xu', 'zhi', 'hua', 'altman']
print(bicycle[0])
print(bicycle[-1])
print(bicycle[0])
bicycle[1] = 'zhizhi'
print(bicycle)
bicycle.append('altman025')
print(bicycle)
bicycle.insert(2,'insertTest')
print(bicycle)
del bicycle[3]
print(bicycle)
bicycle_pop = bicycle.pop();
bicycle_pop_3 = bicycle.pop(3);
print(bicycle_pop)
print(bicycle_pop_3)
print(bicycle)
bicycle.remove('zhizhi')
print(bicycle)
bicycle = ['xu','zhi','hua','altman']
bicycle.sort()
print(bicycle)
bicycle = ['xu','zhi','hua','altman']
print(sorted(bicycle))
print(bicycle)
bicycle.reverse()
print(bicycle)
print(bicycle)
print(len(bicycle))


##forLoop for, range, list, min, max, sum, list[m:n], ()
magicians = ['alice', 'david', 'carolina']
for magician in magicians:
    print(magician + ", that was a great trick")
    print("I can't to see your next trick " + magician.title() + ".\n")
for value in range(1,6):
    print(value)
numbers = list(range(1,10))
print(numbers)
even_number = list(range(2,11,2))
print(even_number)

squares = []
for value in range(1,11):
    squares.append(value**2)
print(squares)
squares = [value**2 for value in range(11,15)]
print(squares)

digits = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
print(min(digits))
print(max(digits))
print(sum(digits))

players = ['charles', 'martina', 'michael', 'florence', 'eli']
print(players[0:3])
print(players[:4])
print(players[2:])
print(players[-3:])
#值赋值
players_copy = players[:]
#引用赋值
players_copy_ref = players
print(players_copy)
#元组,值不可变
dimensions = (200,500)
print(dimensions[0])
print(dimensions[1])
dimensions[0] = 100    # wrong 值不可变
dimensions = (400,400) # yes    直接赋值新元组