博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
python_day1学习笔记
阅读量:4572 次
发布时间:2019-06-08

本文共 4556 字,大约阅读时间需要 15 分钟。

一、Python 2.7.x 和 3.x 版本的区别小结

  •  print函数

1、python2

1 import platform2 3 print ‘Python’, platform.python_version()4 print ‘Hello, World!’5 print(“Hello,World!’)6 print "text", ; print 'print more text on the same line'
输出结果: 1 Python 2.7.62 Hello, World!3 Hello, World!4 text print more text on the same line

2、python3

1 import platform2 3 print('Python', platform.python_version())4 print('Hello, World!')5 6 print("some text,", end="") 7 print(' print more text on the same line')
结果输出:1 Python 3.5.12 Hello, World!3 some text, print more text on the same line
1 >>> print 'Hello,World!'2   File "
", line 13 print 'Hello,World!'4 ^5 SyntaxError: Missing parentheses in call to 'print'

 注意:

在Python 2中使用额外的括号也是可以的。但反过来在Python 3中想以Python2的形式不带括号调用print函数时,会触发SyntaxError。

  • 整数除法

 1、python2

1 print '3 / 2 =', 3 / 22 print '3 // 2 =', 3 // 23 print '3 / 2.0 =', 3 / 2.04 print '3 // 2.0 =', 3 // 2.0
结果输出: 1 3 / 2 = 12 3 // 2 = 13 3 / 2.0 = 1.54 3 // 2.0 = 1.0

2、python3

1 print('3 / 2 =', 3 / 2)2 print('3 // 2 =', 3 // 2)3 print('3 / 2.0 =', 3 / 2.0)4 print('3 // 2.0 =', 3 // 2.0)
结果输出: 1 3 / 2 = 1.52 3 // 2 = 13 3 / 2.0 = 1.54 3 // 2.0 = 1.0
  • 触发异常

1、python2

Python 2支持新旧两种异常触发语法,而Python 3只接受带括号的的语法(不然会触发SyntaxError):

1 >>> raise IOError, "file error"2 Traceback (most recent call last):3   File "
", line 1, in
4 IOError: file error
1 >>> raise IOError("file error")2 Traceback (most recent call last):3   File "
", line 1, in
4 IOError: file error

2、python3

1 >>> raise IOError, "file error"2   File "
", line 13 raise IOError, "file error"4 ^5 SyntaxError: invalid syntax
1 >>> raise IOError("file error")2 Traceback (most recent call last):3   File "
", line 1, in
4 OSError: file error
  • 异常处理

1、python2

1 try:2     let_us_cause_a_NameError3 except NameError, err:4     print err, '--> our error message'
结果输出: 1 name 'let_us_cause_a_NameError' is not defined --> our error message

2、python3

1 try:2     let_us_cause_a_NameError3 except NameError as err:4     print(err, '--> our error message')
结果输出: 1 name 'let_us_cause_a_NameError' is not defined --> our error message

Python 3中的异常处理也发生了一点变化。在Python 3中必须使用“as”关键字。

  • input()解析用户的输入

Python 3改进了input()函数,这样该函数就会总是将用户的输入存储为str对象。在Python 2中,为了避免读取非字符串类型会发生的一些危险行为,不得不使用raw_input()代替input()。

1、python2

1 >>> my_input = input('enter a number: ') 2  3 enter a number: 123 4  5 >>> type(my_input) 6 
7 8 >>> my_input = raw_input('enter a number: ') 9 10 enter a number: 12311 12 >>> type(my_input)13

2、python3

1 >>> my_input = input('enter a number: ')2 enter a number: 1233 >>> type(my_input)4 
  • 返回可迭代对象

1、python2

1 print range(3)2 print type(range(3))
结果输出: 1 [0, 1, 2]2 

2、python3

1 print(range(3))2 print(type(range(3)))3 print(list(range(3)))
结果输出: 1 range(0, 3)2 
3 [0, 1, 2]

 

二、用户输入

1 #!/usr/bin/env python2 # -*- coding: utf-8 -*-3  4 # 将用户输入的内容赋值给 name 变量5 name = raw_input("请输入用户名:")6  7 # 打印输入的内容8 print name
结果输出: 1 >>> name = raw_input("请输入用户名:")2 请输入用户名:yinjia3 >>> print name4 yinjia

输入密码时,如果想要不可见,需要利用getpass 模块中的 getpass方法,即:

1 #!/usr/bin/env python 2 # -*- coding: utf-8 -*- 3   4 import getpass 5   6 # 将用户输入的内容赋值给 name 变量 7 pwd = getpass.getpass("请输入密码:") 8   9 # 打印输入的内容10 print pwd
结果输出: 1 >>> import getpass2 >>> pwd = getpass.getpass("请输入密码:")3 请输入密码:4 >>> print pwd5 123456

 

三、while循环

  • 基本循环

Python 编程中 while 语句用于循环执行程序,即在某条件下,循环执行某段程序,以处理需要重复处理的相同任务。其基本形式为:

while 判断条件:    执行语句……

执行语句可以是单个语句或语句块。判断条件可以是任何表达式,任何非零、或非空(null)的值均为true。

当判断条件假false时,循环结束。

执行流程图如下:

1 #!/usr/bin/env python2 3 count = 04 while (count < 9):5    print 'The count is:', count6    count = count + 17 8 print "Good bye!"
结果输出:  1 The count is: 0 2 The count is: 1 3 The count is: 2 4 The count is: 3 5 The count is: 4 6 The count is: 5 7 The count is: 6 8 The count is: 7 9 The count is: 810 Good bye!
  • break

break用于退出所有循环

1 #!/usr/bin/env python 2  3 sum = 0 4 number = 0 5  6 while number < 20: 7       number += 1 8       sum += number 9       if sum >= 100:10             break11 print("The number is", number)12 print("The sum is", sum)
结果输出: 1 The number is 142 The sum is 105
  • continue

continue用于退出当前循环,继续下一次循环。

1 #!/usr/bin/env python 2  3 sum = 0  4 number = 0 5  6 while number < 20: 7     number += 1 8     if number == 10 or number == 11: 9           continue10     sum += number11 print("The sum is", sum)
结果输出: 1 The sum is 189

 

转载于:https://www.cnblogs.com/yinjia/p/5495090.html

你可能感兴趣的文章
Android游戏可能遇到的3个问题及解决方案
查看>>
DataBase First创建数据库
查看>>
真事儿!——我们官网被全站拷贝了!
查看>>
边工作边刷题:70天一遍leetcode: day 27-1
查看>>
清理C盘的一个新发现,Visio Studio在调试过程中产生的垃圾文件
查看>>
抽象类及抽象方法
查看>>
Canvas基本绘画学习
查看>>
要习惯用vector代替数组
查看>>
Django ORM 最后操作
查看>>
HDU 1050(贪心)
查看>>
java设计模式之代理模式
查看>>
spring心得2--bean的生命周期@Spring监听器的作用@Spring初始化容器案例分析@web项目使用...
查看>>
顺序栈
查看>>
Rsync详解
查看>>
【每日一读】Java编程中“为了性能”尽量要做到的一些地方
查看>>
什么是内网、什么是公网、什么是NAT
查看>>
【堆/排序】堆排序的两种建堆方法
查看>>
类的内置方法
查看>>
项目中使用的第三方开源库
查看>>
NOIP2009 潜伏者
查看>>