Day 7
Day 7
Gmail: [email protected]
What is List?
• A list in Python is a way to store a collection of items. Think of it like a shopping list,
where you write down all the things you need to buy. In Python, instead of writing
down things to buy, you can store words, numbers, or even other lists inside a list.
It's like a box where you can put different things inside. You can add, remove, or
change the items in the list. Just like how you can add or remove items from your
shopping list.
# Here's an example of a list in Python:
print(fruits)
In this example, fruits is the name of the list and it contains three items: 'apple', 'banana',
and 'orange'. These items are called elements of the list.
# We can also create a list with numbers:
numbers = [1, 2, 3, 4, 5]
print(numbers)
[1, 2, 3, 4, 5]
• Sort and pick out the things you want from the list
Lists are very helpful to organize and keep track of different things in python. They allow
you to keep many things in one place and you can easily add, remove or get the things from
the list.
We may go over how to apply append, and remove in the following advanced notes of the
list.
What is Tuple?
• A tuple in python is like a bag of things where you can put different things inside.
Once you put the things in the bag, you can't take them out or add more things to the
bag. But you can still look at the things inside the bag and count how many things
are in the bag.
For example, you have a bag of marbles and you put red, blue and green marbles in it. Once
you put the marbles in the bag, you can't take them out or add more marbles to the bag. But
you can still look at the marbles in the bag and count how many marbles are in the bag.
In python, a tuple is a similar way to store a collection of items, once you create it, you can't
change it, you can't add or remove items, but you can still access the items and check how
many items are in the tuple.
marbles = ('red', 'blue', 'green')
print(marbles)
In this example, marbles is the name of the tuple and it contains three items: 'red', 'blue',
and 'green'. These items are called elements of the tuple.
# We can access an item in the tuple by its index
red
print(len(marbles)) # prints 3
in this example we created a tuple named marbles and we put red, blue, green items in it
and we can access the items and check the number of items in the tuple but we can't change
the items that we put in the tuple.
Use of Tuple
• To group related data together: Tuples can be used to store multiple values that are
related to each other, such as the x and y coordinates of a point on a graph or the
name and age of a person.
• As function return values: Many functions return multiple values, and tuples are a
convenient way to return multiple values from a function.
• To represent data structures: Tuples can be used to represent data structures such
as a point in a 2D space or a RGB color.
• To use as keys in dictionaries: Because tuples are immutable, they can be used as
keys in a dictionary, while lists can't.
• To make data read-only: Because tuples are immutable, they can be used to make
data read-only. Once a tuple is created, its values can't be changed, which can help to
ensure data integrity.
• To make code more efficient: Tuples are generally faster and more memory efficient
than lists, so they can be used in situations where performance is critical.
Note
• A list is like a big box where you can put different things inside and you can add,
remove or change the things inside the box. A tuple is like a bag where you can put
different things inside but you can't add, remove or change the things inside the bag
once you put them in. Lists are written with square brackets, like [], while tuples are
written with parentheses, like (). Lists are good when things might change and
tuples are good when things will stay the same.
• Lists are mutable, which means that you can add, remove, or change items in a list
after it's been created. Tuples are immutable, which means that you can't change the
items in a tuple once it's been created.
• Lists are generally more flexible than tuples, because you can easily add, remove, or
change items as needed. Tuples are more efficient and faster than lists when it
comes to access the items, because you don't need to go through the list to find the
item you are looking for.
• Lists are useful when you are working with a collection of items that may change
over time, while tuples are useful when you have a fixed collection of items that will
not change and you want to access the items quickly.
What is Set?
• A set in Python is like a special bag where you can put toys in, but we can't have two
of the same toy in the bag!
• It's like a toy box where we can only have one of each toy. It's kind of like a game
where we collect different toys, but we don't want to have any duplicates!
# create an empty set
my_toy_set = set()
What is Dictionary?
• A dictionary in Python is like a special box where we can put things inside and label
them with a name, it's like a toy chest where you have different compartments and
each compartment has a label on it,
for example,
we can have a compartment labeled "teddy bears" and another labeled "action figures", and
we can put your toys in the right compartment.
It's like a game where we collect different toys and we have to put them in the right drawer
so we can find them later!
In the future, we can translate this example into python scripts, but for now, I'll just
offer you a simple dictionary example.
# create a dictionary with some key-value pairs
my_dict = {"apple": "red", "banana": "yellow", "orange": "orange"}
print(my_dict)
• in the dictionary {"apple": "red", "banana": "yellow", "orange": "orange"} the keys
are "apple", "banana", and "orange" and the values are "red", "yellow", and "orange"
respectively.
• The key is the word before the colon (:) and the value is the word after the colon. So
in the example above, "apple" is the key and "red" is the value, "banana" is the key
and "yellow" is the value and so on
• Mapping: Dictionaries can be used to map values from one domain to another. For
example, you can create a dictionary that maps the names of colors to their
hexadecimal codes.
For now just get an idea we will discusssion every points in future before that we focus on
some important basic concepts
Thank You Learn Python With Soam
Support the helping hand of Soam