• Home

On this page

  • Numerical types
  • String types
  • Limited-size types

Data types

Numerical types

def main() -> None:
    x = int(3)
    x = float(3.14)
    x = int(float(3.14))

Floating precision problem

def main() -> None:
    sum = 0.1 + 0.1 + 0.1
    expected = 0.3

    print(f"sum == {sum}, expected == {expected}")
    assert sum == expected

if __name__ == "__main__":
    main()
sum == 0.30000000000000004, expected == 0.3
---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
Cell In[1], line 9
      6     assert sum == expected
      8 if __name__ == "__main__":
----> 9     main()

Cell In[1], line 6, in main()
      3 expected = 0.3
      5 print(f"sum == {sum}, expected == {expected}")
----> 6 assert sum == expected

AssertionError: 

It’s better to use Decimal type in this case, and the floating point precision problem is fixed.

from decimal import Decimal

def main() -> None:
    sum = Decimal("0.1") + Decimal("0.1") + Decimal("0.1")
    expected = Decimal("0.3")

    print(f"sum == {sum}, expected == {expected}")
    assert sum == expected

if __name__ == "__main__":
    main()
sum == 0.3, expected == 0.3

String types

# Strings can be surrounded by either single or double quotation marks
single_quoted_string = 'Hello, World!'
double_quoted_string = "Hello, World!"

# Strings using single or double quotes interchangeably
print(single_quoted_string)
print(double_quoted_string)

# Multi-line strings using triple quotes
multi_line_string = """This is a multi-line string.
It spans multiple lines.
This is often used for documentation."""
print(multi_line_string)

# Strings behave like lists: slicing, concatenation, and searching
# Slicing
substring = single_quoted_string[0:5]
print("Sliced string:", substring)

# Concatenation
concatenated_string = single_quoted_string + " How are you?"
print("Concatenated string:", concatenated_string)

# Searching
search_result = "World" in single_quoted_string
print("Searching for 'World':", search_result)

#Reassigning string
single_quoted_string = 'Goodbye, World!'
print("Reassigned string:", single_quoted_string)

# Python treats single characters and strings of characters as the same type
single_character = 'A'
print("Type of single_character:", type(single_character))
print("Type of single_quoted_string:", type(single_quoted_string))
Hello, World!
Hello, World!
This is a multi-line string.
It spans multiple lines.
This is often used for documentation.
Sliced string: Hello
Concatenated string: Hello, World! How are you?
Searching for 'World': True
Reassigned string: Goodbye, World!
Type of single_character: <class 'str'>
Type of single_quoted_string: <class 'str'>

Strings are immutable, so we can’t change them after creation.

single_quoted_string[0] = 'G'
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[4], line 1
----> 1 single_quoted_string[0] = 'G'

TypeError: 'str' object does not support item assignment

We can concatenate strings using the + operator.

concatenated_string = single_quoted_string + " How are you?"
print(concatenated_string)
Goodbye, World! How are you?

We can also use the * operator to repeat a string.

repeated_string = single_quoted_string * 3
print(repeated_string)
Goodbye, World!Goodbye, World!Goodbye, World!

We can also use the in operator to check if a string is in another string.

search_result = "World" in single_quoted_string
print("Searching for 'World':", search_result)
Searching for 'World': True

Limited-size types

  • Boolean is an example of a limited-size type, we have another type called Enum.
from enum import Enum

class EmployeeType(Enum):
    FULL_TIME = 1
    PART_TIME = 2
    CONTRACT = 3

def main() -> None:
    e_type = EmployeeType.FULL_TIME
    print(e_type.value)

if __name__ == "__main__":
    main()
1

Or we can use auto() and StrEnumso that we do not have to manually assign the value to the enum and we can use the string name of the enum.

from enum import StrEnum, auto

class EmployeeType(StrEnum):
    FULL_TIME = auto()
    PART_TIME = auto()
    CONTRACT = auto()

def main() -> None:
    e_type = EmployeeType.FULL_TIME
    print(e_type.value)

if __name__ == "__main__":
    main()
full_time