can you please answer in matlab code.
Python Programming Exercise 2 List in Python List Index We can use the index operator [] to access an item in a list. Index starts from 0. So, a list having 5 elements will have index from 0 to 4. Trying to access an element other that this will raise an IndexError. The index must be an integer. We can't use float or other types, this will result into TypeError. Nested list are accessed using nested indexing 1) Create a list of the following letters ('p', 'r','o',b',e') and save it as my_list. Now try to access all 5 elements of your list. Negative indexing Python allows negative indexing for its sequences. The index of -1 refers to the last item. -2 to the second last item and so on. 2) Use the negative indexing to access all elements of the list in question (1). A list can contain any sort object, even another list (sublist), which in turn can contain sublists themselves, and so on. This is known as nested list. 3) Create a Nested List that has two sublists "Happy' and 2,0,1,5'. Try to access the second element of Happy and the last digit. How to slice lists in Python? We can access a range of items in a list by using the slicing operator (colon). 4) Create a list of the following letters (p.7,0','g'r', 'a', 'm', 1, z") and save it as my_new_list. Now try to access a) elements 3 to 5 b) elements beginning to 4th c) elements 6to end d) elements beginning to end How to delete item from list in Python? We can delete one or more items from a list using the keyword del. 5) Create a list of following letters ('p',r,'0','b',T,'e','m') and save it as my_new_list. a) Delete one item. b) Delete multiple item c) Delete the entire list How to change or add elements to a list? List are mutable, meaning, their elements can be changed unlike string or tuple. We can use assignment operator (-) to change an item or a range of items. 6) Create a list of digits (2,4,6,8,10). a) Change the first item to digit 1. b) Change the second to fifth items to 3,5,7,9. How to change or delete a string? Strings are immutable. This means that elements of a string cannot be changed once it has been assigned. We can simply reassign different strings to the same name. We cannot delete or remove characters from a string. But deleting the string entirely is possible using the keyword del. Concatenation of Two or More Strings Joining of two or more strings into a single one is called concatenation. The + operator does this in Python. Simply writing two string literals together also concatenates them. The * operator can be used to repeat the string for a given number of times. 7) Try to add the string "Hello" and "World!" together. Also, try to repeat the string "Hello" three times. Built-in functions to Work with Python Various built-in functions that work with sequence, works with string as well. Some of the commonly used ones are enumeratel) and len(). The enumerate() function returns an enumerate object. It contains the index and value of all the items in the string as pairs. This can be useful for iteration Similarly, len() returns the length (number of characters) of the string, Try this example: str='cold # enumerate listenumerate listenumerate(str)) print("list(enumerate(str) - list_enumerate) #character count print('len(str) "len(str)) Python Programming Exercise 2 List in Python List Index We can use the index operator [] to access an item in a list. Index starts from 0. So, a list having 5 elements will have index from 0 to 4. Trying to access an element other that this will raise an IndexError. The index must be an integer. We can't use float or other types, this will result into TypeError. Nested list are accessed using nested indexing 1) Create a list of the following letters ('p', 'r','o',b',e') and save it as my_list. Now try to access all 5 elements of your list. Negative indexing Python allows negative indexing for its sequences. The index of -1 refers to the last item. -2 to the second last item and so on. 2) Use the negative indexing to access all elements of the list in question (1). A list can contain any sort object, even another list (sublist), which in turn can contain sublists themselves, and so on. This is known as nested list. 3) Create a Nested List that has two sublists "Happy' and 2,0,1,5'. Try to access the second element of Happy and the last digit. How to slice lists in Python? We can access a range of items in a list by using the slicing operator (colon). 4) Create a list of the following letters (p.7,0','g'r', 'a', 'm', 1, z") and save it as my_new_list. Now try to access a) elements 3 to 5 b) elements beginning to 4th c) elements 6to end d) elements beginning to end How to delete item from list in Python? We can delete one or more items from a list using the keyword del. 5) Create a list of following letters ('p',r,'0','b',T,'e','m') and save it as my_new_list. a) Delete one item. b) Delete multiple item c) Delete the entire list How to change or add elements to a list? List are mutable, meaning, their elements can be changed unlike string or tuple. We can use assignment operator (-) to change an item or a range of items. 6) Create a list of digits (2,4,6,8,10). a) Change the first item to digit 1. b) Change the second to fifth items to 3,5,7,9. How to change or delete a string? Strings are immutable. This means that elements of a string cannot be changed once it has been assigned. We can simply reassign different strings to the same name. We cannot delete or remove characters from a string. But deleting the string entirely is possible using the keyword del. Concatenation of Two or More Strings Joining of two or more strings into a single one is called concatenation. The + operator does this in Python. Simply writing two string literals together also concatenates them. The * operator can be used to repeat the string for a given number of times. 7) Try to add the string "Hello" and "World!" together. Also, try to repeat the string "Hello" three times. Built-in functions to Work with Python Various built-in functions that work with sequence, works with string as well. Some of the commonly used ones are enumeratel) and len(). The enumerate() function returns an enumerate object. It contains the index and value of all the items in the string as pairs. This can be useful for iteration Similarly, len() returns the length (number of characters) of the string, Try this example: str='cold # enumerate listenumerate listenumerate(str)) print("list(enumerate(str) - list_enumerate) #character count print('len(str) "len(str))