Python

Posted wwqdata

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python相关的知识,希望对你有一定的参考价值。

函数

https://docs.python.org/3/library/functions.html

编程实例

1.打印质数

for n in range (2,100):
    if n==2:
          print(n)
          continue
for i in range (2, n):
    if (n %i) ==0:
          break
    else:
          print(n)

注意对齐

打印质数改良版

def is_prime(n):
      if(n<2):
            return False
      if(n==2):
            return True
      for m in range (2, int(n**0.5)+1):
            if(n%m)==0:
                return False
            else:
                return True
for i in range(80,110):
      if is_prime(i):
           print(i)

2.基本概念

1.from IPython.core.interactiveshell import InteractiveShell
InteractiveShell.ast_node_interactivity = "all"
11+10-9/8//7%5
‘3.14‘+3

运行结果

21.0

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-12-d1e427b7cc92> in <module>
      2 InteractiveShell.ast_node_interactivity = "all"
      3 11+10-9/8//7%5
----> 4 ‘3.14‘+3

TypeError: can only concatenate str (not "int") to str

 

2.from IPython.core.interactiveshell import InteractiveShell
InteractiveShell.ast_node_interactivity = "all"
type(3)
type(3.0)
type(range(10))
type(‘3.14‘)
type(True)
type([1,2,3])
type(‘a‘:1,‘b‘:2,‘c‘:3)

type((1,2,3))
type(1,2,3)

tuple
set

 

3.from IPython.core.interactiveshell import InteractiveShell
InteractiveShell.ast_node_interactivity = "all"
‘Python‘+‘Awesome‘
‘Python‘,‘Awesome‘
‘Python, ‘+‘Awesome! ‘ * 3
‘o‘ in ‘Awesome‘ and ‘o‘ not in ‘Python‘


6.print(‘Hello‘,‘Jack‘,‘Mike‘,‘...‘,‘and all you guys‘)
Hello Jack Mike ... and all you guys

7.

name=‘ANN‘
age=‘22‘
print(f‘name is age years old.‘)

ANN is 22 years old.


8.

import sys
print(‘Hello‘,‘World‘)
print(‘Hello‘,‘World‘,sep=‘ ‘,end=‘\n‘,file=sys.stdout,flush=False)
print(‘Hello‘,‘World‘,sep=‘-‘,end=‘\t‘)
print(‘Hello‘,‘World‘,sep=‘~‘)
print(‘Hello‘,‘World‘,sep=‘\n‘)

Hello World
Hello World
Hello-World	Hello~World
Hello
World


9.print(print(1))

1
None


10.

from IPython.core.interactiveshell import InteractiveShell
InteractiveShell.ast_node_interactivity="all"
sorted(‘abcd‘)
sorted(‘abcd‘,reverse=True)



11.

from IPython.core.interactiveshell import InteractiveShell
InteractiveShell.ast_node_interactivity="all"
divmod(11,3)
a,b=divmod(11,3)
a
b

(3,2)

3

2

 

 

12.

from IPython.core.interactiveshell import InteractiveShell
InteractiveShell.ast_node_interactivity="all"
pow(2,3)
pow(2,3,4)

8

0

//pow(2,3,4):2^3%4

 

 

13.

from IPython.core.interactiveshell import InteractiveShell
InteractiveShell.ast_node_interactivity="all"
bool()
bool(3.14159)
bool(-3.14159)
bool(1==2)
bool(None)

以上是关于Python的主要内容,如果未能解决你的问题,请参考以下文章

PythonPython库之机器学习

PythonPython库之机器学习

PythonPython库之游戏开发

PythonPython库之游戏开发

PythonPython XML 读写

PythonPython库之Web信息提取