☰
Python Across Disciplines
with Python + AI Tool   
×
Table of Contents

1.1.   Introduction 1.2.   About the Author & Contact Info 1.3.   Book Conventions 1.4.   What (Who) is a Programmer? 1.5.   Programming Across Disciplines 1.6.   Foundational Computing Concepts 1.7.   About Python 1.8.   First Steps 1.8.1 Computer Setup 1.8.2 Python print() Function 1.8.3 Comments
2.1. About Data 2.2. Data Types 2.3. Variables 2.4. User Input 2.5. Data Structures (DS)         2.5.1. DS Concepts         2.5.2. Lists         2.5.3. Dictionaries         2.5.4. Others 2.6. Files         2.6.1. Files & File Systems         2.6.2. Python File Object         2.6.3. Data Files 2.7. Databases
3.1. About Processing 3.2. Decisions         3.2.1 Decision Concepts         3.2.2 Conditions & Booleans         3.2.3 if Statements         3.2.4 if-else Statements         3.2.5 if-elif-else Statements         3.2.6 In-Line if Statements 3.3. Repetition (a.k.a. Loops)         3.3.1  Repetition Concepts         3.3.2  while Loops         3.3.3  for Loops         3.3.4  Nested Loops         3.3.5  Validating User Input 3.4. Functions         3.4.1  Function Concepts         3.4.2  Built-In Functions         3.4.3  Programmer Defined Functions 3.5. Libraries         3.5.1  Library Concepts         3.5.2  Standard Library         3.5.3  External Libraries 3.6. Processing Case Studies         3.6.1  Case Studies         3.6.2  Parsing Data
4.1. About Output 4.2. Advanced Printing 4.3. Data Visualization   4.4  Sound
  4.5  Graphics
  4.6  Video
  4.7  Web Output
  4.8  PDFs & Documents
  4.9  Dashboards
  4.10  Animation & Games
  4.11  Text to Speech

5.1 About Disciplines 5.2 Accounting 5.3 Architecture 5.4 Art 5.5 Artificial Intelligence (AI) 5.6 Autonomous Vehicles 5.7 Bioinformatics 5.8 Biology 5.9 Bitcoin 5.10 Blockchain 5.11 Business 5.12 Business Analytics 5.13 Chemistry 5.14 Communication 5.15 Computational Photography 5.16 Computer Science 5.17 Creative Writing 5.18 Cryptocurrency 5.19 Cultural Studies 5.20 Data Analytics 5.21 Data Engineering 5.22 Data Science 5.23 Data Visualization 5.24 Drone Piloting 5.25 Economics 5.26 Education 5.27 Engineering 5.28 English 5.29 Entrepreneurship 5.30 Environmental Studies 5.31 Exercise Science 5.32 Film 5.33 Finance 5.34 Gaming 5.35 Gender Studies 5.36 Genetics 5.37 Geography 5.38 Geology 5.39 Geospatial Analysis ☯ 5.40 History 5.41 Humanities 5.42 Information Systems 5.43 Languages 5.44 Law 5.45 Linguistics 5.46 Literature 5.47 Machine Learning 5.48 Management 5.49 Marketing 5.50 Mathematics 5.51 Medicine 5.52 Military 5.53 Model Railroading 5.54 Music 5.55 Natural Language Processing (NLP) 5.56 Network Analysis 5.57 Neural Networks 5.58 Neurology 5.59 Nursing 5.60 Pharmacology 5.61 Philosophy 5.62 Physiology 5.63 Politics 5.64 Psychiatry 5.65 Psychology 5.66 Real Estate 5.67 Recreation 5.68 Remote Control (RC) Vehicles 5.69 Rhetoric 5.70 Science 5.71 Sociology 5.72 Sports 5.73 Stock Trading 5.74 Text Mining 5.75 Weather 5.76 Writing
6.1. Databases         6.1.1 Overview of Databases         6.1.2 SQLite Databases         6.1.3 Querying a SQLite Database         6.1.4 CRUD Operations with SQLite         6.1.5 Connecting to Other Databases
Built-In Functions Conceptss Data Types Date & Time Format Codes Dictionary Methods Escape Sequences File Access Modes File Object Methods Python Keywords List Methods Operators Set Methods String Methods Tuple Methods Glossary Index Appendices   Software Install & Setup
  Coding Tools:
  A.  Python    B.  Google CoLaboratory    C.  Visual Studio Code    D.  PyCharm IDE    E.  Git    F.  GitHub 
  Database Tools:
  G.  SQLite Database    H.  MySQL Database 


