目录
一、python的基本数据类型:
number(数字)、bool(布尔类型表示真假)、complex(复数)
一)、bool类型
1、首字母必大写
例:
>>> true
Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
true
NameError: name 'true' is not defined
>>> True
True
>>> False
False
2、bool类型是一种数
>>> type(True)
<class 'bool'>
>>> type(False)
<class 'bool'>
>>> int(True)
1
>>> int(False)
0
3、不一定用0/1表示真假
例1:数字,包括负数、小数只要非0即True,只有0是False
>>> bool(1)
True
>>> bool(0)
False
>>> bool(2)
True
>>> bool(1.2)
True
>>> bool(-1.1)
True
例2:其他进制数同理
>>> bool(0b10)
True
>>> bool(0b00)
False
例3:还可以判断字符串、数组和字典,非空为True
>>> bool('abc')
True
>>> bool('')
False
>>> bool([1,2,3])
True
>>> bool([])
False
>>> bool({1,1,2})
True
>>> bool({})
False
二)、复数
数字+j,和数学一样
>>> 36j
36j
>>> 3.6j
3.6j
三)、字符串中单引号双引号
直接上图了
单双引号:
>>> 1
1
>>> '1'
'1'
>>> type(1)
<class 'int'>
>>> type('1')
<class 'str'>
>>> 'helloworld'
'helloworld'
>>> "helloworld"
'helloworld'
>>> 'let's go'
SyntaxError: invalid syntax
>>> "let's go"
"let's go"
>>> 'let"s go'
'let"s go'
>>> 'let\'s go'
"let's go"
三引号:(换行)
>>> '''h
e
l
l
o'''
'h\ne\nl\nl\no'
>>> """h
e
l
l
o"""
'h\ne\nl\nl\no'
>>> """h\ne\nl\nl\no\n"""
'h\ne\nl\nl\no\n'
>>> print("""h\ne\nl\nl\no\n""")
h
e
l
l
o
#引号可换为单双引号
>>> 'hello\
world'
'helloworld'
#换行:先一个',然后\,再加另一个'
二、字符串运算
>>> 'hello'+'hello'
'hellohello'
>>> 'hello'*3
'hellohellohello'
>>> print('hello\n'*3)
hello
hello
hello
>>> 'hello world'[9]
'l'
>>> 'hello world'[5]
' '
>>> 'hello world'[0]
'h'
>>> 'hello world'[-1]
'd'
>>> 'hello world'[-6]
' '
>>> 'hello world'[0:4]
'hell'
>>> 'hello world'[0:5]
'hello'
>>> 'hello world'[0:-1]
'hello worl'
>>> 'hello world'[0:11]
'hello world'
>>> 'hello world'[6:11]
'world'
代码均在python3.6.6IDLE中实现