Chapter 3 Data Types
In Python, data types are classifications that specify which type of value a variable can hold. Here are some of the basic and commonly used data types in Python:
- Numeric Types:
- int: Integer type, e.g.,
x = 5. - float: Floating-point type, e.g.,
y = 3.14. - complex: Complex number type, e.g.,
z = 2 + 3j.
- int: Integer type, e.g.,
- Text Type:
- str: String type, e.g.,
s = "Hello, World!".
- str: String type, e.g.,
- Sequence Types:
- list: Ordered collection of items, e.g.,
my_list = [1, 2, 3]. - tuple: Immutable ordered collection of items, e.g.,
my_tuple = (1, 2, 3).
- list: Ordered collection of items, e.g.,
- Set Types:
- set: Unordered collection of unique items, e.g.,
my_set = {1, 2, 3}. - frozenset: Immutable version of a set.
- set: Unordered collection of unique items, e.g.,
- Mapping Type:
- dict: Dictionary, a collection of key-value pairs, e.g.,
my_dict = {'a': 1, 'b': 2}.
- dict: Dictionary, a collection of key-value pairs, e.g.,
- Boolean Type:
- bool: Boolean, representing
TrueorFalse.
- bool: Boolean, representing
- None Type:
- NoneType (
None): A special type representing the absence of a value or a null value.
- NoneType (
- Binary Types:
- bytes: Immutable sequence of bytes, e.g.,
b = b'hello'. - bytearray: Mutable sequence of bytes.
- memoryview: A view object that exposes an array’s buffer interface.
- bytes: Immutable sequence of bytes, e.g.,
These data types are the building blocks for creating variables, structures, and performing various operations in Python. You can use the type() function to check the type of a variable or value. For example:
x = 5
print(type(x)) # Output: <class 'int'>
y = 3.14
print(type(y)) # Output: <class 'float'>
s = "Hello, World!"
print(type(s)) # Output: <class 'str'>Understanding and working with these data types is fundamental to writing Python code. Keep in mind that Python is dynamically typed, meaning you don’t need to explicitly declare the data type of a variable; it is determined at runtime.
3.0.1 Numeric Types
3.0.1.1 Mathematical Operations
Python supports a variety of mathematical operations, which can be performed on numerical data types. Here are some common mathematical operations in Python:
3.0.1.2 Arithmetic Operations:
Addition (
+):Subtraction (
-):Multiplication (
*):Division (
/):Floor Division (
//):Modulus (
%):Exponentiation (
**):
3.0.2 Other Mathematical Functions:
Absolute Value (
abs()):Round (
round()):Minimum (
min())/Maximum (max()):
These are just a few examples, and Python provides a rich set of mathematical functions and operations through the math module as well. To use it, you can import the math module and access functions like math.sqrt(), math.sin(), math.cos(), etc.
import math
# Square root
result = math.sqrt(16) # result is 4.0
# Trigonometric functions (input in radians)
sin_result = math.sin(math.radians(30)) # result is 0.5 (sin of 30 degrees)
cos_result = math.cos(math.radians(60)) # result is 0.5 (cos of 60 degrees)
tan_result = math.tan(math.radians(45)) # result is 1.0 (tan of 45 degrees)
# Logarithmic functions
log_result = math.log(100, 10) # result is 2.0 (log base 10 of 100)
# Exponential function
exp_result = math.exp(2) # result is approximately 7.389
# Constants
pi_value = math.pi # value of pi (3.141592653589793)
euler_number = math.e # Euler's number (2.718281828459045)
# Other functions
factorial_result = math.factorial(5) # result is 120 (5!)