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 ...
随机推荐
- UOJ117:欧拉回路——题解
http://uoj.ac/problem/117 (作为一道欧拉回路的板子题,他成功的令我学会了欧拉回路) (然而我不会背……) 就两件事: 1.无向图为欧拉图,当且仅当为连通图且所有顶点的度为偶数 ...
- 洛谷4525 & 4526:【模板】自适应辛普森法——题解
参考:https://phqghume.github.io/2018/05/19/%E8%87%AA%E9%80%82%E5%BA%94%E8%BE%9B%E6%99%AE%E6%A3%AE%E6%B ...
- Spring多个数据源问题:DataSourceAutoConfiguration required a single bean, but * were found
原因: @EnableAutoConfiguration 这个注解会把配置文件号中的数据源全部都自动注入,不会默认注入一个,当使用其他数据源时再调用另外的数据源. 解决方法: 1.注释掉这个注解 2. ...
- POJ1742 Coins(男人八题之一)
前言 大名鼎鼎的男人八题,终于见识了... 题面 http://poj.org/problem?id=1742 分析 § 1 多重背包 这很显然是一个完全背包问题,考虑转移方程: DP[i][j]表示 ...
- MyBatis代码生成工具mybatis-generator在Myeclipse10中的使用
一.在MyEclipse安装目录下新建myPlugin目录,如下图所示: 二.将 mybatis.zip 里面的文件放在MyEclipse的dropins目录下,如下图所示: 三.在Myeclipse ...
- Hadoop及Zookeeper+HBase完全分布式集群部署
Hadoop及HBase集群部署 一. 集群环境 系统版本 虚拟机:内存 16G CPU 双核心 系统: CentOS-7 64位 系统下载地址: http://124.202.164.6/files ...
- JAVA对象的深度克隆
有时候,我们需要把对象A的所有值复制给对象B(B = A),但是这样用等号给赋值你会发现,当B中的某个对象值改变时,同时也会修改到A中相应对象的值! 也许你会说,用clone()不就行了?!你的想法只 ...
- sleep方法和wait方法的区别?
sleep 是线程类(Thread)的方法,导致此线程暂停执行指定时间,给执行机会给其他线程,但是监控状态依然保持,到时后会自动恢复.调用sleep 不会释放对象锁.wait 是Object 类的方法 ...
- syslog大小限制
位置 /etc/logrotate.d/rsyslog 相关配置信息察看man logrotate size k/M/G /var/log/syslog { rotate daily missingo ...
- HDU 2154 跳舞毯 | DP | 递推 | 规律
Description 由于长期缺乏运动,小黑发现自己的身材臃肿了许多,于是他想健身,更准确地说是减肥. 小黑买来一块圆形的毯子,把它们分成三等分,分别标上A,B,C,称之为“跳舞毯”,他的运动方式是 ...