preface
Recent work and research involved in data mining and machine learning, for the purpose of inducing and summarizing knowledge to write down a series of articles, this series will include basic Python data types and data structure, function and object-oriented related knowledge, and then introduces data mining and machine learning is often used Numpy, Pandas. Hopefully, this series of articles will help anyone new to Python or data mining and machine learning.
Basic data types
digital
Python treats all numbers with a decimal point as floating point numbers, and the basic operations of addition, subtraction, multiplication and division are no different from those of other languages.
string
Python, like most languages, declares strings with “”.
poet = "We are all in the gutter, but some of us are looking for stars"
print(poet);
Copy the code
Strings are whitespace sensitive and do not actively remove whitespace. We can remove strings on the left and both ends of a string by lstrip and strip.
name = " Python"
name1 = " Python "
print(name.lstrip())
print(name1.strip())
Copy the code
Python also concatenates strings with +
name = "hello"+" Python"
print(name.lstrip())
Copy the code
Note that Python differs from other languages such as Java in that Python does not actively convert other types to string types when concatenating strings
age = 23
message = "Happy" + age + "rd Birthday"
print(message.lstrip())
# error
TypeError: can only concatenate str (not "int") to str Traceback (most recent call last): File "D:/Program Files/project/hello.py", line 3, in <module> message = "Happy" + 23 + "rd Birthday" TypeError: can only concatenate str (not "int") to str Copy the code
Must be changed to
age = 23
message = "Happy" + str(age) + "rd Birthday"
print(message.lstrip())
Copy the code
The list of
A list of basic
A list is a series of elements arranged in a particular order. A list can be declared by [].
names = ["Python".'Java'.'C++']
Copy the code
Python lists access and modify elements similar to data in some languages, elements are accessed and modified by subscripts starting at 0.
names[0] = 'go'
Copy the code
You can use appEnd to add elements to the end of the list
names.append('C')
Copy the code
Insert to insert an element at a specific location
names.insert(0, 'Ruby')
Copy the code
There are several ways to remove elements from a list. You can use del to remove elements.
del names[0]
Copy the code
You can also use pop to delete elements
names.pop(0)
Copy the code
Remove removes an element by value. Note that remove removes only one value. If there are multiple values in the list, you need to call remove multiple times.
names.remove('Java')
Copy the code
Sorting of lists: You can use sort to permanently sort the list, or you can sort backwards by passing the parameter Reverse = True.
nums = [1, 9, 7, 5, 4, 3, 2]
nums.sort(reverse=True)
Copy the code
Sort changes the list permanently, and you can use sorted if you just want to sort it temporarily.
nums = [1, 9, 7, 5, 4, 3, 2]
print(sorted(nums))
Copy the code
You can use reverse to reverse the list
nums = [1, 9, 7, 5, 4, 3, 2]
nums.reverse()
print(nums)
Copy the code
Len can be used to get the length of the list
len(nums)
Copy the code
To avoid overstepping bounds when accessing the list, Python represents the last element as -1, the second-to-last element as -2, and so on
nums[-1]
Copy the code
The list of operations
We can use the for in statement to iterate over a list. Note that Python uses the: and indentation instead of the {} commonly used in other languages to represent blocks, which is also a Pyhthon feature.
names = ["Python".'Java'.'C++'.'go'.'Ruby']
for item in names:
print(item)
Copy the code
We can also use the range function to generate a list of numbers, range(1, 5) returns 1, 2, 3, 4, and range(1, 5, 2) can set the step size, range(1, 5, 2) returns 1, 3.
for i in range(1, 5, 2):
print(i)
Copy the code
You can construct a list of numbers using list plus range. You can use min, Max, and sum to find the minimum, maximum, and sum of a list of numbers.
nums = list(range(1, 5))
print(max(nums))
print(min(nums))
print(sum(nums))
Copy the code
We can also create lists through list parsing, as follows, where ** in Python stands for power operation.
squares = [value**2 for value in range(1, 5)]
Copy the code
Is equivalent to
squares = []
for value in range(1, 5):
squares.append(value**2)
Copy the code
Python can get a portion of a list by slicing.
names = ["Python".'Java'.'C++'.'go'.'Ruby']
print(names[1:3])
# return ['Java', 'C++']
Copy the code
[1:3] is a bit like the previous mathematical interval, the right side is the open interval, [1:3] is [1,3] is 1,2.
tuples
A tuple is a special list that cannot be changed after it is declared and can be declared by ().
names=('java'.'python')
Copy the code
The dictionary
Lists are ordered and elements are accessed by position, whereas dictionaries are unordered and elements are stored and accessed by key-value pairs. Dictionaries can be declared with {}.
D = {}
D = {'egg': 1, 'ham'2} :Copy the code
Dictionaries can be nested
D ={'food': {'egg': 1, 'ham': 2}}
Copy the code
Dictionaries can retrieve keys, values, and key + values by keys, values, and items.
D = {'egg': 1, 'ham'2} :print(D.keys())
print(D.values())
print(D.items())
Copy the code
Get to get the value of a key by key.
D = {'egg': 1, 'ham'2} :print(D.get('egg'))
Copy the code
Add or change dictionaries by keys
D = {'egg': 1, 'ham'2} :D['egg'] = 2
print(D)
Copy the code
Delete elements by del and POP
D = {'egg': 1, 'ham'2} :del D['egg']
D.pop('ham')
Copy the code
Merge two dictionaries via update
D1 = {'bread': 3}
D = {'egg': 1, 'ham'2} :D.update(D1)
print(D)
Copy the code
Get the dictionary length by len
D = {'egg': 1, 'ham'2} :print(len(D))
Copy the code
The last
This is a very simple introduction to Python data structures, but it’s best to type them yourself. More articles can be found on QStack.
This article is formatted using MDNICE