python进阶学什么_python进阶学习一

webchat.jpg

python进阶学习

前面学习python的基本语法和基本数据类型,现在开始进入编程阶段(之前的学习是学习基本的语法和数据结构),用代码编辑器或专业的IDE来编写并执行python程序。

1,编写一个简单的python程序

编写一个简单的python程序,打印输出Hello Python。新建文本文件,输入如下程序代码:

1

2

3

4a = 1

b = 2

c = 3

print('Hello Python')

这个小程序的作用很简单,就是打印输出一条语句,我们把文本的后缀名改为.py,这是python文件的格式,把它保存在某个盘的某个目录下,我这里是保存在G:python>目录下,在Windows命令行窗口进入该目录,执行dir命令,可以看到有test.py文件;执行该程序,执行的命令是:python .py,代表文件名,我这里是test.py,python文件的截图如下所示:

pythontest.png

在命令行窗口执行该程序:

exepython.png

结果如上图所示。

2,小知识点以及编码规范

python中形式上的常量字母要大写,当定义一个变量的时候没有定义在类或函数里面并且我们用的是小写字母的时候,,pylint会提示这应该是一个常量,因为pylint就是按照这个标准来判断的。以下代码:

1

2print('please input account')

user_account = input()

pylint

pylint会提示这应该是一个常量,因为这个变量没有在类或函数里,只是在一个模块里。虽然这里input函数返回的应该是一个变量。

pass语句

pass语句,也叫占位语句,空语句,为了保证代码的完整性

1

2if True:

pass :空语句,占位语句,保证代码的完整性,比如写API接口的时候,先写一些空的代码,这时候pass就会经常出现。

snippet片段

snippet 片段,它的作用可以提高我们的编码效率,相当于代码提示,比如我们输入if,IDE会提示我们可以选择实现什么代码,我们选中就会自动生成一小段代码:

snippet.png

选中后就会自动生成如下代码:

snippet_result.png)

ifelse代码块可以嵌套,如下所示:

1

2

3

4

5

6

7

8

9

10if condition:

if condition:

pass

else:

pass

else:

if condition:

pass

else:

pass

if else分支嵌套

代码如下所示,运行,输入1,按理应该是输出apple,可是结果却是shopping,为什么?输入2、3也是同样的结果。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24a = input()

if a == 1:

print('apple')

else:

if a == 2:

print('orange')

else:

if a == 3:

print('banana')

else:

print('shopping')

PS G:python> python .test5.py

1

shopping

PS G:python> python .test5.py

2

shopping

PS G:python> python .test5.py

3

shopping

上述代码的解释:a = input(),a是字符串类型并不是整型,我们在 a = input()语句后面加入print(type(a)),看打印的结果:

test5if_problem.png

这就证明了a的类型是str类型,所以a == 2当然是false,代码会执行最后的else后面的print()语句。我们可以将a强转为int类型,这样就可以和数字做比较了。

1

2

3

4a = input()

a = int(a)

...

...

输入值为1的时候的运行结果:

1

2

3

4PS G:python> python .test5.py

1

apple

打印出了’apple’,说明 “a == 1”返回了True。根本原因就是input函数,接收的值都认为是字符串,这是动态语言的一个不好的特点,变量没有具体的类型,不像java是强类型语言,这种问题很发现,代码没有写错没有报错信息所以很难解决。这是python语言的一个缺点。

elif的用法

elif是else if的简写,上面的代码可以用elif简写如下:

1

2

3

4

5

6

7

8

9

10a = input()

print('a is' + a)

if a == 1:

print('apple')

elif a == 2:

print('orange')

elif a == 3:

print('banana')

else:

print('shopping')

我们用elif对代码进行了重写,这样可以提高代码的阅读性。所有的elif代码块都是同级别的,尽量减少代码的级别减少嵌套,这是很好的编码习惯。上述代码中elif还是要配合if使用的,不能单独出现,最后的else是和elif配对使用的。注意:python中没有switch语句。

3,python循环语句

循环是计算机计算解决问题的最常见的思想,计算机里常见的循环有while和for循环。

3.1 while循环1

2

3

4

5

6# 循环语句

# while for

CONDITION = True

while CONDITION:

print('I am while')

这是一个循环语句,会一直循环下去,是死循环,条件为真时会一直循环。我们不应该把常量作为while的判断语句,因为常量不会变,条件的判断结果也不会变。

1

2

3

4

5

6

7counter = 1

while counter <= 10:

counter += 1

print(counter)

else:

print('EOF')

while条件判断为假时会打印出 “EOF”。

不过循环一般用for循环的多,不过有一种情况下while非常合适,就是递归。

3.2,for循环

for循环主要用于遍历/循环 序列或者集合、字典中,组概念的数据都可以用for循环,遍历—-for循环。

for循环基本结构:

1

2

3

4

5

6

7

8

9

10

11

12a = ['apple','orange','banana','grape']

for x in a:

print(x)

#打印结果

PS G:python> python .test6.py

apple

orange

banana

grape

##########

循环也可以嵌套循环,以上代码修改如下:

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

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122a = [['apple','orange','banana','grape'],[1,2,3]]

for x in a:

print(x)

#运行结果

PS G:python> python .test6.py

['apple', 'orange', 'banana', 'grape']

[1, 2, 3]

#注意,这里并没有打印出每个元素,只是打印出了序列中的两个子序列,那如果我们要打印每个子元素(就是打印"orange"和另一个序列中的1,2,3)应该怎么办呢?这个时候我们就可以用循环的嵌套。

#######

#循环嵌套

a = [['apple','orange','banana','grape'],[1,2,3]]

for x in a:

for y in x:

print(x)

#运行结果

PS G:python> python .test6.py

apple

orange

banana

grape

1

2

3

##########

#这里就是两个循环,这样打印就没有换行。

a = [['apple','orange','banana','grape'],[1,2,3]]

for x in a:

for y in x:

print(x,end='')

#

PS G:python> python .test6.py

appleorangebananagrape123

###########

a = [['apple','orange','banana','grape'],[1,2,3]]

for x in a:

for y in x:

print(y)

else:

print('fruit is gone') #什么时候执行else语句,当元素遍历结束的时候执行。

PS G:python> python .test6.py

apple

orange

banana

grape

1

2

3

fruit is gone

###########

#循环终止

a = [1,2,3]

for x in a:

if x == 2:

break

print(x)

#打印,当x = 2的时候,结束循环

PS G:python> python .test7.py

1

a = [1,2,3]

#############

for x in a:

if x == 2:

break

print(x)

#打印,跳出本次循环,继续下次循环

PS G:python> python .test7.py

1

3

#############

a = [1,2,3]

for x in a:

if x == 2:

break

print(x)

else:

print('EOF')

#执行结果

PS G:python> python .test7.py

1

#为什么else语句没有执行呢?for和else结合使用正常情况下for语句结束是会执行else语句块的,但是如果for循环是异常终止的话,这种情况就不会执行。那么continue呢?会。

#########

a = [['apple','orange','banana','grape'],[1,2,3]]

for x in a:

for y in x:

if y == 'orange':

break

print(y)

else:

print('fruit is gone') #什么时候执行else语句,当元素遍历结束的时候执行。

#执行结果

PS G:python> python .test6.py

apple

1

2

3

fruit is gone

#为什么这里有break语句,但是还是打印了1 2 3,因为这里有两个循环,break跳出的是里面的循环,不是外面的循环。我们试试跳出外层循环。

#########

a = [['apple','orange','banana','grape'],[1,2,3]]

for x in a:

if 'banana' in x:

break

for y in x:

if y == 'orange':

break

print(y)

else:

print('fruit is gone') #什么时候执行else语句,当元素遍历结束的时候执行。

#执行结果

PS G:python>

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15for x in range(0,10):

print(x)

#执行结果

PS G:python> python .test7.py

0

1

2

3

4

5

6

7

8

9

相当于java中的 for (int x=0;x <=10;x++>){System.out.println(x)}

python的range函数,可以按照我们制定的规则生成一个序列。比如上面代码就是生成了一个0到9的序列,rage[0,10]:从1开始,不包含10,10在这里代表偏移量。再比如:range(0,10,2)什么意思呢?

1

2

3

4

5for x in range(0,10,2):

print(x,end = ' | ')

#打印

PS G:python> python .test7.py

0 | 2 | 4 | 6 | 8 |

再看:

1

2

3

4

5for x in range(10,0,-2):

print(x,end = ' | ')

#打印

PS G:python> python .test7.py

10 | 8 | 6 | 4 | 2 |

range(0,10,2)表示从0开始到10(不包含10)递增打印数字(公差为2);

range(10,0,-2)表示从10到0(不包含0)递减打印数字(公差为2),第三个参数为-2,大的数在第一个参数位置。

1

2

3

4

5

6

7

8

9

10a = [1,2,3,4,5,6,7,8]

# for x in range(0,len(a),2):

# print(a[x],end = ' | ')

b = a[0:len(a):2]

print(b)

#执行结果

PS G:python> python .test7.py

[1, 3, 5, 7]

如上代码所示,a[0:len(a):2]意思是

引申:为什么python没有switch语句?

在Design and History FAQ网站中,有关于这个问题的回答。官方的回答是我们可以用if … elif … elif …… else 语句块来起到其他语言中switch语句同样的效果,或者当需要从很多数量的case中去选择,那就建议用字典(dict),官方原话是:”For cases where you need to choose from a very large number of possibilities,you can create a dictionary mapping case values to functions to call. For example:”。

小总结一下,也就是官方推荐这两种方式来代替switch语句,或者准确地说,用这两种方式起到switch语句在其它语言中起到的作用。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值