Ter based learning Python & Python operator

Chapter III: Python basis of the & operator

3.1 Recap & supplements

  • Computer Basics

  • coding

    String: "China" "Hello"

    Characters: the e

    byte:

    unicode: in -> 4 bytes, e -> 4 bytes

    utf-8: in -> 3 bytes, e -> 1 byte

    Bit: 01101111--> 8 represents a byte

unicode: General calculation data memory for

utf-8: generally used for network transmission and data storage

  • Installation interpreter
    • py2
    • py3
  • grammar
    • print / input
    • Int int / string str / Boolean boolen
    • Conditional statements
    • and operator
    • variable
  • Exercise
# 练习题: 用户输入成绩,根据成绩的不同显示不同的级别,评分规则:A >=90; B >=80; C >=70; D 其他。

score = input("请输入成绩:")
score_int = int(score)   # 如果不使用int强制转换,执行会报错 TypeError: '>=' not supported between instances of 'str' and 'int'

if score_int >= 90:
    print("A")
elif score_int >= 80:
    print("B")
elif score_int >= 70:
    print("C")
else:
    print("D")
  • supplement

    # 10086示例
    message = """欢迎致电10086
    1、话费查询
    2、流量服务
    3、业务办理
    4、人工服务
    """
    print(message)
    
    index = input("请输入你要选择的业务:")
    index = int(index)
    if index == 1:
        print('话费查询')
    elif index == 2:
        print('流量服务')
    elif index == 3:
        content = """业务办理
        1、修改密码
        2、更改套餐
        3、停机
        """
        print(content)
        value = input('请输入要办理的业务编号:')
        value = int(value)
        if value == 1:
            print('修改密码')
        elif value == 2:
            print('更改套餐')
        elif value == 3:
            print('停机')
        else:
            print('错误')
    elif index == 4:
        print('人工服务')
    else:
        print('输入错误')
  • pycharm change Interpreter

3.2 Loops

1, the print cycle "Life is short, I used Python"

while True:   # 没有终止循环条件,这叫死循环
    print('人生苦短,我用Python')

2, while the conditions after adding

while 1 > 0:
    print('人生苦短,我用Python')
    
while 1 > 0 and 2 > 1:
    print('人生苦短,我用Python')

3, the digital sum

"""
count = 1
value = count + 1
print(value)
"""

"""
count = 1
count = count + 1
print(count)
"""

4, please circulation, count each cycle +1

count = 1
while True:
    print(count)
    count = count + 1

count = 1
while True:
    print(count)
    count = count + 2
# 练习
while True:   # 这个代码执行的结果就是永远打印1
    count = 1
    print(count)
    count = count + 1

5, please cycle, printing 1 2 3 ... 10

count = 1
while count <= 10:
    print(count)
    count = count + 1
print("结束")

6, please cycle, printing 1234568910

# 错误示例
count = 1
while count <= 10 and count != 7:
    print(count)
    count = count + 1
    
#正确
count = 1
while count <= 6:
    print(count)
    count = count + 1
count = 8
while count <= 10:
    print(count)
    count = count + 1

#正确
count = 1
while count <= 10:
    if count != 7:
        print(count)
    count = count + 1

#正确
count = 1
while count <= 10:
    if count == 7:
        pass
    else:
        print(count)
        count = count + 1

7, Keywords: break - is to terminate the current cycle

while True:
    print(666)
    break  # 终止当前循环
print('结束')

# 练习:

# 通过 break 实现打印 1-10
count = 1
while True:
    print(count)
    if count == 10:
        break
    count = count + 1
print("结束")

# break 是终止当前循环
while True:
    print('你好')
    while True:
        print(666)
        break
    break

8, keyword: continue

count = 1
while count <= 10:
    print(count)
    continue    # 本次循环如果遇到 continue,则不再继续往下走,而是回到 while 条件位置
    count = count + 1
#示例:1 2 3 4 5 6 8 9 10
count = 1
while count <= 10:
    if count == 7:
        continue
    print(count)   # 会打印1到6,但程序不会自动终止
    count = count + 1
    
    
count = 1
while count <= 10:
    if count == 7:
        count = count + 1
        continue
    print(count)   
    count = count + 1

​ 9、while else

count = 1
while count < 10:
    print(count)
    count = count + 1
else:      #不再满足 while 后面的条件时,触发。 或 条件 = False
    print("ELSE代码块")
print('结束')
count = 1
while True:
    print(count)
    if count == 10:
        break
    count = count + 1