Python Across Disciplines
by John Gordon © 2023

Table of Contents

Table of Contents  »  Chapter 1 : Preliminaries : Foundational Computing Concepts

Foundational Computing Concepts

Contents

Overview

Outside of Computer Science (CS), we explore language, culture, and human experience. Computer programming is often perceived as a discipline for Science, Technology, Engineering, & Math (STEM) disciplines. However, learning and using programming in disciplines outside of CS complements the creative, investigative spirit of many disciplines such as Art, Music, Communications, English, History, Linguistics, Philosophy, World Languages & Culture, Writing, Rhetoric, and many others. Programming in these disciplines unlocks new perspectives, especially in digital humanities, data analysis in linguistic research, archival information retrieval, and interactive media. Understanding the basic concepts of programming hence becomes essential. This chapter presents foundational elements of programming in general and then introduces you to the Python programming language and Google CoLaboratory tools where we will begin our hands-on exploration of programming.

Input ⇨ Processing ⇨ Output

It will be important for you to become familiar with and be able to identify elements of, the foundational concepts of computing: input ⇨ processing ⇨ output. You will notice that the structure of this book is based on this concept. input ⇨ processing ⇨ output is also a general concept, meaning it is not a just Python concept, or even just a programming concept, but a concept that underlies all of computing. For example, computer hardware is designed around this concept. We have keyboards specifically to input data. We have computer Central Processing Units (see CPU later on this page) to process the data we input. And we have computer monitors (screens) to see the output produced by the processing of data that we input. input ⇨ processing ⇨ output is involved in nearly everything you do with computing devices and services (desktop computers, laptops, tablets, phones, smart devices, modern vehicles, kiosks, the Internet, bank ATMs, etc.).



Concept: Input ⇨ Processing ⇨ Output

In computing, we frequently use the terms input, processing, and output. Input is anything that enters the computer, such as characters typed on a keyboard, mouse clicks, taps on the screen, recording our voice, recording video, information from sensors, and many others. Processing is what happens to those inputs inside the computer, including calculations, storing values in memory or on hard drives, uploading input data to the Internet, etc. Output is the result of processing, including displaying results on the screen, printing on paper, playing video or sounds, etc.



As programmers, we often produce diagrams to help us visualize the requirements that our programs need to fulfill. We can start with an elementary diagram of the general concept of Input ⇨ Processing ⇨ Output like this:



Figure 1: Flowchart diagram of Input ⇨ Processing ⇨ Output


The symbols used in Figure 1 are examples of a type of diagram used in programming called Flowcharts. We will learn more about how to create flowcharts throughout this book. In Figure 1, each of the three shapes enclose one of the steps of Input ⇨ Processing ⇨ Output. This is a high-level generic example that could represent anything in computing. We can use this generic form to demonstrate a flowchart of a specific example like this:





Figure 2: Flowchart diagram of a Google search

Concept: Algorithm

An algorithm is a step-by-step procedure or a set of rules designed to perform a specific task or solve a particular problem. It is a fundamental concept in computer science and mathematics, acting as a blueprint for the actions needed to achieve a desired outcome. Algorithms are used in many applications, from simple tasks like sorting a list of numbers to complex operations like data encryption or machine learning. They are characterized by clarity, efficiency, and finite nature, meaning they must have a clear starting point, a definitive set of instructions, and an endpoint. The effectiveness of an algorithm is often measured by its speed and the minimal resources it requires to complete its task. In the digital world, algorithms form the backbone of computer programs, enabling computers to process data and make decisions based on predefined logical sequences. We should also realize that algorithms are not exclusive to computing or technology; we (humans) use algorithms all the time in daily life. For example, a cake recipe is an algorithm, the sequence of steps to tie your shoes is an algorithm, etc.


