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)
int
float
range
str
bool
list
dict
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‘
运行结果:
‘PythonAwesome‘
(‘Python‘, ‘Awesome‘)
‘Python, Awesome! Awesome! Awesome! ‘
Falsefrom IPython.core.interactiveshell import InteractiveShell
4.
InteractiveShell.ast_node_interactivity = "all"
‘A‘>‘a‘
ord(‘A‘)
ord(‘a‘)运行结果:
a_list=[1,2,3,4,5]
b_list=[1,2,3,5]
c_list=[‘aba‘,‘bob‘, ‘cindy‘, ‘dude‘, ‘eric‘]
a_list>b_list
10 not in a_list
‘aba‘in c_list
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)
[‘a‘, ‘b‘, ‘c‘, ‘d‘]
[‘d‘, ‘c‘, ‘b‘, ‘a‘]
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)
False
True
True
False
False
14.
from IPython.core.interactiveshell import InteractiveShellInteractiveShell.ast_node_interactivity="all"
int(‘3‘)
float(‘3‘)
int(3.14158)
str(‘3.14159‘)33.03‘3.14159‘
if age<18:
print(‘sorry‘)
else:
print(‘enjoy‘)
Please tell me your age :an int number e.g. 22 18
16.
from IPython.core.interactiveshell import InteractiveShellInteractiveShell.ast_node_interactivity="all"
‘He said,it\‘s fine.‘
"He said,it‘s fine"
"He said,it\‘s fine"不能用‘He said,it‘s fine.‘ 编译器会从it后的‘截断
for char in s:
print(s.index(char),char)
0 P 1 y 2 t 3 h 4 o 5 n
InteractiveShell.ast_node_interactivity="all"
s=‘python‘
s[1]
s[2:]
s[2:5]
s[:5]
s[1:5:2]‘y‘‘thon‘‘tho‘‘pytho‘‘yh‘from IPython.core.interactiveshell import InteractiveShell
InteractiveShell.ast_node_interactivity="all"
ord(‘\n‘)
ord(‘\t‘)
ord(‘\r‘)
chr(65)
s=input(‘please follow the number3.14:‘)
int(‘3‘)
float(s)*9
len(s)
print(s*3)10913‘A‘
please follow the number3.14: 3.14
3
28.26
4
InteractiveShell.ast_node_interactivity="all"
s=‘Now is better than ever‘
s.capitalize()
s.title()s.swapcase()
s.title().swapcase()
‘Now is better than ever‘
‘Now Is Better Than Ever‘
‘nOW IS BETTER THAN EVER‘
‘nOW iS bETTER tHAN eVER‘
以上是关于Python的主要内容,如果未能解决你的问题,请参考以下文章