Variable in Python (Continued)

Tram Ho

Like the previous post, I went through the basics of variables in python (Number, String, List, Tuple). If you have not seen it, see here . In this part, I will continue about variables in python (about Set, Dictionary), the last part about variables. Let’s go.

Remaining data types

Set

Set in Python is a unique set of elements (No duplicate elements), no order. The elements in the set are separated by commas and enclosed in curly braces {}. The elements in Set have no order. Elements of Set can be added and removed.

How to create a Set by enclosing the elements in {}, separated by ‘,’ or using the built-in function set (). The elements in the set can be of many different data types such as Number, String … do not contain elements of mutable types such as list, dictionary.

Since Set is unordered, it is not possible to access an element through their position. Instead we can get all the elements in the Set by for all the elements in the Set:

Here is the result:

We cannot change the elements in Set because there is no order. But we can add an element to the set using the add() function – add an element, and add more elements using the update() function:

Checks if the element is in Set using either in or not in

It is possible to remove elements in the set, again we cannot change the elements in the set because there is no order. But we can use the following functions to remove elements from Set (): remove, discard, pop – delete the last element, clear – delete Set round elements leave Set empty, del delete Set when calling this variable will fail to undefine.

As above you can see:

  • The remove and discard functions to delete an element in a Set through the value you want to delete.
  • The pop function is the function that retrieves the last element of Set. Use caution when using this function because you won’t know which element the last element is. Like the above example, when pop() it gets the string “php”
  • The clear function removes the elements in that Set and leaves the Set empty – the variable reusable
  • The del function will delete Set as well, without using this variable dc – undefine

There are also many other possible operators other than Set: |/union, &/intersection, -/diserence, ^/symmetric_difference

Other supported functions: all(), any(), enumerate(), len(), max(), min(), sorted(), sum()

Dictionary

Dic is a set of mutable, key-Value pairs. You can declare with {} braces, key – value can be any data type. Alternatively, you can declare using the built-in dict() function:

To access each element in the dict we will use the key by typing it automatically in square brackets [ ] or using get ().

To change, or add an element to dict, we need to use the key to access that element (like array =)))

To delete the specific element we have in the dict we use pop()/del – delete the specified element, clear () – delete the entire element

There are also many other methods copy(), items(), keys(), setdefault(),

Summary of 4 data types List, Tuple, Set. Dict

Through 2 articles about variables in Python you can grasp the most basic things about data types in Python, and to end this section we have the following summary table:

NameFeaturedSignInitialization
ListCan contain any data type, like arrays in other languages[]list ()
TupleValues ​​cannot be changed()tuple ()
SetElement is the only one that does not repeat, has no key, so it cannot access each element{}set ()
DictElement of the form key: value{}dict ()

So I went through the introduction of Python, variables in python. In the next section, I will learn about OOP in python. Thank you for your interest

Share the news now

Source : Viblo