Artificial Intelligence Lesson --------- Python3 basis (a)

When any new language for beginners to learn a new language, they are inseparable from the input and output; input and output since taken this step to solve the psychological fear of difficulty of the language. Come, let us first print out:

print("hello world")
print("{}".format("hello world"))

input Output

Entry

I just introduced me to the only doctor in my teacher learned that he only introduced this kind, he taught us that both knowledge and practice, just as the phrase poem written by Lu

Paper come Zhongjue know this practice is essential

Here Insert Picture Description

Legacy output

Old school output means nothing, look just fine, in fact, with Java, c / c ++% plus almost use a special character as a placeholder.

%d  # 十进制
%o  # 八进制
%x  # 十六进制

print("%d"%23)  # 23
print("%o"%23)  # 27  
print("%x"%23)  # 17

print("%f"%2.3333)     #2.333300
print("%.2f"%2.8888)   #2.89 四舍五入
print("%e"%2.3333)     #2.333300e+00
print("%.3e"%2.3333)   #2.333e+00
print("%g"%2222.3333)  #2222.33
print("%g"%22888822.3333) #2.28888e+07
print("%.7g"%2222.8888) #2222.889 .7是有效数字的个数
print("%.3g"%2222.3333) #2.22e+03


print("%s" % "hello everyone")    # hello everyone
print("%65s" % "hello everyone")  # 右对齐,左侧空格补位
print("%-65s" % "hello everyone") #左对齐,右侧空格补位
print("%.5s" % "hello everyone")  #取前5个字符
print("%10.4s" % "hello everyone") #10位占位符,取4个字符右对齐      hell
print("%-10.4s" % "hello everyone") # hell


format the way, I would rather recommend

Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
On the way bald farther and farther.
slogan: Paper come Zhongjue know this practice is essential.

Python3 list of basic data types

Although I've learned in the last year, the python3, and brush it with more than 100 channels ACM problems, but because they do not often use somewhat forgotten, we just Majors artificial intelligence course, it is premised on the knowledge that python, can be used as a review just before the knowledge learned. So I try to do each on a big lesson, she wrote in a blog summary.

What is the slice (pre-knowledge)

Slice (Slice) is a clipping technique index fragments, by slicing technology, we can be processed very flexible type sequence. Generally speaking, the role is to intercept slice sequence object, however, for non-target sequence, is there a way we do slicing it? In the process of using the slice, what points are worth attention, what is the underlying principle of concern it? This article will discuss these with you together content, I hope you can learn together and progress.
Here Insert Picture Description

Create a list

When you create a [], subject to the following manner, starting from 0
Be careful not to use the list as a list of names, because list () to create an empty list

l1 = []
l2 = list()

The combined list

the append (): added element
extend (): append the list based on the original list
+: two lists into a new list
+ = effect and extend () as
an asterisk: repeat list

stu1 = ['Tom','Jack','Peter']
stu1.append('Smith')
print(stu1)
stu2 = ['Rose','Jhon','Mary']
stu1.extend(stu2)
print(stu1)
print(stu2)
List_stu = stu1 + stu2
print(List_stu)
L = List_stu*2#重复2次
print(L)

以下为控制台输出
['Tom', 'Jack', 'Peter', 'Smith']
['Tom', 'Jack', 'Peter', 'Smith', 'Rose', 'Jhon', 'Mary']
['Rose', 'Jhon', 'Mary']
['Tom', 'Jack', 'Peter', 'Smith', 'Rose', 'Jhon', 'Mary', 'Rose', 'Jhon', 'Mary']
['Tom', 'Jack', 'Peter', 'Smith', 'Rose', 'Jhon', 'Mary', 'Rose', 'Jhon', 'Mary', 'Tom', 'Jack', 'Peter', 'Smith', 'Rose', 'Jhon', 'Mary', 'Rose', 'Jhon', 'Mary']

Delete list elements

You can use the del statement to delete the list of elements.
remove () function is used to remove the first occurrence of a value in the list.
Here Insert Picture Description

Interception list

It uses knowledge of the slice, see pictures, knock yourself again is the best way to learn, to the Austrian ~!
Here Insert Picture Description

Python list of functions and methods of operation

List of operations includes the following functions:

  1. cmp (list1, list2): compare elements of two lists; python3 has removed this function, please see the last of knowledge Tips
  2. len (list): a list of number of elements;
  3. max (list): returns a list of the maximum element;
  4. min (list): returns a list of the minimum element;
  5. list (seq): Convert a tuple into a list.

List of operations includes the following methods:

  1. list.append (obj): add new objects in the end of the list;
  2. list.count (obj): count the number of an element that appears in the list;
  3. list.extend (seq): a plurality of one-time value is added at the end of the other sequence list (list with a new original extended list);
  4. list.index (obj): the index to find the location of the first occurrence of a value from the list;
  5. list.insert (index, obj): Insert a list of objects;
  6. list.pop (obj = list [-1]): the value of a removal element of the list (the default last element), and returns the element;
  7. list.remove (obj): remove the list a value of the first match;
  8. list.reverse (): Reverse elements in the list;
  9. list.sort ([func]): the original list is sorted.
    Here Insert Picture Description

Create a list of two-dimensional

Here Insert Picture Description

Knowledge Tips

In Python3 has no cmp () function, if you need to achieve more functionality, the need to introduce operator module, suitable for any object contains methods are:

  • lt (a, b) is equivalent to a <b or a letter from the first digit (ASCII) than the size
  • le (a, b) is equivalent to a <= b
  • eq (a, b) corresponds to exactly the same letters a == b, returns True,
  • ne (a, b) is equivalent to a! = b
  • gt (a, b) corresponds to a> b
  • ge (a, b) corresponds to a> = b
    Here Insert Picture Description
Published 25 original articles · won praise 11 · views 1914

Guess you like

Origin blog.csdn.net/weixin_44350891/article/details/104780453