for circulation and hex conversion

for loop

  1. The basic structure of a for loop

    for+空格+in+可迭代对象+冒号(#整型和布尔值不能进行循环)
    缩进   循环体
    for i in 'asdf':#i是变量名
        pass#pass和...是占位符
    print(i)#当在外围打印i时,值应该是for循环变量最后所获得的值
  2. Examples

    name="你好啊"
    for x in name:#将name中的字符循环依次赋值给x
       print(x)
    #结果为 
    #你
    #好
    #啊
    count=0#因为字符串的元素索引第一个是0,所以count赋值为0
    while count<len(name)#获取name字符串的长度进行判断
       print(name[count])#依次输出name中索引的值
        count+=
        #结果为:
        #你
        #好
        #啊
  3. The for loop is cyclic data structure:

    • String (str)
    • List (list)
    • Tuples (tuple)
    • Dictionary (dict)
    • Collection (set)

    Alone can not circulate is integer (int) and boolean value (bool)

Hexadecimal conversion

--- Digital integer (int)

  • For comparison and arithmetic

  • 32-bit range of -2 31 to 2 ** 32-1 **

  • Range of -2 ** 64 ** 63-1 63-2

  • Decimal Binary transfer calculation:

  • 15的二进制为
    15%2=7...1
    7%2=3....1
    3%2=1....1
    1%2=0....1
    最后结果为1111  
  • Converting into binary integer

  • print(bin(186)) 
  • Binary Coded Decimal Calculation Method:

  • 1111转十进制
    1*2**0+1*2**1+1*2**2+1*2*3
  • Binary Coded Decimal System:

  • print(int("1111",2))#2代表是几进制
  • bool () Boolean value

  • print(bool(1))#非零为True,零为False
    print(bool("123"))#空字符串为Flase,不为空字符串为True

format () format conversion

# 对齐方式:
print(format(122,">20")) 
print(format(122,"<20"))
print(format(122,"^20"))

# 进制转换:
将十进制转换成二进制
print(format(12,"b"))
print(format(12,"08b"))

将十进制转换成八进制
print(format(12,"o"))
print(format(12,"08o"))

将二进制转换成十进制
print(format(0b11001,"d"))

将十进制转换成十六进制
print(format(17,"x"))
print(format(17,"08x"))

Guess you like

Origin www.cnblogs.com/luckinlee/p/11619738.html