Data and Data Types

Table of contents
Every app manages data - usernames, balances, likes, messages. Python organizes this into data types: integers for whole numbers, floats for decimals, and strings for text. Learn to work with and convert between these fundamental types.

Introduction

So far we have looked at what programming is, computer programs, programming languages, translators, the Python interactive shell, IDEs, and we have even written our first Python program. Now let's begin writing meaningful programs.

Understanding Data in the Real World

Every application we interact with manages data. Take Instagram - we see usernames, number of followers, number of likes, bio text, verified status, number of posts. All of this is data.

Move to M-PESA. We have account balance, phone number, PIN, ID number, transaction amount, recipient number. Again - data.

You can pick any other software - your bank app, Gmail, TikTok, Netflix. They all have data. The computer needs a way to store all this information. That means we must learn how Python represents data.

In Python, and in programming generally, we call these data types.

A data type is a category that tells Python what kind of information we're working with and what operations we can perform on it.

Later, we'll discover that technically these are classes, but let us not jump there yet. For now let's think of data types simply as different categories of data.

Let's begin exploring the main types.

Integers

An integer is a whole number with no fractional part.

This concept comes straight from mathematics. Remember learning about whole numbers in school? Numbers like 1, 2, 3, 100, -5? These are integers. In mathematics, integers are the set {..., -3, -2, -1, 0, 1, 2, 3, ...} - they go on forever in both directions but never have decimal points.

Just like in mathematics, Python's integers can be positive, negative, or zero.

bash
0, 6, 4, -9, 27746, -567, 849300

Let's see integers in action:

python
print(0)
print(6)
print(4)
print(-9)
print(27746)
print(-567)
print(849300)

Real-life examples that are integers:

  • Your age → 19
  • Number of countries in Africa → 54
  • Instagram likes → 1200
  • Students in a classroom → 45
  • Unread WhatsApp messages → 8

Let's try some simple calculations with integers:

python
4 + 7    # 11
89 - 78  # 11
3 * 4    # 12
8 / 2    # 4.0 (Observe, division always gives a float)
DISCOVERY MOMENT

Even though we're dividing two integers and getting a whole number result, Python gives us 4.0 instead of 4. And notice the type changed to float!

Python does this because division can produce non-integer results:

python
print(10 / 3)   # 3.3333333333333335

Since division might not always give a whole number, Python prepares for that possibility by always returning a float.

Let's try a few more operations:

python
# Integer division - gives just the whole part
print(10 // 3)  # 3

# Modulo - gives the remainder
print(10 % 3)   # 1

# Exponentiation - raising to a power
print(2 ** 3)   # 8

Integers behave exactly as you expect from arithmetic class - they follow the same mathematical rules you learned in school.

If we want to confirm the type of a number, we use the type() function:

python
type(56)
# <class 'int'>

Floating-Point Values

A floating-point value (or float) is a number with a fractional part (decimal point).

In mathematics, these are called real numbers or decimal numbers. You encountered them when learning about fractions and decimals: 3.14, 0.5, 100.25.

Examples of floats:

python
34.5, 0.0005, 89.455601, -9.7, 100000.0000005
python
print(34.5)
print(0.0005)
print(89.455601)
print(-9.7)
print(100000.0000005)

Why the name floating-point? Because the decimal point "floats." In other words, the decimal can move depending on how large or small the number is. This becomes important later in scientific computing, but just note the idea for now.

Real-life examples of floats:

  • Height → 1.78 meters
  • Weight → 63.4 kilograms
  • Account balance → 4578.50 shillings
  • Temperature → 29.75 °C
  • Speed → 88.9 km/h

Let's perform some operations with floats:

python
print(45.7 + 10.2)  # 55.9
print(5.0 * 2)      # 10.0
print(12.6 / 4)     # 3.15

We can also mix integers and floats:

python
print(10 + 5.5)     # 15.5
print(type(10 + 5.5))  # <class 'float'>
Discovery Moment

When you mix integers and floats in a calculation, Python converts everything to floats for the result.

Here's something interesting about floats:

python
print(0.1 + 0.2)
# 0.30000000000000004

This tiny imprecision happens because computers store decimal numbers in binary (0s and 1s), which can't perfectly represent all decimal fractions. This is normal and happens in most programming languages.

Floats can also represent very large or very small numbers using scientific notation:

python
print(3e8)      # 300000000.0 (3 × 10^8)
print(1.6e-19)  # 1.6e-19 (very small number)

Strings

A string is a sequence of characters enclosed in quotation marks.

In programming, we need a way to represent text - letters, words, sentences, symbols. Unlike mathematics, which primarily deals with numbers, programming works extensively with text data. The term "string" comes from the idea of characters strung together like beads on a string.

Characters can be letters, digits, symbols, spaces, or even special characters like emojis.

We can create strings using:

  • Single quotes: 'Hello'
  • Double quotes: "Hello"
  • Triple quotes: '''Hello''' or """Hello""" (for longer text)

Examples:

python
"Michael"
'James'
"Mt. Kilimanjaro"
"A"
"B"
"1"
"Hello, I am learning Python"
python
print("Michael")
print('James')
print("Mt. Kilimanjaro")
print("A")
print("B")
print("1")
print("Hello, I am learning Python")
Important Rule

Opening quotation marks must match closing quotation marks.

Otherwise Python complains:

python
print("Michael")  # Correct
print('Michael')  # Correct
# print("Michael')  # Error!

Strings represent a lot of real-life data:

  • Your name
  • Your email address
  • A password (yes, a password is a string)
  • Country names
  • Feedback messages
  • Text in a tweet or WhatsApp message

String Operations — Concatenation

Concatenation means adding strings together.

python
print("Michael" + "James")        # "MichaelJames"
print("Hello" + " " + "World")    # "Hello World"

We can repeat strings:

python
"Ha" * 3
# "HaHaHa"

"=" * 10
# "=========="
Tip

This is useful for creating simple text separators or repeating patterns.

Strings also have a length, which we can find using the len() function:

python
len("Python")
# 6

len("Hello, World!")
# 13
The length counts all characters, including spaces, punctuation, and special characters.
Discovery Moment

Even though "123" looks like a number, because it's in quotes, Python treats it as text:

python
print(123)        # Number
print("123")      # String
print(type(123))  # <class 'int'>
print(type("123")) # <class 'str'>

Type Casting/Conversion

Sometimes we need to convert one data type to another. This is called type casting or type conversion

Let us explore conversions one by one.

Converting to Strings

Any data type can become a string using the str() function:

python
print(str(45))    # "45"
print(str(23.89)) # "23.89"
print(str(True))  # "True"

Converting to Integers

Strings that look like whole numbers can become integers using the int() function:

Floats can become integers (decimal part gets truncated):

python
print(int(23.89))  # 23 (not 24!)
print(int(99.999)) # 99 (not 100!)
print(int(-4.7))   # -4
Important

The decimal part is removed, not rounded.

What happens with invalid conversions?

python
# These will cause errors:
# print(int("Michael"))  # ValueError
# print(int("45.67"))    # ValueError - can't convert decimal string directly

To convert a decimal string to integer, go through float first:

python
print(int(float("45.67")))  # 45

Converting to Floats

Strings that look like numbers can become floats using the float() function:

python
print(float("45.98"))  # 45.98
print(float("25"))     # 25.0

Integers easily become floats:

python
print(float(45))  # 45.0

Practical Examples

Let's see these concepts in action with realistic scenarios:

Example 1: Instagram Profile Summary

python
print("=== INSTAGRAM PROFILE ===")
print("Username: " + "python_learner_2024")
print("Followers: " + str(1247))
print("Following: " + str(356))
print("Posts: " + str(89))
print("Bio: " + "Learning Python one day at a time!")
print("Average likes per post: " + str(45.7))

Example 2: M-PESA Transaction

python
print("=== M-PESA TRANSACTION ===")
print("Account Balance: KSH " + str(2847.50))
print("Transaction Amount: KSH " + str(500))
print("Transaction Code: " + "QK7X2M9P")
print("Recipient: " + "Jane Doe")
print("New Balance: KSH " + str(2847.50 - 500))

Example 3: Netflix Episode Info

python
print("=== NOW WATCHING ===")
print("Show: " + "Stranger Things")
print("Season: " + str(4))
print("Episode: " + str(7))
print("Duration: " + str(52.5) + " minutes")
print("Rating: " + str(4.8) + "/5.0")
print("Released: " + str(2024))

Summary

We've explored three fundamental data types that Python uses to organize information:

Integers (int) are whole numbers without decimal points:

  • Examples: 42, -17, 0, 1000
  • Used for counting, ages, quantities, years
  • Come from mathematical concept of whole numbers
  • Division always produces a float, even for whole results

Floats (float) are numbers with decimal points:

  • Examples: 3.14, -0.5, 100.0, 2.5e8
  • Used for measurements, money, temperatures, ratings
  • Based on mathematical concept of real/decimal numbers
  • Sometimes show tiny precision differences due to binary storage

Strings (str) are sequences of characters in quotes:

  • Examples: "Hello", 'Python', "123", "user@email.com"
  • Used for text, names, messages, usernames, codes
  • Can be concatenated (+) and repeated (*)
  • Have measurable length with len()

Type conversion allows changing between types:

  • Any type can become a string with str()
  • Number-like strings can become int() or float()
  • Floats become integers by removing decimal part
  • Invalid conversions raise ValueError

We can always check a data's type using the type() function.

These data types form the foundation for all the information our programs will handle - from simple calculations to complex applications like the Instagram and M-PESA examples we see every day.

In our next chapter, we'll learn about variables - a way to store and reuse these different types of data throughout our programs.

Discussion (0)

Be the first to comment

Join the discussion

Share your thoughts and engage with the community

Your comment will appear immediately

No comments yet

Be the first to share your thoughts on this article!