š Python Basics 2/6: Lists, Dictionaries, Sets and Tuples
date
Mar 27, 2023
slug
python-basics-lists-dictionaries-sets-tuples
status
Published
tags
Python Basics
summary
Learn more data types:Ā
lists
,Ā dictionaries
,Ā sets
Ā andĀ tuples
.type
Post
Last updated
Mar 28, 2023 01:00 AM
š Hey guys, in this second quick content, we are gonna learn more data types:
lists
, dictionaries
, sets
and tuples
. The main goal of this content is not to show you everything you can do with this data types, but yes, what they are and when/how to use them!Ā
You can get all the material in this GitHub Repo: python-basics-posts.
Ā
Ā
0) Lists
Ā
- are declared using brackets [ ];
- can store different data types;
- can have their values changed;
- can store duplicated values;
- store just values.
Ā
#
# ---- Lists ----
#
my_list = ["hello world", 42, 7.0, True, False]
print(f"The whole list: {my_list}")
print(f"The third element: {my_list[2]}")
Ā
Ā
1) Dictionaries
Ā
- are declared using curly brackets { };
- can store different data types;
- can have their values changed;
- can store duplicated values;
- store keys-values pairs.
Ā
#
# ---- Dictionaries ----
#
my_dict = { "anime": "JoJo's Bizarre Adventure", "fav_character": "Joseph Joestar", "rate": 10 }
print(f"The whole dictionary: {my_dict}")
print(f"Anime: {my_dict['anime']}")
Ā
Ā
2) Sets
Ā
- are declared using the built-in function set( );
- can store different data types;
- can have their values changed;
- cannot store duplicated values;
- store just values.
Ā
#
# ---- Sets ----
#
my_set = set(["a", 4, 5.0, 4.0, 4])
print(f"The whole set: {my_set}")
# it's not possible to use indexes to access sets elements. To do it, you have
# to iterate the set until you find the desired element
Ā
Ā
3) Tuples
Ā
- are declared using parenthesis ( );
- can store different data types;
- cannot have their values changed;
- can store duplicated values;
- store just values.
Ā
#
# ---- Tuples ----
#
my_tuple = ("b", True, 5, 5.0)
print(f"The whole tuple: {my_tuple}")
print(f"The second element: {my_tuple[1]}")
Ā
Ā
You got it! šš
In the next post, we will learn about list slices!!
See ya! š
Ā