else:      # 不再满足 while 后面的条件时,触发。 或 条件 = False
    print("ELSE代码块")  # 这段代码中这行不会执行,因为条件不会为 False 时,break 已经终止了当前循环 
print('结束')

10, while looping code sequence

11, Other

  • Quick Notes Ctrl +?
  • pycharm breakpoint (Debug Mode)

    Debug mode Code, Debug mode lets you see what code is executed every step done

12, summary

  • while the basic structure
  • break
  • continue
  • while else

3.3 Formatting string

1,% s placeholder

name = input('姓名:')
do = input('在干什么:')
template = "%s在教室,%s。" % (name,do,)
print(template)

2,% d placeholder

template = "我是%s, 年龄%s, 职业%s" % ("alex",78,"讲鸡汤")
print(template)

template = "我是%s, 年龄%d, 职业%s" % ("alex",78,"讲鸡汤")
print(template)

​ 3、%%

name = 'alex'
template = '%s现在手机的电量是100%%'%(name,)  # 如果是 100% 会报错
print(template)

4, an example of

name = input('请输入姓名:')
age = input('请输入年龄:')
job = input('请输入职业:')
hobby = input('请输入爱好:')
msg = """
------------ info od Alex ------------
Name  : %s
Age   : %s
Job   : %s
Hobby : %s
------------ end ------------"""

#print(msg%(name,age,job,hobby))

data = msg % (name,age,job,hobby)
print(data)

3.4 Operators

3.4.1 arithmetic

The following assumptions variable a = 2, b = 3

Operators description Examples
+ Add -> two objects are added a+b=5
- Save -> subtract two numbers a-b=-1
* By -> return multiplying two numbers or a string is repeated several times a*b=6
/ In addition -> x divided by y b/a=1.5
% Modulo -> Returns division remainder b%a=1
** Power -> return x to the power of y b**a=9
// Take divisible -> returns the integer part of commercially b // a = 1, if a = 1.0 2.0 3.0 @
  • Plus +
a = 10
b = 20
print(a + b)  # 结果为 30
  • Less -
a = 10
b = 20
print(a - b)  # 结果为 -10
  • Take *
a = 10
b = 20
print(a * b)  # 结果为 200
  • In addition to /
a = 10
b = 20
print(a / b)  # 结果为 0.2
  • % Modulus
value = 11 % 3
print(value)  # 结果为 2
  • power**
a = 10
b = 20
print(a ** b)  # 结果为 100000000000000000000
val = 2**8
print(val)   # 结果为 256
  • Take divisible //
