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

  • print函数

1、python2

 import platform

 print ‘Python’, platform.python_version()
print ‘Hello, World!’
print(“Hello,World!’)
print "text", ; print 'print more text on the same line'
输出结果:
1 Python 2.7.6
Hello, World!
Hello, World!
text print more text on the same line

2、python3

 import platform

 print('Python', platform.python_version())
print('Hello, World!') print("some text,", end="")
print(' print more text on the same line')
结果输出:
1 Python 3.5.1
2 Hello, World!
3 some text, print more text on the same line
 >>> print 'Hello,World!'
File "<stdin>", line 1
print 'Hello,World!'
^
SyntaxError: Missing parentheses in call to 'print'

注意:

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

  • 整数除法

1、python2

 print '3 / 2 =', 3 / 2
print '3 // 2 =', 3 // 2
print '3 / 2.0 =', 3 / 2.0
print '3 // 2.0 =', 3 // 2.0
结果输出:
1 3 / 2 = 1
3 // 2 = 1
3 / 2.0 = 1.5
3 // 2.0 = 1.0

2、python3

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

1、python2

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

 >>> raise IOError, "file error"
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IOError: file error
 >>> raise IOError("file error")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IOError: file error

2、python3

 >>> raise IOError, "file error"
File "<stdin>", line 1
raise IOError, "file error"
^
SyntaxError: invalid syntax
 >>> raise IOError("file error")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
OSError: file error
  • 异常处理

1、python2

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

2、python3

 try:
let_us_cause_a_NameError
except NameError as err:
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

 >>> my_input = input('enter a number: ')

 enter a number: 123

 >>> type(my_input)
<type 'int'> >>> my_input = raw_input('enter a number: ') enter a number: 123 >>> type(my_input)
<type 'str'>

2、python3

 >>> my_input = input('enter a number: ')
enter a number: 123
>>> type(my_input)
<class 'str'>
  • 返回可迭代对象

1、python2

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

2、python3

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

二、用户输入

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

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

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

三、while循环

  • 基本循环

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

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

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

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

执行流程图如下:

 #!/usr/bin/env python

 count = 0
while (count < 9):
print 'The count is:', count
count = count + 1 print "Good bye!"
 结果输出:
1 The count is: 0
The count is: 1
The count is: 2
The count is: 3
The count is: 4
The count is: 5
The count is: 6
The count is: 7
The count is: 8
Good bye!
  • break

break用于退出所有循环

 #!/usr/bin/env python

 sum = 0
number = 0 while number < 20:
number += 1
sum += number
if sum >= 100:
break
print("The number is", number)
print("The sum is", sum)
结果输出:
1 The number is 14
The sum is 105
  • continue

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

 #!/usr/bin/env python

 sum = 0
number = 0 while number < 20:
number += 1
if number == 10 or number == 11:
continue
sum += number
print("The sum is", sum)
结果输出:
1 The sum is 189

python_day1学习笔记的更多相关文章

  1. js学习笔记:webpack基础入门(一)

    之前听说过webpack,今天想正式的接触一下,先跟着webpack的官方用户指南走: 在这里有: 如何安装webpack 如何使用webpack 如何使用loader 如何使用webpack的开发者 ...

  2. PHP-自定义模板-学习笔记

    1.  开始 这几天,看了李炎恢老师的<PHP第二季度视频>中的“章节7:创建TPL自定义模板”,做一个学习笔记,通过绘制架构图.UML类图和思维导图,来对加深理解. 2.  整体架构图 ...

  3. PHP-会员登录与注册例子解析-学习笔记

    1.开始 最近开始学习李炎恢老师的<PHP第二季度视频>中的“章节5:使用OOP注册会员”,做一个学习笔记,通过绘制基本页面流程和UML类图,来对加深理解. 2.基本页面流程 3.通过UM ...

  4. 2014年暑假c#学习笔记目录

    2014年暑假c#学习笔记 一.C#编程基础 1. c#编程基础之枚举 2. c#编程基础之函数可变参数 3. c#编程基础之字符串基础 4. c#编程基础之字符串函数 5.c#编程基础之ref.ou ...

  5. JAVA GUI编程学习笔记目录

    2014年暑假JAVA GUI编程学习笔记目录 1.JAVA之GUI编程概述 2.JAVA之GUI编程布局 3.JAVA之GUI编程Frame窗口 4.JAVA之GUI编程事件监听机制 5.JAVA之 ...

  6. seaJs学习笔记2 – seaJs组建库的使用

    原文地址:seaJs学习笔记2 – seaJs组建库的使用 我觉得学习新东西并不是会使用它就够了的,会使用仅仅代表你看懂了,理解了,二不代表你深入了,彻悟了它的精髓. 所以不断的学习将是源源不断. 最 ...

  7. CSS学习笔记

    CSS学习笔记 2016年12月15日整理 CSS基础 Chapter1 在console输入escape("宋体") ENTER 就会出现unicode编码 显示"%u ...

  8. HTML学习笔记

    HTML学习笔记 2016年12月15日整理 Chapter1 URL(scheme://host.domain:port/path/filename) scheme: 定义因特网服务的类型,常见的为 ...

  9. DirectX Graphics Infrastructure(DXGI):最佳范例 学习笔记

    今天要学习的这篇文章写的算是比较早的了,大概在DX11时代就写好了,当时龙书11版看得很潦草,并没有注意这篇文章,现在看12,觉得是跳不过去的一篇文章,地址如下: https://msdn.micro ...

随机推荐

  1. STL之一:字符串用法详解

    转载于:http://blog.csdn.net/longshengguoji/article/details/8539471 字符串是程序设计中最复杂的变成内容之一.STL string类提供了强大 ...

  2. luncence

    问题的提出: 我们在访问淘宝,京东这些商城系统的时候,我们可以随意的在文本框输入关键字就可以获取到所想要的信息或者相关的信息,那么我们到底是如何实现这个功能的呢,为什么可以随意的输入就可以显示相关的信 ...

  3. POJ3186 DP

    Treats for the Cows Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5753   Accepted: 29 ...

  4. HDU1540 区间合并

    Tunnel Warfare Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)To ...

  5. idea 多模块引用

    roma-server 引用common-utils的类,所以在roma-server 的pom中配置 <dependency> <groupId>org.springfram ...

  6. 搜索:BFS

    如果题目真的要考察宽度优先搜索,那么这类题目往往具有比较大的编码难度,换个说法,就是细枝末节特别多,状态特别复杂.. 剥茧抽丝,这里以一个比较“裸”的BFS作为例子,了解一下实现BFS的一些规范. 直 ...

  7. Doc常用命令

    1. 获取目录: dir 2. 清屏: cls

  8. Ubuntu12.04 安装LAMP及phpmyadmin

    1.安装 Apache apt-get install apache2 2.安装 PHP5 apt-get install php5 libapache2-mod-php5 3.安装 MySQL ap ...

  9. html 制作静态页面新知识

    1.在区块线边框添加一条水平线 例如:<div  style:"height :300px;width:800px;border-bottom: solid 1px orange ;& ...

  10. python+selenium+js 处理滚动条

    selenium并不是万能的,有时候页面上操作无法实现的,这时候就需要借助JS来完成了. 常见场景: 当页面上的元素超过一屏后,想操作屏幕下方的元素,是不能直接定位到,会报元素不可见的. 这时候需要借 ...