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进行改写 '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ...
随机推荐
- (C++)窗口置前SetForegroundWindow(pThis->hwndWindow);
一段代码主要是创建一个Window,然后将其置顶显示.奇怪的是这个功能有时候无效. pThis->bWindowDisplayed = SetForegroundWindow(pThis-> ...
- 在IT行业换一个领域
开发做了不到两个月,就不做了.原因是自己不喜欢开发,感觉开发的东西很麻烦.也许说到麻烦,很多人都要教训我了,干什么不麻烦.我也不想反驳什么,因为失败的次数太多了,反驳也无力. 从放弃开发开始到现在,抑 ...
- TCP 常用总结
SO_RCVBUF SO_SNDBUF TCP socket在内核中都有一个发送缓冲区和一个接收缓冲区,不管进程是否读取socket,对端发来的数据都会经由内核接收并且缓存到socket的内核接收缓冲 ...
- 方便的一站式svn/git服务器软件(linux)
https://www.scm-manager.org/ The easiest way to share and manage your Git, Mercurial and Subversion ...
- 爬虫之scrapy框架
解析 Scrapy解释 Scrapy是一个为了爬取网站数据,提取结构性数据而编写的应用框架. 其可以应用在数据挖掘,信息处理或存储历史数据等一系列的程序中.其最初是为了页面抓取 (更确切来说, 网络抓 ...
- openlayers3 画扇形
参考了http://www.cnblogs.com/lingxue3769/archive/2011/11/01/2231409.html同学的博客 和 百度知道https://zhidao.baid ...
- .Net 连接字符串的解释
https://msdn.microsoft.com/zh-cn/library/cc716756.aspx 连接字符串参数 连接字符串的格式是使用分号分隔的键/值参数对列表: keyword1=va ...
- 牛客网程序员面试金典:1.1确定字符互异(java实现)
问题描述: 请实现一个算法,确定一个字符串的所有字符是否全都不同.这里我们要求不允许使用额外的存储结构. 给定一个string iniString,请返回一个bool值,True代表所有字符全都不同, ...
- 委托、IOC全知道
话说写代码已有数年,曾经花了很多时间,看了很多大牛的文章也是不能参透,日思夜想都没有理解的概念,通过不断的实践与学习,回过头来再看,总算有了一个清晰的理解与认识,也看到一句话说,最好的学习就是把别人教 ...
- Spring对 JDBC 的支持,JdbcTemplate类的使用
导包:spring框架的包 和 连接数据库连接池的c3p0包 连接mysql数据库的包; 在src目录下建立jdbc.properties文件:存放连接数据库的属性值 jdbc.user=root j ...