Records#
- Today I learned about arrays. An array is a form that can store multiple contents.
- In Python, there is no real array form, but rather a form of item list. Item list?
- The form of an array is as shown in the above image, for example: list["a","b","c"], the content in double quotes is the content of the array. Each content also has an index value, with indexing starting from 0. To print any content in the array, you can use print(list[index])
- To change the value of an array, simply use array[index]=value. For example: list[1] = d.
- Print the contents of the array using a for loop.
- One of the differences between arrays in Python and arrays in other languages is that the program automatically recognizes the number of elements in the array.
- Today's challenge is to randomly output a value from the array. Initially, I thought using random.randint(0, array) would work, but it resulted in an error when executed. It seems there is a difference here compared to the for loop. I asked chatgpt and he told me about a new point of knowledge. random.choice(array). This can randomly select a value.
CODE#
import random
lang = [
"hello", "nihao", "hello1", "hello2", "hello3", "hello4", "hello5", "hello6"
]
hi = random.randint(0, 7)
print(lang[hi])