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进行改写 '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ...
随机推荐
- Visual Studio 14 初试,vNext
下了几天的VS 2014 .终于安装上了,花了好几天时间, VS 2014 下载地址, http://www.visualstudio.com/en-us/downloads/visual-stud ...
- va_list结构体
http://stackoverflow.com/questions/4958384/what-is-the-format-of-the-x86-64-va-list-structure 这是我在st ...
- ACM入门
1.给n个数字,将它们重新排序得到一个最大的数字 例子 4123 124 56 90--------------90561241235123 124 56 90 9------------990561 ...
- 树莓派3B 安装微雪LCD5inch显示器(包含软键盘)
树莓派单独使用时,往往需要触摸屏和软键盘用以方便操作,微雪LCD显示器就能较好的实现这个功能, 正好实验室又买入了一个3B的板子和一个5inch的显示器,便对着官方的安装手册,亲自安装了一次. 一:材 ...
- 基于Autofac, Castle.DynamicProxy的动态WCF解决方案(原创)
本方案解决了下面3个主要的问题: 1.减少配置,为了避免每次新增service都需要去修改配置文件,包括服务器端跟各个客户端的. 2.能够使用函数重载,泛型函数,以及泛型类. 3.使项目能够快速地在w ...
- 说说无耻的商河水木清华开发商2013"交房
说说无耻的水木清华开发商2013"交房" 我买的是22号楼,合同里写的是2011年6月30号前交房.4月28我手机响了,电话那边说是水木清华的,29号交房.说交房通知书已经EMS发 ...
- OpenGL学习笔记0——安装库
最近需要做一个基于Zigbee室内无线定位的系统,受到TI公司ZigBee Sensor Monitor软件的启发,打算用OpenGL来做一个3D显示空间内物体位置的程序.学习阶段选择VS2010+O ...
- chrome启用 NPAPI [转]
需前往 chrome://flags/#enable-npapi 手动开启 原帖:http://tieba.baidu.com/p/3737775413 第1步:开启NPAPI, 就是置顶帖里说的 我 ...
- Python体验(09)-图形界面之Pannel和Sizer
import wx class Form(wx.Frame): def __init__(self,parent,ID,title): wx.Frame.__init__(self,parent,ID ...
- webuploader 上传文件参数设置
webUploader 是款很好用的优秀的开源上传组件,由百度公司开发,详细的介绍可参见webUploader 的官方文档: 最近在使用webUploader时,需要添加额外的参数,并在后台获取,参数 ...