程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Get some common data structures of Python!

編輯:Python

author | Become a fine firewood

source | CSDN Blog

python Basic data type

  • Numeric type

    • Type of data in specific operation

    • Boolean types in different cases

  • The representation and conversion of all bases

    • The representation of hexadecimal

    • Base conversion

  • Sequence

    • str character string

    • list (list)

    • Tuples (tuple)

    • Sequence summary

  • aggregate (set)

  • Dictionaries (dict)

  • Escape character

  • Summary of knowledge points

First, you need to install your computer Python Environmental Science , And it's installed Python development tool .

Numeric type

  • Integers :int

  • Floating point numbers :float

python Data types in == have only int and float Two kinds of ==( Nothing like short,long,double Points )

Type of data in specific operation

Here you can see the data types used in various situations python Medium ==type function ==

print(type(1))
print(type(-1))
print(type(1.1111))
print(type(1+1))
print(type(1+1.0))  #  because 1.0 by float type ,python take 1+1.0 Automatically convert to float type
print(type(1*1))
print(type(1*1.0))
#  python The division in '/' The result is float type , Use "//" by int type
print(type(2/2))
print(type(2//2))
print(type(1//2))  #  Similar to other languages python Division by middle ignores the number after the decimal point 

Running results :

== Summary :==

  • 1、 As long as floating-point numbers appear in the formula ( decimal ) Eventually the overall type will become float type

  • 2、 Use '/' The result is float type , Use "//" by int type ( The first point has the highest priority )

  • 3python Division by middle ignores the number after the decimal point , Rounding down

Boolean type :bool ==python in , Boolean type is also a kind of number ==

Boolean types in different cases

#  bool Types include  True and  False Two kinds of
print(type(True))
print(type(False))
#   take bool Type conversion to int type
print(int(True))
print(int(False))
#  python in 0 For false , Not 0 It's true ( Regardless of base )
print(bool(1))
print(bool(0))
print(bool(2.2))
print(bool(0b10))
#   Take a Boolean value on a string
print(bool('abc'))
print(bool(''))
#   Take Boolean values for the list
print(bool([1,2,3]))
print(bool([]))

Running results :

== Summary ==

  • True and False The beginning should be capitalized

  • In the numbers :0 by False, Others are True; In other types : Empty as False, Not empty True

The representation and conversion of all bases

The representation of hexadecimal

  • Binary system :0b

  • octal :0o

  • Hexadecimal :0x

#  The binary identifier is  0b, Print out the decimal number it represents
print(0b10)
print(0b11)
#  The octal identifier is  0o, Print out the decimal number it represents
print(0o10)
print(0o11)
#  The hexadecimal identifier is  0x, Print out the decimal number it represents
print(0x10)
print(0x1F)
#  The input number defaults to decimal
print(10)

result :

== Summary ==: Keep in mind the various decimal representations

Base conversion

  • Convert to binary (binary):bin()

  • Convert to octal (octal):oct()

  • Convert to decimal :int()

  • Convert to hex (hexadecimal):hex()

#  Convert to binary
print(bin(10))
print(bin(0o7))
print(bin(0xE))
#  Convert to octal
print(oct(0b111))
print(oct(0x777))
#  Convert to decimal
print(int(0b111))
print(int(0o777))
#  Convert to hex
print(hex(888))
print(hex(0b111))
print(hex(0o7777))

Running results :


Sequence

str character string

  • The string type is expressed as a single / What's in double quotation marks , Because single quotation marks may appear in English sentences (Let's go), In this case, you can use double quotation marks to enclose the contents of the string

print("Let't go")
print('Let't go')  #   This statement will report an error 

Running results :

Operation of string

  • Two strings can be added and spliced into one string

  • Multiply the string by a number n, obtain n This string

#   Operation of string
print("he"+"llo")
print("hello"*3)

result

Get a single character

  • stay str Add [i](i The representative wants to get str Place subscript in ), Can get the character at the specified position

#   Output the character at the specified position
print("hello world"[0])
print("hello world"[1])
print("hello world"[2])
print("hello world"[-1])
print("hello world"[-2])

result

among ==i It can be negative ==, Represents getting the penultimate i Digit number

Get the string in the specified interval

  • Use ‘:’ Connect the start position and the end position , Such as :[m:n+1] To intercept str Subscript is ==m~n+1== String to intercept (==n+1 Position take off section ==) [m:] It means that the subscript is m The position of is truncated to the end

#   Intercepts the string of the specified region
print("hello world"[0:5])
print("hello world"[-5:11])
print("hello world"[-5:])

list (list)

The type of data stored in the list

  • Any element can be stored in the list

#   List types that can be stored
print(type([1, 2, 3, 4, 5]))
print(type(["hello", 1, False]))
print(type([[1, 2], [3, 4], [True, False]]))  #  Nested list 

Read the elements in the list

  • Read the element method and... In the list str identical

#   Read the elements in the list
print(["hello", "world"][0:])  #  and str Type is read in the same way 

The operation of the list

  • And str The operations of are similar

#   The operation of the list ( and str The operations of are similar )
print(["hello", "world"] + ["hello", "world"])
print(["hello", "world"] * 3)

Tuples (tuple)

  • The basic use of tuples , The rules for storing data are the same as the list , Their differences are mainly in the following points :

  • A list is a dynamic array , They are variable and can be reset in length ( Change the number of internal elements ).

  • Tuples are static arrays , They are immutable , And its internal data cannot be changed once it is created .

  • Tuples are cached in Python Runtime environment , This means that every time we use tuples, we don't need to access the kernel to allocate memory .

Basic operations on tuples

#   Tuple storage is a data type
print(type((1, 2, 3, 4, 5)))
print(type((1, 2, "hello", [1, 2, 3], True)))
#   Get the specified location element
print((1, 2, 3, 4)[2])
#   Get the specified area element
print((1, 2, 3, 4)[1:])
print(type((1, 2, 3, 4)[1:]))  #  The return type is tuple
#   The operation of tuples
print((1, 2, 3, 4)+(5, 6))
print((1, 2, 3, 4)*2)

Sequence summary

  • Method of extracting element at specified position : The sequence is followed by [i]

  • Extract the specified interval element method : The sequence is followed by [m : n](m,n Interval subscript ,n It's an open range )

== The sequence type operates on its elements in exactly the same way ==

aggregate (set)

  • The data in the set is == A disorderly ==, so == You can't use subscripts == Visit

  • The elements in the collection == No repetition ==

The operation of sets

  • The operation method of set is the same as that of set in mathematics

#   Find the difference set of two sets
print({1, 2, 3, 4, 5, 6} - {2, 3})  # '-' For the symbol of the difference set
#   Find the intersection of two sets
print({1, 2, 3, 4, 5, 6} & {2, 3})  # '&' To find the symbol of intersection
#   Find the union of two sets
print({1, 2, 3, 4, 5, 6} | {5, 6, 7})  # '-' For the symbol of the difference set 

  • Definition of empty set :

>>> set()

‍‍‍‍‍‍

Dictionaries (dict)‍‍‍‍‍‍

  • The difference between a dictionary and a set definition :dict:{key1:value1,key2:value2.....} set:{value1,value2....}

  • Key in dictionary == Cannot be repeated or changed ==( Such as list A list is a mutable type )

  • An empty dictionary uses {} Express

#   Input format of dictionary type
print(type({1: 1, 2: 2, 3: 3}))
#   The use of dictionaries
print({1:"Hello", 2:"world"}[2])

Escape character

Looking back

Matplotlib Two methods of drawing torus !

13 individual python Necessary knowledge , Recommended collection !

Artifact , Easy visualization Python Calling process !

Low code out of half a lifetime , Come back or " cancer "!

 Share
Point collection
A little bit of praise
Click to see 

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved