- Python type

 In Python, a "type" refers to the data type of a value or object. Python has several built-in data types, including integers, floating-point numbers, strings, and booleans. You can use the type() function to determine the type of a value or object in Python. Here are some examples:

x = 10 print(type(x)) # Output: <class 'int'> y = 3.14 print(type(y)) # Output: <class 'float'> z = 'hello' print(type(z)) # Output: <class 'str'> b = True print(type(b)) # Output: <class 'bool'>

In addition to the built-in data types, Python also allows you to create custom data types using classes. You can define a class by using the class keyword and specifying the attributes and methods that the class should have. Here is an example of a simple class in Python:

class Point: def __init__(self, x, y): self.x = x self.y = y def distance(self, other): dx = other.x - self.x dy = other.y - self.y return (dx ** 2 + dy ** 2) ** 0.5 point1 = Point(0, 0) point2 = Point(3, 4) print(type(point1)) # Output: <class '__main__.Point'> print(point1.distance(point2)) # Output: 5.0

In this example, Point is a custom data type that represents a point in 2D space. The __init__ method is a special method that is called when you create a new instance of the class. The distance method calculates the distance between two points.

For more information on data types and classes in Python, you can refer to the Python documentation or search online for tutorials and examples.

0 댓글