Python Fundamentals

Mastering Python fundamentals will empower contribution to meaningful AI-driven projects and initiatives.  


Python is an open-source Machine Learning (ML) programming language.  It is dominant and widely adopted by the developer community.  Its English syntax makes it easy to use and appeals to novice users as well.  The architecture is based on reusable code that is expressed through functions.  These functions expedite ML model development where the majority of code is pre-written so that developers only have to model 10-15% lines of code for desired output.  


More on Functions:

Functions are machines that perform specific tasks. 

  • Built-in = predefined function available in Python interpreter including print (), len (), sum (), max (), min (), input (), int(), float (), and str ().

  • User defined = users can create their own function using def keyword followed by function name.

  • Lambda = anonymous function expressed as one single line commonly used for simple tasks like sorting, mapping, filtering; these are concise and easily read and implicitly return value or expression.

  • Recursive = function that calls itself during execution.  This technique is used to solve complex problems by breaking down problems into self-similar problems.


Role of Python in Generative AI:


  • Primary Language for AI = Python’s simplicity and readability make it go to language for developers based on supported libraries.


  • Generative Models = used to implement various generative models servers like GAUs, VAEs, Transformer application for image, text, audio.


  • Integration and Deployment = seamless integration with APIs, web servers, and cloud platforms for scalable AI models.


  • Extensive Libraries = facilitate data manipulation and processing tasks critical for AI development.


  • Vibrant Community = large active, abundant educational resource and support ongoing learning and development of Generative AI.



Five characteristics of Python Object-Orientation:

Reusable blocks of code allow for readability, modularity, and maintenance.  Popular benefits include orchestration and automation.  Everything in Python is an Object (instance).  Objects are entities that interact.  Attributes in Python are characteristics of an object; variables and methods that store data about behavior.


  • Classes = serves a blueprint to define attributes and methods.

  • Encapsulation = class is obfuscated and controlled through methods that promote data integrity and modularity.

  • Inheritance = hierarchical relationship; inherit attributes and methods from existing class that promote code reuse

  • Polymorphism = object suitability determined by methods and acts as common denominator that is automatically maintained.

  • Abstraction = layer to shield user from underlying complexities only exposing essential functionalities. 


Libraries = collections of pre-written code that a project can use as a package into functions to extend capabilities and simplify development.  


Popular Libraries:


  • NUMPY = universal standalone library appropriate for all field use especially science and engineering.  Numerical Python to enable operations on arrays for high level mathematical functions.  Syntax is ndarray.  Benefits are fast queries due to two techniques (1): Vectorization - absence of explicit looping, indexing.  (2) : Broadcasting - precompiled code.

Fixed size at creation

Needed same data type (size and measure)

Facilitates numerical Python

Core are arrays to hold many values under single name


  • PANDA = data analytical process of inferring insights from data manipulation and analysis.  Established techniques include grouping, joining, aggregating, resetting, renaming, return indexing.  Core are DataFrame and Series.  Good for structured data science so advantageous for ML. 


  • MATPLOTLIB =  static, interactive, animated data visualization offering diverse plot types and customization.   


  • SCIKIT LEARN = used commonly for ML tasks with widely available tools for data mining, model building, Classification - categorize based on predefined classes, Regression - predict numerical values, Clustering - grouping data points together without prior knowledge of labels, and Capability to reduce number of variables while preserving properties of original data.


  • TENSOR FLOW = primarily used for ML and AI with strong emphasis on neural networks.  Automatically calculates gradients of operations.  Includes API, Keras, for high-level interface for training models.  Outputs such as image recognition, NLP, and various computational simulations as well as integration with Numpy. 


Pyhthon Data Elements

How Python values are displayed and organized

Immutable, Mutable, Ordered, Unordered, Indexed, Duplicative

Numeric, Boolean, String, Binary, Data Structure 


Numeric 

Primitive individual building blocks of data


Syntax = int: Integers, whole numbers

Syntax = float: numbers with decimal points

Syntax = complex: complex numbers

Use Case: Counting and Quantifying; Measurements and Continuous data; Math; Engineering; Numerical Python; Conditional logic.  


Boolean

Logical values

Syntax = bool: true or false

Use Case: Conditional statements; Interation control; Tracking program state; Data validation; Input checking; Function returns.


Binary

Syntax = bytes: Immutable sequences of intermediate source code

Use Case: Network communication; Encoding and decoding text; Direct manipulation of memory; Data serialization.


Syntax = bytearray : Immutable sequences of source code

Use Case: Inherited functionality of byte capabilities in addition with better performance.


Syntax = memoryview: visibility into another’s object memory without copying.  

Use Case: Handling large datasets as it avoids memory overhead; Shared memory in multiprocessing.  



String 

Syntax str “or “” Immutable, Indexed, Ordered, Indexed

Use Case: Representing text data; File Operations, Data Cleaning; Error handling and logging; Natural Language Processing; Generating dynamic web content



Popular Data Structures

Enable efficient organization and manipulation of values

Formed from primitive data elements

Immutable, Mutable, Ordered, Unordered, Indexed, Duplicative



  • Tuples =  Syntax tuple () Immutable, Ordered, Indexed, Duplicative

Use Case:  Data Integrity where data needs to remain unchanged and constant; Hasability; Memory efficient and fast; Heterogeneous - ability to store different data types in a single collection sequence.


  • Sets = Syntax set {} Mutable, Unordered, No duplications

Use Case: Removing duplicates; Member testing; Math operations. 


  • Lists = Syntax list []  Mutable, Ordered, Indexed, Heterogeneous that can grow dynamically

Use Case:  Storing collections of data; Iterating; Processing; Implementing stacks; Data Handling - data added, removed, or modified; Algorithmic; Matrixed.


  • Dictionary = Syntax dict {} Unordered, Mutable collection of key-value pairs.  Each key must be unique and immutable while values can be of any data type.  

Use Case: Storing and retrieving data on specific identifier; Counting frequency; Mapping relationships; Implementing programming to store recursive computations; Parsing and generating APIs and web services.