2020_9_25_ numbers and lists

Ternary Operator (Ternary Operator)

  1. Ternary operator in C language:
    expression 1? expression 2: expression 3-judge whether the value of expression 1 is True, if the whole operation result is the result of expression 2, otherwise the operation result is the result of expression 3
    result = age>=18?'Adult':'Minor'

  2. Ternary operator in python
    expression 2 if expression 1 else expression 3-judge whether the value of expression 1 is True, if the whole operation result is the result of expression 2, otherwise the operation result is the result of expression 3
    result = 'Adult' if age>=18 else'Underage'

age = 3
result ='adult' if age >= 18 else'minor'
print(result)

age = 3
if age >= 18:
result ='adult'
else:
result ='minor'
print(result)

Number related types

Types related to numbers in python: int (integer), float (floating point), bool (boolean), complex (complex)
1. int (integer)-the type corresponding to all integers

Integer in python supports 4 ways of writing: decimal number, binary number, octal number and hexadecimal number.
Decimal number: base number is 0~9, directly write: 98, 123, 1567
binary number: base number is 0 and 1. , Add prefix 0b/0B: 0b1100, 0B1101
octal number: base 0~7, add prefix 0o/0O: 0o625, 0o127
hexadecimal number: base is 0 9, a f(A~ F), add the prefix 0x/0X when indicating: 0x12, 0x3f, 0xabc

num1 = 1923
num2 = 0b1011
num3 = 0o625
num4 = 0x23
print(type(num1), type(num2), type(num3), type(num4))

1) If python wants to get the equivalent decimal number corresponding to another hexadecimal number, it can be printed directly with
print (num1, num2, num3, num4) # 1923 11 405 687

2)bin(number)
-convert other numbers into binary form print(bin(100)) # 0b1100100
print(bin(0o56)) # 0b101110

3) oct (number)
-convert other numbers into octal form print(oct(100)) # 0o144

4)hex(number)
-convert other hexadecimal numbers into hexadecimal forms print(hex(100)) # 0x64
print(hex(0o5263)) # 0xab3
print(hex(255)) # 0xff

num3 = 0b1021 # Report an error! Any number other than 0 and 1 cannot appear in a binary number

2. Float (floating point)-the type corresponding to all decimals
1) Support scientific and technical methods: xey-x multiplied by 10 to the power of y
2) The computer cannot store absolutely equal values ​​when storing floating point numbers

num5 = 1.23e7
num6 = 3.25e-4
1.92 - 1.919999999999999999
print(1.23 == 1.23)
print(23 == 45)

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

print(1+True) # 2
print(1+False, False*23) # 1 0

4. complex (complex number)
""" A number
composed 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, this 1 cannot be saved

1) The complex number in python directly supports the mathematical complex number operation
"""
num7 = 10+20j
num8 = 3+1j
print(type(num7)) # <class'complex'>

print(num7 + num8) # (13+21j)
print(num7 * num8) # (10+70j)
print(num7 / num8)

List

1. What is a list (list)

The list is a container data type (sequence), with [] as the symbol of the container, and multiple elements in it are separated by commas: [element 1, element 2, element 3,...] The
list is variable (the number and value of elements 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

1)空列表:[]
list1 = []
print(list1, type(list1)) # [] <class ‘list’>

2) The element is any type of data
list2 = [19,'abc', True, [1, 2],'abc']
print(list2)

3) List naming convention: plural forms of English words or add list
scores = [89, 78, 67, 34, 99, 56]
score_list = [78, 23, 98]

