Data Types in Python

Fundamental to all programming languages are datatypes and operators. This lesson provides a practical overview of the associated Python syntax. Key takeaways from this unit should be the ability to distinguish between different data types (int, float, str). Additionally, you should understand how to assign a variable with a value using the assignment operator. Finally, it is essential to understand the difference between arithmetic and comparison (or "relational") operators. Make sure to thoroughly practice the programming examples to solidify these concepts.

We start with objects in Python. Objects can be of different types, including numbers (integers and floats), strings, arrays (vectors and matrices), and others. Any object can be assigned to a name so that we can refer to the object in our code. We'll start with the basic types of objects.


Numeric variables

The following is a line of Python code, where we assign the value 1.2 to the variable a.

The act of assigning a name is done using the = sign. This is not equality in the mathematical sense and has some non-mathematical behavior, as we'll see:

a = 1.2

This is an example of a floating-point number or a decimal number, which in Python is called a float. We can verify this in Python itself.

type(a)
<class 'float'>

Floating point numbers can be entered either as decimals or in scientific notation

x = 0.0005
 y = 5e-4 # 5 x 10^(-4)
 print(x == y)
True

You can also store integers in a variable. Integers are of course numbers but can be stored more efficiently on your computer. They are stored as an integer type, called int

b = 23
 type(b)
<class 'int'>

These are the two primary numerical data types in Python. There are some others that we don't use as often, called long (for long integers) and complex (for complex numbers)


Operations on numbers

There is arithmetic and logic available to operate on elemental data types. For example, we do have addition, subtraction, multiplication, and division available. For example, for numbers, we can do the following:

Operation Result
x + y The sum of x and y
x - y The difference of x and y
x * y The product of x and y
x / y The quotient of x and y
- x The negative of x
abs(x) The absolute value of x
x ** y x raised to the power y
int(x) Convert a number to an integer
float(x) Convert a number to a floating point

Let's see some examples:

x = 5
 y = 2
x + y
7
x - y
3
x * y
10
x / y
2.5
x ** y
25


Strings

Strings are how text is represented within Python. It is always represented as a quoted object using either single ('') or double ("") quotes, as long as the types of quotes are matched. For example:

first_name = "Abhijit"
 last_name = "Dasgupta"

The data type that these are stored in is str.

type(first_name)
<class 'str'>


Operations

Strings also have some "arithmetic" associated with them, which involves, essentially, concatenation and repetition. Let's start by considering two character variables that we've initialized.

a = "a"
 b = "b"

Then we get the following operations and results

Operation Result
a + b 'ab'
a * 5 'aaaaa'


We can also see if a particular character or character string is part of an exemplar string

 last_name = "Dasgupta" 
  "gup" in last_name 
 True

String manipulation is one of the strong suites of Python. There are several functions that apply to strings, that we will look at throughout the workshop, especially when we look at string manipulation. In particular, there are built-in functions in base Python and powerful regular expression capabilities in the re module.


Truthiness

Truthiness means evaluating the truth of a statement. This typically results in a Boolean object, which can take values True and False, but Python has several equivalent representations. The following values are considered the same as False:

None, False, zero (0, 0L, 0.0), any empty sequence ([], '', ()), and a few others

All other values are considered True. Usually, we'll denote truth by True and the number 1.


Operations

We will typically test for the truth of some comparisons. For example, if we have two numbers stored in x and y, then we can perform the following comparisons

Operation Result
x < y x is strictly less than y
x <= y x is less than or equal to y
x == y x equals y (note, it's 2 = signs)
x != y x is not equal to y
x > y x is strictly greater than y
x >= y x is greater or equal to y


We can chain these comparisons using Boolean operations

Operation Result
x | y Either x is true or y is true or both
x & y Both x and y are true
not x if x is true, then false, and vice versa


For example, if we have a number stored in x, and want to find out if it is between 3 and 7, we could write

(x >= 3) & (x <= 7)
True


A note about variables and types

Some computer languages like C, C++, and Java require you to specify the type of data that will be held in a particular variable. For example,

int x = 4;
 float y = 3.25;

If you try later in the program to assign something of a different type to that variable, you will raise an error. For example, if I did, later in the program, x = 3.95;, that would be an error in C.

Python is dynamically typed, in that the same variable name can be assigned to different data types in different parts of the program, and the variable will simply be "overwritten". (This is not quite correct. What actually happens is that the variable name now "points" to a different part of the computer's memory where the new data is then stored in the appropriate format). So the following is completely fine in Python:

x = 4  # An int
 x = 3.5  # A float>
 x = "That's my mother"  # A str
 x = True  # A bool


Variables are like individual ingredients in your recipe. It's mis en place or setting the table for any operations (functions) we want to do to them. In a language context, variables are like nouns, which will be acted on by verbs (functions). In the next section, we'll look at collections of variables. These collections are important in that it allows us to organize our variables with some structure.


Source: Abhijit Dasgupta, https://www.araastat.com/BIOF085/a-python-primer.html#data-types-in-python
Creative Commons License This work is licensed under a Creative Commons Attribution 4.0 License.

Last modified: Wednesday, September 28, 2022, 6:30 PM