0
点赞
收藏
分享

微信扫一扫

【Class 2】 Python 基础


“ ​/​ ”: 除,返回结果为 float 型 3/2 = 1.5

“ ​//​ ”: 整除,返回结果为 int 型 3//2 = 1

二进制转换: bin(xxx)

bin(0o7) = 0b111
bin(0xE) = 0b1110

十六进制转换:hex(xxx)

hex(888) = 0x378
hex(0o7777) = 0xfff

八进制转换: oct(xxx)

oct(0b111) = 0o7
oct(0b777) = 0o3567

bool 类型​:

True​ : 真 int(True) = 1 只要非0 都是 bool 真

False​ : 假 int(False) = 0

True

bool(1)

bool(2.2)

bool(‘abc’)

bool([1,2,3])

bool({1,2,3})

False

bool(0)

bool(None)

bool(’’)

bool([ ])

bool({ })

复数​ : 15j

字符串

单引号 : ’ let’s go’

双引号 : " let’s go "

三引号(三个单引号或者双引号) : 当字符串较长时,可以实现字符串换行输入

例如:

>>> '''first line
second line
third line
end
thks'''
'first line\nsecond line\nthird line\nend\nthks'

----------------------------------------------------------------
命令行的字符串中包含 \n 不会自动换行
>>> """hello world\nhello world\n123"""
'hello world\nhello world\n123'

----------------------------------------------------------------
print的字符串中包含 \n 会自动换行
>>> print("""hello world\nhello world\n123""")
hello world
hello world
123

----------------------------------------------------------------
字符串输入换行使用 \
>>>> 'hello\
world'
'helloworld'
>>>

转义字符 及 r"hello\nworld"

字符串前面添加 r 或者 R,表明该字符串时不是普通字符串,而是原始字符串,原始字符串中的数据不会做任何处理。

>>> print("hello \\n world")

hello \n world
>>> print(r"hello \\n world")

hello \\n world
>>> print(r"hello \n world")

hello \n world
>>>

字符串的运算

+ 字符串的拼接
>>> 'hello'+' '+"world"
'hello world'

* 连续打印相同字符串
>>> 'hello '*3
'hello hello hello '

[n]获取字符串内第n个 单个字符
>>> "hello world"[0] + " " +"hello world"[6]
'h w'

[n]获取字符串内单个字符,负数说明时反向查找第|n|个字符
>>> "hello world"[-7] + " " +"hello world"[-1]
'o d'

[0:4]获取字符串中第0到第3个字符
>>> "hello world"[0:4]
'hell'
>>> "hello world"[0:5]
'hello'
>>> "hello world"[0:-2]
'hello wor'

>>> "hello world"[6:11]
'world'

如果输入数字大于字符串长度,则按字符串长度来取
>>> "hello world"[6:50]
'world'
>>> "hello world"[6:]
'world'
>>> "hello world"[-5:]
'world'
>>> "hello world"[-5:-2]
'wor'



举报

相关推荐

0 条评论