Share knowledge of Python fifth day

Section python

1. Integer and Boolean values

1.1 hex conversion

Binary Decimal ----

Binary Decimal ----

The method of calculating ordinary 8421

Decimal Binary turn python Example: bin(51)>>>0b110011

Example Binary Decimal in python: int(0b110011)>>>51

1.2 Boolean value

Only numbers 0 is False, the rest are True

String as long as the content is True, no content is False

Examples

print(bool(""))
print(int(True))
print(int(False))

2. Detailed strings

Strings are ordered, immutable

2.1 Index (accurately and quickly find values)
s = "zhan"
print(s[0])
print(s[-1])

2.2 slice (cut without cutting head trailing)

Gets an interval

s = "zhan_learn_in_oldboy"
print(s[0:10]) # [起始索引:终止索引]
print(s[-6:])    # [起始索引:默认获取字符串末尾的内容]
print(s[11:20])
print(s[:])      # [默认获取字符串开始的内容:默认获取字符串末尾的内容]

对比:
s = "alex_baoyuan|oldboy"
索引
print(s[20])  #报错: string index out of range
切片
print(s[1:100]) # 切片超出索引值不会报错

Steps

Step can decide to find direction

It is a positive step from left to right to find

Step right to left to find negative

s = "zhan_learn_in _oldboy"
print(s[0:3])  #步长默认为1
print(s[0:3:2])  #步长为2

Palindrome: Shanghai water from the sea

Example:

user_input = input("请输入你认为的回文:")
if user_input == user_input[::-1]:
    print("你输入的是回文")
else:
    print("你输入的不是回文")

3. The method of string

s="zhan_learn   "
print(s.strip())
print(s.split("_"))
print(s.replace("_","-"))
print(s.upper())
print(s.lower())
print(s.startswith("zh"))
print(s.endswith("sg"))
print(s.count("n"))


# is系列
s = "123你好啊aaaa"
print(s.isalpha())    # 判断是不是由字母,中文组成 -- 返回的是布尔值 ***
print(s.isdigit())    # 判断是否是数字 -- bug
print(s.isdecimal())  # 判断是否是十进制的数     ****
print(s.isalnum())      # 判断是不是字母,数字,汉字

4.for cycle

basic structure

for i in 可迭代对象:
    循环体

Example:

s = "zhan"
for i in s:
    print(i)

Interview questions

问:打印输出的结果
s = '123'
for i in s:
    pass
print(i)

问:打印输出的结果
s = "12"
for i in s:
    print(s)

Guess you like

Origin www.cnblogs.com/tianming66/p/11706962.html