Detailed explanation of basic data types of python Foundation

Basic data type 1, Number (number...
1, Number (number)
2, Character type
Basic data type

1, Number (number)

1. Classification

  • integer
  • Floating point
  • Boolean
  • complex

2. Integer

  • explain

    python can handle integers of any size, including complex numbers

  • General definition

    # General definition num1 = 10 # ID (variable name): you can view the memory address of the variable # Hex (decimal number): converts a decimal number to a hexadecimal number (beginning with 0x, not part of the data content) print(id(num1), hex(id(num1))) # Type (variable name): you can view the type of variable, int represents integer number print(type(num1))
  • Continuous definition

    # Continuous definition num2 = num3 = num4 = 5
  • Interaction definition

    # Interaction definition num5, num6 = 1, 2
  • Explore address issues

    # Address problem (small integer object [- 5 ~ 256]) # If multiple variables are equal to small integer objects, the addresses of these variables are the same, because small integers are often used, which can save space and improve efficiency num7 = 1 num8 = 1 print(id(num7), id(num8)) #Same address num9 = 501 num10 = 501 print(id(num9), id(num10)) #The address is different. It needs to be executed under Linux

3. Floating point

  • explain

    It consists of integral part and decimal part

  • be careful

    Operation may have rounding error

  • Example

    num1 = 0.123434645 num2 = 0.2 print(num1 + num2) print(type(num1), hex(id(num1))) print(num1)

4. Boolean value

Description: a boolean variable has only two values: True and False

Function: as a true or false judgment

a = True b = False print(a, b)

5. print supplement

height = 173.555 print("lucky is a good man!His height is %f"%(height)) print("lucky is a good man!His height is %.2f"%(height)) age = 18 print("lucky is a nice man!He is *%d* years old"%(age)) print("lucky is a nice man!He is *%4d* years old"%(age)) print("lucky is a nice man!He is *%-4d* years old"%(age))

6. Mathematical function

Description: operation of operands

  • abs

    # Find absolute value num1 = 5 num2 = abs(num1) print("num2 = %s"%(num2))
  • max

  • # Find the maximum of many numbers num3 = max(2,3,5,6,1,5,77,54,2) print("num3 = %s"%(num3))
  • min

    # Finding the minimum value of many numbers print(min(2,3,5,6,1,5,77,54,2))
  • pow

    # Find the Y power of X pow(x, y) print(pow(2,3))
  • round

    # rounding # round(x[, n]) [round(x) round(x, n)] # Round x, n is the number of decimal places print(round(3.1415926)) print(round(3.1415926, 4))
  • math.ceil

    # Import mathematics module import math
    # Round up print(math.ceil(10.2))
  • math.floor

    # Round down print(math.floor(10.9))
  • math.modf

    # Get the decimal and integer parts of floating-point numbers # The result is a tuple. The first element of the tuple is the decimal part, and the second element is the integer part print(math.modf(10.3))
  • math.sqrt

    # Square root print(math.sqrt(10))

7. Random function

Import random module

# Import random module import random
  • random.choice

    # choice(seq) # Get an element at random from a sequence (set) print(random.choice([1,2,4,3,5,6,7,8,9,0]))
  • random.randrange

    # randrange([start,]stop[,step]) # randrange(start,stop,step) # randrange(start,stop) # randrange(stop) # Start: Specifies the start value of the range, included in the range, starting from 0 by default # stop: Specifies the end value of the range, not included in the range # Step: specify step value, default is 1 print(random.randrange(1, 10, 2)) print(random.randrange(1, 10))
  • random.random

    # random() # Randomly generate a real number with a range of [0,1], and the result is a floating-point number print(random.random())
  • random.uniform

    # uniform(x, y) # Random Shencheng a real number, range in [x, y], get a floating point number print(random.uniform(3, 7))
  • random.randint

    # randint(start, stop) # Get an integer in the specified range [start, stop] print(random.randint(1, 4))

2, Character type

1. What is a string?

A string is any text enclosed in single or double quotation marks

str1 = 'lucky is a good man' str2 = "lucky is a nice man"

be careful:

1. Quotation mark itself is a form of expression and does not belong to the content of string
2. If the string itself is enclosed with single quotation marks, the outer side is enclosed with double quotation marks ("he's a good man")

2. Multiline character

Any text with '' or ''

str3 = ''' good nice cool handsome''' str4 = """ good nice cool handsome"""

15 May 2020, 11:55 | Views: 3629

Add new comment

For adding a comment, please log in
or create account

0 comments