前言
Python现在非常火,语法简单而且功能强大,很多同学都想学Python!所以小的给各位看官们准备了高价值Python学习视频教程及相关电子版书籍,欢迎前来领取!
本来不应该把这个章节放在那面前面的,因为还没进行学习之前,直接看这个章节,会感觉有很多莫名其妙的东西。
但是把这个章节放在前面的用意,只是让大家预览一下,有个印象,而且在以后的学习中,也方便大家查阅。

#-*-coding:utf-8-*-标识每行代码尽量不超过 80 个字符(在特殊情况下可以略微超过 80 ,但最长不得超过 120)
理由:
简单说,自然语言使用双引号,机器标示使用单引号,因此 代码里 多数应该使用 单引号
"..."
例如错误信息;很多情况还是 unicode,使用u"你好世界"'...' 例如 dict 里的 keyr"...""""......"""class A:
def __init__(self):
pass
def hello(self):
pass
def main():
pass #-*-conding:utf-8-*-标识# 正确的写法
import os
import sys
# 不推荐的写法
import sys,os
# 正确的写法
from subprocess import Popen, PIPE# 正确的写法
from foo.bar import Bar
# 不推荐的写法
from ..bar import Barimport os
import sys
import msgpack
import zmq
import foofrom myclass import MyClassimport bar
import foo.bar
bar.Bar()
foo.bar.Bar()[=,-,+=,==,>,in,is not, and]:# 正确的写法
i = i + 1
submitted += 1
x = x * 2 - 1
hypot2 = x * x + y * y
c = (a + b) * (a - b)
# 不推荐的写法
i=i+1
submitted +=1
x = x*2 - 1
hypot2 = x*x + y*y
c = (a+b) * (a-b),之后要有空格# 正确的写法
def complex(real, imag):
pass
# 不推荐的写法
def complex(real,imag):
pass# 正确的写法
def complex(real, imag=0.0):
pass
# 不推荐的写法
def complex(real, imag = 0.0):
pass# 正确的写法
spam(ham[1], {eggs: 2})
# 不推荐的写法
spam( ham[1], { eggs : 2 } )# 正确的写法
dict['key'] = list[index]
# 不推荐的写法
dict ['key'] = list [index]# 正确的写法
x = 1
y = 2
long_variable = 3
# 不推荐的写法
x = 1
y = 2
long_variable = 3Python 支持括号内的换行。这时有两种情况。
foo = long_function_name(var_one, var_two,
var_three, var_four)def long_function_name(
var_one, var_two, var_three,
var_four):
print(var_one)使用反斜杠\换行,二元运算符+ .等应出现在行末;长字符串也可以用此法换行
session.query(MyTable).\
filter_by(id=1).\
one()
print 'Hello, '\
'%s %s!' %\
('Harry', 'Potter')禁止复合语句,即一行中包含多个语句:
# 正确的写法
do_first()
do_second()
do_third()
# 不推荐的写法
do_first();do_second();do_third();if/for/while一定要换行:
# 正确的写法
if foo == 'blah':
do_blah_thing()
# 不推荐的写法
if foo == 'blah': do_blash_thing()docstring 的规范中最其本的两点:
"""Return a foobar
Optional plotz says to frobnicate the bizbaz first.
"""
"""Oneline docstring"""