study notes for python
some useful materials
http://www.cnblogs.com/taowen/articles/11239.aspx (from taowen, BITer)
Note:
Part 1 Basic Data Structure
List, Dict(dictionary) and Turple are three main data structures in python, which are respond to set,mapping and ? in math.
List
my_list=[]
my_list=[1,2]
print my_list
my_list.append(3)
print my_list
list can be indexed: print my_list[1], which outputs 2
Dict
contact={}
contact["name"]="taowen"
contact["phone"]=68942443
Turple
my_turple = (1,2,3)
my_list = []
for i in my_turple:
my_list.append(i)
print my_list
Part 2 Basic Grammer
i = 5
n = 0
while i>0:
n = n + i
i = i - 1
print n while 1:
inputed_num = input("input a number between 1 and 10\n")
if inputed_num >= 10:
pass
elif inputed_num < 1:
pass
else:
break
print "hehe, don't follow, won't out"
Part 3 IO
raw_input: return your inputs as a string.
input:convert your inputs to a digital number and return it.
COFFE BREAK
# import the module to create a GUI
fromt Tkinter import *
# create a main window
root = Tk()
# create a label which is located in the main window
w = Label(root, text="Hello, world!")
# put the label in main window in default way
w.pack()
# start a loop waiting for your input
root.mainloop()
Part 4 Exception Handling
try:
print input()
except:
print "there is an error in your input"
Part 5 Function
def square(x):
return x**2
print square(5) def swap(a,b):
# return a turple
return (b,a)
print swap(1,2)
Whether the paramater in function can be modifed or not depends on the type of paramater. Data types such as number or string cannot be modified in funciton, while list could be modified when used as param. So it's better to think twice before sending the complex params to function(you may need to copy it in advance).
Part 6 File Operation in Python
#input
xxx = file('c:\\a.txt', 'r')
xxx_content = infile.read()
xxx_content = infile.readlines()
for xxx_line in xxx.readlines():
print "Line:", xxx_line
xxx.close() #output
xxx=file('c:\\test.txt','w')
xxx.write("billrice")
xxx.write("testtest")
xxx.write("enter\n")
xxx.writelines(['billrice','ricerice'])
xxx.close
Note that before running the xxx.close() sentence, there is just an empy test.txt file in C disk, xxx.close() completes the save operation.
COFFE BREAK : useful functions
eval(raw_input())
# input 1+2, output 3
Part 7 Class
class person:
school = 'bit'
def _init_(self):
self.name = 'taowen'
self.id = 20022479
def say_id(self):
print "%s's id is %d" % (self.name, self.id) me = person()
me.say_id()
study notes for python的更多相关文章
- Machine Learning Algorithms Study Notes(2)--Supervised Learning
Machine Learning Algorithms Study Notes 高雪松 @雪松Cedro Microsoft MVP 本系列文章是Andrew Ng 在斯坦福的机器学习课程 CS 22 ...
- Machine Learning Algorithms Study Notes(3)--Learning Theory
Machine Learning Algorithms Study Notes 高雪松 @雪松Cedro Microsoft MVP 本系列文章是Andrew Ng 在斯坦福的机器学习课程 CS 22 ...
- Machine Learning Algorithms Study Notes(1)--Introduction
Machine Learning Algorithms Study Notes 高雪松 @雪松Cedro Microsoft MVP 目 录 1 Introduction 1 1.1 ...
- [Python Study Notes]匿名函数
Python 使用 lambda 来创建匿名函数. lambda这个名称来自于LISP,而LISP则是从lambda calculus(一种符号逻辑形式)取这个名称的.在Python中,lambda作 ...
- [Python Study Notes]字符串处理技巧(持续更新)
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ...
- [Python Study Notes]with的使用
在 Python 2.5 中, with 关键字被加入.它将常用的 try ... except ... finally ... 模式很方便的被复用.看一个最经典的例子: with open('fil ...
- [Python Study Notes]实现对键盘控制与监控
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ...
- [Python Study Notes]实现对鼠标控制
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ...
- [Python Study Notes]批量将wold转换为pdf
本文代码,由原ppt2pdf.py进行改写 '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ...
随机推荐
- 家里蹲大学数学杂志 Charleton University Mathematics Journal 官方目录[共七卷493期,6055页]
家里蹲大学数学杂志[官方网站]从由赣南师范大学张祖锦老师于2010年创刊;每年一卷, 自己有空则出版, 没空则搁置, 所以一卷有多期.本杂志至2016年12月31日共7卷493期, 6055页.既然做 ...
- 在sql脚本中获取变量中的查询结果
)--变量 ) set @itemValue='select @a=getdate()'--赋值 exec sp_executesql @itemValue,N'@a nvarchar(max) ou ...
- Python 之 时间字符串、时间戳、时间差、任意时间字符串转换时间对象
1. 时间字符串 --> 时间戳 1) time 模块 timestring = '2016-12-21 10:22:56' print time.mktime(time.strptime(ti ...
- 01-C#入门(分支控制语句)
说实话,<C#入门经典>这本书对入门的同学来说真的太棒了,先不说内容如何,就作者先以控制台(命令行)调试程序的方法,就能够最大限度地让你关注学习的内容,而不是花哨的界面调试. 现在学习是下 ...
- 配置grunt进行css、js的检查、合并和压缩
现在会进行代码的合并和压缩已成为前端人员的必备知识,那么现在来介绍一个grunt的工具.grunt是个风靡世界的工具,它的首页是 http://www.gruntjs.net 这是个中文网站,有文档 ...
- SQL归档
/* 作用:归档3个月前的短信发送记录 创建人:zhuxiang 日期:2012-10-30 */ ALTER Proc [dbo].[Sms_Job_History_Send] As Begin D ...
- SQL 会议消费记录统计
统计 /****** Object: Procedure [dbo].[JOB_UP_Summit_UserConfStat] Script Date: 2014-3-17 10:00:50 **** ...
- flex acionscript png图片去除多余空白,生成合适大小图片
//最小矩形(非透明最小区域) public static function getMinRect(target:BitmapData):Rectangle{ return target.getC ...
- Raspberry pi之wifi设置-3
1.配件套装里最好有wifi网卡,非常小如下图 插入Raspberry pi的USB口,用lsusb来查看USB设备列表如下 pi@raspberrypi~/Desktop $ lsusb Bus 0 ...
- 1,SFDC 开发篇 - 类对象和SOQL查询
1,类对象 & 接口 & 属性 2, Object & SOQL