1. 创建变量,输出变量
savings = 100print(savings)
2. 变量运算
savings = 100factor = 1.10result = savings * factor ** 7print(result)
3. 符串和布尔型变量
desc = "compound interest"profitable = Trueprint(desc)print(profitable)
savings = 100factor = 1.1desc = "compound interest"year1 = savings * factorprint(type(savings))print(type(year1))print(type(desc))
5. 字符串拼接
直接用加号就能实现字符串的拼接,或者使用 * 数字 来实现字符串的重叠复制。
desc = "compound interest"doubledesc = desc + descprint(doubledesc)
6. 基本类型转换
基本类型转换函数:int(), float(), bool(), str()
savings = 100result = 100 * 1.10 ** 7print("I started with $" + str(savings) + " and now have $" + str(result) + ". Awesome!")pi_string = "3.1415926"pi_float = float(pi_string)print(pi_float)