Showing posts with label del. Show all posts
Showing posts with label del. Show all posts

Jun 29, 2020

Dictionary in Python | Initializing | Deletion | Addition | Coding Winds |

Python Dictionary

Hello guys, we will be discussing about Python Dictionary. It is an unordered representation and collection of items. Each item has a key/value pair. They are to obtain value when the key is known.

Initializing of dictionary

Initializing a dictionary requires the elements to be in between the {} brackets. Each item has a key and a corresponding value expressed as a pair. Representation is shown below,

Output

As seen we have used built-in function, dict(), to create a dictionary.

Accessing elements from dictionary

We use indexing to access other data types but in dictionary, we use keys. Keys can be used either inside square brackets [] or with the get() method.

KeyError is raised in case a key is not found in the dictionary. On the other hand, the get() method returns None if the key is not found.

Output

Changing and Adding of Elements

Dictionaries are mutable. We can easily add or change items using an assignment operator.

Output

Removal of Elements from Dictionary

We use pop() to remove a particular item from a dictionary. This removes an item with the provided key and returns the value. popitem()  can also be used, but this method selects the element randomly and return the item pair from the dictionary. clear() will empty the dictionary while del  will delete the dictionary or can be used to delete a specific item.

Output

Python Dictionary Methods

We have discussed few of the methods above like pop(), del, popitem(), get(), clear()

1)     Python Dictionary fromkeys()

It creates a new dictionary with the sequence of elements with a value provided by the user.

Syntax is,

dictionary.fromkeys(sequence, keys)

Sequence: - a sequence of elements which is to be used as keys for the new dictionary.

Value (optional): - values which will be set to each element of the dictionary by the user.

A dictionary from mutable object list,

Output

2)     Python Dictionary values()

It returns the list of all the values in the dictionary.

The syntax is,

dictionary.value()

This doesn’t take any parameters.

Output

What if the dictionary is modified?

Output

3)     Python Dictionary update()

The syntax is,

dict.update(other)

 This actually adds elements (from another dictionary, d1) if the key is not in dictionary, d. If the key is there then it updates the value of the key.

How update() works with an iterable?

Output

4)     Python Dictionary keys()

It returns the list of all the keys in the dictionary.

The syntax is,

dict.keys()

This doesn’t take any parameters

Look for the output on your own for better understanding.

5)     Python Dictionary items()

It returns the list of dictionary’s tuple pairs.

The syntax is,

dict.items()

This doesn’t take any parameters

Look for the output on your own for better understanding.

6)     Python Dictionary setdefault()

The setdefault() method returns the value of a key. If not, it inserts key with a value to the dictionary.

The syntax is,

dict.setdefault(key, defaultvalue)

Key: - key to be searched in the dictionary

Defaultvalue: - key with a value defaultvalue is inserted to the dictionary if key is not in the dictionary. If not provided, the defaultvalue will be None.

When key is the dictionary,

The output will be the value of the key which is 0.

When the key is not in the dictionary,

Check out the output.

 

PYTHON DICTIONARY COMPREHENSION

 

 Hello Python people, for this blog we have taken help from the book Python : The Complete Reference.

Hope all your doubts regarding this are clear now.

If you still have any doubt on this topic then do come to us via email "sophomoretechs@gmail.com" or via Instagram "@coding.winds".


Do subscribe to our daily blog update by clicking here.


Thank You!

 

 

 

 

 

 


Jun 24, 2020

Python Tuple | Creation | Deletion | Indexing | Coding Winds

TUPLE

Hello guys, we are going to study about, tuple. Tuple is almost similar to list. The only difference is that we cannot change the elements of a tuple when assigned.

Creating a tuple

Initializing a tuple is as simple as initializing a list, instead we keep our variables between () than [].



Note: - tuple can be created with the parentheses. This is known as tuple packing.   



Creating a single value stored tuple is bit tricky, check out the code and its output to find out.


Output


Accessing and indexing of elements of tuple

Accessing tuple is easy. We can use indexing to access elements of the tuple.

Negative indexing is also covered in the example below.

Check out the example


Output