val = 9/2
print(val)  # 结果为 4.5
c = 9
d = 2
print(c // d)   # 结果为4, 9.0//2.0输出结果为4.0

Exercise:

#练习题:打印 1 - 100 之间的奇数
count = 1
while count <= 100:
    val = count % 2
    if val == 1:
        print(count)
    count = count + 1
    
#练习题:打印 1 - 100 之间所有的数相加
total = 0
count = 1
while count <= 100:
    total = total + count
    count = count + 1
print(total)

#练习题:打印 1 - 2 + 3 -4 + 5...+ 99 - 100 的结果
total = 0
count = 1
while count <= 100:
    if count % 2 == 1:
    total = total + count
else:
    total = total - count
count = count + 1
print(total)

3.4.2 Compare operation

The following assumptions variables a = 10, b = 20

Operators description Examples
== Equal to -> whether the comparison is equal (A == b) returns False
!= Is not equal -> Compare objects are not equal (A! = B) returns True
> Greater than -> Returns whether x is greater than y (A> b) returns False
< Less than -> Back x is less than y, (A <b) Return True
>= Greater than or equal -> Returns whether x is greater than or equal to y (A> = b) returns False
<= Or less -> Back x is less than or equal to y (A <= b) returns True

3.4.3 Assignment Operators

The following assumptions variables a = 10, b = 20

Operators description Examples
= Simple assignment operator c = a + b a + b is the assignment of the operation result c
+= Addition assignment operator c + = a is equivalent to c = c + a
-= Subtraction assignment operator c- = a is equivalent to c = ca
*= Multiplication assignment operator equivalent to c * = a c = c * a
/= Division assignment operator c / = a is equivalent to c = c / a
%= Modulo assignment operator c% = a is equivalent to c = c% = a
**= Power assignment operator c ** = a is equivalent to c = c ** a
//= Assignment operator take divisible c // = a is equivalent to c = c // a

Exercise:

count = 1
while count <= 100:
    print(count)
    count += 1   # 等效于  count = count + 1

3.4.4 logic operation

Operators description Examples
and Boolean 'and' If a is False, a and b returns False, otherwise it returns calculated value of b (A and b) returns False
or Boolean 'or' if a is True, it returns True, otherwise it returns calculated value of b (A or b) Return True
not Boolean 'non' if a is True, it returns False, if a is False, it returns True not (a and b) Return True

1, using the general case:

if 1 > 0 and 1 < 2:
    print(666)
if 1 > 0 and 1 > 2:
    print(777)  
if 1 > 0 or 1 > 2:
    print(888)
if not 1 > 0 or 1 > 2:
    print(999)

2, under two general cases:

小知识:
    - int
    - str
    - bool

#数字转字符串
v1 = 666
v2 = str(v1)
print(v2)  # "666"

#字符串转数字
v1 = "666"
v2 = int(v1)
print(v2)  # 666

#数字转布尔值
v1 = 1
v2 = bool(v1)
print(v2)  # True
v1 = 0
v2 = bool(v1)
print(v2)  # False

#字符串转布尔值
v1 = "1"
v2 = bool(v1)
print(v2)  # True
v1 = "0"
v2 = bool(v1)
print(v2)  # True
v1 = " "
v2 = bool(v1)
print(v2)  # True
v1 = ""  # 表示空字符串
v2 = bool(v1)
print(v2)  # Flase

#布尔值转换其他
v1 = True
v2 = int(v1)
print(v2)  # 1
v1 = False
v2 = int(v1)
print(v2)  # 0
v1 = True
v2 = str(v1)
print(v2)  # True
v1 = False
v2 = str(v1)
print(v2)  # False

3, converted to remember:

  • String-to-digital
  • Digital to String
  • "" / 0 转换布尔值之后是 False

    4、经典面试题

    #对于 or :
    value = 1 or 9
    第一个值转换成布尔值如果是真,则 value = 第一个值。
    第一个值转换成布尔值如果是假,则 value = 第二个值。
    如果有多个 or 条件,则从左到右依次进行上述流程。
    示例:
    v1 = 0 or 1
    v2 = 8 or 10
    v3 = 0 or 9 or 8

    #示例:
    value = 0 or 9
    print(value)  # 结果为 9
    value = 0 or 0
    print(value)  # 结果为 0
    value = 0 or ""
    print('--->',value,'<---')  # 结果为 --->  <---
    value = 0 or 9 or 8
    print(value)  # 结果为 9
#对于 and :
如果第一个值转换成布尔值是True,则 value = 第二个值。
如果第一个值转换成布尔值是False,则 value = 第一个值。
如果有多个 and 条件,则从左到右依次进行上述流程。
#示例:
v1 = 1 and 9
print(v1)  # 结果为 9

v1 = 1 and 0
print(v1)  # 结果为 0

v1 = 0 and 7
print(v1)  # 结果为 0

v1 = 0 and ""
print(v1)  # 结果为 0

v1 = 1 and 0 and 9
print(v1)  # 结果为 0
#and 和 or 综合 : 先看 and 再看 or (前提是在没有括号的情况下)

#示例:
v1 = 1 and 9 or 0 and 6
print(v1)  # 结果为 9

​ 5、逻辑运算的优先级:

​ 在没有 () 的情况下 not 的优先级高于 and , and 的优先级高于 or ,即优先级关系为 ()>not>and>or,同一优先级从左往右计算。

3.4.5 成员运算

运算符 描述 示例
in 如果在指定的序列中找到值返回True,否则返回False x 在 y 的序列中,如果x在y的序列中返回True
not in 如果在指定的序列中没有找到值返回True,否则返回False x 不在 y 的序列中,如果x不在y的序列中返回True
  • in
value = "我是中国人"
#判断‘中国’是否在 value 所代指的字符串中。  或者说 ‘中国’是否是 value 所代指的字符串的子序列。
v1 = "中国" in value

#示例
content = input('请输入内容:')
if "退钱" in content:
    print('包含敏感字符!')
#示例
while True:
    content = input('请输入内容:')
    if "退钱" in content:
        print('包含敏感字符')
    else:
        print(content)
        break
  • not in
value = "我是中国人"
#判断‘北京’是否不在 value 所代指的字符串中。  或者说 ‘北京’是否不是 value 所代指的字符串的子序列。
v1 = "北京" not in value

3.4.6 身份运算

运算符 描述 示例
is is 是判断两个标识符是不是引用自一个对象 x is y,如果 id(x) 等于 id(y),is 返回结果 1
is not is not 是判断两个标识符是不是引用自不同对象 x is not y,如果 id(x) 不等于 id(y),is not 返回结果 1
a = 20
b = 20

print(id(a)) # 打印对象id值
print(id(b))

if a is b:
    print('1 - a 和 b 有相同的标识')
else:
    print('1 - a 和 b 没有相同的标识')  
    
if a is not b:
    print('2 - a 和 b 没有相同的标识')
else:
    print('2 - a 和 b 有相同的标识')
    
#修改 b 的变量
b = 30
if a is b:
    print('3 - a 和 b 有相同的标识')
else:
    print('3 - a 和 b 没有相同的标识')

if a is not b:
    print('4 - a 和 b 没有相同的标识')
else:
    print('4 - a 和 b 有相同的标识')

以上示例输出结果:
1543007904
1543007904
1 - a 和 b 有相同的标识
2 - a 和 b 有相同的标识
3 - a 和 b 没有相同的标识
4 - a 和 b 没有相同的标识
#is 和 == 的区别 后面学到列表时会学习
#is 用于判断两个变量引用对象是否为同一个(同一块内存空间), == 用于判断引用变量的值是否相等。

a = [1,2,3]
b = a
print(b is a)  # True
print(b == a)  # True
b = a[:]
print(b is a)  # False
print(b == a)  # True

3.4.7 位运算

运算符 描述 示例
& 按位与运算符 (a & b) 输出结果 12,二进制解释: 0000 1100
| 按位或运算符 (a | b) 输出结果 61,二进制解释: 0011 1101
^ 按位异或运算符 (a ^ b) 输出结果 49,二进制解释: 0011 0001
~ 按位取反运算符 (~a)输出结果 -61, 二进制解释: 1100 0011 ,在一个有符号二进制数的补码形式
<< 左移动运算符 a << 2 输出结果 240,二进制解释:1111 0000
>> 右移动运算符 a >> 2 输出结果 15,二进制解释: 0000 1111
a = 60  # 60 = 0011 1100
b = 13  # 13 = 0000 1101
c = 0
c = a & b;  # 12 = 0000 1100
print("1 - c 的值为: %s"% c)
c = a | b;  # 61 = 0011 1101
print("2 - c 的值为: %s"% c)
c = a ^ b;  # 49 = 0011 0001
print("3 - c 的值为: %s"% c)
c = ~a;  # -61 = 1100 0011
print("4 - c 的值为: %s"% c)
c = a << 2;  # 240 = 1111 0000
print("5 - c 的值为: %s"% c)
c = a >> 2;  # 15 = 0000 1111
print("6 - c 的值为: %s"% c)

以上示例输出结果:
1 - c 的值为: 12
2 - c 的值为: 61
3 - c 的值为: 49
4 - c 的值为: -61
5 - c 的值为: 240
6 - c 的值为: 15

3.4.8 运算符优先级

运算符 描述
** 指数 (最高优先级)
~ + - 按位翻转, 一元加号和减号 (最后两个的方法名为 +@ 和 -@)
* / % // 乘,除,取模和取整除
+ - 加法减法
>> << 右移,左移运算符
& 位 'AND'
^ | 位运算符
<= < > >= 比较运算符
<> == != 等于运算符
= %= /= //= -= += *= **= 赋值运算符
is is not 身份运算符
in not in 成员运算符
not and or 逻辑运算符

3.5 编码补充

  • 编码扩展
    • ascii

    • unicode

      • ecs2 — Unicode 通过2个字节来表示,这样表示的内容不够多
      • ecs4 — Unicode 通过4个字节来表示,现在基本上都是这种
    • utf-8 — 全球通用,中文用3字节。

    • utf-16

      在Windows系统使用记事本写完数据后,点击文件-另存为,弹框中点击编码后的选择按钮,有个Unicode的选项,这个指的就是utf-16,因为Unicode不能写在硬盘上。

    • gbk2312 ,中文用2字节。

      由中华人民共和国政府制定的,简体汉字编码规范,大陆所有计算机中的简体中文,都使用此种编码格式。

    • gbk ,中文用2字节。

      gbk2312的升级版,又称GBK大字符集,简而言之就是将所有亚洲文字的双字节字符,包括简体中文,繁体中文,日语,韩语等 。

  • 单位转化
8bit = 1byte
1024byte = 1kb
1024kb = 1MB
1024MB = 1GB
1024GB = 1TB
1024TB = 1PB
1024PB = 1EB
1024EB = 1ZB
1024ZB = 1YB
1024YB = 1NB
1024NB = 1DB
常用到 TB 就够了

有不足之处欢迎各位评论指正!

Guess you like

Origin www.cnblogs.com/duncan1863/p/11421836.html