Figure 2 is a high-level Input ⇨ Processing ⇨ Output flowchart of a Google search. We call this high-level because there is no real detail about any of the three parts work, but it demonstrates the concept of Input ⇨ Processing ⇨ Output with an example of an everyday activity you are likely familiar with. We could speak about the diagram in Figure 2 with a bit more description like this:

I could have offered countless other examples here in place of the Google search example. Remember that Input ⇨ Processing ⇨ Output is foundational to computing, so while we used an Internet example in Figure 2, this is not only related to the Internet and web pages. With that in mind, can you think of other examples? As programmers, we must have a fundamental understanding of Input ⇨ Processing ⇨ Output as we proceed. We will continuously build upon the simple examples in Figures 1 and 2 above.



Programming Languages

What is a Programming Language?

A programming language is a formal language comprising instructions that produce various output in a computing device. Formal Languages are languages (such as logic, mathematics, computer programming, etc.) constructed by human beings as precisely defined to eliminate ambiguity and contain their syntax and semantics. Formal languages are in contrast to Natural Languages, which are languages (such as English, Spanish, French, Chinese, Russian, etc.) that evolved with humans over time to facilitate communication between them. Natural Languages contain syntax and semantics, but also include ambiguity, a key linguistic difference between Formal Languages and Natural Languages.

Concept: Natural Language

Natural language refers to any language that has evolved organically among humans for the purpose of communication. It is characterized by its complex structure and use in everyday spoken and written communication by people. Unlike artificial or constructed languages, natural languages develop over time through use in a community of speakers, and are subject to the dynamics of cultural evolution and human interaction. They consist of intricately structured rules for grammar, syntax, semantics, and phonetics, which enable a wide range of expressions, emotions, and ideas to be communicated. Natural languages, such as English, Mandarin, Spanish, and thousands of others, are central to human identity and social interaction, forming the basis for the vast majority of human-to-human communication, literature, and cultural expression.


Concept: Formal Language

A formal language, in the context of mathematics, computer science, and linguistics, is a set of strings of symbols governed by specific grammatical rules or a formal system. Unlike natural languages, which are used for everyday human communication and are rich in ambiguity and idiomatic expressions, formal languages are constructed with a precise, unambiguous structure. They consist of an alphabet – a finite set of symbols – and rules that define valid strings or expressions in the language. Formal languages are essential for various applications, such as programming languages that provide instructions to computers, mathematical notation that precisely conveys mathematical concepts, and theoretical models in linguistics. They enable clear communication of instructions and information in systems where ambiguity and misinterpretation must be minimized, making them indispensable in fields like computer science, logic, and mathematics.


Concept: Syntax

Syntax in language refers to the set of rules, principles, and processes that govern the structure of sentences in a given language, specifically the arrangement of words and phrases to create well-formed sentences. It acts as a guideline for constructing meaningful and grammatically correct sentences, determining how different parts of speech, such as nouns, verbs, adjectives, and adverbs, are combined. Syntax varies widely across languages, and it encompasses various components such as word order, agreement (e.g., between subjects and verbs, in number or gender), and the hierarchical structure of sentences. In essence, syntax is not about the meaning of individual words, but rather how these words are organized and interact with each other to convey complex ideas and relationships in a clear and logical manner. Understanding syntax is crucial in linguistics for analyzing sentence structure, in language learning for mastering a new language, and in computational linguistics for programming computers to process human language.


Concept: Semantics

Semantics in language is the study of meaning and interpretation of words, phrases, sentences, and larger units of text. It delves into how human beings comprehend and attribute meaning to language, bridging the gap between the abstract or symbolic representations of words and their real-world implications. Semantics considers the connotations and implications of language, including the literal meaning of words (denotation), the implied or suggested meanings (connotations), and how context influences the interpretation of language. This field is crucial for understanding not just what is explicitly stated, but also the nuances and complexities inherent in human communication. Semantics plays a pivotal role in linguistics, philosophy of language, and computational linguistics, where understanding and replicating human language comprehension is a key goal. It is fundamental in determining how language conveys different types of information, such as facts, beliefs, commands, desires, and questions, and in distinguishing between different meanings of words that appear similar.



