- Get link
- X
- Other Apps
DATA TYPES IN PYTHON
Python is a versatile and powerful programming language that supports various data types, enabling developers to perform a wide range of operations. In Python, a data type specifies the type of a value that is stored in a variable. Understanding Python's built-in data types is essential for every Python programmer, whether you're a beginner or an experienced developer. In this blog, we will explore the most commonly used data types in Python and provide examples to help you better understand how to work with them.
1. Numeric
In Python, numeric data types are used to represent numbers. Python has three built-in numeric types:
- int (Integer)
- float (Floating point)
- complex (Complex numbers)
Integer (int)
An integer is a whole number, which can be either positive or negative. It doesn't contain any decimal points.
Characteristics of int:
- Can be any number without a decimal point.
- Python integers can be of any size, limited only by the available memory.
- Integer division always returns a float in Python 3.
Example:
Code:
Output:
Operations with Integers:
- Addition, subtraction, multiplication, division, etc.
- Division (
/) returns a float result in Python. - Integer division (
//) returns the quotient without the remainder. - Modulus (
%) returns the remainder of the division.
Example:
Code:
x = 15y = 4
print(x / y) # Division (float)
print(x // y) # Integer Division
print(x % y) # ModulusOutput:
Floating Point Numbers (float):
A floating point number is a number that has a decimal point or is expressed in scientific notation. Floats are used when more precision is needed, for example in calculations involving real numbers or measurements.
Characteristics of float:
- Can represent real numbers, positive or negative, with decimal points.
- Can also be represented in scientific notation.
Example:
Code:
a = 3.14 # Positive float
b = -0.001 # Negative float
c = 2.0 # Float with zero fractional partprint(a, b, c)
Output:
3.14 -0.001 2.5
Operations with Floats:
- Floating point numbers support all standard arithmetic operations.
- Precision errors can occur due to the way floats are stored in memory.Code:
x = 1.4y = 2.8print(x + y) # Addition
print(x / y) # Division
print(x * y) # Multiplication
Output:
4.20.5 3.92
Complex Numbers (complex):
A complex number is a number that has both a real and an imaginary part. In Python, complex numbers are written as a + bj, where a is the real part and b is the imaginary part, with j denoting the imaginary unit.
Characteristics of complex:
- A complex number is written as
a + bj, whereais the real part andbis the imaginary part. - The imaginary part is represented by
j. - You can use complex numbers for scientific calculations, electrical engineering, etc.
Example:
Code:
a = 3 + 4j # Complex number with real part 3 and imaginary part 4
b = 1 - 2j # Complex number with real part 1 and imaginary part -2
print(a, b)
Output:
(3+4j) (1-2j)
Operations with Complex Numbers:
- Addition, subtraction, multiplication, and division can be performed on complex numbers.
- Python provides
.realand.imagattributes to access the real and imaginary parts separately.
Example:
Code:
x = 3 + 4j
y = 1 - 2j
print(x + y) # Complex number addition
print(x - y) # Complex number subtraction
print(x.real) # Access real part
print(x.imag) # Access imaginary part
Output:
(4+2j)
(2+6j)
3.0
4.0
Type Conversion in Numeric Data Types
Python allows for easy type conversion between different numeric types. You can convert an integer to a float, a float to an integer, or a complex number to other types (though complex to int or float directly is not supported).
Code:
# Converting int to float
a = 7
b = float(a)
print(type(b)) # <class 'float'>
# Converting float to int
x = 3.14
y = int(x) # Converts to 3 (decimal part is discarded)
print(type(y)) # <class 'int'>
Output:
<class 'float'>
<class 'int'>
Visual Representation of Numeric Types
Integer:
An integer is a whole number, like:
5, -12, 0, 245
Floating Point:
A floating-point number includes a decimal point, like:
3.14, -0.99, 7.5
Complex Number:
A complex number has both real and imaginary parts:
3 + 4j, -1 + 2j2.Strings:
Strings are sequences of characters enclosed within single (') or double (") quotes. Strings are used to represent textual data.
- Strings can be sliced, concatenated, and repeated.
- Strings in Python are immutable, meaning their content cannot be changed once created.
Example:
Code:
name = "Python"
greeting = 'Hello, World!'
You can also use multi-line strings with triple quotes:
multi_line_string = '''This is a multi-line string.'''
3. Lists:
Lists are ordered, mutable collections of elements. Lists can contain items of different data types, including other lists. You can modify, add, or remove elements in a list.
fruits = ["apple", "banana", "cherry"]
print(type(fruits)) # <class 'list'>
print(fruits[0]) # apple
Lists are flexible and support various operations such as appending, removing, and slicing:
Example:
Code:
fruits.append("orange") # Adds 'orange' to the list
print(fruits) # ['apple', 'banana', 'cherry', 'orange']
4. Tuples:
Tuples are similar to lists but are immutable. Once created, you cannot modify their content. They are useful for storing data that should not be changed.
Example:
Code:
coordinates = (1, 2, 3)
print(type(coordinates)) # <class 'tuple'>
print(coordinates[1]) # 2
Tuples are often used to represent fixed collections of items, such as coordinates or RGB values.
5. Dictionaries:
Dictionaries are unordered collections of key-value pairs. Each key must be unique, and the values can be of any data type. Dictionaries are ideal for storing data that can be accessed by keys.
Example:
Example1:
Code:
student = {"name": "Alice", "age": 22, "major": "Computer Science"}
print(type(student)) # <class 'dict'>
print(student["name"]) # Alice
Dictionaries are versatile and support operations such as adding or modifying key-value pairs:
Example2:
Code:
student["graduation year"] = 2025 # Add a new key-value pair
print(student)
6. Sets:
A set is an unordered collection of unique elements. Sets do not allow duplicate values, and their elements are immutable. However, sets themselves are mutable, meaning you can add or remove items.
Example1:
Code:
numbers = {1, 2, 3, 4, 5}
print(type(numbers)) # <class 'set'>
print(numbers) # {1, 2, 3, 4, 5}
Sets are useful for removing duplicates from a list or performing set operations like union, intersection, and difference:
Example2:
Code:
set1 = {1, 2, 3}
set2 = {2, 3, 4}
print(set1.union(set2)) # {1, 2, 3, 4}
7. Booleans (bool):
Booleans represent one of two possible values: True or False. They are used in conditional statements and logical operations.
Example:
Code:
x = True
y = False
print(type(x)) # <class 'bool'>
Booleans are essential for controlling the flow of a program through conditional expressions.
8. None Type (None):
The None type represents the absence of a value or a null value. It is commonly used to signify that a variable has no value assigned to it.
Example:
Code:
x = None
print(type(x)) # <class 'None Type'>
Type Conversion
Python provides functions to convert between different data types. Some of the most commonly used conversion functions are:
int(): Converts to integerfloat(): Converts to floatstr(): Converts to stringlist(): Converts to listtuple(): Converts to tuple
Example:
x = "123"
y = int(x) # Convert string to integer
print(type(y)) # <class 'int'>
- Get link
- X
- Other Apps
Comments
Post a Comment