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

最新文章

  1. Netty_UDP丢包解决
  2. ajax+php+mysql 实现点赞、局部刷新,每个IP只能对一篇文章点赞一次
  3. JAVA演算法---約瑟夫問題
  4. App开发(Android与php接口)之:短信验证码
  5. sql server 警报管理,实时监听数据库动向,运筹帷幄之中
  6. JavaScript基础——理解变量作用域
  7. VS2010 水晶报表的使用
  8. 2015腾讯web前端笔试题
  9. Ubuntu 上创建常用磁盘阵列
  10. zhihu spark集群,书籍,论文
  11. PHP数组函数试题
  12. leetcode--009 Linked List Cycle I
  13. 一起写框架-Ioc内核容器的实现-基础功能-ComponentScan(四)
  14. Hadoop:Hadoop简介及环境配置
  15. Centos7 编译安装Nginx 教程
  16. centos7之zabbix邮件报警(短信报警)
  17. 【高速接口-RapidIO】5、Xilinx RapidIO核例子工程源码分析
  18. LeetCode(109):有序链表转换二叉搜索树
  19. java字符串转义,把&amp;lt;&amp;gt;转换成&lt;&gt;等字符【原】
  20. 44.scrapy爬取链家网站二手房信息-2

热门文章

  1. Linux下SuperLU安装
  2. CSS学习笔记之样式声明
  3. 35.multi-index和multi-type搜索模式
  4. webpack 配置 Vue 多页应用 —— 从入门到放弃
  5. Spring Cloud-Ribbon负载均衡策略类IRule(五)
  6. 洛谷 P3183 BZOJ 4562 [HAOI2016]食物链
  7. Spring Boot项目@RestController使用重定向redirect
  8. x86、Linux、GNU、GNOME是什么
  9. AutoReplace in pl/sql developer
  10. WindowsclientC/C++编程规范“建议”——函数