Why do I have to learn the language of Python? In fact, it is very simple, I want to expand the technical side at the same time, try more direction, it may end up, who knows? If possible, I will go to the crawler and data analysis in this direction. So I started my python learning journey. Of course, the premise is that my job in Java is going forward.
Because there is the basis of Java, so getting started Python is relatively simple, but I I do not want to quickie, so, will deliberately slow down a bit. When it comes to the installation of the Python development environment and the use of the tools, I choose the Python3.6, and the current tool, because in the early days, so chose the python comes with IDLE
, then to summarize my study of these days! By the way, the tutorial I chose is Python基础教程3
. You need to work during the day, so it's time to take a look at the book when you get off work.
Like 8 basic data types in Java, there are some basic data types in Python, and today it's about what the basic data structures are in Python.
- List
- Meta-group
- String
- Dictionary
The first 3 types collectively referred to as the sequence, why forcibly put the top 3 categories together? Because they have a common operation. which includes 索引,切片,相加,相乘和成员检查(即 in 操作符)
.
An analysis, an index in Python 从 0 开始递增
, and can be used -1 定位到最后一个元素,依次向左递减
. Slicing is very advanced, that is, you can access the values in the fixed index range, for example s = 'hello' s[1:3] = 'el'
, note that the index value is not included in the previous. This is common in Python. Slices There are many comparison of the operation of the show, such as, s[-3:-1] = 'll' (注意刚刚说过的含前不含后)
s[:] = 'hello' s[1:] = 'ello' s[:3] = 'hel'
, these more commonly used operations bar, there are slices to support the specified step size, the default step is 1 s[::2] = 'hlo'
the rest I do not say, I can not remember the good! It's good to have an impression when you learn. The addition of sequences is an example: [1,2,3]+[6] = [1,2,3,6];
multiplication is similar, the 'h'*3 = 'hhh'
membership is the value is not used in the sequence in
to judge. For example, s = ['A','B','C']; 'A' in s = True
just a simple description of the operation of the Python sequence. There are also some common methods for finding the length, maximum, and minimum values that len() max() min()
should be used to understand that there should be no problem. Once the sequence is done, let's take a look at how each is used.
First of all, what is a list, the list is to use the data around [ ]
, this is the list, and the tuple is the use of data around ( )
, the string used is ' ' or " "
such a summary, it feels a lot more refreshing. To ask why the use of these symbols, people are defined, you can drop! Specific use I do not want to say too much, or a kind of feeling in the transcription ah! You know, the list has a lot of built-in methods can be used, remember it's OK, you can use the command to dir(list)
get all the methods in the list, know that the method does not use the method Ah, there is a command is help(list.methodName)
so that can be explained. However, this is English, or do not understand, then Baidu go. Even if I say something now, I believe that if I don't use it, I will forget it. So I'm not going to say much. To keep in mind, there are many and many ways that you may have thought you might have already existed!
Tuples and strings are immutable, and tuples and lists are very similar, lists are mutable, tuples are immutable, this is the difference, and tuples are used especially rarely, but there are still useful, not seen at the moment, but also need to practice cultivation. This is a tuple. (1,2,3)
note that tuples use parentheses, separated by commas, but one thing to note is that if the tuple has only one element, be sure to add a comma, like this, 1, or (1,)
or else who knows if you are expressing the number 1 or tuple (1,)?
String we should be familiar with, say a bit, about the string format problem, we can use %s
to set the format, like this so name = 'David'; print('Hello , %s' %name )
that the name of the string can be filled into the string to be filled. %s
called the conversion specifier, I understand that the pit is occupied. There is also the use of the Format method to '{0},{2} and {1}'.format('frist','second','third') 结果: first,third and second
Note that this is a premeditated pit, can specify the location, of course, can not be specified, that is, in order. String method is more outrageous to the point, it's OK, then a little bit of use, I write the method name you come to guess it center find join lower upper split title strip lstrip rstrip translate isspace isdigit isupper ...
, you look at it!
All of the data types described above can be manipulated using an index, but it is always necessary to describe the mapping relationship with a data type, so the dictionary is born! That's what we often call Key-value key-value pairs. What does the dictionary say? So phonebook = {'David':'1234','Lucy':'9876'}
you see, is a curly brace + colon + comma combination to create a dictionary, this symbol is a lot of Ah, recall the list with, [ ]
tuple used, ( )
string with ' ' or " "
, the dictionary { }
so say to be good at summing ah, so remember to prison ah. The dictionary has a class called dict
, why use, is to create a dictionary, similar list is the list
tuple is tuple
. The comparison, always remember to live, understand clearly. The dictionary also has some basic operations, but also to look at the method name when it is also important len(dict) || dict[key] || dict[key] = value || del dict[key] || key in dict || dict-表示一个字典类型的变量,key-字典的键,value-为键赋的值
to note that the dictionary key value must be immutable. This has been said in the Java finishing section ~
Also have to introduce a dictionary of related methods, really annoying, do not want to write. Just a little bit easier dir(dict)
, the method names can be seen. The use of the method help(dict.methodName)
is also done. It is useful to use a dictionary to format a string, say it alone, using a method to populate a string with values from a dictionary, and format_map(dict)
feel very powerful, for example:
data = {'a':'I','b': ' You'}
' {A} love {b}"I Love You
That's it, the disguise of the confession Ah.
To summarize, just understand the basic data types and basic usage of Python, and then you can do simple logic programming after you have learned the statements later.