Python入门(一)

###1.代码风格 建议遵守以下约定:

  • 使用 4 个空格来缩进
  • 永远不要混用空格和制表符
  • 在函数之间空一行
  • 在类之间空两行
  • 字典,列表,元组以及参数列表中,在 , 后添加一个空格。对于字典,: 后面也添加一个空格
  • 在赋值运算符和比较运算符周围要有空格(参数列表中除外),但是括号里则不加空格:a = f(1, 2) + g(3, 4)

###2.注释 Python 的注释以 # 字符开始的,在 # 字符到行尾之间的所有东西都被程序忽略为注释。

###3.模块 模块是包含了我们能复用的代码的文件,包含了不同的函数定义,变量。模块文件通常以 .py 为扩展名。

>>> import math    # 导入math模块
>>> print(math.e)

###4.关键字 可在python命令行中输入:help() --> keywords 查看

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

###5.键盘读取输入 Python 中使用函数 input() 来做到这一点,input() 有一个用于打印在屏幕上的可选字符串参数,返回用户输入的字符串。

#!/usr/bin/env python3
amount = float(input("Enter amount: "))  # 输入数额
inrate = float(input("Enter Interest rate: "))  # 输入利率
period = int(input("Enter period: "))  # 输入期限
value = 0
year = 1
while year <= period:
    value = amount + (inrate * amount)
    # 字符串格式化,大括号和其中的字符会被替换成传入 str.format() 的参数,也即 year 和 value。其中 {:.2f} 的意思是替换为 2 位精度的浮点数。
    print("Year {} Rs. {:.2f}".format(year, value))
    amount = value
    year = year + 1


输出结果:
$ chmod +x investment.py
$ ./investment.py
Enter amount: 10000
Enter Interest rate: 0.14
Enter period: 5
Year 1 Rs. 11400.00
Year 2 Rs. 12996.00
Year 3 Rs. 14815.44
Year 4 Rs. 16889.60
Year 5 Rs. 19254.15
原文地址:https://www.cnblogs.com/tomtellyou/p/10777715.html