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