iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > Python >Python 3版本较之前版本语法的一些
  • 787
分享到

Python 3版本较之前版本语法的一些

版本语法Python 2023-01-31 07:01:47 787人浏览 八月长安

Python 官方文档:入门教程 => 点击学习

摘要

市面上的python教程基本都是以3.0以下版本来讲解的,Python 从3.0之后一些语法都做了写更改,有时候可能会浪费比较多的时间,记录下使用过程中遇到的情况以备后查。 1、Print (1)需要加括号 (2)打印文件重定向 (1)pr

市面上的python教程基本都是以3.0以下版本来讲解的,Python 从3.0之后一些语法都做了写更改,有时候可能会浪费比较多的时间,记录下使用过程中遇到的情况以备后查。

1、Print (1)需要加括号 (2)打印文件重定向

(1)print ('hello world!!')

(2) print([object, ...], *, sep=' ', end='\n', file=sys.stdout)

  1. log = open('test.txt','a')  
  2. print (1,2,3, file=log, end='\n')  
  3. print (4,5,6, file=log, end='\n')  
  4. log.close()  
  5. print (7,8,9) 

 2、commands modle 在3.0之后使用subprocess来代替。

Deprecated since version 2.6: The commands module has been removed in Python 3.0. Use the subprocess module instead.

  1. >>> import commands  
  2. >>> commands.getstatusoutput('ls /bin/ls')  
  3. (0, '/bin/ls')  
  4. >>> commands.getstatusoutput('cat /bin/junk')  
  5. (256, 'cat: /bin/junk: No such file or directory')  
  6. >>> commands.getstatusoutput('/bin/junk')  
  7. (256, 'sh: /bin/junk: not found')  
  8. >>> commands.getoutput('ls /bin/ls')  
  9. '/bin/ls' 
  10. >>> commands.getstatus('/bin/ls')  
  11. '-rwxr-xr-x  1 root        13352 Oct 14  1994 /bin/ls' 
  1. >>> import subprocess  
  2. >>> subprocess.getstatusoutput('ls /bin/ls')  
  3. (0, '/bin/ls')  
  4. >>> subprocess.getstatusoutput('cat /bin/junk')  
  5. (256, 'cat: /bin/junk: No such file or directory')  
  6. >>> subprocess.getstatusoutput('/bin/junk')  
  7. (256, 'sh: /bin/junk: not found')  
  8. >>> subprocess.getoutput('ls /bin/ls')  
  9. '/bin/ls' 

 3. input 替代raw_input

raw_input
Converts raw_input() to input().

  1. while True:  
  2.     reply = input('Enter Text:')  
  3.     if reply == 'stop': break 
  4.     print (reply.upper()) 

 

 

--结束END--

本文标题: Python 3版本较之前版本语法的一些

本文链接: https://www.lsjlt.com/news/191842.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作