string类型转bool类型_6.基本数据类型 number/bool/str

Python的基本数据类型:

  • number---------->数字类型
  • str---------------->字符串类型
  • bool-------------->布尔类型
  • list---------------->列表
  • tuple------------->元组
  • dict--------------->字典
  • set---------------->集合

本文先介绍numberbool,以及str类型的索引以及切片部分。


Number类型

Python 支持三种不同的数值类型:

  • 整型(Int) - 通常被称为是整型或整数,是正或负整数,不带小数点。Python3 整型是没有限制大小的,可以当作 Long 类型使用,所以 Python3 没有 Python2 的 Long 类型。
  • 浮点型(float) - 浮点型由整数部分与小数部分组成,浮点型也可以使用科学计数法表示(2.5e2 = 2.5 x 102 = 250)
  • 复数(complex) - 复数由实数部分和虚数部分构成,可以用a + bj,或者complex(a,b)表示, 复数的实部a和虚部b都是浮点型。

☑可以使用八进制以及16进制来表示整数。

>>> number = 0xA0F # 十六进制
>>> number
2575

>>> number=0o37 # 八进制
>>> number
31

看下图理解:

93d2bd1178366e67a0f9020479d78e89.png

Bool类型

取值为:True, False;⚠bool值没有操作。

转换问题:

str => int ---------- int(str)

int => str ---------- str(int)

int => bool--------- bool(int). 0是False 非0是True

bool=>int ---------- int(bool). True是1, False是0

str => bool --------- bool(str) 空字符串是False, 不空是True

bool => str --------- str(bool) 把bool值转换成相应的"值"

String类型

