The basic operation of pthon

Three ways formatted output

1, the placeholder

Procedure, there is often such a scenario: the user to enter information, and printing a fixed format

Such as requiring the user to enter a user name and age, and then print the following format:

my name is xxx,my age is xxx
age=19
name='lzs'
print("my name is "+name+"my age is"+str(age))
##  my name islzs my age is19

Upgraded version:

name='lzs'
age=19
print('my name is %s my age is %s' %(name,age))
##my name is lzs my name is 19
2, format format
name='lzs'
age=19
print("hello,{}.you are {}'.".format(name,age))
##helle,lzs.you are 19
3, f-String Format

Compared placeholder way, python3.6 version adds f-String formatted way, relatively simple to understand, it is recommended to use this way

name='lzs'
age=19
print(f'hello,{name}.you are {age}.')
## hello,lzs.you are 19.

Basic operators

1, arithmetic operators
x=10
y=10
res=x+y
print(res)
##  20
#有零整除,得到一个浮点型
print(10/3)
##3.3333333
##地板除,只取整数部分
print(10//3)
print(10//4)
##3
##2
##幂
print(10**3)
##1000
2, comparison operators
pwd=123
print(pwd!='123')
print(pwd=='123')
##false
##true
3, the assignment operator
age=19
age+=1
print(age)
##20
4, logical operators
##从左到右的方式找到逻辑运算符,找到逻辑运算符的左边,左边成立,再去找到逻辑运算符的右边
print(3>3 and 1>2 or 2>1)
##false
5, the identity of the operator

Means for storing the identity of the operator to compare two objects

and is the difference between == is caused determining whether two variables for the same objects (whether in the same memory space), and another value for determining a reference variable is equal

x=257
y=x
z=257
print(f'x is y:{x is y}')   ##true
print(f'x==y:{x==y}')       ##true
print(f'x is z:{x is z}')   ##false
print(f'x==z':{x==z})       ##true
6, python operator precedence

python priority mathematical operators corresponds to the calculated first and then count subtraction, multiplication and division, a higher priority in parentheses

Operators description
** Index (highest priority)
~=+- Bitwise inversion, Unary plus and minus (the last two named methods and @ + - @)
*/%// Multiply, divide, modulo, and rounded
+- Addition Subtraction
>> << Right, left operator
& Place '
^| Bitwise Operators
<=<>>= Comparison Operators
<>==!= Equality operator
=%=/=//=-=+='=' Assignment Operators
is is not Identity operator
is not in Member operator
not and or Logical Operators

if condition is determined

In fact, if the judge is to make a judgment in the simulation, equivalent meaning if, what if, then what action

1、if

if 条件:
     代码块
    #代码块(同一缩进级别的代码,几个代码组合在一起就是一个代码块)
score=90
if score==90:
    prin('成绩优秀')

2、if...else

if 条件:
    代码块
else:
    代码块
 ##if...else表示if成立代码成立会干什么,else不成立会干什么
score=60
if score==90:
    print('成绩优秀')
 else:
    print('成绩合格')

3、if...elif...else

if 条件:
    代码块
elif 条件:
    代码块
else:
    代码块
##if...elif...else表示if条件1成立干什么,elif条件2成立干什么....
score=55
if score>=90:
    print('成绩优秀')
elif score>=80
    print('成绩良好')
elif score<60:
    print('成绩不合格')

Guess you like

Origin www.cnblogs.com/lzss/p/11279131.html