python基础语法(1)
来源:互联网 发布:中国网络零售交易额 编辑:程序博客网 时间:2024/05/22 01:35
注释
- 单行注释:#
- 多行注释:(单引号,双引号在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 ielse : print "I have got to 100"i = 0while i < 10000000: i = i + 1else: print 'I have got to the round ',ifor i in range(100): print ielse: print 'The loop is done!'
导入模块
import moduleName1,moduleName2...From module import sayhiimport moduleName as newName 导入模块的路径:(优先级依次下降(在前面的路径下找到了模块,就不往下找了))import syssys.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 sysimport readlineimport rlcompleterimport atexitimport 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: passatexit.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 pythonimport sysimport osusername = 'fdl'password = 'fdl123'locked = 0 retry_counter = 0while 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-8import reimport urllibdef 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+=1html=getHtml("http://tieba.baidu.com/p/2460150866")print getImg(html)
0 0
- python基础语法(1)
- Python 基础语法(1)
- Python基础语法1
- Python基础语法1
- python基础语法(1)
- 【python】(1)基础语法
- python基础(基础语法)
- python语法基础归纳(1)
- Python 基础语法笔记1
- python学习1-基础语法
- Python基础语法(一)
- Python基础语法(二)
- Python基础语法(三)
- Python 基础语法(二)
- Python 基础语法(三)
- Python基础语法(四)
- python基础语法(3)
- Python基础(语法知识点)
- Multiplication Table (二分)
- 设置导航栏透明
- FragmentPagerAdapter
- Codeforces Round #177 (Div. 1) C. Polo the Penguin and XOR operation(贪心)
- 我的JavaScript笔记
- python基础语法(1)
- memwatch使用
- 进程通信之文件读写
- 【TomoEngine】 第六讲 基于TomoEngine的3D产品(游戏/应用)制作流程
- Android 布局
- c++ 初始化列表
- Android Studio2.0 教程从入门到精通Windows版 - 提高篇
- JQuery面试题1
- Codeforces Round #218 (Div. 2) C. Hamburgers【二分】