字符串就是在python中⽤', ", ''', """引起来的内容,把字符连成串。

1.索引

索引表示下标的意思,注意,下标是从0开始。

str1 = "我真的很喜欢你"
# 索引:  0 1  2 3  4 5  6
print(str1[0])
print(str1[1])
print(str1[2])
print(str1[3])
print(str1[4])
print(str1[5])
print(str1[6])
# 索引可以是负数,理解为倒数第几个元素
print(str1[-1])    # 倒数第一个元素
print(str1[-3])    # 倒数第三个元素

----------------------------------------------------------------------------------------------
Microsoft Windows [版本 10.0.17134.885]
(c) 2018 Microsoft Corporation。保留所有权利。

C:UsersASUS>python
Python 3.7.2 (tags/v3.7.2:9a3ffc0492, Dec 23 2018, 23:09:28) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> str1 = "我真的很喜欢你"
>>> # 索引:  0 1  2 3 4  5  6

... print(str1[0])
我
>>> print(str1[1])
真
>>> print(str1[2])
的
>>> print(str1[3])
很
>>> print(str1[4])
喜
>>> print(str1[5])
欢
>>> print(str1[6])
你
>>> # 索引可以是负数,理解为倒数第几个元素
... print(str1[-1])
你
>>> print(str1[-3])
喜
>>>

2.切片

使用索引下标截取字符串的部分内容,就理解为“切片”。

☑语法:str[start : end : step]->str[起始点 : 结束点(不可取此点元素) : 步长]

☑用法特点:从start切到end,顾头不顾尾,左闭右开,就是不取end。

str2 = "Python真的很秀"       # 字符串变量

# 从0获取到4,不包括4.
print(str2[0:4])

# 从6到9
print(str2[6:9])

# 从6到10
print(str2[6:10])

# 从6到最后
print(str2[6:])

# 从-1取到-7,这是娶不到任何结果的。
print(str2[-1:-7])

# 从-6取到-1
print(str2[-6:-1])

# 从-6取回去,取完
print(str2[-6:])

# 一直取到倒数第一个元素
print(str2[:-1])

# 全部内容
print(str2[:])

----------------------------------------------------------------------------------------------
Microsoft Windows [版本 10.0.17134.885]
(c) 2018 Microsoft Corporation。保留所有权利。

C:UsersASUS>python
Python 3.7.2 (tags/v3.7.2:9a3ffc0492, Dec 23 2018, 23:09:28) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> str2 = "Python真的很秀"
>>> # 从0获取到4,不包括4.
... print(str2[0:4])
Pyth
>>> # 从6到9
... print(str2[6:9])
真的很
>>> # 从6到10
... print(str2[6:10])
真的很秀
>>> # 从6到最后
... print(str2[6:])
真的很秀
>>> # 从-1取到-7,这是取不到任何结果的。

... print(str2[-1:-7])
(没结果)
>>> # 从-6取到-1
... print(str2[-6:-1])
on真的很
>>> # 从-6取回去,取完
... print(str2[-6:])
on真的很秀
>>> # 取到倒数第一个元素
... print(str2[:-1])
Python真的很
>>> # 全部内容
... print(str2[:])
Python真的很秀
>>>

现在引入一个新的item,第三个位置step,简称“步长”,理解为跨接个元素再取值。

# 用法特点:跳着取, 步⻓
str3 = "python最⽜B"

# 从第⼀个开始取, 取到第5个,每2个取1个
print(s2[1:5:2])

# 从头开始到第五个. 每两个取⼀个
print(s2[:5:2]) 

# 从4开始取到最后. 每两个取⼀个
print(s2[4::2]) 

# 从-5取到最后.每两个取⼀个
print(s2[-5::2]) 

# -1:-5什么都没有. 因为是从左往右获取的.
print(s2[-1:-5]) 

# 步⻓是-1. 这时就从右往左取值了
print(s2[-1:-5:-1]) 

# 从倒数第5个开始. 到最开始. 每3个取⼀个, 结果oy
print(s2[-5::-3]) 

----------------------------------------------------------------------------------------------
Microsoft Windows [版本 10.0.17134.885]
(c) 2018 Microsoft Corporation。保留所有权利。

C:UsersASUS>python
Python 3.7.2 (tags/v3.7.2:9a3ffc0492, Dec 23 2018, 23:09:28) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> # 用法特点:跳着取, 步⻓
... str3 = "python最⽜B"
>>> # 从第⼀个开始取, 取到第5个,每2个取1个
... print(str3[1:5:2])
yh
>>> # 从头开始到第五个. 每两个取⼀个
... print(str3[:5:2])
pto
>>>  # 从4开始取到最后. 每两个取⼀个
... print(str3[4::2])
o最B
>>>  # 从-5取到最后.每两个取⼀个
... print(str3[-5::2])
o最B
>>>  # -1:-5什么都没有. 因为是从左往右获取的.
... print(str3[-1:-5])

>>>  # 步⻓是-1. 这时就从右往左取值了
... print(str3[-1:-5:-1])
B⽜最n
>>>  # 从倒数第5个开始. 到最开始. 每3个取⼀个, 结果oy
... print(str3[-5::-3])
oy
>>>

☑步⻓: 如果是整数, 则从左往右取,如果是负数,则从右往左取;默认是1。


后续会接着熟悉了解字符串的操作方法,比如现在常用的字符串格式化format()等犯法。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面这段代码是什么意思?/* Maximum length of a string read from the Configuration file (/etc/cyusb.conf) for the library. */ #define MAX_CFG_LINE_LENGTH (120) /* Maximum length for a filename. */ #define MAX_FILEPATH_LENGTH (256) /* Maximum size of EZ-USB FX3 firmware binary. Limited by amount of RAM available. */ #define FX3_MAX_FW_SIZE (524288) static struct cydev cydev[MAXDEVICES]; /* List of devices of interest that are connected. / static int nid; / Number of Interesting Devices. */ static libusb_device *list; / libusb device list used by the cyusb library. */ /* struct VPD Used to store information about the devices of interest listed in /etc/cyusb.conf / struct VPD { unsigned short vid; / USB Vendor ID. / unsigned short pid; / USB Product ID. / char desc[MAX_STR_LEN]; / Device description. */ }; static struct VPD vpd[MAX_ID_PAIRS]; /* Known device database. / static int maxdevices; / Number of devices in the vpd database. / static unsigned int checksum = 0; / Checksum calculated on the Cypress firmware binary. */ /* The following variables are used by the cyusb_linux application. / char pidfile[MAX_FILEPATH_LENGTH]; / Full path to the PID file specified in /etc/cyusb.conf / char logfile[MAX_FILEPATH_LENGTH]; / Full path to the LOG file specified in /etc/cyusb.conf / int logfd; / File descriptor for the LOG file. / int pidfd; / File descriptor for the PID file. */ /* isempty: Check if the first L characters of the string buf are white-space characters. */ static bool isempty ( char *buf, int L) { bool flag = true; int i; for (i = 0; i < L; ++i ) { if ( (buf[i] != ' ') && ( buf[i] != '\t' ) ) { flag = false; break; } } return flag; }
最新发布
07-12

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值