您的位置:首页 > 编程语言 > Python开发

python基础语法(1)

2016-07-28 13:50 281 查看

注释

单行注释:#

多行注释:(单引号,双引号在python中区别不大)

#~!@#!@#$(真正的注释)

""" (虽然注释了,但是可以被当做字符串使用)
~!@#!@#$
"""

'''
~!@#!@#$
'''


for 和 while循环

后跟else:

表示循环正常结束后,执行else后跟的东西

for i in range(100):
if i==50:
print "I have got to 50!"
continue
elif i>70:
break;
print i
else :
print "I have got to 100"

i = 0
while i < 10000000:
i = i + 1
else:
print 'I have got to the round ',i

for i in range(100):
print i
else:
print 'The loop is done!'


导入模块

import moduleName1,moduleName2...
From module import sayhi
import moduleName as newName

导入模块的路径:(优先级依次下降(在前面的路径下找到了模块,就不往下找了))
import sys
sys.path
(打印下面的内容)
['', '/usr/local/lib/python2.7/dist-packages/autopep8-1.2.4-py2.7.egg', '/usr/local/lib/python2.7/dist-packages/pep8-1.7.0-py2.7.egg', '/usr/lib/python2.7', '/usr/lib/python2.7/plat-x86_64-linux-gnu', '/usr/lib/python2.7/lib-tk', '/usr/lib/python2.7/lib-old', '/usr/lib/python2.7/lib-dynload', '/usr/local/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages']

默认把自己添加的模块文件放到下面的路径下:
/usr/local/lib/python2.7/dist-packages


代码实例

tab.py(tab模块代码)

#!/usr/bin/env python
# python startup file
import sys
import readline
import rlcompleter
import atexit
import os
# tab completion
readline.parse_and_bind('tab: complete')
# history file
histfile = os.path.join(os.environ['HOME'], '.pythonhistory')
try:
readline.read_history_file(histfile)
except IOError:
pass
atexit.register(readline.write_history_file, histfile)
del os, histfile, readline, rlcompleter


录入信息

name = raw_input('what is your name?:').strip()
if  len(name) == 0:
print 'Error: you must input sth as name.'
age =raw_input('how old are you?:')
job = raw_input('what is your job?')

msg = """
Information of %s as below:
Name : \033[42;1m%s \033[0m;
Age  : %s
Job  : %s
""" %(name,name,age,job)

if int(age) >=50:
print "You are too old, you can only work for ..."
elif int(age) >=30 :
print "You are now in the middle age,so enjoy your life before getting too old.... "

else:
if int(age) >=20:
print 'you are still very young!'
else:
print 'not adult'

print msg


小登陆系统

#!/usr/bin/env python
import sys
import os
username = 'fdl'
password = 'fdl123'
locked = 0

retry_counter = 0

while retry_counter <3 :
user = raw_input('Username:')
if len(user) ==0:
print 'Username cannot be empty!'
continue
passwd = raw_input('Password:')
if len(passwd) == 0:
print 'Password cannot be empty!'
continue
if locked == 0:#means the user is locekd
print 'Your username is locked!'
sys.exit()

else:
if user == username  and passwd == password:
sys.exit('Welcome %s login to our system!' % user )

else:
#retry_counter = retry_counter + 1
retry_counter += 1
print 'Wrong username or password, you have %s more chances!' % (3 - retry_counter  )
else :
os.system("sed -i '6d' login.py")
os.system("sed -i '5a\locked = 0 ' login.py")


小爬虫爬取图片

#!/usr/bin/env python
# coding=utf-8
import re
import urllib

def getHtml(url):
page = urllib.urlopen(url)
html = page.read()
return html
def getImg(html):
reg = r'src="(.+?\.jpg)" pic_ext'
imgre=re.compile(reg)
imglist=re.findall(imgre,html)
x=0
for imgurl in imglist:
urllib.urlretrieve(imgurl,'%s.jpg'%x)
x+=1

html=getHtml("http://tieba.baidu.com/p/2460150866")
print getImg(html)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  python