Python data type, python

Source: Internet
Author: User

Python data type, python
Data Type unit conversion

  • The smallest unit is bit, indicating a binary value of 0 or 1. Generally, it is represented by lowercase B.

  • The minimum unit of storage is Byte, 1B = 8b

  • 1024B = 1KB

  • 1024KB = 1 MB

  • 1024 MB = 1 GB

  • 1024 GB = 1 TB

  • 1024 TB = 1 Pb

  • 2 ^ 32 = 2 ^ 10*2 ^ 10*2 ^ 10*2 ^ 2 = 4 GB

Hexadecimal conversion
  • Decimal

    • Base is 0 ~ 9, every 10 to 1

    • Instance: 123 = 1*10 ^ 2 + 2*10 ^ 1 + 3*10 ^ 0 = 100 + 20 + 3 = 123

  • Binary

    • Base 0 and 1, every 2 in 1, python starts with '0b'

    • Example: 0b110100 = 2 ^ 5 + 2 ^ 4 + 2 ^ 2 = 32 + 16 + 4 = 52

    • Decimal to binary: writes in reverse order except for the remainder of 2.

      • 22 = 0b10110

  • Octal

    • Base 0 ~ 7, every 8 to 1, starting with '0o' in python

    • Example: 0o123 = 1*8 ^ 2 + 2*8 ^ 1 + 3*8 ^ 0 = 64 + 16 + 3 = 83

    • Octal to binary: octal 1-bit binary

      • 0o123 = 0b 001 010 011

  • Hexadecimal

    • Base 0 ~ 9. ~ F, every 16 to 1, starting with '0x 'in python

    • Example: 0xAB = 10*16 ^ 1 + 11*16 ^ 0 = 160 + 11 = 171

    • Hexadecimal to binary: hexadecimal 1-bit indicates 4-bit binary

      • 0 xABC = 0b 1010 1011 1100

  • In which operating system is the data stored in the computer? Why?

    • Binary, because the computer only recognizes binary, the specific reasons are as follows:

    • High Stability (only 0 and 1), low cost (technically easy to implement), consistent with true and false logic in life

  • How is data stored in a computer?

    • How to store negative numbers?

      • The highest digit is used as the symbol bit. 0 indicates a positive number, and 1 indicates a negative number.

      • Although it sacrifices the least commonly used highest bit, it solves the negative number problem and simplifies the hardware design (cost saving)

    • Source code, anti-code, and supplemental code?

      • Three positive numbers are the same, and only negative numbers have backcodes and supplementary codes.

      • The reverse code is equal to the original code's symbol bit, and the rest are reversed

      • Complement equals to add 1 to the anti-code

      • All data in the computer exists in the form of a complement.

Data Type
  • Data Types exist to process different operations,

  • Data Types in python include integer, floating point, String, list, tuples, Dictionary, set, and null.

  • INTEGER (int): an integer in mathematics.

  • Float: refers to the decimal point in mathematics.

    # Decimal point (floating point number)
    B = 3.14
    Print (type (B ))

    # Scientific notation
    C = 3.14e-3
    Print (c)
  • Boolean: corresponds to the true and false logic in life


    # Bool type, only two values, True and False
    D = False
    Print (type (d ))
  • NULL (NoneType): a special type with only one value (None)

  • String (str): a string of characters enclosed in quotation marks.

    • Either single or double quotation marks are allowed, or three single or double quotation marks are allowed.

    • Double quotation marks in single quotation marks and single quotation marks in double quotation marks must be escaped.

      • Escape characters: newline (\ n), tabulation (\ t), etc.

      • Sometimes '\' also needs to be escaped

    • Line feed processing when a line cannot be displayed


    g = 'hello' \
       ' world'
    print(g)

    h = 'hello \
    world'
    print(h)

    i = '''
    abc
    def
    123
    '''
    print(i)
    • Count characters


    # No matter what character, it is a length
    S = 'Hello Day'
    Print (len (s ))
    • String concatenation


    # Directly splice
    S2 = 'hello' + 'World'
    Print (s2)

    # Repeat the specified string several times and splice it together
    S3 = 'abc' * 3
    Print (s3)
    • Extract A Single Character


    S4 = '000000'
    # Extract from the beginning, and the following table starts from 0
    Print (s4 [0])
    # Extract from the end. The following table starts from-1.
    Print (s4 [-2])
    • String slices (extract segments)


    S5 = 'abcdefg'
    # S [start table: end table], including the start table elements, excluding the end table elements
    Print (s5 [0: 3])
    # Extract from the beginning to the specified position
    Print (s5 [: 4])
    # Extract from the specified table to the end
    Print (s5 [3:])
    # The following table starts from 1 and is extracted to the second to the last, excluding the second to the last.
    Print (s5 [1:-2])
    # Extract the last 4 characters
    Print (s5 [-4:])
    # Extract all
    Print (s5 [:])
    # Step value. The default value is 1.
    Print (s5 [: 2])
    # Reverse string order
    Print (s5 [:-1])
    • Format


    Name = 'gouda'
    Like = 'wangdahua'
    Age = 18

    Print ('My name is % s, crush % s, she % d years old '% (name, like, age ))

    # Character => ASCII
    Print (ord ('A '))
    # ASCII => character
    Print (chr (65 ))

