码迷,mamicode.com
首页 > 编程语言 > 详细

Python

时间:2019-09-14 18:34:02      阅读:117      评论:0      收藏:0      [点我收藏+]

标签:fun   text   rev   def   error:   prime   ace   span   most   

函数

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

标签:fun   text   rev   def   error:   prime   ace   span   most   

原文地址:https://www.cnblogs.com/wwqdata/p/11519737.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!