Python3与2的故事一
2024-08-23 21:36:56
print
函数:(Python3中print为一个函数,必须用括号括起来;Python2中print为class)
Python 2 的 print 声明已经被 print()
函数取代了,这意味着我们必须包装我们想打印在小括号中的对象。
Python 2
print 'Python', python_version()
print 'Hello, World!'
print('Hello, World!')
print "text", ;
print 'print more text on the same line'
run result:
Python 2.7.6
Hello, World!
Hello, World!
text print more text on the same line
Python 3
1
2
3
4
|
print('Python', python_version())
print('Hello, World!')
print("some text,", end="")
print(' print more text on the same line')
|
run result:
Python 3.4.1
Hello, World!
some text, print more text on the same line
通过input()
解析用户的输入:(Python3中input得到的为str;Python2的input的到的为int型,Python2的raw_input得到的为str类型)统一一下:Python3中用input,Python2中用row_input,都输入为str
幸运的是,在 Python 3 中已经解决了把用户的输入存储为一个 str
对象的问题。为了避免在 Python 2 中的读取非字符串类型的危险行为,我们不得不使用 raw_input()
代替。
Python 2
Python 2.7.6
[GCC 4.0.1 (Apple Inc. build 5493)] on darwin
Type “help”, “copyright”, “credits” or “license” for more information.
>>> 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'>
Python 3
Python 3.4.1
[GCC 4.2.1 (Apple Inc. build 5577)] on darwin
Type “help”, “copyright”, “credits” or “license” for more information.
>>> my_input = input('enter a number: ')
enter a number: 123
>>> type(my_input)
<class 'str'>
整除:(没有太大影响)(Python3中/表示真除,%表示取余,//表示地板除(结果取整);Python2中/表示根据除数被除数小数点位得到结果,//同样表示地板除)统一一下:Python3中/表示真除,%表示取余,//结果取整;Python2中带上小数点/表示真除,%表示取余,//结果取整
Python 2
1
2
3
4
5
|
print 'Python', python_version()
print '3 / 2 =', 3 / 2
print '3 // 2 =', 3 // 2
print '3 / 2.0 =', 3 / 2.0
print '3 // 2.0 =', 3 // 2.0
|
run result:
Python 2.7.6
3 / 2 = 1
3 // 2 = 1
3 / 2.0 = 1.5
3 // 2.0 = 1.0
Python 3
1
2
3
4
5
|
print('Python', python_version())
print('3 / 2 =', 3 / 2)
print('3 // 2 =', 3 // 2)
print('3 / 2.0 =', 3 / 2.0)
print('3 // 2.0 =', 3 // 2.0)
|
run result:
Python 3.4.1
3 / 2 = 1.5
3 // 2 = 1
3 / 2.0 = 1.5
3 // 2.0 = 1.0
最新文章
- Netty_UDP丢包解决
- ajax+php+mysql 实现点赞、局部刷新,每个IP只能对一篇文章点赞一次
- JAVA演算法---約瑟夫問題
- App开发(Android与php接口)之:短信验证码
- sql server 警报管理,实时监听数据库动向,运筹帷幄之中
- JavaScript基础——理解变量作用域
- VS2010 水晶报表的使用
- 2015腾讯web前端笔试题
- Ubuntu 上创建常用磁盘阵列
- zhihu spark集群,书籍,论文
- PHP数组函数试题
- leetcode--009 Linked List Cycle I
- 一起写框架-Ioc内核容器的实现-基础功能-ComponentScan(四)
- Hadoop:Hadoop简介及环境配置
- Centos7 编译安装Nginx 教程
- centos7之zabbix邮件报警(短信报警)
- 【高速接口-RapidIO】5、Xilinx RapidIO核例子工程源码分析
- LeetCode(109):有序链表转换二叉搜索树
- java字符串转义,把&;lt;&;gt;转换成<;>;等字符【原】
- 44.scrapy爬取链家网站二手房信息-2
热门文章
- Linux下SuperLU安装
- CSS学习笔记之样式声明
- 35.multi-index和multi-type搜索模式
- webpack 配置 Vue 多页应用 —— 从入门到放弃
- Spring Cloud-Ribbon负载均衡策略类IRule(五)
- 洛谷 P3183 BZOJ 4562 [HAOI2016]食物链
- Spring Boot项目@RestController使用重定向redirect
- x86、Linux、GNU、GNOME是什么
- AutoReplace in pl/sql developer
- WindowsclientC/C++编程规范“建议”——函数