A quick start to the Python language

  As we all know, the weight of python in programming languages ​​is getting larger and larger, so more and more people start learning python. You must know that TensorFlow, machine learning, and data analysis are all based on the python language, but python is actually a Very simple, today I will introduce you to the basic syntax of python.
  First of all, you need to download python2 or 3, and anaconda, which is a very good IDE, which encapsulates various common libraries in python, which is very convenient.
  Let's start with the basic syntax in python. Variables can be used directly in python without definition, such as the following:
message="hello world!"
print(messgae)
The output is as follows:
hello world!
The naming and use of variables can refer to the C language, which is similar to it.
The data types in python include integers, floating-point numbers and strings. The usage of escape characters is similar to that of other languages, but it is more elaborated. Another thing to understand is that Unicode encoding is generally used in computer memory. When it needs to be saved to the hard disk Or it is converted to UTF-8 encoding during transmission, so when writing code in python, you need to pay attention to the conversion of the two encoding methods, otherwise an error may be reported.
The use of format strings is also similar to the C language, such as the following:
'hello,%s'%'world'
output:
hello,world

Python provides a variety of methods to perform some simple operations, such as modifying the case of strings, merging strings, deleting blanks, etc., which are not available in some other programming languages. Examples are as follows:
name="ada love"
print(name.title())
The output is as follows:
Ada Love

The list in python is similar to the array in the c language, the index still starts from 0, you can modify the elements in the list in some ways, which is not available in the c language, for example:
//Use the method sort to sort the list
cars = ['bmw', 'audi', 'toyota']
cars.sort()
print(cars)

// output as follows
['audi', 'bmw', 'toyota']
You can also customize a sorting function, and then use the method sorted ([~], the defined sorting function) to implement custom sorting.
When using -1 as the index, you can directly get the last element in the list.
Add elements to the end of list.append().
Insert the specified position list.insert(1,'~') //Insert the second position
Delete the element at the specified position list.pop(1) //Delete the element at the second position
The slicing function in python means that you can take the elements of the list as you like, see the following example:
list【:10:2】
// Take the first 10 elements of the list, and take one every two
List comprehensions are simple but useful, like this:
【x*x for x in range(1,11)】
//[1,4,9,16,25,36,49,64,81,100]
When you only want to get some elements of the list but the list is very large, you can turn the generated list into a generator, so that when you use an element, the generator will generate it, saving a lot of space , the elements in the print generator can be printed with the next method or directly with the for loop.
The for loop in python can traverse the entire list to perform some more complex operations. Here is a reminder that python is very strict with indentation, so you must always check your indentation format. In python, you can use the function range() to easily generate a series of numbers, and there are some functions for generating random numbers. If necessary, you can check it online.
Similar to lists, tuples in python also store information, but the values ​​in tuples cannot be modified. The use of the if statement and the while loop in python is also the same as the c language.
Dictionaries in python can associate related information, so that various real-world objects can be modeled. //It should be noted here that the key in the dictionary must be an immutable object
The powerful built-in function library in python can meet any of your needs. You can use help (function name) in the interactive command line to view the usage of the function. You can also assign the function name to a variable, and then you can pass the variable. Just call the function directly.
Let's talk about the parameters of the function: required parameters, default parameters, variable parameters, keyword parameters.
1. Required parameters are parameters that you must assign
2. Default parameters, you can directly assign a value to a parameter when the function is defined, and it will appear as a default value in the subsequent calling process. Reasonable use of default parameters can simplify function calls.
3. Variable parameters, add a * in front of the parameter and it becomes a variable parameter, which means that you can send several actual parameters if you want to send several actual parameters, which is undoubtedly very convenient.
4. Keyword parameters, add two * in front of the parameter and it becomes a keyword parameter, which means that you can pass in any number of parameters with parameter names, and they will be automatically assembled into a dictionary when called.
Let's learn how to define some simple functions:
def greet():
    print("hello!")

greet()

// output as follows
hello
The general structure is def + function name + parentheses, and then the function body part. If you want to define a function with parameters, you can add the corresponding parameters in the parentheses.
The following will introduce the simple concept of functional programming, and introduce two commonly used functions map, reduce and filter.
Functional programming means that functions dominate the program, and its abstract program is higher and closer to mathematical calculations. In functional programming, one function can accept another function as a parameter. The two functions described below are examples of this. The map() function accepts two parameters, one is a function, and the other is a sequence, which can be a list or a tuple, etc. When you call it, the map function will act as a parameter on the elements in the sequence in turn and Return a new sequence. Similar to the map function, the parameters accepted by the reduce function are similar to those of the map function, but the parameters of its parameter function must be two. The function is called again with arguments, always acting on the last element. The filter function also accepts a function and a sequence. The function acts on each element in the sequence, and retains or deletes the element according to the positive or negative value of the return value. It can be seen that the functions in python are more vivid, the meaning of filter is filtering.
python is an object oriented programming language so it can create and use classes to simulate some things in life. You can import certain modules to use classes or functions in the module, so it is very convenient. In the future, I will use an article to analyze the code of the game Snake to deepen my understanding of python.


I am a novice, please give me more advice if I am not good at writing. There will be other learning articles in the future. Welcome to exchange, and the articles will be continuously updated.



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325161896&siteId=291194637