Skip to main content

Collection in python

There are basic two datatypes in python 


1. Immutable datatype : numeric , string , tuple

2. Mutable : list , dictionary , set


Immutable : Content cannot be changed or modified using the indexing operator ( assignment  statement )

Mutable : Content can be changed or modified using the indexing operator ( assignment  statement )


Numeric : int, float , complex numbers

Sequence : list, string, tuple, range

Mapping : dictionary

Set : set , frozenset

Boolean : is used for making more conditions for 'if' in True and False form



Collections in python :


1) Dictionary :

It is used to map arbitrary keys to value

Only immutable ( content can be changed ) objects can be used as keys to dictionaries

Ex :  D = { 'a' : 10 , 'b' : [20, 30] }   

           

2)  List :

It can collect multiple values, multiple types of objects and data collection in single variable. 

It is mutable. So operation such like remove, append , insert can be done here

Append : to add an item or whole list to the end of an existing list 

Extend : to add every items separated by commas to the end of an existing list (due to for loop working behind)

Indexing, slicing can be done here

Indexingreferring to an element position of an iterable within an ordered list

Slicing : is way of retrieving values from a list. use to modify or delete the items of mutable

Ex : L = [1 , 'a' , [ 2 , 'b' ] , ( 3, 4 )]


3)  Tuples  : 

It is immutable 

Ex : T = ( 1, 2, 3)   or   T =  1, 2, 3







Comments

Popular posts from this blog

Python program to check if variable is of integer or string

Let's say if you want to input something of any datatype and want to get datatype only of it. So... Whenever you input some data whether it is string, integer or float like this: i = input('enter something here: ') means without int, str or float put before the syntax, that time your given input is always consider as string  or if you make it like this to add int before syntax; i = int(input('enter something here: '))  it always consider as integer and gives value error when you input string and same thing happens with float, So here is a program to solve this problem of input and get datatype var = input('input to check if variable is of integer or string: ') if var.isdigit() == False:     print(type(var)) else:     var1 = int(var)     print(type(var1))

Multiple classification from many of directories

  # %%  Import nessacary libraries import  numpy  as  np import  pandas  as  pd import  cv2 import  matplotlib.pyplot  as  plt import  os import  glob # %%   Keras Tensorflow libraries from  keras  import  layers from  keras.models  import  Model from  keras.optimizers  import  RMSprop , Adam , Nadam from  keras.preprocessing.image  import  ImageDataGenerator from  keras.layers  import  Input, BatchNormalization, Dense, Dropout, Conv2D, Flatten, GlobalAveragePooling2D, LeakyReLU from  keras.preprocessing.image  import  ImageDataGenerator, img_to_array, load_img # %%  Path path  =   r 'G:/Machine Learning/Project/Lego Mnifigures Classification/dataset' open_dir  =  os....

Digit Recognition

Here you can import digit dataset from scikit learn library which is in-built, So you don't need to download from other else Note: If you use visual code, I recommend you to turn your color theme to Monokai because it has a few extra and important keyword and attractive colors than other theme.   # %%  Import libraries import  numpy  as  np import  pandas  as  pd import  matplotlib.pyplot  as  plt import  random  # %%   Load dataset from  sklearn.datasets  import  load_digits dataset  =  load_digits() dataset.keys() output: d ict_keys(['data', 'target', 'target_names', 'images', 'DESCR']) You have to check all to direct print them Here DESCR is a description of dataset # %%   divide the dataset into input and target inputs  =  dataset.data target  =  dataset.target # %% ...