Choose Your Language
WHAT IS PYTHON STRUCTURE |FULL DETAIL ITS EXTRA KNOWLEDGE|..

WHAT IS PYTHON STRUCTURE |FULL DETAIL ITS EXTRA KNOWLEDGE

All Compound Data Structures of Python Data Structures

Each language has its own grammatical rules. To understand and learn a language, it’s essential to know its units and basic grammar.

Similarly, programming languages ​​also have their own grammatical rules.

So, if you want to learn the Python programming language, you’ll first need to learn about Python’s Compound Data Structures.

To know more about Fundamentals of Python and Basic Data Structures of Python, read this –

Having learned about the basic data structures of Python, in today’s article we will learn about the Compound Data Structures of Python.

There are a total of four types of compound data structures in the Python language –

Lists

Tuples

Dictionary

Sets

LIST

What are Lists?
How are lists created?

SLICING AND INDEXING OF LISTS IN PYTHON

USES OF OPERATORS IN LISTS
arithmetic operators
Concatenation (+) operator

Replication () operator LIST FUNCTIONS (Compound Data Structures of Python) 1) Append() method 2) Updating item through index number 3) Deleting items from a list Pop method of deletion

1) The index method 2) The extended method 3) The insert method 4) The remove method 5) The clear method 6) The count method 7) The Reverse method 8) The sort method TUPLESA What is Tuple?

(Compound Data Structures of Python) How are tuples created? Slicing and indexing of Tuples in Python Uses of operators in Tuples arithmetic operators Replication operator ()

Packing and unpacking of Tuples.

Tuple function:
1) len() method
2) max() method
3) min() method
4) index() Function
5) Count() function
6)Tuple() function

Sets

How are sets created?
Slicing and indexing of sets in Python
Sets Mutable or Immutable
Mixed Data type Set
empty set

Operations in Sets – Compound Data Structures
Add Method
Remove Method

Iterating Over in Set
Set Functions and Method of Compound Data Structures of Python
dictionary
How are dictionaries created?

Initializing a Dictionary

Adding key: value pairs to an empty dictionary

Creating a dictionary from name and value pair

Specifying key value pairs as keyboard arguments to dict() functions
2) specify comma separated key : value pairs
3) Specify keys separated and corresponding values ​​separated
4) Specify key valve separately. Sequence in form of sequence

Nested Dictionary

Dictionaries are Mutable
Deletion of an element from Dictionary:
Dictionary functions and methods – Compound Data Structures
The get method:
Python Complete Course (Fundamentals of Python)

LISTS

What are Lists?

The Python programming language has created data structures called compound data structures to store and use large amounts of data in one place.

These data structures allow us to store values ​​of all data types simultaneously, and with the help of special numbering, we can also use each value individually.

Lists are also a type of compound data structure. Lists are ordered structures of various data types, which can store values ​​of different data types such as integer, float, string, boolean, etc.

A collection of values ​​of these different data types, enclosed within square braces [] and separated by commas, is called a list. These values ​​stored within a list are called “items.”

The concept of these data structures is considered very useful in programming languages. Their greatest utility is that these lists are mutable, meaning that any type of change can be made to their items once they are created.

Let’s learn how lists are created in programming.

How are lists created?

Creating lists in Python programming is very easy. Before creating a list, we need to store our list in a variable; that variable serves as the name of the list.

To reuse the same list, there is no need to write that list again; instead, we can reuse it through the name of that list. The variable in which the list is stored is an “Identifier”.

To create a list, first an identifier or variable is written and after putting equal to operator in front of it, all the items or values ​​are written inside square bracket [] separated by commas.

The number of items in a list is not fixed; you can keep as many items as you want inside a list.

Slicing and Indexing of Lists in Python

In Python programming, the process used to retrieve a single item or a group of items (slices of the list) from a list is called slicing.

To retrieve an item or a group of items from a list, each item is assigned a specific number, called an index number, and this process is called indexing.

Slicing and indexing are used extensively in the coding world.

In indexing, all the items in a list are numbered in two ways.

In the first type, numbering starts from 0 from the first item to the last item.

In the second type, numbering starts from -1 from the last item to the first item.

After numbering the items according to the rules, we can use index numbers to use them in the code. To use items, write the index number of that item in square brackets along with the name of the list.

