1、python语言的学习流程:python
一、基础知识:web
学习每一种新的编程语言都是从最基本的开始,对于python而言也是须要先学习其基础知识。正则表达式
python的基础知识包括:变量和数据类型,List和Tuple,条件判断和循环,Dict和Set, 函数,切片,迭代和列表生成式。算法
注意:学习基础知识切莫着急,必定要打好基础,这样才会更好的应用python数据库
二、进阶知识:编程
学完掌握基础知识以后,就要学习进阶知识了。安全
python的进阶知识包括:函数式编程,模块,面向对象编程基础,类的继承和定制类数据结构
三、python装饰器:闭包
装饰器是很重要的一个知识点。app
关于装饰器须要涉及到函数做用域.闭包的使用和装饰器的概念及使用
四、高阶知识:
文件处理,错误和异常和正则表达式
五、提高阶段:
数据库操做,Django框架和爬虫技术
2、Python初学的17个学习小技巧
一、交换变量
有时候,当咱们要交换两个变量的值时,一种常规的方法是建立一个临时变量,而后用它来进行交换。例:
# 输入
a = 5
b = 10
#建立临时变量
temp = a
a = b
b = temp
print(a)
print(b)
但在Python中,其实咱们有一种更简洁的写法:
二、if 语句在行内
print "Hello" if True else "World"
>>> Hello
三、链接
下面的最后一种方式在绑定两个不一样类型的对象时显得很酷。
nfc = ["Packers", "49ers"]
afc = ["Ravens", "Patriots"]
print nfc + afc
>>> [''Packers'', ''49ers'', ''Ravens'', ''Patriots'']
print str(1) + " world"
>>> 1 world
print `1` + " world"
>>> 1 world
print 1, "world"
>>> 1 world
print nfc, 1
>>> [''Packers'', ''49ers''] 1
四、计算技巧
#向下取整
print 5.0//2
>>> 2
# 2的5次方
print 2**5
>> 32
注意浮点数的除法
print .3/.1
>>> 2.9999999999999996
print .3//.1
>>> 2.0
五、数值比较
x = 2
if 3 > x > 1:
print x
>>> 2
if 1 < x > 0:
print x
>>> 2
六、两个列表同时迭代
nfc = ["Packers", "49ers"]
afc = ["Ravens", "Patriots"]
for teama, teamb in zip(nfc, afc):
print teama + " vs. " + teamb
>>> Packers vs. Ravens
>>> 49ers vs. Patriots
七、带索引的列表迭代
teams = ["Packers", "49ers", "Ravens", "Patriots"]
for index, team in enumerate(teams):
print index, team
>>> 0 Packers
>>> 1 49ers
>>> 2 Ravens
>>> 3 Patriots
八、列表推导
已知一个列表,刷选出偶数列表方法:
numbers = [1,2,3,4,5,6]
even = []
for number in numbers:
if number%2 == 0:
even.append(number)
九、用下面的代替
numbers = [1,2,3,4,5,6]
even = [number for number in numbers if number%2 == 0]
十、字典推导
teams = ["Packers", "49ers", "Ravens", "Patriots"]
print {key: value for value, key in enumerate(teams)}
>>> {''49ers'': 1, ''Ravens'': 2, ''Patriots'': 3, ''Packers'': 0}
十一、初始化列表的值
items = [0]*3
print items
>>> [0,0,0]
十二、将列表转换成字符串
teams = ["Packers", "49ers", "Ravens", "Patriots"]
print ", ".join(teams)
>>> ''Packers, 49ers, Ravens, Patriots''
1三、从字典中获取元素
不要用下列的方式
data = {''user'': 1, ''name'': ''Max'', ''three'': 4}
try:
is_admin = data[''admin'']
except KeyError:
is_admin = False
替换为
data = {''user'': 1, ''name'': ''Max'', ''three'': 4}
is_admin = data.get(''admin'', False)
1四、获取子列表
x = [1,2,3,4,5,6]
#前3个
print x[:3]
>>> [1,2,3]
#中间4个
print x[1:5]
>>> [2,3,4,5]
#最后3个
print x[-3:]
>>> [4,5,6]
#奇数项
print x[::2]
>>> [1,3,5]
#偶数项
print x[1::2]
>>> [2,4,6]
1五、60个字符解决FizzBuzz
前段时间Jeff Atwood 推广了一个简单的编程练习叫FizzBuzz,问题引用以下:
写一个程序,打印数字1到100,3的倍数打印“Fizz”来替换这个数,5的倍数打印“Buzz”,对于既是3的倍数又是5的倍数的数字打印“FizzBuzz”。
这里有一个简短的方法解决这个问题:
for x in range(101):print"fizz"[x%34::]+"buzz"[x%54::]or x
1六、集合
用到Counter库
from collections import Counter
print Counter("hello")
>>> Counter({''l'': 2, ''h'': 1, ''e'': 1, ''o'': 1})
1七、迭代工具
和collections库同样,还有一个库叫itertools
from itertools import combinations
teams = ["Packers", "49ers", "Ravens", "Patriots"]
for game in combinations(teams, 2):
print game
>>> (''Packers'', ''49ers'')
>>> (''Packers'', ''Ravens'')
>>> (''Packers'', ''Patriots'')
>>> (''49ers'', ''Ravens'')
>>> (''49ers'', ''Patriots'')
>>> (''Ravens'', ''Patriots'')
在python中,True和False是全局变量,所以:
False = True
if False:
print "Hello"
else:
print "World"
>>> Hello
3、实用的基础学习:
若是要在python中写中文,则要在xx.py的最前面声明
1
|
#coding:utf-8
|
一、基础语法:变量,字符串,函数,逻辑判断,循环
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
|
varline
=
2
;
print
(varline);
#打印字符串
print
(
"hello Python"
);
print
(
"你好,Python"
);
#整型和字符串的转化
num1
=
100
;
num2
=
"100"
;
num3
=
num1
+
int
(num2);
print
(num3);
#字符串操做
str1
=
"hello world"
;
str2
=
str1
*
3
;
string_count
=
len
(str1);
print
(string_count);
print
(str2);
#字符串索引等价
print
(str1[
0
]);
print
(str1[
-
11
])
#===>h
print
(str1[
1
]);
print
(str1[
-
10
])
#===>e
print
(str1[
2
]);
print
(str1[
-
9
])
#===>l
#能够将字符串进行分割
print
(str1[
0
:
5
]);
print
(str1[
6
:
11
]);
#===> hello world
print
(str1[
-
4
:]);
#函数的定义和使用
def
Print
():
print
(
"hello world"
);
return
"sss"
;
sss
=
Print
();
print
(sss);
def
add(arg1 , arg2):
return
arg1
+
arg2 ;
print
(add(
1
,
2
));
def
getTempatuare(temp):
return
temp
*
9
/
5
+
32
;
print
(
str
(getTempatuare(
35
))
+
"'F"
);
#克转千克算法
def
print_kg(g):
return
float
(g
/
1000
) ;
print
(
str
(print_kg(
1
))
+
"kg"
);
#求直角三角形斜边的长度
def
Line_print(arg1,arg2):
return
((arg1
*
arg1
+
arg2
*
arg2))
*
*
0.5
print
(
"The right triangle third side's length is "
+
str
(Line_print(
3
,
4
)));
#str_rp = str1.replace(str1[:3],'*'*9);
#print(str_rp)
str11
=
"{} a word she can get what she {} for."
str12
=
"{preposition} a word she can get what she {verb} for"
str13
=
"{0} a word she can get what she {1} for."
str111
=
str11.
format
(
'With'
,
'came'
);
str121
=
str12.
format
(preposition
=
'With'
,verb
=
'came'
)
str131
=
str13.
format
(
'With'
,
'came'
)
print
(str111)
print
(str121)
print
(str131)
#单首创建
file1
=
open
(
'F:\\'+'
hello.txt
','
w')
file1.write(
"Hello world"
);
file1.close()
#使用函数建立
def
text_create(name, msg):
desktop_path
=
'F:\\'
full_path
=
desktop_path
+
name
+
'.txt'
file
=
open
(full_path,
'w'
)
file
.write(msg)
file
.close()
print
(
'Done'
)
text_create(
'Yang'
,
'hello world'
)
# ????
#变量的比较
teststr1
=
"Hello"
teststr2
=
"World"
teststr3
=
"Hello"
print
(teststr1
in
teststr2)
print
(teststr1
is
teststr3)
print
(
bool
(teststr1))
print
(
bool
(''))
print
(
not
teststr1)
print
(teststr1 < teststr3
and
teststr2 > teststr1)
print
(teststr1 > teststr2
or
teststr3 < teststr1)
#python逻辑判断学习
a
=
1
b
=
3
if
a < b :
a
=
3
b
=
2
else
:
a
=
2
b
=
3
print
(a,b);
if
a < b:
a
=
3
b
=
2
elif
a > b:
a
=
2
b
=
3
else
:
a
=
100
b
=
200
print
(a,b)
for
i
in
1
,
2
,
3
,
4
,
5
,
6
:
print
(i)
for
string_str
in
"hello"
,
"world"
,
"world"
:
print
(string_str)
for
str1111
in
"Hello"
:
print
(str1111)
|
二、Python数据结构:列表,元组,字典,集合
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
#python列表===>
#特色:能够装python的全部类型,包括元组,列表,字典等
city
=
[
'广东'
,
'云南'
,
'广西'
,
'江西'
,
'HongKong'
,
'Shenzhen'
,
123456
]
for
i
in
0
,
1
,
2
,
3
,
4
,
5
,
6
:
print
(city[i])
city.insert(
1
,
'北京'
)
#列表的插入
for
i
in
0
,
1
,
2
,
3
,
4
,
5
,
6
:
print
(city[i])
city.remove(
'HongKong'
)
#列表的删除
for
i
in
0
,
1
,
2
,
3
,
4
,
5
,
6
:
print
(city[i])
del
city[
0
]
#使用del方法删除列表中的元素
for
i
in
0
,
1
,
2
,
3
,
4
,
5
:
print
(city[i])
#python元组 ===>
#特色:不可修改,可被查看以及索引
num
=
(
'1'
,
'2'
,
'3'
,
'4'
,
'5'
)
for
i
in
0
,
1
,
2
,
3
,
4
:
print
(num[i])
#python字典 ===>
#特色:键值成对存在,键不可重复,值可重复,键不可改,值能够变,能够为任何对象
Dog
=
{
'name'
:
'sundy'
,
'age'
:
18
}
Dog.update({
'tel'
:
119
})
#往字典中添加键值对
print
(Dog)
del
Dog[
'name'
]
#往字典中删除键值对
print
(Dog)
#集合
num_set
=
{
1
,
2
,
3
,
4
,
1
,
5
}
num_set.add(
6
)
#往集合里添加元素
print
(num_set)
num_set.discard(
3
)
#从集合里删除元素
print
(num_set)
|
三、Python语言面对对象:类的定义、使用以及类的继承
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
#coding:utf-8
#定义一个类
class
Anmial:
var
=
100
Dog
=
[
'runing'
,
'eat'
,
'sleep'
]
#Dog是这个类的属性
def
function(
self
):
#类里的方法
if
Anmial.var
=
=
10
:
print
(Anmial.var)
else
:
print
(
self
+
str
(Anmial.Dog))
return
Anmial.var
#实例化类
Dog1
=
Anmial()
print
(Anmial.Dog)
#遍历类中的成员
for
i
in
Anmial.Dog:
print
(i)
#建立实例属性===>相似建立一个与Dog同样的属性
Anmial.log
=
'会飞'
,
'Hello'
,
'Monkey'
print
(Anmial.log)
Anmial.function(
"属性:"
)
class
CocaCola():
formula
=
[
'caffeine'
,
'suger'
,
'water'
,
'soda'
]
def
__init__(
self
,local_name):
#===>self至关于能够用来访问类中的成员或者建立属性
self
.logo_local
=
'橙汁'
if
local_name
=
=
'可乐'
:
print
(local_name)
elif
local_name
=
=
'橙汁'
:
print
(local_name)
else
:
print
(
'西瓜汁'
)
def
drink(
self
):
#===>调用该方法的时候等效于 coke = CocaCola.drink(coke)
print
(
'Energy!'
)
coke
=
CocaCola(
'可乐'
)
coke1
=
CocaCola(
'橙汁'
)
coke2
=
CocaCola(
'梨汁'
)
#类的继承===>xuebi至关于CocaCoal的子类,CocaCoal至关于父类
class
xuebi(CocaCola):
formula
=
[
'白色'
,
'黄色'
,
'绿色'
]
xuebi
=
xuebi(CocaCola)
#将CocaCola放在括号中,表面xuebi集成于CocalCola
print
(xuebi.formula)
xuebi.drink()
#这样子类就能够调用父类的方法,继续延用了
|
4、总结:
Python是一种面向对象的解释型计算机程序的设计语言, Python具备丰富和强大的库。它常被称为胶水语言,可以把其余语言制做的各类模块很轻松地结合在一块儿。
相对于Java、C语言等,Python简单易学,更适合没有编程基础的小白入门。Python 的语言没有多少仪式化的东西,因此就算不是一个 Python 专家,你也能读懂它的代码。
Python的发展方向:数据分析、人工智能、web开发、测试、运维、web安全、游戏制做等等