Python basic data types (full)

An ordered container of tuples

Essentially a read-only list

1.

Assign an empty tuple

tuple1 = ()

Print out an empty

print(tuple1)

()


#Print out a tuple type print(type(tuple1))

<class ‘tuple’>

2.

If the tuple has only one element,

Then you must add a comma, or the parentheses will be treated as priority

tuple2 = (8+8,)
print(tuple2)

(16,) Without the comma, the result is 16

print(type(tuple2))

Output type <class'int'>

3.

Tuples cannot be modified

tuple3 = (1,2,3,4,5,6)
print( tuple3[2] )

Read a 3 subscript and count each number in the tuple starting from 0

tuple3[2] = 100 Modification failed

4.

Support slicing

tuple4 = (1,2,3,4,5,6,7,8,9)
print(tuple4[1:5])

(2,3,4,5) does not count as 1

Tuples combine to get new tuples

print((1,2,3)+(4,5,6))

(1,2,3,4,5,6)

print((1,2,3)*2)

Tuple is repeated twice (1,2,3,1,2,3)

print( 1 in tuple4 )

true

5.

tuple5 = (1,2,3,4,5)
print (len (tuple5))

Calculate the tuple length as 5

print(max(tuple5))

Calculate the maximum value to 5

print(min(tuple5))

Computer minimum is 1

print(sum(tuple5))

Calculate the sum to 15

print(max("Hello, I am hey ado"))

Character size based on ASCII code table

The biggest is "Yes"

Computer hard disk storage principle:

"A"->97->01100001->write to hard disk

6.

Tuple support and list conversion

If you want to modify the tuple then convert the list

tuple6 = (1,2,3,4,5,6)

Conversion list operation

list1 = list(tuple6)

Modify list1 and add a 6

list1.append(6)

Revert back to tuple

tuple6 = tuple(list1)
print(tuple6)

The result is (1,2,3,4,5,6,6)

7.

Iterate over tuples

for i in (1,2,3,4,5,6,7):
print(i)

Print 1 2 3 4 5 6 7

tuple7 = (“a”,“b”,“c”,“d”)

Traverse the element element of the tuple index

for index,element1 in enumerate(tuple6):
print("subscript is:",index,"element is:",element1)
# subscript is: 0 element is: 1 and so on

8. Tuples can save the brackets on both sides

tuple8 = 1,2,3,4,5,6,7,8
print(tuple8)

(1, 2, 3, 4, 5, 6, 7, 8)

List ordered container

1.

Can be empty list

list1 = []

Can be a list with elements

list2 = [1,2]

Use subscript to read 2

print(list2[1])

The last element of the access list is 2

print(list2[-1]) #Support
c, c++ and other languages ​​written as 2
print(list2[ len(list2)-1 ])

Get the length of the list (the number of elements) is 2

print (len (list2))

Edit list

list2[1] = 666
print(list2)

[1, 666]

List combination

print([1,2,3]+[1,2,3])

The result is [1,2,3,1,2,3]

Duplicate list

print([1,2,3]*3)

The result is [1,2,3,1,2,3,1,2,3]

in Calculate whether a certain number is in a tuple

print(3 in [1,2,3])

true

2.

Slice and intercept a part of the elements of the list

Syntax list [start subscript: end subscript: step length]

list3 = [“A”,“B”,“C”,“D”,“E”,“F”,“G”]

Positive subscript: 0, 1, 2, 3, 4, 5, 6

Reverse subscript: -7 -6 -5 -4 -3 -2 -1

Take 0 to 5, not including 5

Package start subscript, excluding end subscript

print(list3[0:5])

[‘A’, ‘B’, ‘C’, ‘D’, ‘E’]

Take 2 to the end

print(list3[2:])

[‘C’, ‘D’, ‘E’, ‘F’, ‘G’]

Start from 0 to the end of 3

print(list3[:3])

[‘A’, ‘B’, ‘C’]

Intercept all

print(list3[:])

[‘A’, ‘B’, ‘C’, ‘D’, ‘E’, ‘F’, ‘G’]

Using negative subscripts does not include -2 itself

print(list3[-4:-2])

['D', 'E']

print(list3[-4:])

[‘D’, ‘E’, ‘F’, ‘G’]

3.

list4 = [0,1,2,3,4,5,6,7,8,9,10,11,12]

Step length: how far to cross

print(list4[2:4:1])

[2, 3] Take each from 2 to 4

print(list4[1:2:2])

[1] Take one every other from 1 to 2

The step size is negative, which means reverse

list4 = [0,1,2,3,4,5,6,7,8,9,10,11,12]

Take a number in the range of 10 to 2, and if you take a number and take a step, you don’t go.

print(list4[10:2:-1])

[10, 9, 8, 7, 6, 5, 4, 3]

Take a number in the range of 10 to 2

print(list4[10:2:-2])

[10, 8, 6, 4]

Reverse order

print(list4[::-1])
print(list4)

[12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0]

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]

Guess you like

Origin blog.csdn.net/auyah/article/details/113276626