If the item in your list is a string or a multi-digit number, then all the characters/numbers in that string are numbered in the same manner.

Syntax of Slice: Name of the list [start:stop]

Syntax for accessing individual items using index numbers:

-> Name of the list = [List]

-> Name of the list [index number]

-> Output: item of the corresponding number

USES OF OPERATORS IN LISTS

In Python, operators are symbols used to perform a specific function. To learn more about operators, read the article “Fundamentals of the Python Programming Language.”

We also use operators in compound data structures such as lists, tuples, dictionaries, and sets.

Arithmetic Operators

The +, -, ×, and ÷ symbols used to perform mathematical operations such as addition, subtraction, multiplication, and division are called arithmetic operations. All these symbols have a defined function that allows us to perform operations on data structures.

The Concatenation (+) operator

concatenates two or more lists. Order is important in lists; therefore, the items in the list after the + are added after the items in the list before the +.

A list can only concatenate with another list.
A list can never concatenate with a number, complex number, or string. Let’s try to understand this better with an example.

The Replication (*) operator

It is used to print a list more than once. This operator can only be used between a list and a number. Replication between multiple lists is not possible,

as the number indicates how many times you want to replicate the list. Let’s understand this better with the help of an example.

Apart from arithmetic operations, relational operators are also used in lists. Relational operators use symbols like <,>,==,!=,<=,>=, etc. to compare two lists. To compare two lists, it is necessary that the items in both lists should be of similar type or comparable type.

Membership operators are also very useful in list data structures. They are used to check the membership of items in a list.

LIST FUNCTIONS (Compound Data Structures of Python)

Lists are mutable. Mutable means that after creating a list, it is possible to make changes to it, such as adding, removing, or replacing items. Certain functions are used to make these changes. Let us now discuss some specific Python list functions.

The Append() method

If you want to add an item to the end of your list, use the Append() method. This method allows you to add one item at a time, at the end of the list, i.e., at the -1 index position.

Syntax: Name of the list. append(item to be added)

Updating items using index numbers

If you want to replace an item in your list with another item, you don’t need to recreate the entire list; you can do this using the index number. The syntax for replacing a list item using the index number is as follows:

Syntax: Name of the list[index number] = new item

Deleting items from a list

The del function is used to remove any item from a list. The del function removes the item whose index number is written in square brackets after del.

Syntax of the del function: del name of the list [index number]

Pop method of deletion

The pop method is a special method for removing items from a list, in which the removed item is returned as output. If you don’t enter an index number within the pop bracket, it removes the last item in the list and returns that item as output.

Syntax: Name of the list.pop(index number)

In Python programming, we can perform other operations besides adding, removing, or replacing items in our list.

Syntax of some other operation methods: list object.method name()

The index method

Using this index method, you can output the index number of any item in your list.

The extend method

The extend method is also used to add items to a list, but it differs from the append method. This method allows you to add more than one item to your list at a time. Like the concatenation operation, the added items are added to the end of the list. However, to add items to a list using this method, it is necessary to write those items in list form.

The insert method

The append and extend methods can only add items to the end of the list. If you want to add an item in the middle of your list, you can use the insert method.

This insert method allows you to add any item to your list at any position you want.

In this method, the index number at which you want to place your item is written inside the parentheses of insert.

The remove method

If you want to remove an item from your list and you know its index number, you can easily remove it using the pop method. However, if you don’t know its index number, you can use the remove method to remove it using the item itself.

In the parentheses of remove, you must write the item you want to remove from your list. If that item appears more than once in the list, this method will remove only the item whose index number is closest to 0.

The clear method

The clear method is used to convert any type of list into an empty list. This method removes all items from your list at once, converting the list into an empty list.

The count method

Sometimes the same item appears more than once in our list. In such a case, the count method is used to count how many times a particular item appears.

When we write any item in the list within the parentheses of count, we get the frequency (number of occurrences) of that item in the output.

The Reverse Method

This reverse method writes all the items in our list in their original place in reverse order, giving us the reverse form of that list.

The sort method

As you’ve already learned, order is important in lists. This sort method, by default, arranges all the items in your list in ascending order.

This doesn’t create a new list, but simply changes the position of items within the same list. If you want to sort your list in descending order, the sort method can also be used for this.

TUPLESA

