1、将数据输出到文件中
注意点:1.所指定的盘符要存在 2.使用file=fp(字母可更换)
fp = open("/text.txt", "a+") #a+的含义:如果文件不存在就创建,如果存在就在后面追加。所以随着运行次数的增加,文本会越来越多
print("helloworld",file=fp)
fp.close()
2、不换行输出
print("hello","python",'world')
3、转义字符
3.1 \n \t \r \b
print("hello\nworld") #\n是换行,n是newword
print("hello\tworld") #\t是制表符,4位表示一个制表符,所以这三个输出中间的空格不一样
print("helloo\tworld")
print("hellooo\tworld")
print("hello\rworld") #\r:world将hello进行了覆盖
print("hello\bworld") #\b退一个格
输出:
hello
world
hello world
helloo world
hellooo world
world
hellworld
3.2 斜杠和单引号在斜杠后面可以正常输出
print('http:\\\\www.baidu.com.')
print('老师说:\'大家好!\'') #斜杠和单引号在斜杠后面可以正常输出,如果没有加斜杠会报错
3.3 原字符
如果不希望转义字符起作用,就在字符串之前加上r或者R
#原字符,如果不希望转义字符起作用,就在字符串之前加上r或者R
print(r'hello\tworld')
# !注意:当字符串最后一个字符是\时,无效;但是是\\,还是有效的
# print(r'hello\tworld\') 这会报错
print(r'hello\tworld\\')
4、
#“乘”字的二进制,二进制前面要加上0b,否则计算机不认识
print(chr(0b100111001011000))
print(ord('乘')) #20056是乘的十进制
5、标识符规则
·字母、数字、下划线
·不能以数字开头
·不能是keyword
·严格区分大小写
6、
name = '马来亚'
print('标识',id(name))
print('类型',type(name))
print('值',name)
输出:
标识 2195353400304
类型 <class ‘str’>
值 马来亚
7、十进制前面不加,二进制加0b,八进制加0o,十六进制加0x
print('十进制',118)
print('二进制',0b10101111)
print('八进制',0o167)
print('十六进制',0x1eaf)
#记:print( ,)会输出括号里逗号两边的变量,中间用空格隔开
输出:
十进制 118
二进制 175
八进制 119
十六进制 7855
8、浮点数的相加经常是不准确的
n1 = 1.1
n2 = 2.2
print(n1+n2)
# from decimal import Decimal
# print(Decimal('1.1')+Decimal('2.2'))
输出:
3.3000000000000003
9、bool类型可以直接转成整数计算
f1 = True
f2 = False
print(f1+1) #True表示的是1的结果
print(f2+1) #False表示的是0的结果
输出:
2
1
10、单引号和双引号的字符串必须在一行,三引号的字符串可以在多行显示(如果三引号不赋给任何变量的话,就当做是多行注释)
str1 = '人生苦短,我用python'
str2 = "人生苦短,我用python"
str3 = '''人生苦短,
我用python'''
str4 = """人生苦短,
我用python"""
print(str1,type(str1))
print(str2,type(str2))
print(str3,type(str3))
print(str4,type(str4))
输出:
人生苦短,我用python <class ‘str’>
人生苦短,我用python <class ‘str’>
人生苦短,
我用python <class ‘str’>
人生苦短,
我用python <class ‘str’>
11、类型转换
name = '张三'
age = 24
#print('我叫'+name+','+'今年'+age+'岁') #报错,因为str类型和int类型不能直接连接,要进行类型转换
print('我叫'+name+','+'今年'+str(age)+'岁')
11.1 int()转换
#int()转换
s1 = '128'
f1 = 98.7
s2 = '67.77'
ff = True
s3 = 'hello'
print(type(s1),type(f1),type(s2),type(ff),type(s3))
print(int(s1),type(int(s1))) #字符串为整数串时可以转换
print(int(f1),type(int(f1))) #为浮点数时,保留整数部分
#print(int(s2),type(int(s2))) #字符串为浮点数串时,不能转换,会报错
print(int(ff),type(int(ff))) #转换为1
#print(int(s3),type(int(s3))) #字符串必须为整数串才可转换,非数字串是不允许转换的
输出结果:
<class ‘str’> <class ‘float’> <class ‘str’> <class ‘bool’> <class ‘str’>
128 <class ‘int’>
98 <class ‘int’>
1 <class ‘int’>
11.2 float()转换
s1 = '128.77'
s2 = '77'
ff = True
s3 = 'hello'
i = 802
print(type(s1),type(s2),type(ff),type(s3),type(i))
print(float(s1),type(float(s1)))
print(float(s2),type(float(s2)))
print(float(ff),type(float(ff)))
#print(float(s3),type(float(s3))) #字符串为非数字串无法转换
print(float(i),type(float(i)))
输出结果:
<class ‘str’> <class ‘str’> <class ‘bool’> <class ‘str’> <class ‘int’>
128.77 <class ‘float’>
77.0 <class ‘float’>
1.0 <class ‘float’>
802.0 <class ‘float’>
12、输入函数input()
作用:打印括号里的内容提示用户输入,用户输入的内容就是input()的值
present = input('大圣想要什么礼物呢?')
print(present,type(present)) #input的结果是str类型
input的结果是str类型
那么想当作整数使用,怎么做:
#从键盘录入两个整数,计算两个整数的和
a = int(input('请输入一个加数:'))
b = int(input('请输入另一个加数:'))
print(type(a),type(b))
print(a+b)
13、一些需要注意的运算符
13.1 除法运算与整除运算
print(11/2) # 除法运算
print(11//2) # 整除运算
输出结果:
5.5
5
//注意:一正一负向下取整
#//注意:一正一负向下取整
print(9//4)
print(-9//-4)
print(-9//4) #输出为-3,因为-9除以4为-2.25,向下取整为-3
print(9//-4) #同上
输出结果:
2
2
-3
-3
13.2 取余%
%注意:一正一负遵循公式 余数=被除数-除数*商(计算机算的商)
print(-9%4) #-9-4*(-3)
print(9%-4) #-9-(-4)*(-3)
14、支持批量赋值
a=b=c=20
a,b,c = 10,20,30 #允许这样批量赋值,要求等号两侧的个数要相同
print('交换之前:',a,b)
a,b = b,a
print('交换之后:',a,b)
输出结果:
交换之前: 10 20
交换之后: 20 10
15、比较运算符
比较运算符的类型为bool类型
a,b=10,20
print('a>b吗?',a>b)
输出:
a>b吗? False
15.1 值相等还是id标识相等
a=10
b=10
print(a==b) #True说明a与b的值value是相等的
print(a is b) #True说明a与b的id标识是相等的
16、布尔运算符
16.1 and为并且运算符(两个都是True结果才为True)
16.2 or为或运算符(其中一个为True,结果就为True)
16.3 not:对bool类型操作数取反
f=True
f2=False
print(not f)
print(not f2)
输出结果:
False
True
16.4 in和not in
s = 'helloworld'
print('w' in s)
print('k' in s)
print('w' not in s)
print('k' not in s)
输出结果:
True
False
False
True
17、位运算符
17.1 按位与&(同为1时,结果为1)
4&8:
0 0 0 0 0 1 0 0 ——4
0 0 0 0 1 0 0 0 ——8
0 0 0 0 0 0 0 0 ——结果为0
print(4&8)
输出结果:
0
17.2 按位或|(同为0时,结果为0)
4|8:
0 0 0 0 0 1 0 0 ——4
0 0 0 0 1 0 0 0 ——8
0 0 0 0 1 1 0 0 ——结果为12
print(4|8)
输出结果:
12
17.3 左移位<<(高位溢出,低位补0)
# 0 0 0 0 0 1 0 0 左移一位 ——4
# 0 0 0 0 0 1 0 0(0) ——8
#0 0 0 0 0 1 0 0(0)(0) 再移一位 ——16
左移位一位相当于乘2
print(4<<1) #向左移动一位,相当于乘以2
print(4<<2) #向左移动2位,相当于乘以4
输出结果:
8
16
17.4 右移位>>(高位补0,低位截断)
# 0 0 0 0 0 1 0 0 右移位 ——4
# 0 0 0 0 0 1 0 0 ——2
# 0 0 0 0 0 1 0 0 ——1
右移位一位相当于除以2
print(4>>1) #向右移动一位,相当于除以2
print(4>>2) #向右移动2位,相当于除以4
输出结果:
2
1
18、python运算符的优先级
(其他帖子写的很详细,看其他帖子,嘻嘻 · ~ ·
那就附一下链接
https://blog.csdn.net/yukinoai/article/details/82079900?ops_request_misc=&request_id=&biz_id=102&utm_term=python%E8%BF%90%E7%AE%97%E7%AC%A6%E7%9A%84%E4%BC%98%E5%85%88%E7%BA%A7&utm_medium=distribute.pc_search_result.none-task-blog-2allsobaiduweb~default-0-82079900.nonecase&spm=1018.2226.3001.4187