Showing posts with label Programming. Show all posts
Showing posts with label Programming. Show all posts

Tuesday 16 June 2020

Sets in Python

Understanding Sets
                                                    

Set is one of the built-in data structures or data types baked into Python. Set offers functional features which are crucial for specific purposes.

Functional Features:

Unordered: Set items are unordered i.e. items order during creation is not preserved for further.

Unique: Items in set must be unique. No two same items are allowed.

Immutable: Items in set cannot be changed, only deletion and addition of items in set is allowed.

Sets do not support indexing as they are unordered. Hence, cannot be used to access and change the item.

Sets type also support different types of data types but as their items must be immutable so they cannot have mutable types like Lists, Dictionaries.

 

Creating Sets in Python

Set can be created in two ways. First, Using built-in set ( ) function:

set_1 = set( )                                           // only way to create empty set

set_1 = set ( iterable  )

set_1 = set ( [1, 2, 3, 4] )                      // generates list as iterable into set

set_1

{1, 2, 3, 4}

Creating sets using set() function, takes the argument as iterable.

set_1 = set( ‘sets’ )                                // argument as iterable

set_1

{ ‘e’, ‘t’, ‘s’ }

As sets features, order is not preserved and items are unique.

 

Second, Using curly braces ( { } ):

set_2 = { object1, object2, …}

set_2 = { ‘sets’, ‘tuples’, ‘lists’ }

set_2

{ ‘lists’, ‘tuples’, ‘sets’ }

set_2 = { ‘sets’ }                                             // argument as object

{ ‘sets’ }

Observe the difference, using set () function argument is considered as iterable but using curly braces( {} ) they are considered as objects even they are iterable individually.


as lists are mutable objects and hence cannot be used in the set (set using curly braces { }).

 

 

 

 

 

 

 

 

Saturday 13 June 2020

Methods in Python Lists

Lists Methods
The List data type has some important methods to make it useful.

append( ) : Add or append an item to the end of the list.

Syntax: list.append( item )

list = [1, 2, 3, 4, 5, 6]

list.append( 7 )

print( list )

Output:
[1, 2, 3, 4, 5, 6, 7]

extend( ) : Extend the list by appending all the items of the other list

Syntax: list_1.extend( list_2 )

list_1 = [1, 2, 3, 4, 5, 6]

list_2 = [7, 8, 9, 10, 11]

list_1.extend( list_2 )

print( list_1 )

Output:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]


insert( ) : Insert an item at the given position. If position is not provided then the
item will be added to end of the list.

Syntax: list_1.insert( pos, item )

list_1 = [1, 4, 9, 25, 36]

list_1.insert( 3, 16 )

print( list_1 )

list_1.insert( 49)

print( list_1 )


Output:
[1, 4, 9, 16, 25, 36]

[1, 4, 9, 16, 25, 36, 49]

remove( ) : Remove the first occurrence of the given item in the list. If no such item in
the list exists then, it raises ValueError.

Syntax: list_1.remove( item )

list_1 = [1, 4, 9, 25, 36]

list_1.remove( 25 )

print( list_1 )


Output:
[1, 4, 9, 36]

pop( ) : Pop off the item at the given index in the list and return it. If index is not
provided then, it pops off the last item and returns it.

Syntax: list_1.pop( [index] )

list_1 = [1, 4, 9, 25, 36]

list_1.pop( [2] )


Output:
9


count( ) : Return the number of times the given item appears in the list.

Syntax: list_1.count( item )

list_1 = [1, 4, 9, 25, 36, 25]

list_1.count( 25 )


Output:
2


clear( ) : Empty the whole list.

Syntax: list_1.clear(  )

list_1 = [1, 4, 9, 25, 36, 25]

list_1.clear( )

print( list_1 )


Output:
[ ]


sort ( ) : Sort the list items in the preferred order.

Syntax: list_1.sort( key = None, reverse = False  )

list_1 = [5, 1, 4, 6, 0, 8]

list_1.sort( )

print( list_1 )                            // sorts in ascending order

list_1.sort( reverse = True )

print( list_1 )                          // sorts in descending order 


Output:
[0, 1, 4, 5, 6, 8]

[8, 6, 5, 4, 1, 0]


reverse( ) : Reverse the items of the list in place.

Syntax: list_1.reverse(  )

list_1 = [1, 4, 9, 25, 36]

list_1.reverse( )

print( list_1 )


Output:
[36, 25, 9, 4, 1]