二手产品经理

二手产品经理

THIS IS RENO

Learn Python online for 100 days - Week 5, Day 4.

Record#

  1. Today I learned about arrays. An array is a form that can store multiple contents.
  2. In Python, there is no true array form, but rather a form of item list. Item list?
    Catbox
  3. The form of an array is shown in the above image, for example: list["a","b","c"], the contents within double quotes are the contents of the array. Each content also has an index value, and the index starts from 0. To print any content from the array, you can use print(list[index value]).
  4. To change the value of an array, simply use array[index] = value. For example: list[1] = d.
  5. Use a for loop to print the contents of the array.
    Catbox
  6. One difference between arrays in Python and arrays in other languages is that Python automatically recognizes the number of elements in the array.
  7. Today's challenge is to randomly output a value from the array using random. At first, I thought using random.randint(0, array) would work, but it resulted in an error. It seems there is a difference here compared to the for loop. I asked chatgpt and they told me a new piece 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])
Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.