Fundamental Data Types
Fundamental Data Types in Python
Fundamental data types, such as int, float, bool, and complex, are used to store single values in a computer's memory. They help in performing simple operations like calculations and comparisons efficiently.
Frequently Used Fundamental Data Types in Python:- int
- float
- bool
Int Data Types in Python
The int data type in Python is a built-in type used to store whole numbers (numbers without decimals). It can hold positive numbers, negative numbers, and zero. Integers are commonly used for counting, representing ages, temperatures, and other values that do not require decimals.
age = 10
print(age) # Output: 10
print(type(age)) # Output: < class 'int' >
temperature = -123
print(temperature, type(temperature)) # Output: -123 < class 'int' >
empno = 2345
print(empno, type(empno)) # Output: 2345 < class 'int' >
Number Systems in Python
In Python, integer data can represent numbers in various number systems. Python allows you to work with these different systems, and it automatically handles the conversion between them.
- Decimal Number System (Default)
- Binary Number System
- Octal Number System
- Hexadecimal Number System
- Binary Number System:
- Binary system uses only two digits: 0 and 1.
- It is a base-2 number system, meaning each digit represents a power of 2.
- To store binary data in Python, the binary number must start with 0b | 0B. Syntax : varname = 0b binary_data | 0B binary_data
- Octal Number System:
- Octal number system uses digits from 0 to 7 (total of 8 digits).
- It is a base-8 number system, meaning each digit represents a power of 8.
- It must start with 0o or 0O.
- To store octal data in Python, the octal number must start with 0o | 0O. Syntax: varname = 0o octal_data | varname = 0O octal_data
- Hexadecimal Number System:
- It uses digits from 0 to 9 and letters A to F (where A=10, B=11, C=12, D=13, E=14, and F=15).
- It is a base-16 number system, meaning each digit represents a power of 16.
- To store hexadecimal data in Python, the hexadecimal number must start with 0x | 0X. Syntax : varname = 0x hex_data | 0X hex_data
binary_num = 0b1010 # Binary for 10 in decimal
print(binary_num) # Output: 10
binary_num = 0B11100011 # Binary for 227 in decimal
print(binary_num) # Output: 227
binary_num = 0b00000000000000000000000000001 # Binary for 1 in decimal
print(binary_num) # Output: 1
# binary_num = 0b101012 # SyntaxError: invalid digit '2' in binary literal
octal_num = 0o21 # Octal for 17 in decimal
print(octal_num, type(octal_num)) # Output: 17 < class 'int' >
octal_num = oct(17) # Convert decimal 17 to octal string
print(octal_num) # Output: 0o21
print(octal_num, type(octal_num)) # Output: 0o21 < class 'str' >
octal_num = 0o123 # Octal for 83 in decimal
print(octal_num, type(octal_num)) # Output: 83 < class 'int' >
# octal_num = 0o1278 # SyntaxError: invalid digit '8' in octal literal
hex_num = 0xAC # Hexadecimal for 172 in decimal
print(hex_num, type(hex_num)) # Output: 172
hex_num = 0xBEE # Hexadecimal for 3054 in decimal
print(hex_num) # Output: 3054
hex_num = 0XACE # Hexadecimal for 2766 in decimal
print(hex_num) # Output: 2766
hex_num = 0xFace # Hexadecimal for 64206 in decimal
print(hex_num) # Output: 64206
# hex_num = 0XACER # SyntaxError: invalid syntax
Base Conversion Functions in Python
The Base Conversion Functions in Python allow you to convert numbers between different number systems (binary, octal, hexadecimal, and decimal). Below is an explanation of the three main base conversion functions in Python.
Python provides three base conversion functions:- bin(): Converts a number to binary (base-2).
- oct(): Converts a number to octal (base-8).
- hex(): Converts a number to hexadecimal (base-16).
These functions only work with integer inputs and do not support floating-point numbers.
# You can convert any data type between different number systems.
# Convert Decimal to Binary
decimal_num = 18
binary_result = bin(decimal_num)
print(binary_result) # Output: 0b10010
# Convert Decimal to Octal
decimal_num = 25
octal_result = oct(decimal_num)
print(octal_result) # Output: 0o31
# Convert Decimal to Hexadecimal
decimal_num = 47
hex_result = hex(decimal_num)
print(hex_result) # Output: 0x2f
Float Data Types in Python
Float is a built-in data type in Python, considered a fundamental type, used to store floating-point numbers or real numbers, which are numbers that include decimal places.
- This data type does not support binary, octal, or hexadecimal number system representations.
- It supports scientific notation, written in the form mantissa e exponent (e.g., 1.23e4).
- It is used for prices, measurements, and data requiring decimal precision.
price = 12.34 # Basic float value
print(price, type(price)) # Output: 12.34 < class 'float' >
scientific_large = 34e2 # 34 * 10^2 (scientific notation for 3400.0)
print(scientific_large, type(scientific_large)) # Output: 3400.0 < class 'float' >
scientific_small = 12e-4 # 12 * 10^-4 (scientific notation for 0.0012)
print(scientific_small, type(scientific_small)) # Output: 0.0012 < class 'float' >
# Invalid syntax examples.
# binary_num = 0b101.0b101 # SyntaxError: invalid syntax (binary not allowed in float)
# octal_num = 0o12.0b1111 # SyntaxError: invalid syntax (octal and binary not allowed in float)
# hex_num = 0xBEE.0o123 # SyntaxError: invalid syntax (hexadecimal and octal not allowed in float)
Bool Data Types in Python
bool is a predefined class in Python and a fundamental data type. It is mainly used in conditions, comparisons, and logical operations.
- In Python, True is treated as 1, and False is treated as 0.
- Non-zero and non-empty values are True, while 0, None, and empty values are False.
# Example 1: Boolean values
is_active = True
print(is_active, type(is_active)) # Output: True < class 'bool' >
# Example 2: Arithmetic with Boolean values
result = True + False # 1 + 0
print(result, type(result)) # Output: 1 < class 'int' >
# Example 3: Comparison operation
print(False == 0) # Output: True < class 'bool' >
Exercise
- Fill in the Blanks:
- - What will be the output of `print(True > True)`?_______.
- - In the expression `num = (3e-4) * 4`, the value stored in `num` is _______.
- - The decimal conversion of `0xBEEF` is _______ and it belongs to the _______ number system.
- - The result of evaluating `False * False - True` in Python is _______.
- - The output of the expression `var = 250 * 4 // 3` is _______ and the type is _______.