9月 152020
 
#变量数据类型,整数integer
num = 1
#变量数据类型,字符串string
string = '1'
#不同数据类型无法合并
print(num + string)
PS C:\Users\harveymei> & C:/Users/harveymei/AppData/Local/Programs/Python/Python38/python.exe c:/Users/harveymei/hello.py
Traceback (most recent call last):
  File "c:/Users/harveymei/hello.py", line 6, in <module>
    print(num + string)
TypeError: unsupported operand type(s) for +: 'int' and 'str'
PS C:\Users\harveymei>

 

#变量数据类型,整数integer
num = 1
#变量数据类型,字符串string
string = '1'
#转换数据类型
num2 = int(string)

print(num + num2)
PS C:\Users\harveymei> & C:/Users/harveymei/AppData/Local/Programs/Python/Python38/python.exe c:/Users/harveymei/hello.py
2
PS C:\Users\harveymei> 

 Leave a Reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

(required)

(required)

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据