Question

In: Computer Science

Task 4:         Tuples Explain what is the difference between list, string and tuple? Create a list...

Task 4:         Tuples

  1. Explain what is the difference between list, string and tuple?
  2. Create a list example and check whether you could change the item within the list.
  3. Create a string example and check whether you could change the item within the string.
  4. Create a tuple example and check whether you could change the item within the tuple.

Solutions

Expert Solution

# STAY HOME # STAY SAFE

# a. lists, strings and tuples
# strings are basically a sequence of characters. They are immutable i.e once declared they cannot be changed
s1 = "Hello how are you"
print("Example of string :",s1)

# lists are very useful in python, lists are mutable i.e. we can change the items in a list very easily
# They can be homogenous as well as non-homgenous i.e they can contain elements of different dayatypes
li = [11,22,"hey",20+30,29.99]
print("Example of list :",li)

# tuples can alse be homogenous as well as non-homgenous i.e they can contain elements of different dayatypes
# but indexing in tuples cannot be done i.e they cannot be changed and are immutable
tup = (30,40,"word",34.6)
print("Example of tuple :",tup)

# b. lets check whether we can change item in a list (lists are mutable so they can be changed)
# you won't get any error
li = [11,22,"hey",20+30,29.99]
li[2]=9999
print("List after changing an item :",li)

# c. lets check whether we can change item in a string (strings are immutable so they cannot be changed)
# you will get an error 'str' object does not support item assignment
s1 = "Hello how are you"
s1[3]='G'
print("String after changing an item :",s1)

# d. lets check whether we can change item in a tuple (tuples are immutable so they cannot be changed)
# you will get an error 'tuple' object does not support item assignment
tup = (30,40,"word",34.6)
tup[1]=99
print("Tuple after changing an item :",tup)


Related Solutions

Creating a list/tuple 4. Write a program to accept a string from the user, then replace...
Creating a list/tuple 4. Write a program to accept a string from the user, then replace the first letter with the last letter, the last letter with the middle letter and the middle letter with the first letter. E.g. “Billy” -> “yiBll”.
Creating a list/tuple 3.Given a list of tuples containing student first names and last names e.g....
Creating a list/tuple 3.Given a list of tuples containing student first names and last names e.g. (“John”, “Doe”) that represents a class. Ask the user for a students first and last name and check if that student is a member of the class.
#Write a function called find_max_sales. find_max_sales will #have one parameter: a list of tuples. Each tuple...
#Write a function called find_max_sales. find_max_sales will #have one parameter: a list of tuples. Each tuple in the #list will have two items: a string and an integer. The #string will represent the name of a movie, and the integer #will represent that movie's total ticket sales (in millions #of dollars). # #The function should return the movie from the list that #had the most sales. Return only the movie name, not the #full tuple. #Below are some lines of...
What are the key difference(s) between NAFTA and EU? (List and explain)
What are the key difference(s) between NAFTA and EU? (List and explain)
I. What is the difference between a standing wave in a string and a wave that...
I. What is the difference between a standing wave in a string and a wave that progresses within a string? a) in a standing wave the location of maximums and minimums along the string is constant in time. b) in a wave that progresses the velocity of the wave changes with time. c) in a standing wave the wavelength is constant, whereas in a progressing wave it is not d) a standing wave is only created by having an external...
Define the term tuple, attribute and relation. Discuss the difference between a relation and a table....
Define the term tuple, attribute and relation. Discuss the difference between a relation and a table. What constitutes union compatibility? What is a Cartesian Product operation? When must a GROUP BY clause be used in a query? Explain functional dependency between two attributes? paraphrase please, Do not copy-paste, please.
What is an array-based list? What is a resizable list? What is the difference between a...
What is an array-based list? What is a resizable list? What is the difference between a list’s capacity and its size? When a list is expanded, is the size changed or is its capacity changed?
1. Explain the difference between the difference quotient and average rate of change . create a...
1. Explain the difference between the difference quotient and average rate of change . create a problem for your classmates a problem to solve using the avergae rate of change . 2. A classmate who is confused about limits wonders why you incestigate the value of a function closer and closer to a point , instead of jus finding the value of a function at the point. How would you respond? post a problem evaluating a limit numerically or graphically...
difference between a nursing task and nursing intervention?
difference between a nursing task and nursing intervention?
Python pls Create a function party_freq(dicto:dict): this function returns a list inside tuple that how many...
Python pls Create a function party_freq(dicto:dict): this function returns a list inside tuple that how many times each person party in the day. For example def party_freq(dicto:dict) -> [(str,{(int,int,int): int})]: #code here input dict1 ={'fire1': {(2000,5,20,480) : ('Aaron', 25, 300, ( 0, 300)), (2000,5,20,720) : ('Baily', 45, 1500, (1500,500)), (2000,5,21,490) : ('Aaron', 35, 500, (1300,500)) }, 'fire2': {(2000,5,20,810) : ('Baily', 45, 1400, (600,1600)), (2000,5,20,930) : ('Baily', 43, 1800, ( 0, 0)) }} output [('Aaron', {(2000,5,20): 1, (2000,5,21): 1}), ('Baily', {(2000,5,20):...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT