Thursday, May 24, 2018

python chap 6(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 )

TUPLE

A tuple is another sequence data type and they are immutable means we cannot update the items in the tuple they consists of number of items separated by commas the items may be of different types also. Unlike list tuples are enclosed within parenthesis. These are the main difference between lists and tuples that list are enclosed in square brackets and tuples are enclosed in parenthesis and tuples are immutable and lists are mutable. Like in the Lists tuples also can be indexed sliced and concatenated the tuples with one element can be written by placing a comma after the element and we can create an empty tuple and here are some examples for them 
 
my_tuple = ('jack',256,'mom',12.35,'lisy')
next_tuple = ('nadeem',457)
empty_tuple = ()       #empty tuple
single_tuple = (54,)  # a tuple with single element
print (my_tuple)       #prints complete tuple
print (my_tuple[0])   #prints first element of the tuple
print(my_tuple[1:3]) #prints elements from second to third element
print(my_tuple[2:])   #prints element from second to end
print (next_tuple*2)   #prints tuple two times
print (my_tuple + next_tuple ) #prints concatenated tuple

now we can see the results
('jack',256,'mom',12.35,'lizy')
jack
(256,'mom')
('mom',12.35,'lizy')
('nadeem',457,'nadeem',457)
('jack',256,'mom',12.35,'lisy')
And like in lists we can't update values in tuple because they are immutable here is an example below were we try to update a tuple and a list the list updates the value but the tuple will show error as the tuple can't be updated
my_tuple = (24,54,65,75)
my_list = [24,54,47,85]
my_tuple[2] = 321 #invalid syntax with tuple
my_list[2] = 542  # the syntax is a valid syntax



PYTHON DICTIONARY

                      Python dictionary are unordered set of key value pairs they are like associative arrays in other languages unlike sequences which are indexed by a range of number the dictionary are indexed by key's which can be any immutable type but are usually numbers and strings . Tuples can be used as key if they only contains strings, numbers or tuples if a tuple contains any mutable object directly or indirectly they can't be used as key's .Values in the dictionary are arbitrary objects
                     Dictionaries are enclosed by curly braces  ({}) and  a pair of braces creates empty dictionary and coma separated list of key:value pairs adds initial key:value pairs to the dictionary this is also the way dictionaries are written on the output and values can be assigned and accessed using square brackets.
                     In dictionary we can store a value with a key and access the value using the key it is also possible to delete the key value pair with the del if you store a value using a key that is already in use the old value in the already used key is forgotten. It is an error to extract a value using a non existent key.here some examples are given below

                       
my_dict = {}
my_dict = {'name':'nads','age':27,'height':6.6,'hair':'blue'}
my_dict1['name']={'majeed'}
my_dict1[27] = {'age'}
print (my_dict) #prints the complete my_dict
print(my_dict1['name']) #prints the value of 'name' key
print(my_dict1[27])    #prints the value of 27
print(my_dict.keys()) #prints the keys in my_dict


Now we can see the results
{'name' : 'nadeem','age':27 , 'height':6.6 ,'hair':'blue'}
majeed
age
['name','age','height','hair']

Data Type Conversions

Some times we need  conversions between built in data types like to change a data type of a value into another data type to convert a data type we simply use the type name as a function(function is a block of  organised reusable code to perform a single related action more on functions will be described on coming lessons) there are several builtin functions to perform conversion from one data type to another data type these functions returns the converted result (here the return means in a function after the process is complete in a function the result is outcomes as return you will understand more on returns in the coming lessons).Here are some examples of some builtin conversion functions given below

  • int (x ,base):This function converts any data type to integers base specifies the base in which the string is if data type is string
  • float (x) : This will convert any data type to floating point numbers
  • str(x) :Converts object x to string representation
  • complex(real,imag):Creates a complex number
  • hex(x): Converts integer to hexadecimal
  • oct(x):Converts integer to octal string 
  • ord(c):Converts a character to integer
  • tuple(s):This function is used to convert to a tuple 
  • list(s):This function is used to converts  s  to list
  •  dict(d):This function is used to convert a tuple of order (key,value ) into a dictionary 
  • chr(x):Converts an integer to character 
  • unichr(x):Converts an integer to Unicode character 

 learn more in the next post thanks for viewing this post guys follow by email to get new lesson updates of this blog and don't forget to follow ,share and comment your suggestions


No comments:

Post a Comment