- Hands-On Enterprise Automation with Python.
- Bassem Aly
- 206字
- 2021-06-18 19:22:23
Does this mean I can't write code that runs on both Python 2 and Python 3?
No, you can, of course, write your code in Python 2.x and make it compatible with both versions, but you will need to import a few libraries first, such as the __future__ module, to make it backward compatible. This module contains some functions that tweak the Python 2.x behavior and make it exactly like Python 3.x. Take a look at the following examples to understand the differences between the two versions:
#python 2 only
print "Welcome to Enterprise Automation"
The following code is for Python 2 and 3:
# python 2 and 3
print("Welcome to Enterprise Automation")
Now, if you need to print multiple strings, the Python 2 syntax will be as follows:
# python 2, multiple strings
print "welcome", "to", "Enterprise", "Automation"
# python 3, multiple strings
print ("welcome", "to", "Enterprise", "Automation")
If you try to use parentheses to print multiple strings in Python 2, it will interpret it as a tuple, which is wrong. For that reason, we will import the __future__ module at the beginning of our code, to prevent that behavior and instruct Python to print multiple strings.
The output will be as follows:

- UML和模式應用(原書第3版)
- Learn to Create WordPress Themes by Building 5 Projects
- PostgreSQL for Data Architects
- Python網絡爬蟲從入門到實踐(第2版)
- Python 3破冰人工智能:從入門到實戰
- Java程序設計教程
- WebStorm Essentials
- JavaScript編程精解(原書第2版)
- Mastering ASP.NET Core 2.0
- Arduino Electronics Blueprints
- Learning Shiny
- Java面試一戰到底(基礎卷)
- HTML5 Game Development by Example:Beginner's Guide(Second Edition)
- Python人工智能項目實戰
- 面向物聯網的Android應用開發與實踐