Data type, the user interaction, formatted output

Fancy assignment

Chain assignment

>>> a = b =c =10
>>> a
10
>>> a
10
>>> c
10
>>> b
10
>>>

Cross assignment

>>> x = 10
>>> y = 20
>>> x, y = y, x
>>> x
20
>>> y
10
>>>

List

effect

Storing a plurality of elements that can describe a person's hobby

Defined way

A plurality of elements separated by commas [], the element can be any data type

lt1 = [1,2,3,4,5]

s_list = list('abcdef')
print(s_list)   # ['a', 'b', 'c', 'd', 'e', 'f']

Instructions

Index values

lt = [1,2,3,'neo',[666,120]]
print(lt[1])   # 2
print(lt[3])   # neo
print(lt[4][0])  # 666

dictionary

effect

Storing a plurality of values, each value but information are described

Defined way

A plurality of spaced elements in {} comma key stored data, Key (string): value (can be any data type)

dic = {'name':'cwz', 'age': 20}

Instructions

No index dictionary

dic = {'name':'cwz', 'age': 20}
print(dic['name'])   # cwz

Boolean

effect

Conditions for determining a result, only two Boolean values, True / False

definition

True, False usually not directly quoted, required logic operation result obtained

Instructions

Conditions are satisfied is True, not true to False

print(1 > 2)   # False
print(1 < 2)   # True

== carrying all data types Boolean, except None / 0 / empty string / empty list / empty dictionary / False Boolean value is False, the remainder to True. ==

unzip

Unpack only 2-3 elements for container type

a, b, c = [1, 2, 3]
print(a, b, c)     # 1 2 3

# 单个下划线表示这个东西不需要
a, b, _ = [1, 2, 3]
print(a, b)   # 1 2

# *表示把多出来的元素合成放到列表中
x, *y, z = [1, 2, 3, 4]
print(x, y, z)   #   1 [2, 3] 4

User interaction with python

print('你好啊!')
s = input()   # 让程序暂停,用户输入什么,就会输出什么,输出的永远是字符串
print(s)
print('--------------------')


# 你好啊!
  2
  2
  --------------------

== no matter what type of data, input values ​​of the input is received string ==

Output formatting three forms

f- string

With such special significance {f}

s1 = 'name'
s2 = 'cwz'
print(f'{s1} {s2}')   # name cwz

print(f'{20:.2f}')   # 20.00

Placeholder

print(('my name is %s') % ('cwz'))  # my name is cwz

format

name = 'neo'
age = 19
print('{} {}'.format(name, age))  # neo 19

Guess you like

Origin www.cnblogs.com/setcreed/p/11498173.html