What is a Tuple? (Compound Data Structures of Python)
Tuples are one of the compound data structures in the Python programming language, somewhat similar to lists but not actually lists.

Tuples are ordered sets of values ​​of different data types, enclosed within parenthesis boxes and separated by commas. A notable difference between tuples and lists is that tuples are immutable, while lists are mutable.

For example, you can understand that a tuple is like a brand and a list is like products. A company is known for its specific brand; it cannot change its brand, but it can create and modify many different types of products within a brand.

How are tuples created?

To create tuples in Python programming, we first need to choose an identifier. This identifier must be unique.

After writing that identifier, the equal to operator is placed before it and all the values ​​are written inside parenthesis brackets, separated by commas. You can store as many values ​​as you want in a tuple.

Slicing and Indexing of Tuples in Python

We have discussed indexing and slicing in detail in the “List” section of this article. Indexing is done in the same way for tuples. Its syntax is the same for tuples as well.

Uses of Operators in Tuples

In Python, operators are symbols used to perform a specific function.

Operators are used in tuples to perform specific functions, such as concatenation, replication, and comparisons.

Arithmetic Operators

The +, -, ×, and ÷ symbols used to perform mathematical operations such as addition, subtraction, multiplication, and division are called arithmetic operations.

Each of these symbols has a defined function, allowing us to perform operations on data structures.

The concatenation operator (+) concatenates two or more tuples. Order is important in tuples, so the elements of the tuple written after + are added after the elements of the tuple written before +.

A tuple can only concatenate with another tuple. A tuple can never concatenate with a number, complex number, or string.

If you concatenate your tuple with a single element tuple, Python will treat that single element tuple as a number or string, not as a tuple, and hence will generate a concatenation error. However, adding a comma before that single element will not generate an error.

Replication operator (*)

The Replication operator is used to print a tuple more than once. This operator can only be used between a tuple and a number; replication between two tuples is not possible.

Apart from arithmetic operators, relational operators are also used in lists. Relational operators use symbols like <,>,==,!=,<=,>= etc.

to compare two lists. To compare two lists, it is necessary that the items in both lists should be of similar type or comparable type.

Membership operators are also very useful in list data structures. They are used to check the membership of items in a list.

Packing and unpacking of tuples.

Packing is the process of combining multiple elements to form a new tuple. Because these tuples are immutable, you cannot make any changes to them after they are created, such as adding, substituting, or deleting elements.

Although you can use indexing and slicing to access its elements individually, the process of separating all the elements of a tuple and bringing them back to their individual form is called unpacking. This unpacking can also be called traversing a tuple, which can be done using a for loop.

Tuple function:

The Python programming language has several built-in functions and methods for using and manipulating tuples. We will discuss some of these in this article.

The len() method

This is a function that returns the length of a tuple. Tuple length refers to the total number of elements in the tuple. Even if your tuple is nested, the entire tuple inside it will count as a single element.

Syntax: len(Name of the tuple)

The max() method

This function can only be used for float or integer tuples. This function allows you to get the maximum integer or maximum float value of your tuple.

Syntax: max(Name of the tuple)

The min() method

This function, unlike the max() function, identifies the smallest integer or float value in your float or integer tuple and returns it to you as output.

Syntax: min(Name of the tuple)

The index() Function

The index function finds the index number of a tuple’s values. It matches the tuple element written in the parentheses before the index with the other elements of the tuple, and outputs the first index position it finds, starting from index 0.

Syntax: Name of tuple.index(element)

Count() Function

The count() function is a frequency counter that tells you the number of occurrences of a specific element of your tuple, written inside its parentheses, i.e., the number of times it appears in the tuple.

Syntax: Name of the tuple.count(element)

Tuple() function

This function is primarily used to create tuples. It allows you to create any type of tuple, such as an empty tuple, a single tuple, or a long tuple. Leaving the parentheses empty creates an empty tuple.

To create a single or long tuple, you must write all the elements inside the parentheses, so the function arranges them into a tuple.

Sets

Compound Data Structures of Python – Sets are part of the Python programming language’s compound data structures, just like lists and tuples.

Being a part of compound data structures, sets share similar properties. However, a distinct difference between sets is that they allow you to write an element only once within a set. Even if you write the same item more than once within a set, it will be written only once by default.

