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

Basic introduction to python (combined data types)

編輯:Python

Set type definition

         Collection types Consistent with the concept of set in Mathematics , There is no order between set elements , Every Element uniqueness , There is no same element set element cannot be changed , It can't be a variable data type . Set with braces ‘{}’ Express , Separate the elements with commas to establish the collection type with Curly braces ‘{}’ or set(), Create an empty set type , You have to use set().
     

A = {"123", 456, (123, 456)} # Set up with parentheses >>>{456, (123, 456), '123'}
B = set("123456") # Use set Set up a collection >>>{'6', '2', '4', '5', '1', '3'}
C = {"123", 456, "123", 456} # The elements are unique and unordered >>>{456, '123'}

   
Set operators
        

Operator and its application describe S=T and , Return to a new collection , Included in the assembly S and T All elements in S-T Bad , Return to a new collection , Included in the assembly S But not here. T The elements in S&T hand over , Return to a new collection , Including the simultaneous assembly of S and T The elements in S^T repair , Return to a new collection , Including set S and T Different elements in S<=T or S<T return True/False, Judge S and T A subset of S>=T or S>T return True/False, Judge S and T The inclusive relationship of S |= T and , Update collection S, Included in the assembly S and T All elements in S -= T Bad , Update collection S, Included in the assembly S But not here. T The elements in S &= T hand over , Update collection S, Including the simultaneous assembly of S and T The elements in S ^= T repair , Update collection S, Including set S and T Different elements in

A = {123, "456", "p", "y"}
B = set("python")
print("A|B:", A | B)
print("A&B:", A & B)
print("A^B:", A ^ B)
print("A-B:", A - B)
# A|B: {'o', 'y', '456', 'p', 't', 'h', 'n', 123}
# A&B: {'p', 'y'}
# A^B: {'h', 't', 'o', 'n', '456', 123}
# A-B: {'456', 123}

Set processing method

Operation function or method describe S.add(x) If x Don't set S in , take x Add to SS.discard(x) remove S Medium element x, If x Don't set S in , Don't complain S.remove(x) remove S Medium element x, If x Don't set S in , produce KeyError abnormal S.clear() remove S All elements in s.pop() Random return S An element of , to update S, if S Create... For nothing KeyError abnormal S.copy() Back to the assembly S A copy of len(S) Back to the assembly S Number of elements of x in s Judge S Medium element x,x In collection S in , return True, Otherwise return to Falsex not in s Judge S Medium element x,x Don't set S in , return True, Otherwise return to Falseset(x) Put other types of variables x Change to set type


Sequence type definition

         Sequence It's a group of elements that have a precedence relationship . A sequence is a vector of one-dimensional elements , Element types can be different , Like the sequence of mathematical elements :S0, S1, … , Sn-1, Elements are guided by sequence numbers , Access specific elements of a sequence through subscripts .

General operators         

Operator and its application describe x in s If x It's a sequence s The elements of , return True, Otherwise return to Falsex not in s If x It's a sequence s The elements of , return False, Otherwise return to Trues + t Connect two sequences s and ts*n or n*s The sequence of s Copy n Time s[i] Indexes , return s No i Elements ,i It's the serial number of the sequence s[i:j] or s[i: j: k] section , Return sequence s pass the civil examinations i To j With k Is the element subsequence of the step size


Sequence functions and methods

        

Functions and methods describe len(s) Return sequence s The length of , That is, the number of elements min(s) Return sequence s The smallest element of ,s Medium elements need to be comparable max(s) Return sequence s The largest element of ,s Medium elements need to be comparable

s.index(x) or

s.index(x, i, j)

Return sequence s from i Start to j The first element in the position x The location of s.count(x) Return sequence s It appears that x The total number of times


Definition of tuple type

         Tuples Is an extension of the sequence type . It's a kind of Sequence type and inherit all general operations of sequence type , Once created Cannot be modified , Use parentheses () or tuple() establish , Between elements Separate with commas . Brackets can be used or not .

A = (123, 456, 789)
B = (0x1100, 321, A)
print(A) # (123, 456, 789)
print(B) # (4352, 321, (123, 456, 789))

Definition of list type

         list yes Sequence type An extension of , Very often , After creation Can be modified at will , Use square brackets “[]” or list() establish , Elements are separated by commas . Each in the list Element types can be different , No length limit .

Functions and methods in the list

        

