PYTHON | ARRAYS
|
|
ON THIS PAGE
SECTION 1 | Introduction to Lists SECTION 2 | List Manipulation Methods SECTION 3 | 2 Dimensional Lists SECTION 4 | Tuples SECTION 5 | Changing a Tuple to a List SECTION 6 | Dictionaries |
ALSO IN THIS TOPIC
GETTING STARTED VARIABLES AND DATA TYPES OPERATORS SELECTION ITERATION YOU ARE HERE | ARRAYS FUNCTIONS AND PROCEDURES FORMATTING DATA ERROR CHECKING OBJECT ORIENTED PROGRAMMING FURTHER PYTHON PYTHON QUICK REFERENCE |
|
|
WATCH THIS VIDEO TO GUIDE YOU THROUGH THIS SECTION
|
bob = ["fish", "feet", 5, "Cats"] print (bob) #Would print the entire list print(bob[0]) #Would print the first item in the list "fish" print(bob[1:3]) # Would print print the range of items in positions 1 - 2: "feet", 5
studentNames = [ ] for i in range (3): name = input ("Please enter the name of student " + str(i + 1) + ": ") studentNames.append(name) print (studentNames)
starwars = ['IV', 'A New Hope', '1977','V', 'The Empire Strikes Back', '1980'] starwars.clear() print (starwars)
starwars = ['IV', 'A New Hope', '1977','V', 'The Empire Strikes Back', '1980'] moviesWatched = starwars.copy()
studentNames = ["Dave", "Jane", "Bill" ,"Bob" ,"Jack" ,"Ben", "Jane", "Paul", "Fran", "Sally"] inList = studentNames.count("Jane") print (inList)
studentNames = ["Dave", "Jane", "Bill" ,"Bob" ,"Jack" ,"Ben", "Jane", "Paul", "Fran", "Sally"] findName = input("Please enter the name that you want to check is in the list : ").title() inList = studentNames.count(findName) if inList > (1): print (findName + " is in the list " + str(inList) + " times.") elif inList > (0): print (findName + " is in the list " + str(inList) + " time.") else: print (findName + " is not in the list")
studentNames1 = ["Dave", "Jane", "Bill" ,"Bob" , "Jack"] studentNames2 = ["Ben", "Jane", "Paul", "Fran", "Sally"] studentNames1.extend(studentNames2) print (studentNames1)
studentNames = ["Dave", "Jane", "Bill" ,"Bob" , "Jack"] namePosition = studentNames.index("Bill") print (namePosition)
studentNames = ["Dave", "Jane", "Bill" ,"Bob" , "Jack"] studentNames.insert(3,"Sophie") print (studentNames)
studentNames = ["Dave", "Jane", "Bill" ,"Bob" , "Jack"] studentNames.pop(3) print (studentNames)
studentNames = ["Dave", "Jane", "Bill" ,"Bob" , "Jack"] studentNames.remove("Bob") print (studentNames)
studentNames = ["Dave", "Jane", "Bill" ,"Bob" , "Jack"] studentNames.reverse() print (studentNames)
studentNames = ["Dave", "Jane", "Bill" ,"Bob" , "Jack"] myNewList = studentNames.copy() myNewList.reverse() print (myNewList)
studentNames = ["Dave", "Jane", "Bill" ,"Bob" , "Jack"] studentNames.sort() print (studentNames)
studentNames = ["Dave", "Jane", "Bill" ,"Bob" , "Jack"] newList = ' '.join(studentNames) print (newList)
WATCH THIS VIDEO TO GUIDE YOU THROUGH THIS SECTION
|
drivers = [ 1, "Hamilton", "MERCEDES",132, 2, "Verstappen", "RED BULL RACING HONDA", 95, 3, "Bottas", "MERCEDES", 89, 4, "Leclerc", "FERRARI", 45, 5, "Stroll", "RACING POINT BWT MERCEDES", 40] start = 0 end = 4 for i in range (3): print (drivers[start:end]) start = end end = end + 4 #print (drivers[0:4]) #print (drivers[0:12]) #print (drivers[0:4]) The code below shows using the 2 dimensional Array method. It is probably more simple than using a 1 dimensional array for the same task. constructors = [ [1,"MERCEDES", "Bottas", "Hamilton", 221], [2,"RED BULL RACING HONDA","Albon","Verstappen",135], [3,"RACING POINT BWT MERCEDES", "Perez","Stroll",63], [4,"MCLAREN RENAULT","Norris","Sainz",62], [5,"FERRARI","Leclerc","Vettel",61], [6,"RENAULT","Ocon","Ricciardo",36], [7,"ALPHATAURI HONDA","Gasly","Kvyat",16], [8,"ALFA ROMEO RACING FERRARI","Giovinazzi","Raikkonen",2], [9,"HAAS FERRARI","Magnussen","Grosjean",1], [10,"WILLIAMS MERCEDES","Russell","Latifi",0] ] for i in range (3): print ("Team " + (constructors[i][1]) + " drivers are " + (constructors[i][2]) + " and " + (constructors[i][3])) #print (constructors[0]) #print (constructors[0][3]) #print (constructors[0][2:4]) #for i in range (3): # print (constructors[i][2:4])
studentNames = ("Dave", "Jane", "Bill" ,"Bob" , "Jack") print (studentNames[2])
myList = [] myData = [("Tetris",),("Pacman",),("Space Invaders",),("Astroids",)] for i in range (len(myData)): myList.append(myData[i][0]) print (myList)
|
|
SUGGESTIONS
We would love to hear from you |
SUBSCRIBE
To enjoy more benefits |