程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Learning notes for Python

編輯:Python

I just got started python, So write a summary that is easy to forget , I hope that's helpful . The editor I use is IDLE.

1. One can check all python Operation of functions in

dir(__builtins__)

Use interactive mode , Then enter this function , You can come out python All functions .

2. When exchanging two numbers , You can use the following code directly

x,y = y,x

The principle of this and x,y,z = 1,2,3 One principle .

3. stay IDLE in , Press Alt+P It means the last sentence , You can return the code of the previous sentence .

4. Escape characters are those that begin with \ Start character , as follows :

5.""" """: This means that , Whatever is written in it will be output as is .

6. The result of adding characters to characters indicates the connection , for example :

'520' + '1314'

The output of is 5201314

7. The result of multiplying a character by a number indicates copying , for example :

' Hey, hey, hey ' * 1000

It's copying 1000 I mean

8. Press ctrl + C You can force the execution of a program to stop .

9.break sentence : The function is to jump out of a layer of circular body .

10.import It means import , If we want to generate random numbers at random , Import required random modular .

import random

11. The function of random number is ( The latter two numbers represent the range of random numbers ):

random.randint(0,100)

  This function can be attacked , in other words , Make its number not random , But reappearance .

x = random.getstate()
random.randint(1,10)
8
random.randint(1,10)
6
random.randint(1,10)
2
random.randint(1,10)
10
random.randint(1,10)
3
random.randint(1,10)
2
random.randint(1,10)
6
random.setstate(x)
random.randint(1,10)
8
random.randint(1,10)
6
random.randint(1,10)
2
random.randint(1,10)
10
random.randint(1,10)
3
random.randint(1,10)
2
random.randint(1,10)
6
random.randint(1,10)
4
random.randint(1,10)
8

Above , That is to say .

12. Numeric type : Integers , Floating point numbers , The plural

Because of the high precision of floating point numbers , You need to use the following function to add floating-point numbers accurately

a = decimal.Decimal('0.1')
b = decimal.Decimal('0.2')
print(a + b)
0.3

It can also be compared :

c = decimal.Decimal('0.3')
a + b == c
True

‘E Notation ’ That is what we usually call scientific counting , Used to represent some extreme numbers . for example :

0.00005 == 5e-05

The plural : We can go through x.real To get the value of its real part , adopt x.imag To get the value of its imaginary part . A complex number is also a floating point number .

Some operations on numbers :

operation result x//yx Divide y Result ( Floor removal )divmod(x,y) return (x//y,x%y)abs(x)x The absolute value of int(x) take x Convert to integer float(x) take x Convert to floating point number complex(re,im) Returns a complex number ,re Is the set of real Numbers ,im It's an imaginary number c.conjugate() return c The conjugate complex of pow(x,y) Calculation x Of y Power x ** y Calculation x Of y Power

What is the floor except ? Is to ensure that the result of dividing two numbers is an integer . If it's not an integer , Just round down ( Take the largest integer smaller than the target result ). for example :

-3 // 2 == -2
3 // 2 == 1

Besides ,pow Supports three parameters , In fact, it is equivalent to the power operation of the first two parameters , Take the remainder of the third number , for example :

pow(2,3,5) == 3

13. Boolean type ( In fact, it is a special integer type )

The result is false The situation of :

(1) Defined as False The object of :None and False

(2) The value is 0 The number type of :0,0.0,0j,Decimal(0),Fraction(0,1)( This means that the molecule is zero , A rational number whose denominator is one )

(3) Empty sequences and sets :'',(),[],{},set(),range(0)


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved