python_day1学习笔记
一、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学习笔记的更多相关文章
- js学习笔记:webpack基础入门(一)
之前听说过webpack,今天想正式的接触一下,先跟着webpack的官方用户指南走: 在这里有: 如何安装webpack 如何使用webpack 如何使用loader 如何使用webpack的开发者 ...
- PHP-自定义模板-学习笔记
1. 开始 这几天,看了李炎恢老师的<PHP第二季度视频>中的“章节7:创建TPL自定义模板”,做一个学习笔记,通过绘制架构图.UML类图和思维导图,来对加深理解. 2. 整体架构图 ...
- PHP-会员登录与注册例子解析-学习笔记
1.开始 最近开始学习李炎恢老师的<PHP第二季度视频>中的“章节5:使用OOP注册会员”,做一个学习笔记,通过绘制基本页面流程和UML类图,来对加深理解. 2.基本页面流程 3.通过UM ...
- 2014年暑假c#学习笔记目录
2014年暑假c#学习笔记 一.C#编程基础 1. c#编程基础之枚举 2. c#编程基础之函数可变参数 3. c#编程基础之字符串基础 4. c#编程基础之字符串函数 5.c#编程基础之ref.ou ...
- JAVA GUI编程学习笔记目录
2014年暑假JAVA GUI编程学习笔记目录 1.JAVA之GUI编程概述 2.JAVA之GUI编程布局 3.JAVA之GUI编程Frame窗口 4.JAVA之GUI编程事件监听机制 5.JAVA之 ...
- seaJs学习笔记2 – seaJs组建库的使用
原文地址:seaJs学习笔记2 – seaJs组建库的使用 我觉得学习新东西并不是会使用它就够了的,会使用仅仅代表你看懂了,理解了,二不代表你深入了,彻悟了它的精髓. 所以不断的学习将是源源不断. 最 ...
- CSS学习笔记
CSS学习笔记 2016年12月15日整理 CSS基础 Chapter1 在console输入escape("宋体") ENTER 就会出现unicode编码 显示"%u ...
- HTML学习笔记
HTML学习笔记 2016年12月15日整理 Chapter1 URL(scheme://host.domain:port/path/filename) scheme: 定义因特网服务的类型,常见的为 ...
- DirectX Graphics Infrastructure(DXGI):最佳范例 学习笔记
今天要学习的这篇文章写的算是比较早的了,大概在DX11时代就写好了,当时龙书11版看得很潦草,并没有注意这篇文章,现在看12,觉得是跳不过去的一篇文章,地址如下: https://msdn.micro ...
随机推荐
- BZOJ3236:[AHOI2013]作业——题解
https://www.lydsy.com/JudgeOnline/problem.php?id=3236 第一种做法: 建两棵主席树分别处理两个问题. 第一个问题水,第二个问题参考SPOJ3267/ ...
- YBT 1.1 贪心算法
本人因为过于懒所以以后就将题解放进原文件中,存入百度网盘,自行下载,里面包含题目网站,源文件,与相应题解(这次没有写) 链接: https://pan.baidu.com/s/1eSoQ_LFWMxF ...
- 简述JavaScript的类与对象
JavaScript语言是动态类型的语言,基于对象并由事件驱动.用面向对象的思想来看,它也有类的概念.JavaScript 没有class关键字,就是用function来实现. 1. 实现方式及变量/ ...
- iOS中产生随机数的方法
利用arc4random_uniform()产生随机数 Objective-C 中有个arc4random()函数用来生成随机数且不需要种子,但是这个函数生成的随机数范围比较大,需要用取模的算法对随机 ...
- Qt -------- 多线程编程
一.继承QThread(不推荐) 定义一个类,继承QThread,重写run(),当调用方法start(),启动一个线程,run()函数运行结束,线程结束. 二.继承QRunnable Qrunnab ...
- [LeetCode] 6. ZigZag Conversion ☆☆☆
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...
- SpringMVC中ModelAndView addObject()设置的值jsp取不到的问题
controller public class HelloWorldController implements Controller { public ModelAndView handleReque ...
- 洛谷金秋夏令营模拟赛 第2场 T11738 伪神
调了一个下午只有八十分QAQ md弃了不管了 对拍也没拍出来 鬼知道是什么数据把我卡了QAQ 没事我只是个SB而已 这题其实还是蛮正常的 做法其实很简单 根据链剖的构造方法 你每次修改都是一段又一段的 ...
- codevs1066&&noip引水入城
这道题 解决第一问 用灌水法 枚举第一行的每一个点 查找是否最后一行的每一个点是否都能灌到水 第二问 用反灌水发 枚举最后一行的每一个点 解决第一行每一个点所能覆盖的左右端点 可以证明每个点所能覆盖的 ...
- 【BZOJ】1477 青蛙的约会
[算法]扩展欧几里德算法(模线性方程) [题解]http://hzwer.com/2121.html 一些问题写在http://www.cnblogs.com/onioncyc/p/6146143.h ...