ML Diaries: Day 6

Python Detour — Data Types in Python

Adedayo Adeyanju
5 min readAug 29, 2022

— a daily log of my learning and projects built as I take up Machine Learning. Welcome to The Mind Palace by Dayo.

Date: Aug 27, 2022

About

Minor win: Understood, for the first time, how computers understand high-level programming languages and the difference between a compiler and an interpreter.

Table of Contents:

  1. Data types in PythonFundamental data types, Specialized data types and None.
  2. Behind The Scenes: Compilers and Interpreters

1. Data Types in Python

Fundamental Data Types in Python

Fundamental data types refer to built-in data types. They are the default kinds of data that can be worked with in Python so these types of data can be defined without creating or manipulating anything. These data types are int, float, complex, str, list, tuple, bool, set, and dict.

  • int — This means integers. Period. The int data type is a numeric data type that stores variables that contain whole numbers. Basically, ‘int’ tells the computer a variable is an integer and should be handled as such.
  • float — This is a numeric data type. Since fractions can be represented by decimals and vice versa, float represents decimals and fractions. It is worth noting that the result of a division operation is a float value and not an int value. (However, a floor division returns a rounded value of the quotient so a floor division results in an int value.)
  • complex — A third data type reserved for numbers but used to represent only complex numbers. Complex numbers are numbers with a real part and an imaginary part i.e. real part + (imaginary part)j e.g. 2 + 3j.

💡Numeric data refers to data that contains only numbers.

demonstrating numeric data types
numeric data types — the output
  • list — This data type represents an ordered collection of data. The data stored in a list could be of any data type and the elements of the list could be of the same data type or they could be of different data types. They are defined using square brackets [] and the elements of a list can be accessed using the index method. Furthermore, lists are mutable so they can always be modified after creating them.
lists
  • tuple — On the other hand, tuples are immutable. They also represent an ordered collection of data, which may or may not be of the same data type but they cannot be modified after creating them. They are defined using parentheses (). Furthermore, each element in a list can be accessed using the index method.
tuples
  • str — This means strings, and strings represent data that contains text — alphabetic, alphanumeric and or symbols. Some others also explain strings to be a class that represents Unicode characters. Same difference. A single element of a string is called a character and can be accessed using the index method.
strings

💡Strings, lists and tuples are sequence data types, meaning they are collections of data. In lists and tuples, the data may be of different or similar types. Strings, however, are a collection of characters, each of which is of the string data type. A character is a single string.

  • bool — Short for boolean values, the ‘bool’ data type represents True or False values. They are often the results of conditional statements.
boolean values

Sets and dictionaries were not covered on day 6 and would be discussed in time. To read more on Python data types, check here.

Specialized Data Types

Without delving into technicalities, specialized data types refer to user-defined data types. This means that a programmer can create their own data types using classes. (Another resource on Python classes.)

None

Every data type is a kind of variable that contains a value. How then do we create an empty variable? In other words, how to create a storage location but with nothing in it? We use None.

None is a data type of its own. It is not an empty string, list or tuple. It also does not mean 0 or False. Instead, None is a keyword that represents a null value or nothing. None is the value that is returned when a function has no return statements.

2. Behind The Scenes: Compilers and Interpreters

Now, Python is all well and good but it is a high-level language (HLL) which means that it uses a vocabulary that we humans understand. A glitch in computing is that computers are not humans so they do not speak our language. Computers use low-level languages which are a bunch of 0s and 1s.

So when we write in languages like Python, C, etc., how does a computer read and execute the instructions it receives? Translation! A translator program translates the source code written in a high-level language like Python into a low-level language. This translator could be a compiler or an interpreter.

Compilers vs Interpreters

While both convert a language to another, they do not do so in the same way. Compilers receive the HLL source code, read it and give the converted program. The entire source code is translated and then executed by a computer. On the other hand, interpreters translate line-by-line. They read a line of code, execute it and then move on to the next line. An interpreter executes the code but a compiler does not.

Day 6 completed :)

ML Diaries is simply a daily log of my learning and projects built as I take up Machine Learning.

Stories on The Mind Palace still continue every week. You can read this week’s story ‘Single. Dating. Engaged. Married. — Book Review’.

You should subscribe to the newsletter for just story content.

On to day 7.

--

--

Adedayo Adeyanju

I live, I learn, then I write. Welcome to my mind palace! Now only on Substack: themindpalacetmp.substack.com