Finishing day 004 notes

Fancy assignment

Chain assignment

a = b = c = 10
print(a, b, c)

Cross assignment

x = 10
y = 20
x,y = y,x
print(x, y)

List list

1, the role:

A plurality of storage elements, can be of any data type

2. Definition:

A plurality of spaced elements (of any data type) with a comma in []
list1 = [ 'read', ' run', 'music']

3, Usage:

Index
print (list1 [0]) # print result is: Read
Print (List1 [. 1]) # printing results: RUN
Print (List1 [2]) # print result as: music

Dictionary dict

1, the role of

    存储多个值,且每个值都有描述信息

2, the definition of

{} With the plurality comma "key (described, string): the value (a specific value, may be any type of data) pair." Bond quotes wrap, if the value of the string also quotes wrap.
dic = { 'name': ' allen', 'weight': 120, 'height': 160, 'hobby': [ 'read', 'run', 'music']}

3, use

​ 按key取值
​ print( dic['name'])
​ print( dic['hobby'] [0])
​ print( dic['hobby'] [1])
​ print( dic['hobby'] [2])

​ students = [{'name':'allen', 'age':20},{'name':'tony', 'age': 22}]
​ allen_info = students[0]
​ print(allen_info['name'])
​ print(allen_info['age'])

Boolean type bool

1, the role:

Used to determine the condition result, conditions are satisfied, compared True, conditions are not satisfied, False

2. Definition:

True, False generally not directly referenced, generally obtained through the logic judgment result is True or False if necessary

3, Usage:

print (type (True)) # print results are shown as: <class 'BOOL'>
Print (BOOL (0)) # print results are shown as: False
Print (BOOL ( 'Allen')) is displayed as the print result # : True
Print (. 1> 2) results are shown as printed #: False
Print (. 1 <2) # print result shown as: True

Note: Python values of all data types comes Boolean value. Only 0, None, an empty, False four Boolean value is False, the rest to True.

unzip

From the list of a plurality of elements required in several extraction, are list elements extracted
several dictionaries from a plurality of elements required for removing, key is removed

name_list = ['allen', 'Jim', 'jason','Tom','John']
a = name_list[0]
b = name_list[1]
c = name_list[2]
print(f'a:{a}, b:{b}, c:{c}')

Results: x: allen, y: Jim, z: jason

We value the above process can be simplified as:

name_list = ['allen', 'Jim', 'jason','Tom','John']
a,b,c,d,e  = name_list
print(f'a:{a}, b:{b}, c:{c}, d:{d}, e:{e}')

Result: a: allen, b: Jim , c: jason, d: Tom, e: John
just take a few values, we may operate as follows:

name_list = ['allen', 'Jim', 'jason','Tom','John']
a,b,_,_,e  = name_list
print(f'a:{a}, b:{b},  e:{e}')

Result: a: allen, b: Jim, e: John

You can remove unwanted elements, but can not use an underscore underscore the beginning and end
when more elements, we can proceed as follows:

name_list = ['allen', 'Jim', 'jason','Tom','John']
a,*_,e  = name_list
print(f'a:{a}, e:{e}')

Result: a: allen, e: John

User interaction with python

Application is to give people with, then we need to consider making it easier to use (not used will die)
such as user input is required information, then we must consider the format of the information entered by the user a variety of possible irregularities situation, good interaction is automatically corrected as much as possible non-standard situations.

Three ways formatted output

A placeholder

Placeholder,% s (for all data types),% d (only for the number types)

name = 'nick'
age = 19
print('my name is %s my age is %s' % (name, age))

The result is: my name is nick my age is 19

Two, format format

name = 'allen'
age = 30
print("Hello, {}. You are {}.".format(name, age))

Results: Hello, allen You are 30..

Three, f -String formatted

f -String new method is formatted version of python3.6

name = "allen"
age = 30
print(f"Hello, {name}. You are {age}.")   #  此处用大写F也可以

Results: Hello, allen You are 30..

salary = 3.333333
print(f'{salary:.2f}')

The results are: 3.33

Guess you like

Origin www.cnblogs.com/allenchen168/p/11498308.html