Concept explainers
List:
- • A list is an object that has sequence of elements.
- • The contents of the list can be changed at any point of time, so lists in Python are mutable.
- • Lists support various additional operations like “append()”, “insert()”, “remove()”, “del()”, etc.
- • Lists have dynamic size.
- • Lists store elements of same type, i.e. lists are homogenous.
- • List elements are enclosed by square brackets.
Syntax:
The syntax to create a list is as follows:
List_name = [element1, element2,…, elementn]
Explanation:
Here,
- • “List_name” specifies the list name.
- • “element1, element2, and elementn” indicate the list elements.
Example
Consider the below example that creates a list and prints the list.
#Declare a list with numbers
list_ex = [1, 2, 3, 4]
#Print the list
print(list_ex)
Sample Output:
[1, 2, 3, 4]
Program explanation:
In the above code,
- • “list_ex” represents a list having integers as its elements.
- • The list “list_ex” is printed using “print()” function.
Tuple:
- • A tuple is an object that has immutable sequence of elements.
- • The contents of the tuple cannot be altered once it is created, so tuples in Python are immutable.
- • Tuples have static size.
- • The elements of tuples can be of various types, i.e. tuples are heterogeneous.
- • Elements in the tuple are enclosed by parenthesis.
Syntax:
In python, a tuple is created using the below format:
#Create a tuple
Tuple_name = (element1, element2, …, elementn)
Explanation:
- • “Tuple_name” specifies the tuple name.
- • “element1, element2, and elementn” indicate the tuple elements.
Example program:
Consider the below example that creates a tuple and prints the tuple.
#Declare a tuple with numbers, strings
tuple_ex = [1, 'Joe', 'Tom', 4]
#Print the tuple
print(tuple_ex)
Sample Output:
[1, 'Joe', 'Tom', 4]
Program explanation:
In the above code,
- • “tuple_ex” represents a tuple having integers and strings as its elements.
- • Then, the tuple “tuple_ex” is printed using “print()” function.
Want to see the full answer?
Check out a sample textbook solutionChapter 7 Solutions
Starting Out with Python (4th Edition)
- Lab Goal : This lab was designed to teach you more about list processing and algorithms.Lab Description : Write a program that will search through a list to find the smallest number and the largest number. The program will return the average the largest and smallest numbers. You must combine variables, ifs, and a loop to create a working method. There will always be at least one item in the list.arrow_forwardpaython programing question Write a program that creates a list of N elements (Hint: use append() function), swaps the first and the last element and prints the list on the screen. N should be a dynamic number entered by the user. Sample output: How many numbers would you like to enter?: 5 Enter a number: 34 Enter a number: 67 Enter a number: 23 Enter a number: 90 Enter a number: 12 The list is: [12, 67,23, 90, 34]arrow_forwardOCaml Code: Attached are the instructions. Make sure to read the instructions carefully and write your own test cases to make sure the code works. Make sure to include a screenshot of the correct code with the test cases that is used along with the output of the code being passed.arrow_forward
- Assume my_list is a list of integer values. Write a list comprehension statement that creates a second list named gt_ten. The gt_ten list should contain all the values of my_list that are greater than 10. For example, if my_list contains the following values: [1, 12, 2, 20, 3, 15, 4] The gt_ten list should contain these values: [12, 20, 15]arrow_forwardpublic List<String> getLikes(String user) This will take a String representing a user (like “Mike”) and return a unique List containing all of the users that have liked the user “Mike.” public List<String> getLikedBy(String user) This will take a String representing a user (like “Tony”) and return a unique List containing each user that “Tony” has liked. create a Main to test your work. import java.io.BufferedReader;import java.io.FileReader;import java.io.IOException;import java.util.ArrayList;import java.util.Arrays;import java.util.HashMap;import java.util.HashSet;import java.util.List;import java.util.Map;import java.util.Set; public class FacebookLikeManager { public List<String> facebookMap; private Set<String> likesSets; public FacebookLikeManager() { facebookMap = new ArrayList<>(); likesSets = new HashSet<>(Arrays.asList("Mike","Kristen","Bill","Sara")); } public void buildMap(String filePath) {…arrow_forwardAssume list1 references a list. After the following statement executes, list1 and list2 willreference two identical but separate lists in memory:list2 = list1 (T/F)arrow_forward
- In PYTHON; Create a list named people that contains three dictionaries. # Each dictionary must contain a name key and a value that is the # persons name, and a favorite_foods key that has a value that is a list # that contains the person's three favorite food items. Loop through # and print out the contents of the people list with some extra textarrow_forward-Write a statement that assigns the list of seasons of the year to the variable seasons in the following order in Python: Winter Spring Summer Fall -Write one statement that uses the in operator to test if the element 'surfing' is present in list listActivities and print the results. -Using the del keyword and/or the slicing operator:, delete the 3rd through the 5th element from the list listTemps. You've been provided a list named myLst, of six integers. Set the fourth element's value to 7.arrow_forward0arrow_forward
- Create a function called find_big_words . The function should accept a list as a parameter. Parts of the program which you are not coding will call the function and past it lists of words. The function must analyze the words in the list and return all the words which are 6 or more characters long. The returned list should be in alphabetical order. NB: Only write the function. Do not call it. For example: Test Result random_words1 = find_big_words(["nation", "night", "news", "negotiation", "newspaper", "nature"]) print(random_words1) ['negotiation', 'newspaper'] random_words3 = find_big_words(["examination","test", "testing", "quiz", "exam", "assessment"]) print(random_words3) ['assessment', 'examination', 'testing']arrow_forwardcard_t * moveCardBack (card t *head); The moveCardBack function will take the card in front of the pile and place it in the back. In coding terms, you are taking the head of the linked list and moving it to the end. The function has one parameter which is the head of the linked list. After moving the card to the back, the function returns the new head of the linked list.arrow_forwardAfter a tuple is created, its items can be changed. True False A tuple's items can be access by square brackets, similar to lists. True Falsearrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education