一个计算机技术爱好者与学习者

0%

好好学Python:Python基础

1. Python简介

Bruce Eckel: Life is short, you need Python.

Python,是 Bruce Eckel 在1989年为了打发无聊的圣诞节而编写的一门编程语言,特点是优雅、明确、简单,现今拥有丰富的标准库和第三方库。

Python适合开发Web网站和各种网络服务,系统工具和脚本,作为“胶水”语言把其他语言开发的模块包装起来使用,科学计算等等。

本文中,我们就来学习一下Python的基础知识。

参考文档:

2. 准备工作

1、在Python官网下载安装喜欢的版本,郝同学使用的,是当前最新版本3.6.0。
2、打开IDLE,这是Python的集成开发环境,尽管简单,但极其有用。IDLE包括一个能够利用颜色突出显示语法的编辑器、一个调试工具、Python Shell,以及一个完整的Python3在线文档集。

3. hello world

3.1. 交互模式

1、打开IDLE

2、输入想要执行的语句

1
print('hello world')

PS:语句末尾加不加分号;都可以

3、执行语句
回车,即可执行语句。

3.2. 脚本模式

1、新建文件hello.py,内容为:

1
print('hello world')

2、执行脚本

1
python hello.py

3.3. 脚本模式-指定解释器

1、新建文件hello.py,内容为:

1
2
#!/usr/bin/env python
print('hello world')

2、添加执行权限

1
chmod a+x hello.py

3、执行脚本

1
./hello.py

4. 基础语法

常用函数(print)、数据类型、表达式、变量、条件和循环、函数。和其他语言类似,下面选择一部分展开。

5. list链表数组

1、定义数组
myList = ['Hello', 100, True]

2、输出数组
print(myList)

3、输出数组元素
print(myList[0])print(myList[-1])

4、追加元素到末尾
myList.append('voidking')

5、追加元素到头部
myList.insert(0,'voidking')

6、删除元素
myList.pop()myList.pop(0)

7、元素赋值
myList[0]='hello666'

6. tuple固定数组

1、定义数组
myTuple = ('Hello', 100, True)
错误定义:myTuple1=(1),正确定义:myTuple=(1,)

2、输出数组
print(myTuple)

3、输出数组元素
print(myTuple[0])

4、tuple和list结合
t = ('a', 'b', ['A', 'B'])t[2][0]='X'

7. if语句

7.1. if

1
2
3
score = 75
if score>=60:
print 'passed'

7.2. if-else

1
2
3
4
if score>=60:
print('passed')
else:
print('failed')

7.3. if-elif-else

1
2
3
4
5
6
7
8
if score>=90:
print('excellent')
elif score>=80:
print('good')
elif score>=60:
print('passed')
else:
print('failed')

8. 循环

8.1. for循环

1
2
3
4
5
L = [75, 92, 59, 68]
sum = 0.0
for score in L:
sum += score
print(sum / 4)

8.2. while循环

1
2
3
4
5
6
sum = 0
x = 1
while x<100:
sum += x
x = x + 1
print(sum)

8.3. break

1
2
3
4
5
6
7
8
sum = 0
x = 1
while True:
sum = sum + x
x = x + 1
if x > 100:
break
print(sum)

8.4. continue

1
2
3
4
5
6
7
8
9
L = [75, 98, 59, 81, 66, 43, 69, 85]
sum = 0.0
n = 0
for x in L:
if x < 60:
continue
sum = sum + x
n = n + 1
print(sum/n)

8.5. 多重循环

1
2
3
for x in ['A', 'B', 'C']:
for y in ['1', '2', '3']:
print(x + y)

9. dict

dict的作用是建立一组 key和一组value的映射关系。

1
2
3
4
5
6
7
8
9
10
11
12
13
d = {
'Adam': 95,
'Lisa': 85,
'Bart': 59,
'Paul': 75
}
print(d)
print(d['Adam'])
print(d.get('Lisa'))
d['voidking']=100
print(d)
for key in d:
print(key+':',d.get(key))

10. set

set持有一系列元素,这一点和list很像,但是set的元素没有重复,而且是无序的,这点和dict的key很像。

1
2
3
4
5
6
7
8
9
s = set(['Adam', 'Lisa', 'Bart', 'Paul'])
print(s)
s = set(['Adam', 'Lisa', 'Bart', 'Paul', 'Paul'])
print(s)
len(s)
print('Adam' in s)
print('adam' in s)
for name in s:
print(name)
1
2
3
s = set([('Adam', 95), ('Lisa', 85), ('Bart', 59)])
for x in s:
print(x[0]+':',x[1])
1
2
3
4
s.add(100)
print(s)
s.remove(('Adam',95))
print(s)

11. 函数

11.1. 自带函数

1
2
L = [x*x for x in range(1,101)]
print(sum(L))