Print ('My name is {}, I like {}, {} years old this year '. format (name, like, age) print ('My name is {2}, I like {1}, {0} years old this year '. format (name, like, age) print ('My name is {nm}, I like {lk}, {a} years old this year '. format (nm = name, lk = like, a = age ))



> % S: placeholder for the string
>
> % D: placeholder for integer data
>
> % F: placeholder for Floating Point Numbers
>
> % C: placeholder for characters
>
> • ASCII:
>
> • 0 ~ Am ~ 57
>
> • ~ Z: 65 ~ 90
>
> • ~ Z: 97 ~ 122

-List: defined by [], which can store any type of data

-Values: lt [0] And lt [-1]
-The list can be modified. lt [1] = 'min min'
-Out-Of-Boundary Problems
-You can also perform '+' and '*' operations.
-You can also perform slicing.
-Lt [:] = [] indicates clearing the list
-Lt [: 2] = []: Delete the first two elements.

-Tuple: defined by (). Other operations are the same as those in the list.

-The tuples cannot be modified.
-When there is only one element, add a comma after the element.

-Set: defined,

-Empty sets cannot be defined using braces (left for dictionary) and set ().
-The set cannot be modified.
-Unordered element in the Set
-The elements in the set cannot be repeated.
-Easy to use

'''Python
S = {'Li Bai ', 'du Fu', 'bai juyi', 'su Shi ', 'Tao Yuanming', 'Li Bai '}
S2 = {'Li Bai ', 'shang Yin', 'du Mu ', 'su Shi '}
Print (type (s ))
Print (s)
# Intersection
Print (s & s2)
# Union
Print (s | s2)
# Difference set
Print (s-s2)
Print (s2-s)

# Empty set defined in this way, cannot be used {}
S3 = set ()
Print (type (s3 ))
  • Dictionary (dict): defined {}

    • Elements are composed of key-value pairs.

    • Use ':' To connect keys and values.

    • The keys in the dictionary are unique and the values can be arbitrary.

    • The dictionary is variable.

    • Easy to use:


    D = {'name': 'xiaoming', 'age': 18}
    Print (d)
    Print (type (d ))

    # Extract the corresponding value based on the key
    Print (d ['name'])

    # If the key does not exist, an error is returned.
    # Print (d ['abc'])
    # If a key exists, the corresponding value is returned. If the key does not exist, None is returned. You can also customize the default value.
    Print (d. get ('abc', 'default '))

    # Add a key-value pair if no key-Value Pair exists
    # D ['hello'] = 'World'
    # Modify an existing one
    D ['name'] = 'xiaofang'
    # Deleting an element
    # Del d ['name']
    # Print (d)

    # Len returns the number of key-value pairs
    Print (len (d ))
  • Forced type conversion


    Int () # convert to integer
    Float () # convert to floating point
    Str () # convert a string
    List () # convert to list
    Tuple () # convert to tuples
    Set () # convert to a set
    Dict () # convert to Dictionary

    L = [('name', 'xiaoming'), ('age', 18)]
    # Convert a list to a dictionary
    D1 = dict (l)
    Print (d1)

    List deduplication, list => set => List

  • Get variable address


    a = 10
    print(id(a))

    Do not go into the address of the Variable

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.