Basically, sets are a group of different items, separated by commas within curly brackets {}.

How are sets created?

To create sets in Python programming, first choose an identifier. This identifier must be unique. After writing that identifier, all items are written by prefixing them with an equal to operator, separated by commas within curly brackets.

You can enter as many values ​​as you want within a set, but those values ​​must not be repeated.

Slicing and indexing of sets in Python

All items in sets are unique, so there is no need to assign a specific identity to each item; each item has its own unique identity.

For this reason, indexing and slicing cannot be performed on items in sets.

Sets Mutable or Immutable

Items in a set are immutable, but the rest of the set is mutable, meaning that items in a set cannot be changed, but the entire set can be changed or updated.

Mixed Data Type Set

We can insert values ​​of all data types, such as string, integer, float, etc., into sets, but we cannot insert a list as an item in a set because lists are mutable and items in sets are immutable.

However, you can insert tuples as items in sets because tuples are immutable.

Empty Set

An empty set means a set that does not contain any elements. Because the elements of a set are enclosed in curly brackets,

we cannot write empty curly brackets as an empty set. The set() function is required to create an empty set.

Syntax of Empty Set: Name of the set = set()

Operations in Sets – Compound Data Structures

Like lists and tuples, elements can be added or removed from sets, but an existing item cannot be replaced with another. The add() method is used to add an element to a set, and the remove() method is used to delete an element.

Add Method

The add() method is used to add an element to a set. In this method, you write the element you want to add to the set within the brackets before add. This adds the element according to its sequence by default.

Syntax: Name of the set. add(Element to be added)

Remove Method

The remove() method is used to remove an element from a set. In this method, you write the element you want to remove from the set within the brackets after remove. The element you write inside bracket() must already be in the set.

Syntax: Name of the set. remove(element)

Iterating Over a Set

Iterating a set means printing all the elements of the set in a vertical line (one below the other). A for loop is used to iterate a set.

We will discuss the for loop in the next article on Fundamentals of Python.

The syntax for iterating a set is:

Name of the set = {..elements..}

for i in Name of the set:

print(i) In this syntax, i acts as a variable whose value will initially be the first element of the set, and so on until the last element of the set.

Set Functions and Method of Compound Data Structures of Python

all(): This function returns true if all elements of a sequence or set are true.
any(): This function returns true if any single element of a set is true.

It also returns true if more than one or all elements of a set are true.
len(): This function counts the length of a set, i.e., the number of elements in the set.

max(): This function returns the maximum value of the set, i.e., the largest value.
min(): This function returns the minimum value of the set,

i.e., the smallest value.
set(): This function converts any sequence to a set.

sum(): This function adds all the elements of a set and returns its total value.

clear(): The clear function is used to remove all elements from a set at once.
copy(): This function is used to copy a given set.
difference(): This function calculates the difference between two sets and returns it to the output.

difference_update():

This function updates the difference between two given sets.
discard(): This function removes the element you write inside its bracket from the given set.
intersection(): This function encloses the identical elements of two given sets in a separate set and returns it to the output.

intersection_update(): This function is used to find the identical or common elements in two sets.
isdisjoint() : If there is not a single element common in the two given sets, then it is called a disjoint set. If the two given sets are disjoint, then this function returns “true”.
issubset() : If all the elements of one of the two given sets are also present in the other set, then this set is called a subset of the other set.

If any one of the two given sets is a subset of the other, then this function returns true as output.

issuperset() : If one of the two given sets is a subset of the other set, then that second set is called a superset of the first set. This function returns true when the given set is a superset.

pop(): This method is also used to remove an element from a set. After removal, it also returns that value to the output.
symmetric_difference(): This function finds the uncommon elements of any two sets and outputs them in a set.

symmetric_difference_update(): This function finds the uncommon elements of any two sets and returns true or false value depending on their presence or absence.
union(): This function is used to create a new set by joining two sets. It works exactly like the concatenation operation.

update(): This function is used to update the set.

Dictionary

If you’re learning Python or any other programming language, you’ll need a dictionary.

Just like English, each programming language has its own unique words and phrases that it frequently uses. Earlier in this article,

we discussed two compound data structures in Python. In this section, we’ll discuss dictionaries. They differ significantly from lists and tuples.

