Python Notes Chapter 2

Original link:

arithmetic operation

/ + - * (results are all decimals,)
% (remainder) // (for quotient, round down)
** (exponentiation)

  • Arithmetic expressions with decimals, the result is a decimal (unless other conversions are made)

Arithmetic precedence

  1. **
  2. * / // %
  3. + –
    (multi-use () can replace priority)

Simultaneous assignment of arithmetic operations

+= Actual meaning: a+=s ie a=a+s
(mathematical symbols can not be used here)

relational operator

!= == > <>= <=
(can be used for string comparison)

Logical Operators and Expressions

Logical Operators

and or not three operation results True / False

  1. and
 n = 4 n >= 2 and n < 5 #>>Ture print(4 and True) #>>Ture
  • 0,””,[] are all equivalent to false (but not equal to false)
  • Non-zero numbers, non-empty strings and non-empty lists are equivalent to True
  • True can be regarded as 1, False can be regarded as 0
  1. or
  • As long as one is True, the result is True
  • False otherwise
  1. not
    In the operational relationship:
  • The value is True, the result is False
  • The value is False, the result is True

not > and > or

 print(3<4 or 4>5 and 1>2) #>>Ture

( Short-circuit evaluation: evaluation of a logical expression stops when the value of the entire expression has been determined.)

Precedence of various operators

  • Arithmetic operators + – / // % *
  • Relational operators < > == != >= <=
  • Logical operators and or not
    (use more parentheses if you don’t remember)

conditional branch statement

 if 逻辑表达式1:语句组1 elif 逻辑表达式2:语句组2 ···#可以有多个elif elif 逻辑表达式n:语句组n else:语句组n+1
 if 逻辑表达式1:语句组1 else (表达式2):语句组2
 if 逻辑表达式1:语句组1

(note the colon)

  • No spaces or tabs can precede program statements (except in an if while for statement)
  • The statement group in the if statement, the left side of each statement must be indented, and the format is the same

     if int(input()) ==5 print("a",end="") print("b") #>>b(没有对齐,出错)
 if 0 print(0) #>> 无输出
  • Nesting of if statements

     a =int(input()) if a >0: if a % 2: print("good") else: print("bad")


  • String slice (s[x:y] is the substring of s from subscript x to the character to the left of subscript y)

     a = "abcdef" print(a[2:-1]) #>> cde

(Be careful not to write if…else or if…elif…else as multiple parallel ifs)

output format control

Format Control Characters in Strings

  • %s means that a string is to be output here
  • %d means that an integer is to be output here
  • %f means that a decimal is to be output here
  • means that a small tree is to be output here, keep n digits after the decimal point, round up, five is not necessarily

     print("My name is %s,I am %.2fm tall."% ("tom",1.746)) #>> My name is tom,I am 1.75m tall.(操作失败)

Exercises in this chapter

 s = int (input("请输入一个数:")) if s % 2 == 0: print ("此数为偶数。") else : print ("此数为奇数。") #奇数偶数的判断
tangle = input().split() a,b,c= int(tangle[0]),int(tangle[1]),int(tangle[2])#注意该行写法if a+b>c and a+c>b and b+c>a: print("yes.") else: print("no.") #输入三个数,判断是否为三角形的三条边。
 s = input("请输入两个数字,一个符号:").split() n1,n2,c = int(s[0]),int(s[1]),s[2] if c in["+","-","*","/"]: if c =="+": print(n1+n2) elif c =="-": print(n1-n2) elif c =="*": print(n1*n2) elif c =="/": if n2 == 0: print("Divided by zero!") else : print(n1/n2) else : print("invalid operator!") #输入两个数字一个符号进行运算,验证通过#注意:符号要单独拿出来验证并计算,试搜寻可行计划将其放入数字之中进行运算#另一种方法: s = input("请输入两个数字,一个符号:").split() if s[2] not in ["+","-","*","/"]: print("invalid operator!") elif s[2] =="/" and int(s[1])==0: print("Divided by zero!") else: print(eval(s[0]+s[2]+s[1]))#此行既为上述搜寻的方法#此方法未将其用其他的字母代替

Byte Planet Forest Stack 2022-08-03 Without permission, reprinting is strictly prohibited!

This article is reprinted from:
This site is for inclusion only, and the copyright belongs to the original author.

Leave a Comment

Your email address will not be published.