每一种编程语言都有它们本身的语法规则,就像咱们所说的外语。html
下列的标识符是 Python3 的关键字,而且不能用于一般的标识符。关键字必须彻底按照下面拼写:python
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
这些内容能够在 Python3 解释器中获得:编程
在 Python 中 咱们不须要为变量指定数据类型。因此你能够直接写出 abc = 1
,这样变量 abc
就是整数类型。若是你写出 abc = 1.0
,那么变量 abc
就是浮点类型。vim
>>> a = 13 >>> b = 23 >>> a + b 36
经过上面的例子你应该理解了如何在 Python 中定义变量,也就是只须要输入变量名和值就好了。Python 也能操做字符串,它们用单引号或双引号括起来,就像下面这样。bash
>>> 'ShiYanLou' 'ShiYanLou' >>> 'ShiYanLou\'s best' "ShiYanLou's best" >>> "Hello World!" 'Hello World!'
一般状况下,Python 的代码中是不须要从键盘读取输入的。不过咱们仍是能够在 Python 中使用函数 input()
来作到这一点,input()
有一个用于打印在屏幕上的可选字符串参数,返回用户输入的字符串。markdown
咱们来写一个程序,它将会从键盘读取一个数字而且检查这个数字是否小于 100。这个程序名称是 testhundred.py。还记得如何使用 vim 吗?忘了的话能够看看下面的动图:编程语言
#!/usr/bin/env python3 number = int(input("Enter an integer: ")) if number < 100: print("Your number is smaller than 100") else: print("Your number is greater than 100")
若是 number
小于 100,输出“Your number is smaller than 100”,若是大于 100,输出“Your number is greater than 100”。函数
程序运行起来就像这样: (运行时别忘记给文件添加可执行权限,如何添加权限请回想上节实验内容,程序运行时若报错权限不够,为文件添加权限便可)学习
$ ./testhundred.py
Enter an integer: 13 Your number is smaller than 100 $ ./testhundred.py Enter an integer: 123 Your number is greater than 100
下一个程序咱们来计算投资:ui
#!/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) print("Year {} Rs. {:.2f}".format(year, value)) amount = value year = year + 1
运行程序:
$ ./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
while year <= period:
的意思是,当 year
的值小于等于 period
的值时,下面的语句将会一直循环执行下去,直到 year
大于 period
时中止循环。
Year {} Rs. {:.2f}".format(year, value)
称为字符串格式化,大括号和其中的字符会被替换成传入 str.format()
的参数,也即 year
和 value
。其中{:.2f}
的意思是替换为 2 位精度的浮点数。
一些关于变量和数据类型的例子。
下面的程序用来求 N 个数字的平均值。
#!/usr/bin/env python3 N = 10 sum = 0 count = 0 while count < N: number = float(input()) sum = sum + number count = count + 1 average = sum / N print("N = {}, Sum = {}".format(N, sum)) print("Average = {:.2f}".format(average))
运行程序:
$ ./averagen.py 1.2 3.4 3.5 33.2 2 4 6 2.4 4 5.5 N = 10, Sum = 65.2 Average = 6.52
在下面的程序里,咱们使用公式 C = (F - 32) / 1.8 将华氏温度转为摄氏温度。
#!/usr/bin/env python3 fahrenheit = 0 print("Fahrenheit Celsius") while fahrenheit <= 250: celsius = (fahrenheit - 32) / 1.8 # 转换为摄氏度 print("{:5d} {:7.2f}".format(fahrenheit , celsius)) fahrenheit = fahrenheit + 25
{:5d}
的意思是替换为 5 个字符宽度的整数,宽度不足则使用空格填充。
运行程序:
$ ./temperature.py Fahrenheit Celsius 0 -17.78 25 -3.89 50 10.00 75 23.89 100 37.78 125 51.67 150 65.56 175 79.44 200 93.33 225 107.22 250 121.11
你甚至能够在一行内将多个值赋值给多个变量。
>>> a , b = 45, 54 >>> a 45 >>> b 54
这个技巧用来交换两个数的值很是方便。
>>> a, b = b , a >>> a 54 >>> b 45
要明白这是怎么工做的,你须要学习元组(tuple)这个数据类型。咱们是用逗号建立元组。在赋值语句的右边咱们建立了一个元组,咱们称这为元组封装(tuple packing),赋值语句的左边咱们则作的是元组拆封 (tuple unpacking)。
下面是另外一个元组拆封的例子:
>>> data = ("shiyanlou", "China", "Python") >>> name, country, language = data >>> name 'shiyanlou' >>> country 'China' >>> language 'Python'
完成这个实验咱们应该了解 python 关键字有哪些(在这里不要求所有记住),如何赋值变量,怎样从键盘读取输入,以及字符串的格式化,在这里能够了解更多有关字符串格式化的信息:https://docs.python.org/3/library/string.html#formatstrings。最后咱们接触了元组封装和元组拆封,这是一个颇有用很方便的技巧,但愿你能掌握它。