Python Primitive Types

Learn about the primitive types in Python, including integers, floating-point numbers, boolean values, and strings.

Last updated: 2024-12-12

Python Primitive Types

In Python, primitive types represent the most basic data types used to store simple values. These types serve as the building blocks of data manipulation in the language.

List of Primitive Types

Python has the following primitive types:

  1. int (integers)
  2. float (floating-point numbers)
  3. bool (boolean values)
  4. str (strings)

1. int (Integers)

The int type represents whole numbers.

Features:

  • Can hold both positive and negative numbers.
  • Has unlimited size (limited by memory).
  • Supports binary, octal, and hexadecimal representations.

Examples:

x = 42        # Positive number
y = -7        # Negative number
z = 0         # Zero
binary = 0b1010   # Binary (10 in decimal)
octal = 0o12      # Octal (10 in decimal)
hexadecimal = 0xA # Hexadecimal (10 in decimal)

Basic Operations:

a = 10 + 5    # Addition
b = 10 - 3    # Subtraction
c = 10 * 2    # Multiplication
d = 10 // 3   # Integer division
e = 10 % 3    # Modulus
f = 2  3    # Exponentiation (2^3)

2. float (Floating-Point Numbers)

The float type represents numbers with decimal points.

Features:

  • Supports decimal point representation.
  • Accepts scientific notation (e.g., 1.5e2 = 150).
  • Follows IEEE 754 standard.

Examples:

pi = 3.14159
coefficient = -0.5
scientific = 1.2e3  # 1200
negative_float = -3.14

Basic Operations:

a = 3.5 + 2.1   # Addition
b = 7.2 - 1.8   # Subtraction
c = 2.5 * 4.0   # Multiplication
d = 7.5 / 2.5   # Division
e = 9.0 % 4.0   # Modulus

3. bool (Boolean Values)

The bool type represents logical values (True or False).

Features:

  • Only two possible values: True or False.
  • Commonly used in conditional statements and logical operations.
  • Implicitly derived from numeric expressions (0 = False, others = True).

Examples:

truth = True
error = False
boolean_from_int = bool(1)  # True
boolean_from_zero = bool(0) # False

Logical Operations:

print(5 > 3)     # True
print(2 == 4)    # False
print(not True)  # False
print(True and False)  # False
print(True or False)   # True

4. str (Strings)

The str type represents sequences of characters.

Features:

  • Defined using single or double quotes.
  • Immutable (cannot be changed after creation).
  • Supports Unicode characters.
  • Offers a wide range of methods for manipulation.

Examples:

greeting = "Hello, World!"
name = 'John'
multiline = '''This is
a multiline string.'''

Useful Methods:

text = "Python Programming"
print(text.lower())   # Convert to lowercase
print(text.upper())   # Convert to uppercase
print(len(text))      # Get length of string
print(text.replace("Python", "Java"))  # Replace substring
print(text.startswith("Python"))  # Check prefix
print(text.endswith("ing"))       # Check suffix

Conclusion

Primitive types form the core of Python's data handling system. Understanding these types is essential for effective programming, enabling better performance and reducing potential errors. Mastering these types allows developers to build efficient and reliable Python applications.

More Information

  1. Python Data Types