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动态类型的学习---引用的理解
- Python3写爬虫(四)多线程实现数据爬取
- 垃圾邮件过滤器 python简单实现
- 下载并遍历 names.txt 文件,输出长度最长的回文人名。
- install and upgrade scrapy
- Scrapy的架构介绍
- Centos6 编译安装Python
- 使用Python生成Excel格式的图片
- 让Python文件也可以当bat文件运行
- [Python]推算数独
- Python中zip()函数用法举例
- Python中map()函数浅析
- Python将excel导入到mysql中
- Python在CAM软件Genesis2000中的应用
- 使用Shiboken为C++和Qt库创建Python绑定
- FREEBASIC 编译可被python调用的dll函数示例
- Python 七步捉虫法