The data type

  • 1.1 Number Types
  • 1.2 Conversion between Numeric types
  • 1.3 String Types
    • 1.3.1 String Representation
    • 1.3.2 String Formatting
    • 1.3.3 String Search
    • 1.3.4 Converting Strings to Numbers

Python has six standard data types: numbers, strings, lists, tuples, collections, and dictionaries

1.1 Number Types

Let’s keep in mind:

  • Integer type: int

    • Binary: 0b is prefixed and the first digit is 0
    • Octal: the 0o prefix begins with the digit 0
    • Hexadecimal: 0x is the prefix and the first digit is 0
  • Floating point type: float

    • In practice, it is important to note that the calculations between floats are not exact. Try calculating 0.2+0.4 and 1.2+1.4 in IDLE
  • Complex type: complex

    • Note that the real and imaginary parts are floating point numbers (1+2j). Real = 1.0, (1+2j).imag = 2.0
  • Boolean type: bool

    • There are only two values True and False
    • None, False, 0, 0.0, 0j (complex), “”(empty string), [], {}, () are all False, and all others are True

1.2 Conversion between Numeric types

Other than complex numbers, integers, floats, and Booleans can be converted to each other. One is a conversion in an operation: example: Integer + Boolean = integer integer, Boolean + float = float Convert to Boolean

1.3 String Types

1.3.1 String Representation

Print (' ordinary string in single quotes ') print(" ordinary string in double quotes ") print(" abcdefghijklmn """) print(r" ABC (){}\n") # This tells Python that the quotes are the original string and do not need to be escapedCopy the code

1.3.2 String Formatting

There are many ways to format, just to illustrate

Name = 'Mary' age = 18 money = 1234.5678 print(f'{name} year {age}, salary {money} yuan ') print('{} year {} year, {} $'. Format (name,age,money)) print('{1} $'. Format (name,age,money)) print('{1} $'. Format (age,name,money)) 0 = age,1=name,2=money print('%s year %d, monthly salary %.4f '% (name,age,money) # %Copy the code

1.3.3 String Search

There are many ways to manipulate strings, and the book only describes two search methods str.find() and str.rfind(). You can view the string method by typing dir(STR) in idle. As the count, index, isalnum isalpha, split, upper/lower, join, strip, replace, etc., are the commonly used method.

1.3.4 Converting Strings to Numbers

Very simple, just int(STR), float(STR) and STR (allType) can convert any type to a string type.

This article is from the SDK community: www.sdk.cn