Friday, May 25, 2018

python chap 7 (Hi friends this is the continuation of previous post so i kindly request you to read that before this post. As this blog is also prepared for beginners so i advice you that you don't need to by-heart this lessons just read and understand it if you have any doubt ask in the comment section)

SET TYPES:set and frozenset

The sets are unordered collection of unique items that is iterative and mutable the benefits of using sets other than list is that they can highly optimized method for checking whether a specific element is contained in the set this is base on a data structure known as hash table (hash tables are a type of data structure in which the address or the index of the data element is generated from a hash function that makes accessing the data faster as index value behaves as a key for the data value.hash table is advanced topic we will learn about hash table later) simply there is no duplicate elements in the sets set object also supports mathematical operations like intersection , union ,difference and symmetric difference
                                      Curly braces or the set() function can be used to create sets and to create an empty set you have to create set() not empty curly braces {} this will only create an empty dictionary the sets are mutable they not have hash value and cannot be used as either a dictionary key or as an element of another set other one is the frozenset it is which is immutable and hashable its contents can't be altered after it is created therefore it can be used as a dictionary key or as an element of another set non empty set (not frozensets) can be created by placing comma separated list of  elements within braces for example {'vasu','ramu'}.in addition to the set constructor.
The constructor for both classes work the same for example
set([iterable])
frozenset([iterable])
which returns a new set or frozenset object whose elements are taken from iterable the elements of a set must be hashable to represent a set of set the innerset must be frozen set objects if iterable is not specified a new empty set is returned here are some examples given
Instance of sets and frozen set provides the following operation
  • len(s) : Returns the number of elements in the set (cardinality of s)
  • x in s : Test if x is a member of s 
  • x not in s : Test if is not a member of s
  • isdisjoint(other):Return true if the set has no elements in common with other set are disjoint if and only if their intersection is the empty set 
here are some samples of sets
my_set = {'swaraj','ramz','ayoob',25,56,89}
normalset = set(['arun','thariq',45,85])
print (my_set)
#will print the my_set set values
frozset = frozenset (["e","f","g"])
print(frozenset)  #this will print the complete frozenset values
More on this will be discussed later 

 OPERATORS

The operators are the special symbol in python that carry out arithmetic or logical computation the values which the operation takes place is the operands and the for example 5+3 the 5 and 3 are operands and + is the operator here we will learn about different types of python operators
  • Arithmetic operators : this operators which are (+) addition, (- )subtraction, (*) multiplication , ( / ) division, its simple every body knows what this operators do and there is one more operator which is floor ( // )  its a type of division were we get a result after discarding the fractional value we get a integer value simply the results are the quotients in which the digits after the decimal point are removed but if one of the operand is negative the result is floored rounded away from zero (towards negative infinity ) other one is the ( % ) Modulus divides left hand operand by right hand operand and returns remainder,(**)exponent performs exponential calculations on operators and here are some  examples for all
9//2 = 4
9.0//2.0 = 4.0
-11//3 = - 4
-11.0//3 = -4.0
#other examples for + , - , / , *
5 + 3 = 8
5 * 3 = 15
5 - 3 = 2
4 / 2 = 2
21 % 10 = 1 
2 ** 4 = 16

More on operators will be on the next post and thanks for viewing my post hope you like the post. Friends to get latest and next lessons on this subject please follow by email or follow me and don't forget to write your valuable suggestions on the comments and share this blog with your friends

No comments:

Post a Comment