python_format格式化输出、while else、逻辑运算符、编码初识

1、格式化输出 。%d  %s

 格式化输出:% 占位符,d 表示替换整型数,s表示要替换字符串。
name = input('请输入名字:')
age = input('请输入年龄:')
sex = input('请输入性别:')
msg = '我的名字是' + name + '我的年龄是' + age + '我的性别是' + sex
print(msg)
msg = '''
------------ info of Alex Li -----------
Name  : Alex Li
Age   : 22
job   : Teacher
Hobbie: girl
------------- end -----------------
'''
print(msg)

d 表示替换整型数,s表示要替换字符串。

name = input("请输入姓名:")
age = int(input("请输入年龄:"))
job = input("请输入工作:")
hobby = input("请输入爱好:")
msg = '''
------------ info of %s -----------
Name  : %s
Age   : %d
job   : %s
Hobbie: %s
------------- end -----------------
''' % (name, name, age, job, hobby)
print(msg)
dic = {
    'name': '老男孩',
    'age': 58,
    'job': 'boss',
    'hobby': 'money',
}
msg = '''
------------ info of %(name)s -----------
Name  : %(name)s
Age   : %(age)d
job   : %(job)s
Hobbie: %(hobby)s
------------- end -----------------
''' % dic
print(msg)

在格式化输出中,需要表示单纯的百分号%,要用双百分号%%表示。

msg = '我叫%s,今年%d,学习进度2%%.' % ('爽妹儿',18)
print(msg)

2、while else

while    else 中,当while的循环被break打断时,不走else程序。

count = 0
while count <= 5:
    count += 1
    print('loop',count)
    if count == 4:
        break
else:
    print('循环正常执行完啦')
print("-----out of while loop ------")

3、运算符

1、逻辑符前后都是比较运算

优先级概念:

  () > not > and > or

  同一优先级从左到右以此计算。

print(2 > 1 and 3 < 4 or 8 < 10 and 4 > 5)
print(2 > 1 and 3 < 4 or 4 > 5 and 2 < 1)
print(1 > 2 and 3 < 4 or 4 > 5 and 2 > 1 or 9 < 8)
print(1 > 1 and 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6)

2、逻辑符前后都是数字

  or

  x or y , if x Ture,Return x , else Return y .

  x !=0,x is Ture .

print(3 or 5)
print(2 or 5)
print(0 or 5)
print(-4 or 5)
print(1 or 3 or 0)
print(1 or 3 or 4 or 0)

and

  and 的规则与or完全相反。

print(3 and 5)
print(1 > 2 and 3 or 4)

数字与bool值转化  

int ---> bool  非零 True ,零 False
bool---> int True 1, False 0
print(bool(100))
 
print(bool(0))

4、编码初识

 谍战片:滴滴滴 滴滴 高低电平,0101010

  电脑文件的存储,与文件的传输。010101010

  初级密码本:

  ASCII码  字母、数字、特殊符号。

  0000 0001   8位 == 一个字节,一个字节表示一个字符。

  字符:组成内容的最小单元。

    abc a b c

    中国 中 国

  万国码:unicode

  创建初期 16位,两个字节表示一个字符。

    a :01100001 01100001

    中:01100011 01100001

  升级后 32位,四个字节表示一个字符。

    a :01100001 01100001 01100001 01100001

    中:01100011 01100001 01100011 01100001

  浪费资源。(硬盘,流量等)

  对Unicode升级:utf-8

  utf-8:最少用8位数表示一个字符。

    a:01100001(字母用1个字节表示。)

    欧洲文字:01100001 01100001(欧洲用2个字节表示。)

    亚洲文字——中:01100001 01100001 01100001 (欧洲用3个字节表示。)

  utf-16:最少用16位表示一个字符。

  gbk:国家标准。

    a : 01100001

    中: 01100001 01100001

  8位组成一个byte

  1024bytes  1kb

  1024kb  1MB

  1024MB  1GB

  1024GB  1TB

 

原文地址:https://www.cnblogs.com/ZN-225/p/9587973.html