Think of a programming language as a set of rules and syntax that translates human ideas into commands a computer can understand and execute. These languages vary in complexity and purpose, ranging from high-level languages like Python, which closely resembles human language, to low-level languages like Assembly, which are more directly related to machine code. Programming languages are the backbone of software development, enabling the creation of everything from simple scripts that automate mundane tasks to complex systems like operating systems and intricate algorithms. They serve as an intermediary, allowing humans to communicate with machines in a structured and efficient manner, turning lines of code into functional applications and systems.

There are well over 1,000 programming languages in existence1 and are often grouped is language families, like c-based languages, much like natural languages, like romance languages. Programming languages are also grouped in other ways as well, for example, by their influence on other programming langauges, as shown by the work of researcher Ramiro Gómez on his webiste programminglanguages.info.



Figure 3: Python influence network diagram.
Ramiro Gómez and programminglanguages.info ]

Levels of Programming Languages

Computers operate on machine language, also called low-level, meaning in a binary form. Everything in a computer is represented by binary numbers at its low level. For example, 01001000 01100101 01101100 01101100 01101111 is how a computer sees and stores the word Hello. For humans, 01001000 01100101 01101100 01101100 01101111 is indecipherable. So, over time, computer scientists have developed ways for humans to interact with machine language through interpretation processes. For example, a human user can type Hello and save it (in a word processor or other software). In the background, their input is stored, and "Hello" is interpreted and stored as 01001000 01100101 01101100 01101100 01101111. Later, when the user reopens that data, another process interprets the 01001000 01100101 01101100 01101100 01101111 stored in the computer and the user sees the word Hello. The user need not understand how this process works or even that it is happeningthis is called abstraction:

Concept: Abstraction

In computing, abstraction is a fundamental concept that involves simplifying complex systems by hiding the underlying technical details and exposing only the necessary aspects to the user or programmer. It allows for focusing on higher-level functionality without getting bogged down in the intricacies of lower-level operations. This is achieved by creating more straightforward and generic representations of processes or entities, which can be used to manage and manipulate complex systems more efficiently. Abstraction is evident in various forms, such as programming languages that abstract away machine code, software development methodologies that encapsulate details within objects or functions, and hardware design where complex circuitry is represented as simple, reusable components. By providing a way to manage complexity, abstraction makes it possible to handle large and intricate computing systems, enabling developers and users to interact with technology efficiently and effectively.

As programmers though, while we need to develop more depth of knowledge of the technical details, programming languages are also abstracted to varying degrees. One way in which programming langauges are categorized is by the degree of separation from the low level binary machine language of the machine, which we refer to as:

Concept: High-Level & Low-Level Languages
Full Concepts List: Alphabetical  or By Chapter 

Programming languages are often categorized as high-level or low-level, based on how close they are to human language or machine language. High-level languages, like Python, JavaScript, or Java, are closer to human language. They are more abstract, easier to read, and write, and are generally more intuitive for beginners. High-level languages handle a lot of complexity for you, such as memory management, which makes them more efficient for developing complex applications. On the other hand, low-level languages, like C or assembly language, are closer to machine language. They provide more control over what's happening at the hardware level, such as direct memory management. This can lead to more efficient programs in terms of execution speed and resource usage but often at the cost of longer and more complex code. While high-level languages are great for general-purpose programming, low-level languages are often used in systems programming, where direct interaction with the hardware is necessary.

Purposes (Domains) of Programming Languages

Programming languages are also categorized by their purpose: General-purpose or Domain-specific. General-purpose and domain-specific programming languages differ primarily in their scope and versatility. General-purpose languages, such as Python, Java, or C++, are designed to be flexible and broad in their application, allowing developers to write software for a wide range of tasks across various domains, from web development to scientific computing. They offer extensive libraries and tools to handle diverse programming needs, making them adaptable to many different types of problems. On the other hand, domain-specific languages, such as SQL, R, and Matlab, are tailored for specific types of tasks or industries.

Python is a general-purpose programming language and is also high-level, two characteristics that have helped make it very popular for development in many different types of applications.

Type of Interpretation of Programming Languages

