User interaction, operator
First, the interaction with the user
- User interaction is the person to the computer input / input data, the computer print / output (essentially the input, output)
- python2 you must declare the type of input
- python3 accept user input, no matter what type of user input, the final return must be a string
pyton3
input('name:')
name:Yang
'Yang'
python2
raw_input('name:')
name:Yang
'Yang'
- python2 in raw_input and python3 the input function is exactly the same
Second, formatted output
Placeholder 1.% s: you can receive any type of variable
% D placeholder: receiving a digital only type
Example 1:
print('亲爱的%s您好!您%s月的话费是%d,余额还有%d' %('Tony',11,83,1))
亲爱的Tony您还!您11月的话费是83,余额还有1
Example 2:
>>> name = 'yang'
>>> age = 22
>>> hobby = 'play, money'
>>> print('my name is %s, my age is %d,my hobby is %s' %(name, age, hobby))
my name is yang, my age is 22,my hobby is play, money
Two decimal places: '.% 2f'
>>> a = 3.1415926
>>> print('%.2f' %a)
3.14
2. .format
username = 'Yang'
ages = 22
print('name:{user},age:{age}'.format(user=username,age=ages))
name:Yang,age:22
3.f-string
name = 'chen'
age = 18
hobby = 'yang'
print(f'姓名:{name},年龄:{age},爱好:{hobby}')
姓名:chen,年龄:18,爱好:yang
Third, the operator
- Arithmetic Operators
- Comparison Operators
Assignment Operators
Incremental assignment x = 10 x + = 1 (x = x + 1)
Chain assignment x = y = z = 10
CROSS assignment m = 1, n = 2 m, n = n, m
Decompression assignment
>>> L1=[1,2,3,4,5] >>> a,b,c,d,e=L1 >>> a,b,c,d,e (1, 2, 3, 4, 5) #如果变量名少了或者多了,都会报错 >>> a,b,*_=L1 #取头尾的几个值,可以用*_ >>> a,b (1, 2)
Logical Operators
and
or
not
Member operator
in
not in
>>> 'hello' in 'hello world!'
True
>>> 'hello' not in 'hello world!'
False
- Identity operator
>>> x=y=10
>>> id(x)
1737518848
>>> id(y)
1737518848
>>> x is y
True
>>> x is not y
False