Configure the Python development environment for Mac and concatenate simple strings and Integers

Source: Internet
Author: User

Configure the Python development environment for Mac and concatenate simple strings and Integers

First, install Python 3.4 with HomeBrew. The Python that comes with Apple. If it is developed, forget it. Enter the following command in the terminal.

brew install python3

Next, open Sublime and change the syntax to Python. Input

print ('Hello World')

Note: In Python3, brackets must be added to the print function. For beginners who often use C and Java, they may not be used to using extra points.

After writing this line of code, open the terminal and enter the following code:

python xxx.py

Note that xxx is the complete path of the Python file. After running successfully, you can see the running result in the terminal.

But soon I encountered a problem. For example, I want to output "1 + 2 = 3", where 3 is represented as a variable. Try to run the following code:

print ('100 + 200 = ' + (100+200))

The following error message is displayed:

TypeError: cannot concatenate 'str' and 'int' objects

The reason is that, in Python3.4, strings and numbers cannot be concatenated. The solution is simple. You can use the bytes function to convert the int type to the string type.

The modified code is as follows:

print ('100 + 200 = ' + bytes(100 + 200))

Run it. Everything is OK.

100 + 200 = 300

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.