Buffl

Python

En
by EYAD N.

Array in python

what will be the output of these code

  1. fruits = ["apple", "orange", "banana", "coconut"]

print(fruits[2])

  1. fruits = ["apple", "orange", "banana", "coconut"]

    for x in fruits:

    print(x)

    #——————————

    for y in "fruits":

    print(y)

  2. fruits = ["apple", "orange", "banana", "coconut"]

    print("pinaple" in fruits)

  3. fruits = ["apple", "orange", "banana", "coconut"]

    fruits.append("pineapple")

    fruits.remove ("apple")

    print(fruits)

  4. fruits = ["apple", "orange", "banana", "coconut"]

    fruits.insert(0, "pineapple")

    # Inserting "pineapple" at index 0

    print(fruits)

  5. fruits = ["apple", "orange", "banana", "coconut"]

    fruits.sort()

    print(fruits)

  6. fruits = ["apple", "orange", "banana", "coconut"]

    fruits.reverse()

    print(fruits)

  7. fruits = ["apple", "orange", "banana", "coconut"]

    fruits.clear()

    print(fruits)

  8. fruits = ["apple", "apple", "banana", "coconut"]

    print (fruits.count("apple"))

    1. print (dir(fruits))



  1. banana

  2. apple

    orange

    banana

    coconut

    f

    r

    u

    i

    t

    s


  3. False

    you can use the in operator to find if a value is within a list

  4. ['orange', 'banana', 'coconut', 'pineapple']

    to add an element to the end of a list use the append method

    to remove an element you can use the remove method

  5. ['pineapple', 'apple', 'orange', 'banana', 'coconut']

  6. ['apple', 'banana', 'coconut', 'orange']

    the sort method will sort a list fruits.sort

    these are all in alphabetical order now apple banana coconut orange

  7. ['coconut', 'banana', 'orange', 'apple']

  8. []

  9. 2

  10. ['__add__', '__class__', '__class_getitem__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getstate__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'clear', 'copy', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']




Author

EYAD N.

Information

Last changed