0% found this document useful (0 votes)
43 views7 pages

Day 7

The document provides an introduction to Python lists, tuples, sets, and dictionaries. It defines each data type and provides basic examples of how to create, access, and use them. Key points covered include how lists are mutable while tuples are immutable, and how dictionaries store data in key-value pairs.

Uploaded by

KARRI INDIRA
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views7 pages

Day 7

The document provides an introduction to Python lists, tuples, sets, and dictionaries. It defines each data type and provides basic examples of how to create, access, and use them. Key points covered include how lists are mutable while tuples are immutable, and how dictionaries store data in key-value pairs.

Uploaded by

KARRI INDIRA
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Welcome in Day_7

Learn Python With Soam

Git hub : https://github.com/SoamDASH/Day2

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:

fruits = ['apple', 'banana', 'orange']

print(fruits)

['apple', 'banana', 'orange']

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]

Why we dont use(' ') in numbers ?


• In python, when we write a list of things, we use something called quotes. It's like
giving a name to a thing, like saying "banana" is a banana. When we have a list of
things that are numbers, like 1, 2, 3, we don't use the quotes. That's because python
knows that these are numbers and we can do math with them. Like counting or
adding them. But if we use quotes around the numbers, like "1", "2", "3", then
python thinks that these are just words and not numbers. So we don't use quotes for
numbers in a list.
Use of List
A list in python is like a special box where you can put different things inside. It's like a
shopping list, where you write down all the things you need to buy. But instead of writing
things to buy, you can put words, numbers or even other lists inside a list in python. It's like
a big box that can hold smaller boxes.
There are many things you can do with a list, like:
• Make a list of your favorite fruits

• Keep a list of things you need to do

• Sort and pick out the things you want from the list

• Keep a lot of things in one place

• Make a table of numbers

• Keep track of your score in a game

• Keep track of the result of a ma

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)

('red', 'blue', 'green')

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

print(marbles[0]) # prints 'red'

# the method i use called "index slicing"

#we can discussion in depth upcomin notes

# now i just interact you to with a basic intro

red

# We can also check the number of items in the tuple

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()

# add some toys to the set


my_toy_set.add("teddy bear")
my_toy_set.add("lego car")
my_toy_set.add("action figure")

# try to add a duplicate toy


my_toy_set.add("teddy bear") # nothing happens

# print out the toys in the set


print(my_toy_set)
#output: {'teddy bear', 'lego car', 'action figure'}

{'lego car', 'action figure', 'teddy bear'}


Here is just one example to help you understand. In future notes, we will discuss many
more examples in much more detail, so don't worry. We will cover everything thoroughly

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)

{'apple': 'red', 'banana': 'yellow', 'orange': 'orange'}

# print the keys


print("Keys of my_dict:", my_dict.keys())

Keys of my_dict: dict_keys(['apple', 'banana', 'orange'])


# print the values
print("Values of my_dict:", my_dict.values())

Values of my_dict: dict_values(['red', 'yellow', 'orange'])

• 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

imagine just like:


We can have a compartment labeled "teddy bears" and another labeled "action figures",
and we can put our toys in the right compartment. And the label is the key and the toy
inside the compartment is the value.
Q1. How to change the value fo the Key "banana"
Q2. Add a new key-value pair to the dictionary
print out put the ehole dictionary
Do research on your own i will explain you it very well in next notes
Use of dictionary
• Storing and retrieving data: Dictionaries are used to store key-value pairs, where
the key is a unique identifier and the value is the actual data. This allows you to
easily retrieve data by its key.

• Representing complex data: Dictionaries can be nested to create complex data


structures that can represent real-world objects or relationships.
• Counting and grouping: Dictionaries can be used to count the occurrences of items
in a list or group similar items together.

• 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.

• Configuration: Dictionaries can be used to store configuration settings for a


program, such as user preferences or runtime options.

• Function arguments: Dictionaries can be used as keyword arguments in function


calls to make the function more flexible and easy to use.
• Dictionaries are commonly used in web scraping, data science, and machine
learning, they are also used in many popular Python libraries such as Pandas,
NumPy, and many more.

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

You might also like