2. Add, delete, modify and check list elements
1) Check-get elements
a. Get a single element
"""
Syntax:
list [subscript]-get the element corresponding to the specified subscript

Description:
1. List-I want to get the list corresponding to the element
2. []-Fixed notation
3. Subscript-also called index; It is the label corresponding to the position information of the element in the list, and there are two subscripts corresponding to each element :Increase from 0 (0 means the first one, 1 means the second,...); decrease in sequence starting from -1 (-1 means the first from the bottom, -2 means the second from the bottom,...)
"" "
movies = ['Eight Bai','Mulan','Creed','Interstellar','Bear Haunt','Xiao Shengke's Salvation']
print(movies[1]) # 花木兰
print(movies[ -5]) # 花木兰
print(movies[-2]) # 熊出没

Note: Subscripts cannot be
out of range print(movies[10]) # IndexError: list index out of range

b. Traverse-take out all the
elements
in the list one by one, traverse directly to get the elements for variable in the list:
loop body

The variables here are the elements in the list

print(’=Traverse 1==’)
for x in movies:
print(x)

Traverse to get the list elements and the corresponding subscripts of the elements at the same time

for variable 1, variable 2 in enumerate (list):
loop body

Variable 1 gets the subscript of each element, and variable 2 gets each element

print(’=Traverse 2’)
for x, y in enumerate(movies):
print(‘x:’, x, ‘y:’, y)

d. Traverse the elements indirectly by traversing the subscript of each element in the list
print('=Traverse 3’)
for x in range(0, 6, 2):
print(movies[x])

print(’=Traverse 4’)
for x in range(-1, -7, -1):
print(movies[x])

Exercise: Use a list to save the scores of 6 students in a class, and calculate the total
scores of the entire class. scores = [78, 67, 52, 78, 99, 23]
total_score = 0
for s in scores:
total_score += s
print( total_score)

2) Add-add element
a. list. append (element)
-add the specified element at the end of the specified list hero_list = ['Luban 7','Yasuo','Lucian']
print(hero_list) # ['鲁班7','Yasuo','Lucian']

hero_list.append('Daji')
print(hero_list) # ['Luban 7','Yasuo','Lu Xian','Daji']

hero_list.append('Ritz')
print(hero_list) # ['Luban 7','Yasuo','Lucian','Daji','Ritz']

b.List.insert(subscript, element)
-insert the specified element before the specified subscript in the list hero_list = ['Luban 7','Yasuo','Lu Xian','Daji','
Ryze '] hero_list. insert(1,'Blind Monk')
print(hero_list) # ['Luban 7','Blind Monk','Yasuo','Lu Xian','Daji','Ryze']

hero_list.insert(0,'Stone Man')
print(hero_list) # ['Stone Man','Luban No. 7','Blind Monk','Yasuo','Lu Xian','Da Ji','Ryze' ]

3) Delete-delete element
a.del list [subscript]-delete the element corresponding to the specified subscript in the list
masters = ['Diao Chan','Xiao Qiao','Zhen Ji','Wang Zhaojun','Shangguan Wan'er ','周瑜']
del masters[-2]
print(masters) # ['Diao Chan','Xiao Qiao','Zhen Ji','Wang Zhaojun','Zhou Yu']

b.List.remove(element)-delete the specified element in the list
masters = ['Diao Chan','Xiao Qiao','Zhen Ji','Wang Zhaojun','Shangguan Waner','Zhou Yu']
masters.remove ('小乔')
print(masters) # ['Diao Chan','Zhen Ji','Wang Zhaojun','Shangguan Waner','Zhou Yu']

Note: If the element does not exist, an error will be reported
masters.remove('庄周') # ValueError: list.remove(x): x not in list

Note: If there are multiple elements to be deleted, only delete the first
masters = ['Diao Chan','Xiao Qiao','Zhen Ji','Wang Zhaojun','Shangguan Wan'er','Zhou Yu', 'Zhen Ji']
masters.remove('甄姬')
print(masters) # ['Diao Chan','小乔','Wang Zhaojun','Shangguan Waner','Zhou Yu','Zhen Ji']

c.
List .pop () - Remove the last element of the list a
list .pop (subscript) - Remove list element corresponding to the specified index
masters = [ 'Diao' 'Joe' 'Zhen Ji', 'Zhaojun ','Shangguan Waner','Zhou Yu','Zhen Ji']

x = masters.pop()
print(masters, x) # ['Diao Chan','Xiao Qiao','Zhen Ji','Wang Zhaojun','Shangguan Waner','Zhou Yu'] Zhen Ji

x = masters.pop(1)
print(masters, x) # ['Diao Chan','Zhen Ji','Wang Zhaojun','Shangguan Waner','Zhou Yu'] Xiao Qiao

4) Modify-modify the value of the element
List [subscript] = new value-modify the element corresponding to the specified subscript of the list to the new value
teleplays = ['The name of the people','Zhen Huan Chuan','House of Cards','Absolute Life
Bad Master','Please answer 1988'] teleplays[0] ='Mountain City Bang Bang Army'
print(teleplays) # ['Mountain City Bang Bang Army','Zhen Huan Biography','House of Cards','Breaking Bad ','please answer 1988']

teleplays[-2] ='The Big Bang
Theory ' print(teleplays) # ['Mountain City Bang Bang Army','Zhen Huan Chuan','House of Cards','The Big Bang Theory','Please answer 1988']

Guess you like

Origin blog.csdn.net/xdhmanan/article/details/108819704