Mathematics, Sciences, and Technologies

“Every secret of a writer’s soul, every experience of his life, every quality of his mind is written large in his works.”
– Virginia Woolf

Python for Scientists 1-1: Basic Data Types and Variables

,

Three Basic Data Types: Integer, Float, String

In Python or any other programming language, we have to play around with different data types, most likely numbers or lists/tables/arrays. We shall begin with the three most fundamental data types in Python: integers, floats, and strings.

Integer

An integer is simply a whole number, one without decimal places. For example, 1919, 0, and -6 are integers.

Float

To put simply, a float is a number with decimal places. Inside the computer system, they are actually stored in terms of bits/bytes (usually 32 bits/64 bits) with different levels of precision. For instance, 12.34, -2.718 are floats. Moreover, by adding a decimal point, even when there are no decimal places after or just followed by many zeros, Python will still recognize the number as a float. For example, 0.0 and 3. are interpreted as floats too.

String

Meanwhile, a string is a sequence of (possibly mixed) characters, numbers, or symbols. As mentioned in the last tutorial, they have to be enclosed by a pair of quotation marks (either "" or '') to be parsed. For instance, "Hello World" (previously), 'Hakuna Matata~', and "chloe123@zmail.com" are strings. Notice that as long as it is surrounded by the quotation marks, “numbers” like "369" and "3.14" will also be considered strings.

Variable Assignment

We also need some ways to store (or more accurately, point to) the data/objects above. For this matter, we can create named variables and assign the data to them. Unlike some of the static programming languages, we don’t need to declare the data type for a variable beforehand (it will be decided dynamically), and we can simply use the = operator after the proposed variable name to carry out the assignment. For example, we can write

myint = 67
myflt = -84.99
mystr = "Welcome to Amagi Brilliant Park!"

then further entering

print(myint)
print(myflt, mystr)

will produce the following outputs:

67
-84.99 Welcome to Amagi Brilliant Park!

Variable names should be meaningful and self-explanatory. They can only consist of letters, numbers, and the underscore _. Further, it cannot start with a number and clash with the reserved keywords. They are also case-sensitive, e.g. maths and Maths are different variables. For more information about the naming convention, one can read this excellent reference.

Chained Assignment

We can do multiple variable assignments in one line, like

lang_books, n_books, cost_books = "English", 108, 79.95
print("There are", n_books, "books, with an average cost of", cost_books, "dollars.")

will yield

There are 108 books, with an average cost of 79.95 dollars.

Another usage is

x = y = 7
print(x, y)

that gives

7 7

Updating Variable

To update a variable, simply use the = operator to associate it with the new object and the old object will be “detached”. This works even for different data types. For example,

ditto = 9
print("Integer:", ditto)
ditto = "Nine"
print("String:", ditto)
ditto = 9.
print("Float:", ditto)

will generate

Integer: 9
String: Nine
Float: 9.0

This example also illustrates the fact that Python executes the lines in a sequential fashion.

Deleting Variable

Finally, we can delete any variable using the function/keyword del to free up computer memory. After deleting it, we can no longer reference it and an error will arise. For instance,

a = 1874
print(a)
del a
print(a, "again?")

will lead to

1874
Traceback (most recent call last):
File "c:\Users\ben19\Downloads\test.py", line 4, in <module>
print(a)
^
NameError: name 'a' is not defined

Exercise

Search for the exchange rate between any two currencies on 2025-07-01. Store it into a variable, and also the currency names into another two variables. Print an informative sentence to describe them. Now, replace any one of the currencies with USD and update the exchange rate, and print them again in two lines this time.

Suggested Solution
cur_X = "EUR"
cur_Y = "CAD"
exch_rate = 1.61
print("The exchange rate between", cur_X, "and", cur_Y, "is", exch_rate)

and then followed by

cur_Y, exch_rate = "USD", 1.18
print("The exchange rate between", cur_X, "and", cur_Y)
print("=", exch_rate)

Leave a Reply

I’m Benjamin

Welcome to my Mathematical World! Here you can find posts and tutorials related to applied topics like Linear Algebra, Calculus, Differential Equations, as well as Programming. Feel free to leave comments and suggestions!

Let’s connect

Discover more from Benjamin's Maths World

Subscribe now to keep reading and get access to the full archive.

Continue reading