Learn Python By Example – Data Structure Basics

Data Structure Basics

Lists

“A list is a data structure that holds an ordered collection of items i.e. you can store a sequence of items in a list.” – A Byte Of Python

Lists are mutable.


/* Create a list of countries, then print the results */
allies = ['USA','UK','France','New Zealand',
          'Australia','Canada','Poland']; allies
['USA', 'UK', 'France', 'New Zealand', 'Australia', 'Canada', 'Poland']

/* Print the length of the list */
len(allies)
7

/* Add an item to the list, then print the results */
allies.append('China'); allies
['USA',
 'UK',
 'France',
 'New Zealand',
 'Australia',
 'Canada',
 'Poland',
 'China']

/* Sort list, then print the results */
allies.sort(); allies
['Australia',
 'Canada',
 'China',
 'France',
 'New Zealand',
 'Poland',
 'UK',
 'USA']

/* Reverse sort list, then print the results */
allies.reverse(); allies
['USA',
 'UK',
 'Poland',
 'New Zealand',
 'France',
 'China',
 'Canada',
 'Australia']

/* View the first item of the list */
allies[0]
'USA'

/* View the last item of the list */
allies[-1]
'Australia'

/* Delete the item in the list */
del allies[0]; allies
['UK', 'Poland', 'New Zealand', 'France', 'China', 'Canada', 'Australia']

/* Add a numeric value to a list of strings */
allies.append(3442); allies
['UK', 'Poland', 'New Zealand', 'France', 'China', 'Canada', 'Australia', 3442]

Tuples

“Though tuples may seem similar to lists, they are often used in different situations and for different purposes. Tuples are immutable, and usually contain an heterogeneous sequence of elements that are accessed via unpacking (or indexing (or even by attribute in the case of namedtuples). Lists are mutable, and their elements are usually homogeneous and are accessed by iterating over the list.” – Python Documentation

“Tuples are heterogeneous data structures (i.e., their entries have different meanings), while lists are homogeneous sequences.” – StackOverflow

Parentheses are optional, but useful.


/* Create a tuple of state names */
usa = ('Texas', 'California', 'Maryland'); usa
('Texas', 'California', 'Maryland')

/* Create a tuple of countries */
/* (notice the USA has a state names in the nested tuple) */
countries = ('canada', 'mexico', usa); countries
('canada', 'mexico', ('Texas', 'California', 'Maryland'))
/* View the third item of the top tuple */
countries[2]
('Texas', 'California', 'Maryland')
/* View the third item of the third tuple */
countries[2][2]
'Maryland'

Dictionaries

“A dictionary is like an address-book where you can find the address or contact details of a person by knowing only his/her name i.e. we associate keys (name) with values (details). Note that the key must be unique just like you cannot find out the correct information if you have two persons with the exact same name.” – A Byte Of Python


/* Create a dictionary with key:value combos */
staff = {'Chris' : 'chris@stater.org',
         'Jake' : 'jake@stater.org',
         'Ashley' : 'ashley@stater.org',
         'Shelly' : 'shelly@stater.org'
        }

/* Print the value using the key */
staff['Chris']
'chris@stater.org'
/* Delete a dictionary entry based on the key */
del staff['Chris']; staff
{'Ashley': 'ashley@stater.org',
 'Jake': 'jake@stater.org',
 'Shelly': 'shelly@stater.org'}
/* Add an item to the dictionary */
staff['Guido'] = 'guido@python.org'; staff
{'Ashley': 'ashley@stater.org',
 'Guido': 'guido@python.org',
 'Jake': 'jake@stater.org',
 'Shelly': 'shelly@stater.org'}

Sets

Sets are unordered collections of simple objects.

/* Create a set of BRI countries */
BRI = set(['brazil', 'russia', 'india'])
/* Is India in the set BRI? */
'india' in BRI
True
/* Is the US in the set BRI? */
'usa' in BRI
False
/* Create a copy of BRI called BRIC */
BRIC = BRI.copy()
/* Add China to BRIC */
BRIC.add('china')
/* Is BRIC a super-set of BRI? */
BRIC.issuperset(BRI)
True
/* Remove Russia from BRI */
BRI.remove('russia')
/* What items are the union of BRI and BRIC? */
BRI & BRIC
{'brazil', 'india'}

 

Python Example for Beginners

Two Machine Learning Fields

There are two sides to machine learning:

  • Practical Machine Learning:This is about querying databases, cleaning data, writing scripts to transform data and gluing algorithm and libraries together and writing custom code to squeeze reliable answers from data to satisfy difficult and ill defined questions. It’s the mess of reality.
  • Theoretical Machine Learning: This is about math and abstraction and idealized scenarios and limits and beauty and informing what is possible. It is a whole lot neater and cleaner and removed from the mess of reality.

Data Science Resources: Data Science Recipes and Applied Machine Learning Recipes

Introduction to Applied Machine Learning & Data Science for Beginners, Business Analysts, Students, Researchers and Freelancers with Python & R Codes @ Western Australian Center for Applied Machine Learning & Data Science (WACAMLDS) !!!

Latest end-to-end Learn by Coding Recipes in Project-Based Learning:

Applied Statistics with R for Beginners and Business Professionals

Data Science and Machine Learning Projects in Python: Tabular Data Analytics

Data Science and Machine Learning Projects in R: Tabular Data Analytics

Python Machine Learning & Data Science Recipes: Learn by Coding

R Machine Learning & Data Science Recipes: Learn by Coding

Comparing Different Machine Learning Algorithms in Python for Classification (FREE)

Disclaimer: The information and code presented within this recipe/tutorial is only for educational and coaching purposes for beginners and developers. Anyone can practice and apply the recipe/tutorial presented here, but the reader is taking full responsibility for his/her actions. The author (content curator) of this recipe (code / program) has made every effort to ensure the accuracy of the information was correct at time of publication. The author (content curator) does not assume and hereby disclaims any liability to any party for any loss, damage, or disruption caused by errors or omissions, whether such errors or omissions result from accident, negligence, or any other cause. The information presented here could also be found in public knowledge domains.  

Google –> SETScholars