As mentioned above, there is a process of interpretation between what we as humans see in computers and binary machine code. Another way programming languages are categorized is by the method of making that interpretation of the code written in those languages and machine code. We use the terms compiled and interpreted to categorize programming languages depending on the type of process used to convert code in that language to machine code. Some languages are compiled, meaning the code is translated into machine code before execution. Others are interpreted, where the code is executed line-by-line by an interpreter. Python, for instance, is an interpreted language. Compiled languages generally produce "faster" programs, that is, their run time processing tends to be faster than programs written in interpreted languages. While this may be a key factor for some types of software, other types of software may not rely on speed as much.

Programming Approaches

Keeping the foundation concept of Input ⇨ Processing ⇨ Output in mind, next we'll consider approaches to writing computer programs. We can approach writing code to fulfill a need or to complete a task in many ways. Since there are so many programming languages to choose from, and many programming tools to use with those languages, we have many options to consider. This book is based on the Python programming language. That was a choice I made based on several factors. Python is currnetly one of the most common and popular programming languages, which is a factor in that choice. Also, the academic and industry teaching that I am focused on currently is based on Python. So, it's logical that this book is based on Python to support my teaching.

Could I have written this book based on another programming language? Yes. A significant portion of this book would be nearly identical as it is right now even if I had chosen to use a different programming language. For example, this book is structured around the general concept of Input ⇨ Processing ⇨ Output. That concept transcends programming language, meaning I can use nearly any programming language to take input, process it, and produce output. So, teaching from that foundational perspective will work with any programming language.

There are many conventional methods and strategies to learn programming. Traditional programming approaches have been foundational in teaching and learning programming for decades. These methods focus on structured learning, understanding foundational concepts, and hands-on practice. While I use this book as the basis of my teaching, I recommend anyone learning programming and Python to explore as many resources as you have time for and that you have access to (there are many resources for these topics).

Software for Programmers

Programmers use software to write software, and we often refer to the software they use as tools. One of the first steps in learning programming is to familiarize yourself with the tools and software commonly used by programmers to write, test, and manage their code and subsequent applications. These tools are designed to make the coding process more efficient, organized, and error-free. There are a large number of types of tools and many vendors who make the various tools. This can be overwhelming if you try to be familiar with all of them. Here's a brief overview of the types of software you can encounter with a few of examples of each. In this book, we'll start by using a simple tool called Google CoLab, then we'll explore some of the other tools as we make our way through the book.

Hardware for Programmers

Understanding of computing hardware will help you as a Python programmer, as your code's performance will often depend on the hardware on which it is used. So, let's look at the environment Python operates in—the computer itself. Computer hardware refers to the physical components that make up a computer system. These components include the central processing unit (CPU), memory (RAM), storage (hard drives or SSDs), and input/output devices such as keyboards, mice, and monitors. Each of these hardware components plays a crucial role in the operation of a computer and, by extension, how your Python programs run.

Computing Devices

Computing devices have evolved significantly over the years, moving from large, room-sized machines to devices small enough to fit in our pockets. As a Python programmer, you should be familiar with the various types of devices that you use in your work. These include desktops, laptops, tablets, and even cloud systems.

Computing devices have evolved significantly over the years, moving from large, room-sized machines to devices small enough to fit in our pockets. As a Python programmer, you should be familiar with the various types of devices that you might use in your work. These include desktops, laptops, tablets, and even cloud systems.


Desktop computers, including personal computers (PCs) and Macs, have long been a standard platform for programmers. They offer a powerful and steady work environment, often providing superior processing power and memory compared to other device types. They offer a more comfortable coding environment for longer programming sessions with a larger screen and dedicated keyboard. Python runs smoothly on desktop computers, and a vast range of development tools and integrated development environments (IDEs) are available for these systems.



Laptop computers offer the benefits of desktop computers with added mobility. Laptops are a good choice for programmers who are on the need a portable work environment, laptops are a good choice. Modern laptops come with CPUs and RAM that can compete with desktop computers, so running Python and various development tools should not be a problem. Laptops also provide the flexibility to code in various environments.


Tablets: While not traditionally used for programming, tablets are increasingly used to write and run Python code. Apps like Pythonista for the iPad allow you to write and execute Python scripts directly on your tablet. Although they may not offer the same level of functionality or performance as a desktop or laptop, tablets can be suitable for light coding, learning, or prototyping ideas.


