List, Typle and Set in python

List, Tuple and Set in python

List basic

A list is also a sequence type, meaning the contained elements are ordered by position in the list, known as the element's index, starting with 0. my_list = [ ] creates an empty list with no elements.

Adding and removing list elements

Since lists are mutable, a programmer can use methods to add and remove elements in the list. A method instructs an object to perform some action and is specified by providing the object name followed by a "." symbol and then the method name. The append() list method is used to add new elements to a list. Elements can be removed from a list using the pop() or remove() methods. Methods are covered in greater detail in another section.

Adding elements to a list:

Operation Description
list.append(value) Adds value to the end of the list.
Ex: my_list.append("abc")
list.pop(i) Removes the element at index i from the list.
Ex: my_list.pop(1)
list.remove(v) Removes the first element whose value matches v.
Ex: my_list.remove("abc")

Some of the functions and methods useful to lists.

Operation Description
len(list) Find the length of the list.
list1 + list2 Produce a new list by concatenating list2 to the end of list1.
min(list) Find the element in the list with the smallest value. All elements must be of the same type.
max(list) Find the element in the list with the largest value. All elements must be of the same type.
sum(list) Find the sum of all elements of a list (numbers only).
list.index(val) Find the index of the first element in the list whose value matches val.
list.count(val) Count the number of occurrences of the value val in the list.

Here is the exercise about list

  • Define a list, my_list, containing the user inputs: my_flower1, my_flower2, and my_flower3 in the same order.
  • Define a list, your_list, containing the user inputs, your_flower1 and your_flower2, in the same order.
  • Define a list, our_list, by concatenating my_list and your_list.
  • Append the user input, their_flower, to the end of our_list.
  • Replace my_flower2 in our_list with their_flower.
  • Remove the first occurrence of their_flower from our_list without using index().
  • Remove the second element of our_list.

Tuple basic

A tuple, usually pronounced "tuhple" or "toople," stores a collection of data, like a list, but is immutable – once created, the tuple's elements cannot be changed. A tuple is also a sequence type, supporting len(), indexing, and other sequence functions. A new tuple is generated by creating a list of comma-separated values, such as 5, 15, 20. Typically, tuples are surrounded with parentheses, as in (5, 15, 20). Note that printing a tuple always displays surrounding parentheses. A tuple is not as common as a list in practical usage but can be useful when a programmer wants to ensure that values do not change.

Set basic

A set is an unordered collection of unique elements. A set has the following properties: Elements are unordered: 

  • Elements in the set do not have a position or index. 
  •  Elements are unique: No elements in the set share the same value. 

A set can be created using the set() function, which accepts a sequence-type iterable object (list, tuple, string, etc.) whose elements are inserted into the set. A set literal can be written using curly braces { } with commas separating set elements. Note that an empty set can only be created using set().

Some of the methods are useful to sets.

OperationDescription
len(set)Find the length (number of elements) of the set.
set1.update(set2)Adds the elements in set2 to set1.
set.add(value)Adds value into the set.
set.remove(value)Removes value from the set. Raises KeyError if value is not found.
set.pop()Removes a random element from the set.
set.clear()Clears all elements from the set.



Common set theory operations.

OperationDescription
set.intersection(set_a, set_b, set_c...)Returns a new set containing only the elements in common between set and all provided sets.
set.union(set_a, set_b, set_c...)Returns a new set containing all of the unique elements in all sets.
set.difference(set_a, set_b, set_c...)Returns a set containing only the elements of set that are not found in any of the provided sets.
set_a.symmetric_difference(set_b)Returns a set containing only elements that appear in exactly one of set_a or set_b



Code is here

my_flower1 = input()
my_flower2 = input()
my_flower3 = input()

your_flower1 = input()
your_flower2 = input()

their_flower = input()

# 1. TODO: Define my_list containing my_flower1, my_flower2, and my_flower3
# in that order
my_list = [my_flower1,my_flower2,my_flower3]
# 2. TODO: Define your_list containing your_flower1 and your_flower2
# in that order
your_list = [your_flower1,your_flower2]
# 3. TODO: Define our_list by concatenating my_list and your_list
our_list = my_list + your_list
print(our_list)

# 4. TODO: Append their_flower to the end of our_list
our_list.append(their_flower)
print(our_list)

# 5. TODO: Replace my_flower2 in our_list with their_flower
index = our_list.index(my_flower2)
our_list[index] = their_flower
print(our_list)

# 6. TODO: Remove the first occurrence of their_flower from our_list without using index()
our_list.remove(their_flower)
print(our_list)

# 7. TODO: Remove the second element of our_list
our_list.pop(1)
print(our_list)
  

Comments

Popular posts from this blog

Commands homelab

Wifi on ubuntu server