From entry to prison-numbers and lists

Getting started four day

Types related to numbers in python: int (integer), float (floating point), bool (boolean), complex (complex)

1.int integer type: Integer in
python supports 4 ways of writing: decimal number, binary number, octal number, hexadecimal
decimal number: when the base is 0-9, write directly: 1, 2, 3, 4, 5,6...
Binary number: base 0 and 1, add prefix 0b/0B:0b1010, 0B1010 when expressing
Octal number: base 0~7, add prefix 0o/0O:0o623,0O623 when expressing
: The base is 0 9, a f(A~F), add 0x/0X when expressing: 0xff, 0X456

If you want to get a copy of the equivalent decimal number corresponding to other hexadecimal numbers, print it directly with print

print(0X456)
 #bin(数字) 将其他进制数转换为二进制
print(bin(0o123))  # 0b1010011
print(bin(20))  # 0b1010
#oct(数字) 将其他进制数转换为八进制
print(oct(20))  # 0o24
print(oct(0b1010))  # 0o12
# hex(数字) 将其他进制数转换为十六进制
print(hex(255))  # 0xff
print(hex(0b1010))  # 0xa

2. The type corresponding to all decimals of float floating point
supports scientific notation: xey means x is multiplied by 10 to the power of y
1.27e3, 1.27e-3
computers cannot store absolutely equal values ​​when storing floating-point numbers

print(1.27e3)

3. bool (Boolean type)
The essence of bool type is a special integer type, where True is 1, and False is 0
'''

print(1 + True)  # 2
print(1 + False)  # 1

'''
4.complex (complex number type)
complex number: the number consisting of real and imaginary parts is a complex number. The unit of imaginary number in mathematics is i, and the unit of imaginary number in python is j: A+Bj
Note: If the imaginary part is 1,1 Can not save
the complex numbers in python to directly support mathematical complex number operations
'''

# 复数的加法运算,实部加实部,虚部加虚部
num1=5+7j
num2=1+6j
print(num1+num2)
# 复数乘法运算 j*j=-1
print(num1*num2)

[] List: container data type (sequence), use [] as the symbol of the container, and multiple elements in it are separated by commas
[1,2,'wadwa',13.5,'a'] The
list is variable: container The number and value of the elements are variable. The
list is ordered: the elements are ordered in the container.
Any type of data can be used as the elements of the
list. List naming convention: the plural form of English words, or after _list

lists = []  # 表示空列表
list1 = [1, 2, 3, [4, 5], 0o12345]
list1[3], list1[0] = list1[0], list1[3]

print(list1)

Additions, deletions, and changes to list elements

Check: Get a single element
Syntax: list [index] - Gets element corresponding to the specified index
index: Index known, is an element in the position corresponding to the reference list information, each element corresponding to subscript two
one kind It starts from 0 and increases in sequence (0 means the first one, 1 means the second...)
one starts from -1 and decreases in sequence (-1 means the first one, -2 means the second one...)

movies = ['八佰', '花木兰', '信条', '星际穿越', '熊出没', '肖申克的救赎']

print(movies[0])  # ’八佰‘
print(movies[-1])  # ’肖申克的救赎‘

#print(movies[10])  越界会报错 IndexError:list index out of range(下标错误)

Traverse: Take out all the elements in the list one by one,
traverse directly to get the elements

for variable in list:
loop body,
each time the loop variable gets the elements in the list

print('........遍历1.........')
for i in movies:
    print(i)
'''
遍历同时获取列表元素和下标
enumerate
for 变量1,变量2 in enumerate(列表):
    循环体
变量1 获取到的是每个元素的下标,变量2获取到的是每个元素
'''
print('........遍历2.........')

for i, j in enumerate(movies):
    print(i, j)

#通过遍历元素中的下标来间接地遍历每个元素

print('.........遍历3........')
for i in range(0, 6, 2):
    print(movies[i])

Practice for total score

scores = [78, 67, 52, 78, 99, 23]
a = 0
for i in scores:
    a += i
print(a)

for i in scores:
    b=scores.pop()
    print(b)

Increase: add elements

List.append (element): add the specified element at the end of the specified list

hero_list = ['鲁班7号', '亚索', '卢锡安']
hero_list.append('瑞兹')
print(hero_list)  # ['鲁班7号', '亚索', '卢锡安', '瑞兹']

List.insert (subscript, element)-insert the specified element in the specified subscript of the list

hero_list = ['鲁班7号', '亚索', '卢锡安','瑞兹']
hero_list.insert(1,['盲僧','1'])
print(hero_list)

delete

del list[subscript] delete the element corresponding to the specified subscript in the list

masters = ['貂蝉','小乔','甄姬','王昭君','周瑜','小乔']

del masters[0]
    
print(masters)

List.remove(element)-delete the specified element, if there are multiple elements to be deleted, only delete the first one.
If there is no element to be deleted, an error will be reported

masters = ['貂蝉','小乔','甄姬','王昭君','周瑜','小乔']
masters.remove('小乔')
print(masters)

List.pop() takes out the last element in the
list List.pop() takes out the element corresponding to the subscript in the list The
element still exists

masters = ['貂蝉','小乔','甄姬','王昭君','周瑜','小乔']
x=masters.pop()
print(masters,x)

x=masters.pop(-2)
print(masters,x)


#改  修改元素的值
'''
列表[下标]=新值  将列表指定下标对应的元素修改成新值
'''



teleplays=['人民的名义','甄嬛传','纸牌屋','绝命毒师']
print(teleplays)
teleplays[0]='三十而已'
print(teleplays)


Guess you like

Origin blog.csdn.net/weixin_44628421/article/details/108801939