Changing of elements of tuple

You can’t change the elements of the tuple. Tuple is immutable. If a nested tuple is given including a list, we can change the element of the list.

Else we can use + and * operators to add two or more tuple. This is called concatenation.

The + operator will consequently add the tuples.

 To repeat any element we can use * operator.

Deleting a tuple

We can delete the whole tuple with del. We cannot delete or remove a specific element from the tuple.

Tuple method

Methods that add or delete items of a tuple aren’t available. We have only index and count method to use.



Hello Python people, for this blog we have taken help from the book Python : The Complete Reference.


Hope all your doubts regarding this are clear now.

If you still have any doubt on this topic then do come to us via email "sophomoretechs@gmail.com" or via Instagram "@coding.winds".


Do subscribe to our daily blog update by clicking here.


Thank You!


Python list | Creation | Deletion | Indexing | Coding Winds

Python Lists

Hello guys, here we will talk about python list. List is the most useful and frequent used data type in python.

Creating list

It is very easy to create a list in python, anything that comes between these brackets, [ ].

It can be pure integer or pure alpha or mixed.

We will now look into a program,


Let’s have a look into the output too,


Similar to nested loops (check out loops, here), we have nested list:-


Accessing and indexing of list

Accessing elements of list in python isn’t that much of a difficulty.

Look at the code below,

Output will be not shocking for some and killing it for ;)

Change, add and deletion of elements in list

List is mutable, i.e. we can easily make a change or add or delete members of list.

Trying to go out of the index will bring up IndexError.  We use only integer while indexing other than an integer will give TypeError.

 

Changing of elements,


Output,


Adding of elements in the list can be done using two in-built functions, known as append() and extend()

If we have to add one item we will go for append() else if more than one we go for extend(). We should know that this will make the adding element go last in the list.


Predict the output.

We can actually add an element to a desired location by insert(),


Output


Deleting or removing elements of a list

We can easily delete elements of the list or the whole list with del.

Check out the example,


Output is as given,


Well as soon as we deleted the list the compiler no longer can detect the past initialized list.

We also have pop() and remove() to remove items from the list.

The clear() method will empty the list.


Output


We can also remove a range of elements from the list by assigning them to an empty list.


Output


Python list method

Above we have already discussed few of the methods such as append(), extend(), insert(), remove(),pop(),clear().

We will talk about the rest now.

Index()

It returns the index value of the element present in the list.

The syntax is,

list.index(element,start,end)

 

Note:- The index() method only returns the first occurrence of the matching element.


Output


count()

count() returns the occurrence of the element.

The syntax is,

list.count(element)


Check out the output on your own.

sort()

The sort() method sorts the elements of a list in a specific ascending or descending order.

The syntax is,

list.sort(key=....., reverse=......)

 There is an alternative python’s built-in sorted() function for the same.

Note: The simplest difference between sort() and sorted() is: sort() changes the list directly and doesn't return any value, while sorted() doesn't change the list and returns the sorted list.

Parameter of sort()

a)     Reverse: if True, then the sorted list is reversed.(actually descending order)

b)     Key: function that serves as a key for the sort comparison


Output


If you want your own implementation for sorting, the sort() method also accepts a key function as an optional parameter.

We will discuss this later as we haven’t yet discussed functions in python.

reverse()

This reverse() method reverses the elements of a list.

The syntax is,

list.reverse()

 The reverse() doesn’t take any argument.

The reverse() method doesn’t return any value. It updates the existing list.


Output


Another way for reversing a list is by using slicing operator, :


Output


If you need to access individual elements of a list in the reverse order, it's better to use reversed() function.


Output


Similar concept is also used for copy() 

Copying a list through slicing operator


Output

 List comprehension is a must topic in list, it makes the code easy and one liner and simple, what not. To learn, click here

Hello Python people, for this blog we have taken help from the book Python : The Complete Reference.


Hope all your doubts regarding this are clear now.

If you still have any doubt on this topic then do come to us via email "sophomoretechs@gmail.com" or via Instagram "@coding.winds".


Do subscribe to our daily blog update by clicking here.


Thank You!