[More • algorithm • May 27 th] basis python

Preface ▎

  See this topic, you will be very curious as to why you want to learn to play NOIP learn python? In fact, python for us is very useful!

  NOIP although does not support the use of python submit code, but in NOILinux naturally comes with python. python is a high-level language than C ++, the only drawback is slow! But we do not have to write python code but to use its own packaged function much better implement some C ++ code to do.

  Such as playing table, to shoot, calculations, etc., can also be used to help you write code, then ado, direct you quickly get started with python!

▎ Calculator

"expression"

  In python, as long as the direct input an expression, they will tell you the results.

1 >>> 1+1
2 2

  Well, this is too simple, let's make a mix operations:

1 >>> (2+16)*3/2
2 27.0

  While it is counting on, but it can be noted: when there is a floating-point result of the division.

  Of course, such as a modulo calculation or the like are also possible.

"divisible"

  So how in C ++ "/" mean? use"//".

1 >>> 16//3
2 5

"power"

  Two "*" indicates power:

1 >>> 2**3
2 8

"precision"

  python comes naturally with high accuracy.

1 >>> 192608171145142333333333333333333323424213545324654654765765777776575676*4213423575686765974537687674568567
2 811539809172854192357590409566235439234427211936317976434431655370121871259439787490543441886511926376292

 

▎ variable

"define"

  Python variables not defined as a type of hidden data, automatically determine the data type, so long as the assignment can.

1 >>> a=1
2 >>> a
3 1

 

1 >>> a="python"
2 >>> a
3 'python'

  By the way, python string can be single quotes, double quotes can be.

1 >>> a=3.141592653
2 >>> a
3 3.141592653

 

1 >>> a=111
2 >>> b=6
3 >>> a*b
4 666

"use"

  As mentioned above, the direct variable like C ++ with it.

▎ input and output

"Input"

  Use input Input:

1 >>> a=input()
2 2
3 >>> a
4 '2'

  Since the default is a string, it returns a string, if you want to replace the type, then we cast:

1 >>> a=int(input())
2 2
3 >>> a
4 2

"output"

  The output is not the same as less a "f":

1 >>> print("Hello,world!")
2 Hello,world!

▎ list

  The list can be understood as an array, but easy to use than the array, and more advanced, inherently vector.

"define"

  A little bit different, is enclosed in square brackets, but do not write data type and size, directly on it:

1 >>> list=[1,1,4,5,1,4]
2 >>> list
3 [1, 1, 4, 5, 1, 4]

  You can also use different types of elements on the same list.

1 >>> list=[1,2,3.14,"hello"]
2 >>> list
3 [1, 2, 3.14, 'hello']

"reference"

  And an array of exactly the same.

1 >>> list=[1,2,3.14,"hello"]
2 >>> list
3 [1, 2, 3.14, 'hello']
4 >>> list[1]
5 2

  Also numbered from 0.

  Of course, the index can be negative, this is the first of several backwards meaning of numbers.

1 >>> list=[1,2,3,4,5]
2 >>> list[-1]
3 5

"interval"

  We can use ":" to take the middle period of the interval.

1 >>> list=[1,2,3,4,5]
2 >>> list[1:3]
3 [2, 3]

  We want output should be 2, 3, but why only output 2,3 it? This is because the left and right to open and close, that is to say only show interval [l, r-1].

"Other"

  Support is added to the end of the list.

1 >>> list1=[1,2,3]
2 >>> list2=[4,5,6]
3 >>> list1+=list2
4 >>> list1
5 [1, 2, 3, 4, 5, 6]

  May also skip some number :( colon can be added more, wherein the subscript is the number of two is omitted)

1 >>> list1=[1,2,3,4,5,6]
2 >>> list1[1:3:2]
3 [2]

 ▎if

  if C ++ and python are very similar, but python no parentheses, to add a colon and a space to perform in strict accordance with (4 spaces)

1 >>> a=1
2 >>> if a==1:
3 ...     print("YES")
4 ...
5 YES

  Let's look at elif (else if):

1 >>> a=1
2 >>> b=2
3 >>> if a==2:
4 ...     print(a)
5 ... elif b==2:
6 ...     print(b)
7 ...
8 2

 ▎for

  python bit different, there is no end condition.

>>> list=[1,2,3,4,5]
>>> for i in list:
...     print(list[i])
...
2
3
4
5

  I can see is the default starting with 1, then we try to start from 0:

1 >>> list=[1,2,3,4,5]
2 >>> for i in range(0,5,1):
3 ...     print(list[i])
4 ...
5 1
6 2
7 3
8 4
9 5

  Which range is a good thing, one of the three numbers are the initial value, end value, and each time the number increased.

  Note: python left and right to open and close, so the interval to become [0,5].

 ▎ end language

  Of course, python command, there are many, we recommend to novice tutorial on learning.

 

Guess you like

Origin www.cnblogs.com/TFLS-gzr/p/11260240.html