20200914-Python学习笔记1
基本数学计算
>>> 1+1 2 >>> 300-11 289 >>> 500/2 250.0 >>> 8*9 72 >>>
print()函数:打印指定文字
>>> print ('hei,you') hei,you >>> print ('yi,er.san') yi,er.san >>> print ('1+1=') 1+1= >>> print ('1+1=2') 1+1=2 >>>
逗号=空格
>>> print ('one','two','three') one two three >>>
单引号中内容为字符串,不带单引号为数学公式,逗号左边打印出来是字符串,右边打印出来是公式的结果
>>> print ('2+2=',2+2) 2+2= 4
input():接受一个标准输入数据,返回为字符串类型
例子:ID的值自己输入,完成后输入ID会显示值,也就是字符串。字符串会存放在ID或name变量里,输入name或ID可查看对应的变量内容。
>>> id = input() 67 >>> id '67' >>> name = input() wo >>> name 'wo' >>> id '67' >>>
除了直接输入ID或name外,也可以用print()函数,输入print(name)可以显示name变量内容,且显示结果没有单引号
>>> print (name) wo >>> print (id) 67 >>>
将input()函数和print()函数结合,以下是hello1.py的文件内容
name = input() print('hello,',name)
name是一个变量,执行这段代码,输入变量内容,结果会展示为hello,变量内容
D:\桌面>python hello1.py damao hello, damao D:\桌面>python hello1.py ermao hello, ermao D:\桌面>python hello1.py 三毛 hello, 三毛
然后将hello1.py加以改进,会有一条友好的提示信息
name = input('请输入你的名字:') print('hello,',name)
结果展示,输入名字,结果为hello,你的名字。不输入直接回车,显示为空
D:\桌面>python hello1.py 请输入你的名字:四毛 hello, 四毛 D:\桌面>python hello1.py 请输入你的名字: hello,
练习:请利用print()输出1024*768=xxx :
方法一;
number1 = int(input('请输入数值1:')) number2 = int(input('请输入数值2:')) print(int(number1)*int(number2))
结果:3.0以上版本,input返回值的类型为字符串,需要用int转换为整数
D:\桌面>python hello1.py 请输入数值1:1024 请输入数值2:768 786432
方法二;hello2.py代码内容
print('1024 * 768 =',int(1024)*int(768))
结果
D:\桌面>python hello2.py 1024 * 768 = 786432
转义字符:\
\n 表示换行
\t 表示制表符
\\ 表示 \
>>> print ('i\'m ok.') i'm ok. >>> print ('你问我来自哪里?\n我来自地球。') 你问我来自哪里? 我来自地球。 >>> print ('1\n2\n3\n\\\n\\') 1 2 3 \ \
r'' 表示 '' 中间的内容不转义
>>> print ('1\n2\n3\n\\\n\\') 1 2 3 \ \ >>> print (r'1\n2\n3\n\\\n\\') 1\n2\n3\n\\\n\\ >>>