02. Data Types in Python

The commonly used data types in Python include numeric types, string types, boolean types, and composite types (sequences, indices, tuples), among others.Using the built-in function type(), you can check the data type of a variable. For example:

a=2
b=type(a)
print(b)  # Output: <class 'int'>

1. Numeric TypesIncludes integers, floating-point numbers, etc.

x=100
y=100.0
print('The data type of x is:', type(x))  # Output: The data type of x is: <class 'int'>
print('The data type of y is:', type(y))  # Output: The data type of y is: <class 'float'>

Note: Adding two floating-point numbers may lead to precision issues, known as uncertain trailing digits. For example:02. Data Types in Python2. String Data TypeThe string data type is one of the most commonly used types in Python. It is a sequence of characters that can represent any character recognizable by the computer. Its delimiters can be single quotes, double quotes, or triple quotes, with triple quotes indicating a multi-line string.For example:

city='Suzhou'
print(city)
info='''Address: Suzhou, Jiangsu Province, China
Contact: Zhang San
Phone: 051212345678'''  
print(info)

Some special strings

  1. Escape Characters

Escape characters are a type of special string.
Newline character
Horizontal tab, used to jump to the next tab stop” Double quote’ Single quote\ A backslashNote: To disable escape characters, prefix with r or R.
Additionally, strings can be manipulated, such as: x+y concatenates strings x and y.x*n or n*x replicates the string n times, e.g., print(‘-’*10) outputs ten dashes.x in s checks if x is a substring of s, returning True if it is, otherwise False.3. Boolean Data TypeUsed to represent true or false, in Python represented by True and False, where True represents 1 and False represents 0. For example:

a=2
b=3
print(a>b)  # Output: False
print(a<b)  # Output: True

4. Composite TypesIncludes sequences, indices, tuples, which will be introduced in a separate article.5. Data Type ConversionData types can be converted between each other:int(x)float(x)str(x)ord(x)hex(x)oct(x)bin(x)For example:02. Data Types in PythonFinally, a supplementary function: eval(), a built-in Python function used to remove the outermost quotes, often used with the input() function.Syntax format:variable=eval(string)For example:02. Data Types in Python

Leave a Comment