☰
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 2 : Data (Input) : Data Types

Data Types

Subscribe Contact


Contents

Overview

In programming, data types are an essential concept that define the type of data that can be stored and manipulated within a program. They determine the nature of the data (such as numeric, textual, or more complex structures) and the operations that can be performed on that data. Basic data types include integers, floating-point numbers, characters, strings, and booleans. Languages like Python offer more complex types like lists, tuples, and dictionaries. Each data type has its own attributes and behaviors; for instance, integer types can store whole numbers, while floating-point types are used for numbers with decimal points. Choosing the appropriate data type is crucial for efficient memory use and ensuring that operations on the data are performed correctly. Python is a dynamically typed language, which means the interpreter infers the data type automatically. Understanding data types is fundamental to programming, affecting how data is stored, retrieved, and manipulated.

Concept: Data Type
Full Concepts List: Alphabetical  or By Chapter 

A data type in programming is a classification that specifies the type of value a variable can hold and how the computer interprets that value. Think of data types as different kinds of containers, each designed to hold a specific type of item. Common data types in Python include integers (whole numbers like 3 or -42), floating-point numbers (numbers with a decimal point like 2.5 or 3.14), strings (sequences of characters like "hello" or "1234"), and booleans (true or false values). Each data type in Python has its own set of operations that can be performed on it. For example, you can add and multiply integers or concatenate (join together) strings. Understanding data types is crucial in programming because it affects what operations you can perform with your variables and how you can manipulate your data. For instance, you can't perform mathematical operations on strings, even if they contain numbers. Knowing the data types helps you structure your code logically and prevent errors.

Concept: Dynamic Typing
Full Concepts List: Alphabetical  or By Chapter 

Dynamic typing in programming refers to a feature of languages like Python, where the type of a variable is determined at runtime, not in advance. This means that when you write code, you don't have to specify what type of data (like number, text, or something else) a variable will hold. Instead, Python figures it out automatically when the program is run. For example, in Python, you can create a variable x and assign it a number (x = 5), and later, you can assign a string to the same variable (x = "Hello"). The ability to change types dynamically makes Python and similar languages very flexible and user-friendly, especially for beginners. However, it also means that you need to be careful about how you use your variables, as incorrect types can lead to bugs in your programs. Dynamic typing is one of the features that makes Python so versatile and popular for a wide range of programming tasks.

Data Types

Python supports a variety of built-in data types that provide different data-handling scenarios. We will begin by exploring four fundamental types:

Let's consider a couple of concepts that are key to understanding data types:

Concept: Alphanumeric
Full Concepts List: Alphabetical  or By Chapter 

Alphanumeric characters include the alphabetic letters A thru Z, both upper case and lowercase, as well as the digits 0 thru 9. When a digit is within an alphanumeric value, it is not considered a number but a textual representation of the digit. Alphanumeric also includes other characters found in keyboards, such as @ $ # & * ( ) { } [ ] , = - _ . + ; ' /. Also, a blank space (created on the keyboard by the spacebar) is considered alphanumeric.

Concept: Precision & Scale
Full Concepts List: Alphabetical  or By Chapter 

When we talk about numeric data types, we often need to know the precision and scale of the data types. Precision means the number of significant number of digits possible in a value. Scale refers to the number of digits to the right of the decimal point. So, for example, 12345.67 has a precision of 7 and a scale of 2.

Why is this important? When setting up our programs to process data, we choose data types based on the values we expect we will need. So, for the age example above, we would likely want to store that as an integer, that is with no decimal values. So, knowing that we will not store decimal values, we choose the integer data type. We can also decide on a reasonable range of values to expect in that data. If we prompted a user for their age and they entered 4592, which has a value much larger than known human age, we would want to reject that value as invalid. That's an example of applying knowledge of both the data we are working with and knowledge of what is available in Python to store values.

Examples

Consider the following practical example of the data types listed above.

We need to write a program that processes data entered by a user. The user fills out a form (Figure 1.) and clicks a Save button.

This form represents information about the user, Bob Smith. How many datum items does this form contain? It prompts the user for their first name, last name, age, employment status, job title, and hourly wage, so six datum. There would be seven, however the Employed? question would be stored as a single value based on their answer.


Figure 1: Example of a data entry form.

