Skip to main content
Chemistry LibreTexts

2: Introduction to Python

  • Page ID
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)

    \( \newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\)

    ( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\)

    \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)

    \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\)

    \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)

    \( \newcommand{\Span}{\mathrm{span}}\)

    \( \newcommand{\id}{\mathrm{id}}\)

    \( \newcommand{\Span}{\mathrm{span}}\)

    \( \newcommand{\kernel}{\mathrm{null}\,}\)

    \( \newcommand{\range}{\mathrm{range}\,}\)

    \( \newcommand{\RealPart}{\mathrm{Re}}\)

    \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)

    \( \newcommand{\Argument}{\mathrm{Arg}}\)

    \( \newcommand{\norm}[1]{\| #1 \|}\)

    \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)

    \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    \( \newcommand{\vectorA}[1]{\vec{#1}}      % arrow\)

    \( \newcommand{\vectorAt}[1]{\vec{\text{#1}}}      % arrow\)

    \( \newcommand{\vectorB}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vectorC}[1]{\textbf{#1}} \)

    \( \newcommand{\vectorD}[1]{\overrightarrow{#1}} \)

    \( \newcommand{\vectorDt}[1]{\overrightarrow{\text{#1}}} \)

    \( \newcommand{\vectE}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash{\mathbf {#1}}}} \)

    \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)

    Python as OOP (Object Oriented Programming)

    There are multiple programming paradigms of which Object-Oriented-Programming (OOP) is one, and it may be easiest to understand OOP by comparing it to the Procedural Programming (PP) paradigm.  In Procedural Programming you have a bunch of functions (procedures) that perform tasks and these are structured to be called on and executed sequentially as they operate on the data through assignment to variables.  In OOP you have objects that contain functions and variables.  In python these objects are classes, and a class can have methods (functions) and attributes (variables) and so a task can be coded by calling on an object that performs the desired process.  These classes can be stored in libraries that can be imported into a program when needed, and thus do not have to be coded directly in a program.  For example, you could have a library of code designed to read environmental sensors, and that could have classes related to air quality sensors, water quality sensors and temperature sensors.  By importing these objects you can then communicate with them through their associated methods (functions related to each class), and the coding becomes simplified.

    Key Characteristics of Object Oriented and Procedural Programming.

    1. Encapsulation: In OOP each Python class has methods (functions) and attributes (variables) that are encapsulated into the object, and can thus be hidden from the code you are writing. In procedural programming all of this information must be in the program body itself, as if it was a giant object.
    2. Inheritance:  In OOP you can create a hierarch of classes where subclasses inherit the attributes and methods of the superclasses.  For example you could have a class of human, which has a birth date, age, etc.  Then you could have a subclass of professors and students.  The students may have a data attribute (variable) related to grades, which the professor would not have, but they both have an age, birthdate, etc.,  This allows for simplification of the code, and if you made a class of people, you could then import it into any program and not have to rewrite it.
    3.  Polymorphism:  In OOP you can have different methods with the same name that have different roles, depending on the object that defines them.  That is, the name space can be limited to the object, while in procedural all names are essentially global in nature.  So for example, if you had a class called "quiz" and another called "exam", you could have a method (function) called "average", which would work on different data if it was a quiz method or an exam method.

    Libraries, packages and modules

    • Module is a single file with code that can be used in multiple scripts
    • Package is a directory structure of multiple modules
    • Library is the most general structure, and is a collection of packages and/or modules

    This page titled 2: Introduction to Python is shared under a not declared license and was authored, remixed, and/or curated by Robert Belford.

    • Was this article helpful?