python basic data types and the initial application

1. Integer: int - Calculation and Comparison
2--10 push Bit
8421 2 0 2 . 1 - 2. 7 **
10 - 2
BIT_LENGTH binary bits occupied effective

# 123  # 计算和比较

# 14  0  # 十进制
# 7   1
# 3   1
# 1   1

# 128 64 32 16 8 4 2 1
# print(2**0)  1
# print(2**1)  1
# print(2**2)  1
# print(2**3)  1
# print(2**4)  1
# print(2**5)  1
# print(2**6)  1
# print(2**7)  1

# num = 7   # 8421   00000111
# print(num.bit_length())  # 求十进制数转换成二进制时所占用的位数

2. Boolean:
Digital converted into Boolean values:

字符串转换成布尔值:
    空字符串 是False
    非空字符串 是True

布尔值 转换成 数字, 字符串
数字 True - 1  False - 0
字符串类型
# bool # 布尔值 -- 用于条件使用
# True  真
# False 假

# print(bool(-10))       # 0 是 False             非0的都是True
# print(bool(" "))       # 空的字符串是 False      非空的就时True
# print(type(str(True))) 布尔值可以转换成字符串
# print(int(False))      # True 转化成数字 1   False 转换成数字 0

3. String
effect: small amounts of data stored in
the index: 0,1,2,3 left to right, right to left, -1, -2, -3 for out of range being given
slice: [Starting position: ending position] care regardless of the end of time to find nothing out of range
step: Find the direction of the decision, decided to find the step size defaults to 1 by default direction is from left to right

字符串方法:
    全部大写  upper
    全部小写  lower
    以什么开头 startswith
    以什么结尾 endswith
    去头尾空格 -- 默认去掉空格和换行符  strip
    分割  默认是空格分割,自己制定      split
    替换  replace  第一个参数是旧的 第二个参数是新的
    统计  count   统计某个内容出现的次数
    第三种字符串格式化  format() 三种使用方式
 
    is 系列
    isdecimal  判断的是不是十进制
    isalnum    判断的是不是中文,字母,数字
    isalpha    判断的是不是中文,字母
    
字符串,列表,元组  --  都是有索引(下标)
#     索引是准确的定位某个元素
#     从左向右 0,1,2,3
#     从右向左 -1,-2,-3,-4
#     支持索引的都支持切片   [索引]
#     切片长度可以超出范围,索引长度不能超出范围  [起始位置:终止位置]
#     步长: 决定要走的方向,决定走的步子多大  [起始位置:终止位置:步长]
#     # 起始位置 + 步长

# 以什么开头
# name = "alex"
# print(name.startswith('a'))  # 就是以a开的头

# 以什么结尾
# name = "zhuxiaodidi"
# print(name.endswith("i"))   # 就是以i结尾

# count # 统计
# name = "zhudidi"
# print(name.count("zhu"))   # 查询某个内容出现的次数

# 替换  ****
# name = "alexnbnsnsn"
# name1 = name.replace('n','s')  # 替换
# name1 = name.replace('n','s',2)  # 替换  2是替换的次数
# print(name1)

# 除去头尾两边的空格  脱   *****
# name = " alex   "
# name1 = name.strip()  # 可以写想要去掉的内容
# print(name1)
# if name == "alex":
#     print(666)

# name = "   alex     "
# print(name.strip())

# 分割    *****
# name = 'alex,wusir'
# print(name.split("w"))
# 默认是以空格分割 ,也可以自己制定分割
# 分割后返回的内容是一个列表

# 第三种字符串格式化
# name = "alex{}wusir{}"
# name1 = name.format('结婚了',"要结婚了")  # 按照位置顺序去填充的

# name = "alex{1}wusir{0}"
# name1 = name.format('结婚了',"要结婚了")    # 按照索引位置去填充

# name = "alex{a}wusir{b}"
# name1 = name.format(a="结婚了",b="马上结婚")  # 指名道姓 填充
# print(name1)

# is 系列  -- 判断

# name = "②"
# print(name.isdigit())  # 判断是不是阿拉伯数字

# name = "666"
# print(name.isdecimal()) # 判断是不是十进制  -- 用它来判断是不是数字

# name = "alexs你好"
# print(name.isalpha())    # 判断的是中文和字母

4.for cycle
for the keyword
variable i (artificially modified)
in the keyword
s iterables not include (int, bool)
face questions: after the variable i points to the last cycle of an element
placeholder: pass ...

range()  # 范围
python3打印是它自己本身
python2打印是列表

range(起始位置,终止位置,步长)
range(10)  -- range(终止位置)  起始位置0
print(len(s))  # 公用的  工厂函数 -- 所有类型都能用  他俩除外int -  bool
 count = 0
while count < len(s):
     print(s[count])  # s[0] s[1] s[2]
     count += 1

 for 循环

 for i in s:
     print(i)

 for 关键字
 i 变量
 in 关键字
 s 可迭代对象  int - bool


# for a in "alex":
#     print(a)
#     # pass  # 过 -- 占位符
#     # ...   # 过
# print(a)
# s = range(1,10)  # 面试大坑  python2 和 python3
# print(s)  # 范围

# for i in  range(1,10,2): # (起始位置,终止位置,步长)
#     # range(10) 指定了终止位置,起始位置默认是0
#     print(i)

# for i in range(100,-11,-1):
#     print(i)
倒序循环
# for i in range(1):
#     print(i)

Guess you like

Origin www.cnblogs.com/ayongxin93/p/11041168.html