11.2. 自定义函数

1
2
3
4
5
6
def my_abs(x):
if x >= 0:
return x
else:
return -x
print(my_abs(-100))

11.3. 引入函数库

1
2
3
4
5
6
7
8
9
10
11
12
import math

def quadratic_equation(a, b, c):
x = b * b - 4 * a * c
if x < 0:
return none
elif x == 0:
return -b / (2 *a)
else:
return ((math.sqrt(x) - b ) / (2 * a)) , ((-math.sqrt(x) - b ) / (2 * a))
print(quadratic_equation(2, 3, 0))
print(quadratic_equation(1, -6, 5))

11.4. 可变参数

1
2
3
4
5
6
7
8
9
def average(*args):
if args:
return sum(args)*1.0/len(args)
else:
return 0.0

print(average())
print(average(1, 2))
print(average(1, 2, 2, 3, 4))

12. 切片

12.1. list切片

1
2
3
4
5
6
L = ['Adam', 'Lisa', 'Bart', 'Paul']
print(L[0:3])
print(L[:3])
print(L[1:3])
print(L[:])
print(L[::2])

12.2. 倒序切片

1
2
3
print(L[-2:])
print(L[-3:-1])
print(L[-4:-1:2])
1
2
3
L = range(1, 101)
print(L[-10:])
print(L[4::5][-10:])

PS:range是有序的list,默认以函数形式表示,执行range函数,即可以list形式表示。

12.3. 字符串切片

1
2
3
4
def firstCharUpper(s):
return s[0:1].upper() + s[1:]

print(firstCharUpper('hello'))

13. 迭代

Python的for循环不仅可以用在list或tuple上,还可以作用在其他任何可迭代对象上。
迭代操作就是对于一个集合,无论该集合是有序还是无序,我们用for循环总是可以依次取出集合的每一个元素。
集合是指包含一组元素的数据结构,包括:

  • 有序集合:list,tuple,str和unicode;
  • 无序集合:set
  • 无序集合并且具有key-value对:dict
1
2
3
for i in range(1,101):
if i%7 == 0:
print(i)

13.1. 索引迭代

对于有序集合,元素是有索引的,如果我们想在for循环中拿到索引,怎么办?方法是使用enumerate()函数。

1
2
3
4
5
6
7
L = ['Adam', 'Lisa', 'Bart', 'Paul']
for index, name in enumerate(L):
print(index+1, '-', name)

myList = zip([100,20,30,40],L);
for index, name in myList:
print(index, '-', name)

13.2. 迭代dict的value

1
2
3
4
d = { 'Adam': 95, 'Lisa': 85, 'Bart': 59 }
print(d.values())
for v in d.values():
print(v)

PS:Python3.x中,dict的方法dict.keys(),dict.items(),dict.values()不会再返回列表,而是返回一个易读的“views”。这样一来,k = d.keys();k.sort()不再有用,可以使用k = sorted(d)来代替。
同时,dict.iterkeys(),dict.iteritems(),dict.itervalues()方法不再支持。

13.3. 迭代dict的key和value

1
2
3
d = { 'Adam': 95, 'Lisa': 85, 'Bart': 59 }
for key, value in d.items():
print(key, ':', value)

14. 列表生成

14.1. 一般表达式

1
2
L = [x*(x+1) for x in range(1,100)]
print(L)

14.2. 复杂表达式

1
2
3
4
5
6
7
8
9
10
11
12
d = { 'Adam': 95, 'Lisa': 85, 'Bart': 59 }
def generate_tr(name, score):
if score >=60:
return '<tr><td>%s</td><td>%s</td></tr>' % (name, score)
else:
return '<tr><td>%s</td><td style="color:red">%s</td></tr>' % (name, score)

tds = [generate_tr(name,score) for name, score in d.items()]
print('<table border="1">')
print('<tr><th>Name</th><th>Score</th><tr>')
print('\n'.join(tds))
print('</table>')

14.3. 条件表达式

1
2
L = [x * x for x in range(1, 11) if x % 2 == 0]
print(L)
1
2
3
4
def toUppers(L):
return [x.upper() for x in L if isinstance(x,str)]

print(toUppers(['Hello', 'world', 101]))

14.4. 多层表达式

1
2
L = [m + n for m in 'ABC' for n in '123']
print(L)
1
2
L = [a*100+b*10+c for a in range(1,10) for b in range(0,10) for c in range(1,10) if a==c]
print(L)
  • 本文作者: 好好学习的郝
  • 原文链接: https://www.voidking.com/dev-python-start/
  • 版权声明: 本文采用 BY-NC-SA 许可协议,转载请注明出处!源站会即时更新知识点并修正错误,欢迎访问~
  • 微信公众号同步更新,欢迎关注~