Python笔记
1、获取桌面路径
import os desktop_path = os.path.join(os.path.expanduser('~'), 'Desktop') print('desktop_path: ' + desktop_path)
2、读写文件
#写入文件 file = open(full_path, 'w') file.write(msg) file.close() #读取文件 file = open(full_path, 'r') content = file.read() file.close() print(content)
3、算数运算
a = 10 b = 20 print(a + b) #加:30 print(a - b) #减:-10 print(a * b) #乘:200 print(a / b) #除:0.5 print(a % b) #取模:10 print(a ** b) #幂函数:100000000000000000000 print(9 // 2) #取整除:4 print(9.0 // 2.0) #取整除:4.0
4、字符串格式化
a = '{},我学{}'.format('人生苦短', 'Python') b = '{preposition},我学{lang}'.format(preposition = '人生苦短', lang = 'Python') c = '{0},我学{1}'.format('人生苦短', 'Python') print(a) #人生苦短,我学Python print(b) #人生苦短,我学Python print(c) #人生苦短,我学Python
5、比较运算
print(1 > 2) #False print(1 < 2 < 3) #True print(42 != '42') #True #print(42 > '42') #TypeError: '>' not supported between instances of 'int' and 'str' print('Name' == "name") #False print(True > False) #True,等价于:1 > 0 print(True + False > False + False) #True,等价于:1 + 0 > 0 + 0