Python 官方文档:入门教程 => 点击学习
python>>> s='tou siheoiw' >>> '%s is number %d' % (s[:6],1) 'tou si is number 1'>>> hi='''hi
>>> s='tou siheoiw'
>>> '%s is number %d' % (s[:6],1)
'tou si is number 1'
>>> hi='''hi
there'''
>>> hi
'hi\nthere'
>>> book ={'title':'Python WEB Development','year':2008}
>>> book
{'year': 2008, 'title': 'Python Web Development'}
>>> 'year' in book
True
>>> 'pub' in book
False
setdefault和get一样,dict.get(key)或是dict[key]
>>> d ={'title':'Python Web Development','year':2008}
>>> d
{'year': 2008, 'title': 'Python Web Development'}
>>> d.setdefault('pub','Addison Wesley')
'Addison Wesley'
>>> d
{'year': 2008, 'pub': 'Addison Wesley', 'title': 'Python Web Development'}
>>> del d['pub']
>>> d
{'year': 2008, 'title': 'Python Web Development'}
>>> d['title']
'Python Web Development'
>>> len(d)
2
while循环:
>>> while i<5:
... print i
... i +=1
...
...
0
1
2
3
4
建一个文本
#!/bin/bash
#4.4.sh
i=$[ $1 % 2]
if test $i -eq 0 ; then
echo oushu
else
echo jishu
fi
~
>>> for line in open('4.sh'):
... if 'jishu' in line:
... print line
...
echo jishu
>>> for line in open('4.sh'):
... print line
...
#!/bin/bash
#4.4.sh
i=$[ $1 % 2]
if test $i -eq 0 ; then
echo oushu
else
echo jishu
fi
enumerate是一个能让你同时迭代和计数的内置函数
>>> data =(123,'abc',3.14)
>>> for i, value in enumerate(data):
... print i,value
...
0 123
1 abc
2 3.14
简单的计算
#!/usr/bin/python
#filename:expression.py
length=5
breadth=2
area=length*breadth
print 'Area is',area
print 'Perimeter is',2*(length+breadth)
"expression.py" [New] 7L, 142C written
# python expression.py
Area is 10
Perimeter is 14
输入转化为×××:int(raw_input(''))
#!/usr/bin/python
#Filename:if.py
number=23
guess=int(raw_input('Enter an integer:'))
if guess==number:
print 'Congratulations,u guessed it.'
print "(but u do not w in any prizes!)"
elif guess< number:
print 'No ,it is a little higher than that'
else:
print 'No,it is a little lower than that'
~
"if.py" [New] 12L, 311C written
# python if.py
Enter an integer:78
No,it is a little lower than that
continue:
有关计算字符串长度len(s)
#!/usr/bin/python
#Filename:continue.py
while True:
s=raw_input('Enter something:')
if s=='quit':
break
if len(s)<3:
continue
print 'Input is of sufficient length'
~
~
"continue.py" 9L, 196C written
# python continue.py
Enter something:77
Enter something:e
Enter something:3
Enter something:eee
Input is of sufficient length
Enter something:
Enter something:quit
#
定义函数
def 函数名():
函数体
函数名()看见没那么快,
#!/usr/bin/python
#filename:func_param.py
def printMax(a,b):
if a>b:
print a,'is maximum'
else:
print b,'is maximum'
printMax(3,4)
~
"func_param.py" [New] 9L, 156C written
# python func_param.py
4 is maximum
局部变量:
#!/usr/bin/python
#filename:func_local.py
def func(x):
print 'x is',x
x=2
print 'Changed localx to',x
x=50
func(x)
print 'x is still',x
~
"func_local.py" 10L, 152C written
# python func_local.py
x is 50
Changed localx to 2
x is still 50
全局变量:
#!/usr/bin/python
#Filename:func_global.py
def func():
global x
print 'x is',x
x=2
print 'Changed local x to',x
x=50
func()
print 'Value of x is',x
~
"func_global.py" [New] 10L, 164C written
# python func_global.py
x is 50
Changed local x to 2
Value of x is 2
定义函数的默认参数:
#!/usr/bin/python
#Filename:func_default.py
def say(message,times=1):
print message*times
say('Hello')
say('World',5)
~
"func_default.py" [New] 6L, 122C written
# python func_default.py
Hello
WorldWorldWorldWorldWorld
关键参数:
#!/usr/bin/python
#filename:func_key.py
def func(a,b=5,c=10):
print 'a is',a,'and b is',b,'and c is',c
func(3,7)
func(25,c=24)
func(c=50,a=100)
~
"func_key.py" [New] 8L, 149C written
# python func_key.py
a is 3 and b is 7 and c is 10
a is 25 and b is 5 and c is 24
a is 100 and b is 5 and c is 50
return语句
#!/usr/bin/python
def returnn(a,b):
if a>b:
return a
else:
return b
print returnn(2,4)
~
~
"return.py" 7L, 111C written
# python return.py
4
def printMax(x,y):
#!/usr/bin/python
def printMax(x,y):
x=int(x)#convert to integers,if possible
y=int(y)
if x>y:
print x,'is maximum'
else:
print y,'is maximum'
printMax(3,5)
print printMax.__doc__
~
"func_doc.py" 11L, 214C written
# python func_doc.py
5 is maximum
None
sys模块:
模块是包含了你定义的所有的函数和变量的文件
#!/usr/bin/python
#Filename:using_sys.py
import sys
print 'The com m and line arguments are:'
for i in sys.argv:
print i
print '\n',sys.path,'\n'
~
# python using_sys.py we are arguments
The com m and line arguments are:
using_sys.py
we
are
arguments
['/root',
'/usr/lib64/python26.zip', '/usr/lib64/python2.6',
'/usr/lib64/python2.6/plat-linux2', '/usr/lib64/python2.6/lib-tk',
'/usr/lib64/python2.6/lib-old', '/usr/lib64/python2.6/lib-dynload',
'/usr/lib64/python2.6/site-packages',
'/usr/lib/python2.6/site-packages']
其中:using_sys.py 是sys.argv[0]
we 是 sys.argv[1]
are 是sys.argv[2]
arguments是sys.argv[3]
字节编译的.pyc文件
模块的__name__
#!/usr/bin/python
#filename
if __name__=='__main__':
print 'This program is being run by itself'
else:
print 'I am being imported from another module'
~
"using_name.py" [New] 7L, 161C written
[root@10-8-11-204 ~]# python using_name.py
This program is being run by itself
[root@10-8-11-204 ~]# python
Python 2.6.6 (r266:84292, Nov 22 2013, 12:16:22)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-4)] on linux2
Type "help", "copyright", "credits" or "license" for more infORMation.
>>> import using_name
I am being imported from another module
创建字节的模块
#!/usr/bin/python
import mymodule
mymodule.sayhi()
print 'Version',mymodule.version
~
"mymodule_demo.py" [New] 4L, 84C written
[root@ ~]# python mymodule_demo.py
Hi,this is mymodule speaking
Version 0.1
from...import
from mymodule import sayhi,version
(sayhi,version是mymodule的方法和变量)
#!/usr/bin/python
#filename
from mymodule import sayhi,version
sayhi()
print 'Version',version
~
"mymodule_demo2.py" [New] 5L, 95C written
[root@ ~]# python mymodule_demo2.py
Hi,this is mymodule speaking
Version 0.1
dir()函数
[root@ ~]# python
Python 2.6.6 (r266:84292, Nov 22 2013, 12:16:22)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> dir(sys)
['__displayhook__', '__doc__', '__excepthook__', '__name__', '__package__', '__stderr__', '__stdin__', '__stdout__', '_clear_type_cache', '_current_frames', '_getframe', 'api_version', 'argv', 'builtin_module_names', 'byteorder', 'call_tracing', 'callstats', 'copyright', 'displayhook', 'dont_write_bytecode', 'exc_clear', 'exc_info', 'exc_type', 'excepthook', 'exec_prefix', 'executable', 'exit', 'flags', 'float_info', 'getcheckinterval', 'getdefaultencoding', 'getdlopenflags', 'getfilesystemencoding', 'getprofile', 'getrecursionlimit', 'getrefcount', 'getsizeof', 'gettrace', 'hexversion', 'maxint', 'maxsize', 'maxunicode', 'meta_path', 'modules', 'path', 'path_hooks', 'path_importer_cache', 'platform', 'prefix', 'ps1', 'ps2', 'py3kwarning', 'setcheckinterval', 'setdlopenflags', 'setprofile', 'setrecursionlimit', 'settrace', 'stderr', 'stdin', 'stdout', 'subversion', 'version', 'version_info', 'warnoptions']
。
。
。未完待续
--结束END--
本文标题: Python开发(一)
本文链接: https://lsjlt.com/news/185221.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0