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进行改写 '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ...
随机推荐
- git学习3:远程仓库
Git是分布式版本控制系统,同一个git仓库,可以分布到不同的机器上,那么需要有一台机器上有一个原始版本库,这样别的机器可以克隆这个原始版本库,那么这台机器就是github. 1,创建SSH Key. ...
- mint安装相关数据库lib
sudo apt-get install libmysqlclient-dev sudo apt-get install sqlite3 libsqlite3-dev
- ActiveX控件打包、签名、嵌入详解
ActiveX控件打包.签名.嵌入详解 前言 在我们的一个项目中,使用到了大华网络监控摄像头枪机,网络上下载了其ActiveX插件,但是发现其所提供的类库没有打包处理.这就导致我们每次给用户安装的时候 ...
- centos 安装 python2.7 运行webpy 项目
1.服务器是centos5,在virtualbox里装的.网络选择桥接,ip与主机在一个网段类.主机ip为xxx.xxx.xxx.69,服务器ip定义为xxx.xxx.xxx.66,GATEWAY与N ...
- 初学PHP
这东西必须得静下心来学,快是快不来的,得有一个痛苦的过程.<PHP和MySQL WEB开发>这本书很值得一看,有了坚实的基础,推荐看<深入php++面向对象.模式与实践+第三版> ...
- sftp自动授权登录
客户的账号下执行 ssh-keygen -t rsa 生成秘钥文件 ~/.ssh/id_rsa --秘钥文件 ~/.ssh/id_rsa.pub --公钥文件 将公钥文件id_rsa.pub放到sft ...
- linux 查看系统信息
一.查看内存信息 可以使用free命令显示系统的物理内存和交换分区的总量,以及已使用的.空闲的.共享的.在内核缓冲内的和被缓存的内存数量. 使用free命令可以显示计算机系统的内存容量. [root@ ...
- cmd执行mysql操作
(以下已安装到本机的mysql为例) 登录mysql数据库,如果没有在环境变量配置path到mysql中的bin目录,需要手动进入该目录中 执行:mysql -u用户名 -p密码 (注意:只要进入了m ...
- iptables--简单的防火墙
iptables--简单的防火墙 如果你执行iptables --list你将看到防火墙上的可用规则.下例说明当前系统没有定义防火墙,你可以看到,它显示了默认的filter表,以及表内默认的input ...
- Fuel快速安装OpenStack
1 介绍 1.1 关于 Mirantis Mirantis,一家很牛逼的openstack服务集成商,他是社区贡献排名前5名中唯一一个靠软件和服务吃饭的公司(其他分别是Red Hat, HP, IBM ...