What are the data types of these items? See Figure 2. The first name, last name, and job title are all expected to be alphanumeric (strings), so they would be str's. As we saw earlier, age is an integer (whole counting number with no decimal or fractional portion) or int in Python. The employment status is a yes or no value. As the above chart indicates, we call this boolean, or bool in Pthon. And the hourly wage is a number, but not an integer because it has scale. So hourly is a decimal number, which in Python programming we call a floating-point number, or float.


Figure 2: Example of a data entry form.

The type() Function

The type() function in Python is used to determine an object's data type. It returns the type of the given object. The syntax of the type function is as follows:

type(object)

The object parameter represents the object whose type needs to be determined. The function's return value is called the type object, which reports the type or class of the given object. We will now use the type() function to check the data type of constant and literal values. We will expand the use of the type() function soon when discussing variables.

Concept: Object
Full Concepts List: Alphabetical  or By Chapter 

An object" is a fundamental concept in programming, particularly in Object-Oriented Programming (OOP) languages (see the concept below) like Python. It represents a self-contained entity that encapsulates both data and procedures to manipulate that data. The data part, known as attributes or properties, defines the state of the object, while the procedures, known as methods or functions, deterrmine the object's behavior. For instance, in a program modeling a library, a Book object would have attributes like title, author, ISBN, and methods for actions such as borrowing or returning the book. This programming approach emphasizes modularity and organization, allowing complex systems to be broken down into more manageable, interacting components. Objects interact with one another through their methods, enabling programmers to create sophisticated, real-world simulations and applications in an intuitive and reusable way.

Concept: Object-Oriented Programming (OOP)
Full Concepts List: Alphabetical  or By Chapter 

Object-Oriented Programming (OOP) is a programming paradigm centered around the concept of "objects", which are instances of classes. In OOP, classes act as blueprints for creating objects, defining the data structure (attributes) and behavior (methods) of these objects. This paradigm is characterized by several fundamental principles: encapsulation (bundling data and methods that operate on the data within one unit and hiding the internal state of the object from the outside), inheritance (creating new classes based on existing classes to facilitate code reuse and create hierarchical relationships), and polymorphism (the ability of different classes to be treated as instances of the same class through a standard interface). OOP makes it easier to structure and manage complex software systems, as it allows programmers to model real-world entities and their interactions more intuitively. This approach is widely used in software engineering to improve code maintainability, scalability, and collaboration. Popular object-oriented languages include Python, Java, C++, and C#.

Code Examples

print(type(10))
print(type(3.14))
print(type('A'))
print(type("Bob Smith"))
print(type(10 / 20))
print(type(10 > 20))

Output

< class 'int' >
< class 'float' >
< class 'str' >
< class 'str' >
< class 'float' >
< class 'bool' >

Code Details

  • Code Line 1: This line uses the type() function to report on the data type of the value 10, which is an integer. Output Line 1 shows the result of printing the outcome of the type function on 10, as an 'int', meaning an integer type value.
  • Code Line 2: This line uses the type() function to report on the data type of the value 3.14, which is a decimal float value. Output Line 2 shows the result of printing the outcome of the type function on 3.14, as a 'float', meaning a decimal float type value.
  • Code Line 3: This line uses the type() function to report on the data type of the value 'A', which is a string. Output Line 3 shows the result of printing the outcome of the type function on 'A', as an 'str', meaning a string type value.
  • Code Line 4: This line uses the type() function to report on the data type of the value 'Bob Smith', which is a string. Output Line 4 shows the result of printing the outcome of the type function on 'Bob Smith', as an 'str', meaning a string type value.
  • Code Line 5: This line uses the type() function to report on the data type of the expression (10 / 20). This expression, dividing 10 by 20, results in a float value 0.05, so the result of using the type() function on this expression means to report on the data type of the result of the expression (remember our order of operations). Output Line 5 shows the result of printing the outcome of the type function on this expression is a 'float', meaning the result of the expression is a decimal float type value.
  • Code Line 6: This line uses the type() function to report on the data type of the expression (10 > 20). This expression asks the question - is the value 10 greater than the value 20? The result of this expression is False. Remember, though, the type() function reports the data type of that result, so Output Line 6 shows the type of 'bool', which means boolean.

Conclusion

On this page we learned basic ideas about data and data types. There is much more to learn about these topics, which we will continue to explore as we learn Python. Our next step is to learn how to store data in variables.







© 2023 John Gordon
Cascade Street Publishing, LLC