A Python dictionary is a collection of key-value pairs in which the keys and values ​​are related. The order of dictionaries is not important, so the key:value pairs change their positions in each output.

How are dictionaries created?

Compound Data Structures – Key:value pairs in dictionaries are enclosed within curly brackets {} and separated by commas (,).

Keys and values ​​are written within single, double, or triple quotes, separated by a colon (:) to indicate a relationship between them.

Syntax: d= {“key”: “values”, “key” : “value”}

An empty dictionary is like an empty container in which you can put as many key:value pairs as you want. A pair of curly brackets {} with nothing written inside it is called an empty dictionary.

There are many different methods for creating dictionaries, of which we will discuss the main three methods in this article.

Initializing a Dictionary

In this method, we can directly create a dictionary by writing any identifier for the dictionary, followed by the equal to operator, and then writing the key:value pairs in curly brackets.

Adding key:value pairs to an empty dictionary

In this method, an empty dictionary is first created and then key:value pairs are added to it one at a time. We can create an empty dictionary in two ways.

First, we can create an empty dictionary directly by placing empty curly brackets before the identifier, and second, we can do it by using the dict function.

After this, you can add as many key:value pairs as you want to your empty dictionary.

Creating a Dictionary from Name and Value Pairs

You can create a dictionary using key-value pairs in four ways.

Specifying key-value pairs as keyboard arguments to dict() functions

A dictionary is created by specifically writing key-value pairs within the Dict function. Keys are entered as arguments and values ​​as their values. By default, Python converts these argument names into string-type keys.

Specify comma-separated key: value pairs

In this, key:value pairs are written within curly brackets within the Dict function and separated by commas.

Specify keys and corresponding values

In this process, all keys and values ​​are written within separate parentheses within the dict function in the Zip function, where values ​​are assigned to the keys in the order they are written.

Specify the key valve separately. Sequence in the form of a sequence.

In this process, key:value pairs are written in the form of a list inside the dict function, separated by commas. This dict function converts the entire list into a dictionary.

Nested Dictionary

A dictionary that contains one or more other dictionaries is called a nested dictionary.

Keys in a dictionary are the identity of their values, so you cannot assign a value to the same key twice within a dictionary. Keys are unique.

In a nested dictionary, another dictionary can only be inserted as a value within the original dictionary. An entire dictionary cannot be written in place of a key:value pair.

Dictionaries are mutable

Dictionaries are mutable. You can make changes to them as per your requirement. For example, you can add a key:value pair, delete a key:value pair, and replace a key:value pair.

Addition of an element in a dictionary: You can add a new key-value pair to your dictionary only if that key does not already exist in the dictionary. To add a key-value pair, you can use a simple syntax:

Syntax: Dictionary name [Key] = value

Deletion of an element from Dictionary:

You can delete an element from your dictionary in two ways. First, you can delete an element using the del method, using only the key.

Syntax: del dictionary name [key]

Second, you can delete an element from the dictionary using the pop method.

Syntax: Dictionary name.pop (key)

The pop method not only deletes the key:value pair but also outputs it.

Updating an existing element in a Dictionary: Key-value pairs in a dictionary can never be changed simultaneously, but the values ​​of keys can be exchanged. The syntax for changing the value of a key that already exists in the dictionary is:

Dictionary Name [key] = value

Dictionary Functions and Methods – Compound Data Structures
The Python programming language has several built-in functions and methods for using and manipulating dictionary structures, some of which we will discuss in this article.

The len() method: The len() method is used to count the length of a dictionary, i.e., the number of elements within it. In a dictionary, a pair of keys and elements is considered one. Syntax: len(Dictionary name)

The clear method: The clear function is used to delete all the elements in a dictionary at once. Using it, any dictionary is converted to an empty dictionary.

This method deletes only the dictionary’s elements, not the dictionary itself, thus converting the dictionary to an empty dictionary.

Syntax: Dictionary.clear()

The get method:
With this method, we can retrieve the value of any element in the dictionary by using its key.

Syntax: Dictionary.get(key)

We hope you enjoyed this article on Compound Data Structures of Python and will continue your interest in the Python language.

It takes a lot of effort to provide good information on such topics, and if you share it with your friends, it will benefit them and encourage us. Please share it in educational groups. Thank you.

Leave a Reply

Your email address will not be published. Required fields are marked *