关键字和标识符

下列的标识符是 Python3 的关键字,并且不能用于通常的标识符。关键字必须完全按照下面拼写:

>>> help()
Welcome to Python 3.5's help utility!
If this is your first time using Python, you should definitely check out
the tutorial on the Internet at http://docs.python.org/3.5/tutorial/.
Enter the name of any module, keyword, or topic to get help on writing
Python programs and using Python modules.  To quit this help utility and
return to the interpreter, just type "quit".
To get a list of available modules, keywords, symbols, or topics, type
"modules", "keywords", "symbols", or "topics".  Each module also comes
with a one-line summary of what it does; to list the modules whose name
or summary contain a given string such as "spam", type "modules spam".
help> keywords
Here is a list of the Python keywords.  Enter any keyword to get more help.
False               def                 if                  raise
None                del                 import              return
True                elif                in                  try
and                 else                is                  while
as                  except              lambda              with
assert              finally             nonlocal            yield
break               for                 not                 
class               from                or                  
continue            global              pass


Python 中 我们不需要为变量指定数据类型。所以你可以直接写出 num1 = 1 ,这样变量 abc 就是整数类型。如果你写出 num2= 1.0 ,那么变量 abc 就是浮点类型。

>>> num1=1
>>> num2=1.0
>>> name="wuxinglai"
>>> print(type(num1))
<class 'int'>
>>> print(type(num
num1  num2  
>>> print(type(num
num1  num2  
>>> print(type(num2))
<class 'float'>
>>> print(type(name))
<class 'str'>
>>>


通过上面的例子你应该理解了如何在 Python 中定义变量,也就是只需要输入变量名和值就行了。Python 也能操作字符串,它们用单引号或双引号括起来。


从键盘读取输入

root@zwjfdisk2016:~/file/1# vim input_age.py
#!/usr/bin/python3
#coding:utf8
number=int(input("Please Input your age:"))
if number >= 18:
        print ("你已经是成年人 Your  age is %s and You are already an adult" %number)
else:
        print ("你是未成年 Your age is %s You are a minor" %number)
root@zwjfdisk2016:~/file/1# python3 input_age.py 
Please Input your age:18
你已经是成年人 Your  age is 18 and You are already an adult
root@zwjfdisk2016:~/file/1# python3 input_age.py
Please Input your age:12
你是未成年 Your age is 12 You are a minor
下面是计算年利率的脚本
#!/usr/bin/env python3
#coding:utf8
amount= float(input("Enter amount:")) #输入金额
inrate= float(input("Enter Interest rate:")) #输入利率
period= int(input("Enter period:")) #数入期限
value = 0 #初始化value为0
year = 1 #初始化year为1
while year <= period: #判断当期限大于一年做循环
       value = amount +(inrate * amount) #年化率计算方式
       print("Year {} Rss.{:.2f}".format(year,value)) #打印
       amount = value #金额更新
       year =year + 1 #叠加
:wq
root@zwjfdisk2016:~/file/1# python3 fuli.py 
Enter amount:100000
Enter Interest rate:0.15
Enter period:12
Year 1 Rss.115000.00
Year 2 Rss.132250.00
Year 3 Rss.152087.50
Year 4 Rss.174900.62
Year 5 Rss.201135.72
Year 6 Rss.231306.08
Year 7 Rss.266001.99
Year 8 Rss.305902.29
Year 9 Rss.351787.63
Year 10 Rss.404555.77
Year 11 Rss.465239.14
Year 12 Rss.535025.01

12年可以得到53万多

Year {} Rs. {:.2f}".format(year, value) 称为字符串格式化,大括号和其中的字符会被替换成传入 str.format() 的参数,也即 year 和 value。其中 {:.2f} 的意思是替换为 2 位精度的浮点数。


单行定义多个变量或赋值

>>> a,b = 1,3
>>> print(a,b)
1 3
>>> print(a+b)
4