Cloud systems are increasingly used as software development environments. They offer powerful, scalable, and accessible platforms for running code. Python is well-supported on all major cloud providers, like Amazon Web Services (AWS), Google Cloud Platform (GCP), and Microsoft Azure. Cloud-based development environments, such as Google CoLaboratory, allow you to write, run, and share Python code from a web browser. This means you can program from any device with an internet connection, and the heavy lifting is done by the cloud server, not your local device. This can be particularly helpful when dealing with resource-intensive tasks such as machine learning or big data processing. These cloud platforms offer services beyond just running code, including database systems, machine learning tools, data storage solutions, and more, all of which can interact with your Python programs.

Mobile Devices: We must also recognize the role of mobile devices in programming. Mobile phones are not just communication tools anymore; they are fully-fledged computing devices. Python programming on mobile devices is becoming more feasible as mobile hardware becomes more advanced and specialized applications become available. The biggest advantage of mobile devices is their portability, which allows developers to work on their code anytime, anywhere. Although unsuitable for large-scale development projects due to their size and limitations in processing power and memory, mobile devices can be a handy tool for prototyping, learning, and coding while away from one's full-scale computing devices. Python on Android devices is supported via apps such as Pydroid 3 and QPython. On iOS devices, Pythonista is a popular choice. These applications allow you to write and run Python scripts directly on your mobile device. They even offer some degree of integration with the device's features, such as the accelerometer or the touch screen. It's also worth mentioning that while Python itself isn't typically used for mobile app development, tools like Kivy and BeeWare allow you to write mobile apps using Python, which can then be run on mobile devices. However, it's important to remember that the smaller screens and virtual keyboards on mobile devices can make extensive coding sessions challenging compared to a desktop or laptop.

Central Processing Unit (CPU)

The CPU is the primary component of a computer that performs most of the processing inside the computer. It's often referred to as the "brains" of the computer because it carries out the instructions of a computer program by performing basic arithmetical, logical, control, and input/output (I/O) operations. Python, being an interpreted language, relies on the CPU to execute its instructions one at a time. The CPU's speed (often measured in Gigahertz, GHz) and the number of cores it has can significantly impact the speed at which your Python program runs.

Random Access Memory (RAM)

Random Access Memory (RAM) is a type of computer memory that can be read from and written to by the processor. It's used to store working data and machine code. RAM is volatile, meaning its contents are lost when the computer is powered off. When you run a Python program, it's loaded into RAM for the CPU to process. The more complex your program (i.e., the more variables it uses, the larger the data structures, etc.), the more RAM it requires.



Storage Devices

Storage refers to computer hardware that can be used to retain data, even when powered off. The two most common forms of storage are Hard Disk Drives (HDD) and Solid State Drives (SSD). When you save a Python script to a file, you're writing it to storage. When you run a Python program that reads or writes files, it's accessing the storage hardware.

Input/Output (IO) Devices


Input and output devices are how a computer receives and sends data. Examples of input devices include keyboards, mice, microphones, webcams, graphics tablets, trackballs, barcode readers, gamepads, joysticks, scanners, electronic whiteboards, optical mark readers, optical character readers, and magnetic ink character readers. Output devices include monitors, printers, projectors, and speakers. And some devices are both input and output devices, such as USB drives, modems, CD-RW drives, touch screens, and headsets. When writing a Python program that accepts user input (like input from a keyboard) or produces output (like printing text to the console), you're interacting with these I/O devices.

Hardware & Python

As a Python programmer, it is essential to understand how your program will interact with hardware. A Python program runs on the CPU, uses RAM to store temporary data, reads from and writes to storage, and interacts with I/O devices. If your program is running slowly, it might be because it is performing complex calculations that the CPU cannot handle, or it may be using more RAM than is available, causing the operating system to swap memory to and from disk, which is slow. Conversely, understanding hardware can help you improve your Python programs. For example, if you know you are working on a multi-core CPU, you can use Python's multiprocessing module to execute multiple processes in parallel, potentially making your program faster.

(In)Conclusion

I call this section an (In)Conclusion because the concepts provided on this page is just the beginning. There are many other details we could explore about programming in general, the tools and environments programmers use to write and manage software, and other topics. For now, this has been a good introduction. We will learn more throughout this book. For now, let's turn our attention to Python.







© 2023 John Gordon
Cascade Street Publishing, LLC