Functions or methods describe ls[i]= x Replace list ls The first i Element is xls[i: j: k] = lt Use list lt Replace ls Sub list of corresponding elements after slicing del ls[i] Delete list ls pass the civil examinations i Elements del ls[i: j: k] Delete list ls pass the civil examinations i To the first j With k Element for step size ls + = lt Update list ls, Will list lt Element added to list ls in ls *= n Update list ls, Its elements repeat n Time
ls = ["python", "java", "C Language "]
ls[1:2] = ["C++"]
print(ls) # ['python', 'C++', 'C Language ']
print(ls*2) # ['python', 'C++', 'C Language ', 'python', 'C++', 'C Language ']


 

Functions or methods describe ls.append(x) In the list ls Finally, add an element xls.clear() Delete list ls All elements in ls.copy() Generate a new list , assignment ls All elements in ls.insert(i,x) In the list ls Of the i Position add element xls.pop(i) Will list ls pass the civil examinations i The location element takes out and removes the element ls.remove(x) Will list ls The first element that appears in x Delete ls.reverse() Will list ls Reverse the elements in
ls = ["tiger", "dog", "cat"]
ls.append("mouse")
print(ls) # ['tiger', 'dog', 'cat', 'mouse']
ls.insert(4, "fish")
print(ls) # ['tiger', 'dog', 'cat', 'mouse', 'fish']
ls.reverse()
print(ls) # ['fish', 'mouse', 'cat', 'dog', 'tiger']
lt = [] # Define an empty list
lt += [1, 2, 3, 4] # Add four elements
lt[2] = 5 # Mark the subscript as 2 The element of is assigned the value of 5
lt.insert(2, 6) # The subscript for 2 The value inserted before the element is 6 The elements of
del lt[2] # Delete the subscript as 2 The element value of
del lt[1:3] # Delete subscript 1 to 2 The element value of
print(" The current list contains numbers 0") if 0 in lt else print(" The current list does not contain numbers 0") # Whether the current list contains numbers 0
lt.index(4) # Get the subscript 4 The element value of
len(lt) # Current list length
max(lt) # Maximum value of current list
lt.clear() # clear list 


Definition of dictionary type

         Dictionary type yes “ mapping ” The embodiment of , Key value pair : Key is an extension of data index . A dictionary is a collection of key value pairs , Between key value pairs disorder . use Braces and dict() establish , Use colons for key value pairs : Express {< key 1>:< value 1>,< key 2>:< value 2>, ... ,< key n> :< value n>}.

        

d = {1: " Xiao Ming ", 2: " Zhang San ", 3: " Li Si "}
print(d) # {1: ' Xiao Ming ', 2: ' Zhang San ', 3: ' Li Si '}
print(d[1]) # Xiao Ming
print(type(d)) # <class 'dict'>

Functions and methods in the dictionary

        

Functions or methods describe del d[k] Delete Dictionary d In the key k Corresponding data values k in d Judgment key k Is it in the dictionary d in , If you are returning True, otherwise Fald.keys() Return dictionary d All the key information in d.values() Return dictionary d All value information in d.items() Return dictionary d All key value pairs information in


      

d = {1: " Xiao Ming ", 2: " Zhang San ", 3: " Li Si "}
print(1 in d) # True
print(d.keys()) # dict_keys([1, 2, 3])
print(d.values()) # dict_values([' Xiao Ming ', ' Zhang San ', ' Li Si '])
print(d.items()) # dict_items([(1, ' Xiao Ming '), (2, ' Zhang San '), (3, ' Li Si ')])

Functions and methods describe d.get(k,<default>) key k There is , Then return the corresponding value , If not, return to <default> value d.pop(k,<default>) key k There is , Then take out the corresponding value , If not, return to <default> value d.popitem() Random from Dictionary d Take out a key value pair , Returns... As a tuple d.clear() Delete all key value pairs len(d) Return dictionary d The number of elements in

d = {} # Define an empty dictionary
d[" name 1"] = " Zhang San " # Add two key value pair elements
d[" name 2"] = " Li Si "
d[" name 3"] = " Wang Wu "
d[" name 2"] = " Li San " # Change key to “ name 2” The elements of
print(" name 2 The key is in the current dictionary ") if " name 2" in d else print(" name 2 The key is not in the current dictionary ") # Judge “ name 2” Is it in the current dictionary
print(len(d)) # Dictionary length
d.clear() # Empty dictionary 


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