Quantcast
Viewing all articles
Browse latest Browse all 9596

Python小白的新手教程(一) -- 【网络课堂】

本文是 python 入门级别的基础知识,包括数据类型和变量、输入输出、字符串和编码、list tuple dict set 、条件判断、循环、函数、切片 迭代 列表生成器 生成器 迭代器等。

参考课程:

廖雪峰的官方网站 慕课网-python入门

参考书籍:

侯爵.《编程小白的第1本 Python 入门书》

前言

Python 是一种相当高级的语言,通过 Python 解释器把符合语法的程序代码转换成 CPU 能够执行的机器码,然后执行。Python 非常简洁,完成同一个任务,C 语言要写 1000 行代码,Java 只需要写 100 行,而 Python 可能只要 20 行,但是 Python 的运行速度稍慢。

安装 python 后可以通过文本编辑器来保存代码 .py,通过命令行模式来运行代码,而 Python 自带的 IDLE(交互模式)可以用来验证代码。也可以直接用 IDE 集成开发环境来编写和运行代码。

第一部分 Python基础 数据类型和变量、输入输出、字符串和编码、list tuple dict set 、条件判断、循环、函数、切片 迭代 列表生成器 生成器 迭代器

1. Python 用 # 来注释,用缩进表示语法。Python 区分大小写。

缩进按照约定俗成的习惯,坚持使用 4 个空格的缩进(把 tab 设置为 4 个空格)。

优点:强迫写出缩进较小的代码,把一段很长的代码拆分成若干函数。

缺点:“复制-粘贴”功能失效,需要在 IDLE 上重新检查缩进。

1. 数据类型、常变量、四种运算
Image may be NSFW.
Clik here to view.
Python小白的新手教程(一) -- 【网络课堂】

2. 字符串和编码
Image may be NSFW.
Clik here to view.
Python小白的新手教程(一) -- 【网络课堂】

3. 输入和输出 3.1 输出 print('hello,world') print('100+300=',100+300)  print('hello ,','world') print('hello,'+'world')  # 结果:hello,world  print('bang ! '*3) # 结果:bang ! bang ! bang ! print(type(2)) # 结果:<class 'int'>  search = '168' num_a = '138-6168-0006' num_b = '168-1222-0006' print(search + ' is at ' + str(num_a.find(search)) + ' to ' + str(num_a.find(search)+len(search)) + ' of num_a ') print(search + ' is at ' + str(num_b.find(search)) + ' to ' + str(num_b.find(search)+len(search)) + ' of num_b ') # 结果: 168 is at 5 to 8 of num_a 168 is at 0 to 3 of num_b

2. 输出多个字符串用逗号 "," 隔开,print() 依次打印每个字符串,遇到逗号 ","会输出一个空格。

3. 在 print() 函数中,"+" 在整数和浮点数中为运算符,在字符型中为连接符。字符串与整数、浮点数一起输出必须用 ",",不能用 "+" 。

3.2 输入 name=input('please enter your name') print('hello,',name)

4. 在 input() 函数中添加字符串可以提示用户输入内容。

3.3 格式化 3.3.1 用 % 格式化

5. %d 整数 %f 浮点数 %s 字符串 %x 十六进制整数。

print( 'Hello, %s' % 'world') print('Hi, %s, you have $%d.') % ('Michael', 1000000) print('%2d-%02d' % (3, 1))   # 结果:3-01 print( '%.2f' % 3.1415926) # 结果:3.14

6. 有几个 %? 占位符,后面就跟几个变量或者值,顺序要对应好。如果只有一个 %?,括号可以省略。

7. 格式化整数和浮点数可以指定是否补 0 和整数与小数的位数。

8. 如果不太确定应该用什么,%s 永远起作用,它会把任何数据类型转换为字符串。

9. 字符串里面的 % 是一个普通字符,用 %% 来表示一个 % 。

3.3.2 用 format 格式化 print('Hello, {}'.format('world')) print('Hi, {}, you have ${}.'.format ('Michael', 1000000)) 4. list 和 tuple 4.1 列表 list []

10.list 是一种有序的集合,可以随时添加和删除其中的元素。

classmates = ['Michael', 'Bob', 'Tracy'] print(classmates)  # 结果:['Michael', 'Bob', 'Tracy']

11.用 len() 函数可以获得 list 元素的个数。

print(len(classmates))  # 结果:3

12. 顺序索引、逆序索引。

print(classmates[0],classmates[1],classmates[2])  # 结果:Michael Bob Tracy print(classmates[-1], classmates[-2],classmates[-3])  # 结果:Tracy Bob Michael

13. 在 list 末尾添加元素、在 list 指定位置添加元素、添加多个元素。

classmates.append('Adam') print(classmates)  # 结果:['Michael', 'Bob', 'Tracy', 'Adam']   classmates.insert(1, 'Jack') print(classmates)  # 结果:['Michael', 'Jack', 'Bob', 'Tracy']   classmates = ['Michael', 'Bob', 'Tracy'] classmates.extend(['Adam', 'Jack']) print(classmates) # 结果:['Michael', 'Bob', 'Tracy', 'Adam', 'Jack']

14. 删除 list 末尾的元素、删除指定位置的元素。

classmates.pop() print(classmates)  # 结果:['Michael', 'Bob'] classmates.pop(1) print(classmates)  # 结果:['Michael', 'Tracy']  classmates = ['Michael', 'Bob', 'Tracy'] classmates.remove('Bob') # 用 remove 删除要指定元素 print(classmates) classmates = ['Michael', 'Bob', 'Tracy'] del classmates[2] print(classmates)

15. 更新某个元素。

classmates[1] = 'Sarah' print(classmates)  # 结果:['Michael', 'Sarah', 'Tracy']

16. list 中可以包含各种数据类型。

L = ['Apple', 123, True]

17.嵌套 list。

s = ['python', 'java', ['asp', 'php'], 'scheme'] print(len(s)) # 结果:4 print(s[2][0])  # 结果:asp 4.2 元组 tuple ()

18.tuple 初始化后不能修改,其它同 list ( len()、索引、可以是任何数据类型、嵌套)。

classmates = ('Michael', 'Bob', 'Tracy')

19.只有1个元素的 tuple 定义时必须加一个逗号 “,” ,来消除歧义(数学计算意义上的括号)。

t = (1,) print(t)  # 结果:(1,)

20. "可变的"tuple (嵌套) 。

t = ('a', 'b', ['A', 'B']) t[2][0] = 'X' t[2][1] = 'Y' print(t)  # 结果:('a', 'b', ['X', 'Y'])

21. tuple 所谓的"不变"是说 tuple 的每个元素指向永远不变,即指向 'a' ,不能改成指向 'b' ,指向一个 list ,不能改成指向其他对象,但指向的这个 list 本身是可变的。要创建一个内容也不变的 tuple ,就必须保证 tuple 的每一个元素本身也不能变。


Image may be NSFW.
Clik here to view.
Python小白的新手教程(一) -- 【网络课堂】

Image may be NSFW.
Clik here to view.
Python小白的新手教程(一) -- 【网络课堂】

5. dict 和 set 5.1 字典 dic {}

22.内置的字典,全称dictionary,在其他语言中称为map,使用键-值(key-value)存储,具有极快的查找速度。

d = {'Michael': 95, 'Bob': 75, 'Tracy': 85} print(d['Michael'])  # 结果:95

23.用 len() 函数可以获得 dict 元素的个数。

d = {'Michael': 95, 'Bob': 75, 'Tracy': 85} print(len(d))  # 结果:3

24. 添加 key-value 、添加多个 key-value。

d = {'Michael': 95, 'Bob': 75, 'Tracy': 85} d['Adam']=67 print(d)  # 结果:{'Adam': 67, 'Bob': 75, 'Tracy': 85, 'Michael': 95} d = {'Michael': 95, 'Bob': 75, 'Tracy': 85} d.update({'Adam':67,'Jack':99}) print(d)

25. 更新 value 。

d = {'Michael': 95, 'Bob': 75, 'Tracy': 85} d['Bob'] = 90 print(d['Bob'])  # 结果:90

26. 删除 key-value 。dict 没有 remove

d = {'Michael': 95, 'Bob': 75, 'Tracy': 85} d.pop('Bob') print(d)  # 结果:{'Michael': 95, 'Tracy': 85}  d = {'Michael': 95, 'Bob': 75, 'Tracy': 85} del d['Bob'] print(d)

27.检查 key 是否在 dict 中。

d = {'Michael': 95, 'Bob': 75, 'Tracy': 85} print('Thomas' in d)  # 结果:False d = {'Michael': 95, 'Bob': 75, 'Tracy': 85} print('Michael' in d)  # 结果:True d = {'Michael': 95, 'Bob': 75, 'Tracy': 85} print(d.get('Thomas'))  # 结果:None d = {'Michael': 95, 'Bob': 75, 'Tracy': 85} print(d.get('Michael'))  # 结果:95

28.dict 查找和插入快,占用空间大,dict 无序;list 查找和插入慢,占用空间小,list 有序;

dict 的 key 必须是不可变对象 (字符串、整数等都是不可变的,list 是可变的)。

5.2 set

29.set 和 dict 类似,也是一组 key 的集合,但不存储 value。由于 key 不能重复,所以,在 set 中,没有重复的 key 。

30. set 自动过滤重复的元素, set 中的元素是无序的。

s = set([2,1,2,3]) print(s)  # 结果:{1, 2, 3} # {1, 2, 3} 只是告诉你这个 set 内部有 1,2,3 这 3 个元素,显示的顺序并不表示 set 是有序的

31. 添加元素。

s = set([2,1,2,3]) s.add(4) print(s)  # 结果:{1, 2, 3, 4}

32. 删除元素。set 没有 del pop 。

s = set([2,1,2,3,4]) s.remove(4) print(s)  # 结果:{1, 2, 3}   s = {1,2,3,4} s.discard(4) print(s)

33. 检查 key 是否在 set 中。

s = set(['Adam', 'Lisa', 'Bart', 'Paul']) print('adam' in s) # 结果:False print('Adam' in s)  # 结果:True

34.set 可以看成数学意义上的无序和无重复元素的集合,因此,两个set可以做数学意义上的并、交、差操作。

s1 = set([1, 2, 3]) s2 = set([2, 3, 4]) s3 = set([1, 2, 3, 4, 5]) print(s1 & s2) # 交 结果:{2, 3} print(s1 | s2) # 并 结果:{1, 2, 3, 4} print(s3 - s1) # 差 结果:{4, 5} 35. set 和 dict 的唯一区别仅在于没有存储对应的 value ,但是,set 的原理和 dict 一样。set 不可以放入可变对象,因为无法判断两个可变对象是否相

Viewing all articles
Browse latest Browse all 9596

Trending Articles