1. Python 3.X中,基本输入函数式input :
>>> input('input your name:')
而在 Python 2.X 的是raw_input :
>>> raw_input('input your name:')
2. 强制转换变量类型格式,e.g:
>>> year = raw_input ('input your name:')
>>> print(year)
2014
>>> year+1
Traceback (most recent call last):
File "<interactive input>", line 1, in <module>
TypeError: cannot concatenate 'str' and 'int' objects
>>> int(year)+1
2015
3.Python 2.X 中可以使用中文,但是需要对中文进行处理。如果在.py 文件中使用了中文,则需要在文件的第一行使用如下语句指定字符编码集
#-*- coding: utf-8 -*-
#file name : hellopy.py
chinesewords = """
好好学习Python,
因为它很好用!<span style="white-space:pre"> </span>
可以使用的字符编码有一下几种 :
UTF-8, CP936,GB2312,ISO-8859-1。
"""
print (chinesewords)
如果是在Python 3.X 中执行,则源文件默认是使用 UTF - 8 编码, 这样一来这样一来不但可以方便的在源文件的字符串中使用中文,而且变量名也可以使用中文命名:
>>>中文='Chinese'
>>>print(中文)
Chinese
执行上文中的代码只需修改成以下形式:
#file name : hellopy.py
chinesewords ="""
好好学习Python,
因为它很好用!
可以使用的字符编码有一下几种 :
UTF-8, CP936,GB2312,ISO-8859-1。
"""
print (chinesewords)
>>> 99**99
369729637649726772657187905628805440595668764281741102430259972423552570455277523421410650010128232727940978889548326540119429996769494359451621570193644014418071060667659301384999779999159200499899L
>>>
5. 字符串,数字类型的转换,e.g :
>>> '10'+4 #两种不同类型对象的相加引发异常
Traceback (most recent call last):
File "<interactive input>", line 1, in <module>
TypeError: cannot concatenate 'str' and 'int' objects
>>> int('10')+4 #字符串转化为数字
14
>>> '10'+str(4) #数字转化为字符串
'104'
to be continued....