python用冒号代替{}开启语句块

  1. /usr/bin/python 加在脚本的头部, ./脚本

  2. help("str") : 查看命令帮助

  3. '''三引号可以打印换行字符串

    print('\n') 可以打印换行

  4. 表达式打印

    s = 10

    print 'asdasd',s

  5. if循环,python没有switch,取而代之的是字典

#!/usr/bin/python
guess = int(raw_input('Enter number ...'))
if guess==1:
print '1'
elif guess ==2:
print '2'
else:
print '3'
  1. while循环,有可选的else从句
#!/usr/bin/python
flag = True
num=23
while flag:
guess = int(raw_input('enter number'))
if guess == num:
print 'yes'
flag = False
elif guess>num:
print 'big'
else:
print 'small'
else:
print 'loop end'
  1. for
for i in range(1,5):
print i

函数 :

#!/usr/bin/python
def getMax(a,b):
if a>b:
print a,'is max'
else:
print b,'is max' getMax(1,'as') 67

函数内部声明global变量,让该变量从上下文钟获取变量值

函数的返回值需要return声明,如果不声明,默认在在结尾return None

模块

  1. sys模块带有环境相关的参数
import sys
print 'console arg:'
for i in sys.argv:
print i print 'python path is',sys.path

列出模块的属性方法 : dir

删除模块的属性方法 : del

列表 : [elem1,elem2]

del list[index]:删除列表的元素

len(listname):list长度

applist = ['apple','orange','carrot']
print applist for i in applist:
print i del applist[0] #['orange', 'carrot']
applist.append('wo') #['orange', 'carrot', 'wo']
print applist
applist.sort() #['carrot', 'orange']
print applist

元祖:不可变的列表

zoo = ['wolf',1]

print zoo
print len(zoo) newzoo = [2,3,zoo] #2
print newzoo[2][1] #1

占位打印

age = 22
name = 'zhangsan' print '%s is %d' % (name,age)

字典:map

{k:v,k2:v2}

map1 = {"zs":24,"lisi":25}
print map1["zs"] #24
del map1["zs"]
print map1 #{'lisi': 25} for name,age in map1.items():
print '%s is %d' % (name,age) #lisi is 25 if map1.has_key('lisi'):
print 'lisi is %d' % map1['lisi'] #lisi is 25

序列:元组和列表都是序列

序列2中操作:

(1)索引操作:用脚标获取元素

(2)切片操作:2个角标确定切片,a:b,表示从a开始,到b结束,不包含b角标的子序列

python备份文件

import time
import os source = ['/home/swaroop/byte','/home/swaroop/bin']
target_dir = '/mnt/e/backup/'
today = target_dir + time.strftime('%Y%m%d')
now = time.strftime('%H%M%S') #文件明中加上注释
comment = raw_input('Enter a comment --> ') if len(comment)==0:
target = today + os.sep + now + '.zip'
else:
target = today + os.sep + now + '_' + comment.replace('','_') + 'zip' if not os.path.exists(today):
os.mkdir(today) zip_command = "zip -qr '%s' %s" % (target, ' '.join(source)) if os.system(zip_command)==0:
print 'Successful backup to', target
else:
print 'Backup FAILED'

假设一个类MyClass,他的对象MyObject

类中的方法的第一个参数必须是self对象,即使是空参函数,也要有self

当对象调用MyObject.method(arg1, arg2)时,python会自动转换为MyClass.method(MyObject, arg1,arg2)

构造方法:

init(self,param):self.param = param

消亡方法

del

# -*- coding: utf-8 -*
class Person:
# 类变量
population = 0
# 对象变量
def __init__(self, name): #对象初始化时调用
self.name = name
print '(Initializing %s)' % self.name
Person.population += 1 def __del__(self): # 对象消亡,清空内存时使用
print '%s says bye.' % self.name
Person.population -= 1
if Person.population == 0:
print 'I am the last one.'
else:
print 'There are still %d people left.' % Person.population def sayHi(self):
print 'Hi, my name is %s.' % self.name def howMany(self):
if Person.population == 1:
print 'I am the only person here.'
else:
print 'We have %d persons here.' % Person.population swaroop = Person('Swaroop')
swaroop.sayHi()
swaroop.howMany()

继承

把父类的类名作为一个元组放在声明子类的后面

class SchoolMember:
'''Represents any school member.'''
def __init__(self, name, age):
self.name = name
self.age = age
print '(Initialized SchoolMember: %s)' % self.name def tell(self):
'''Tell my details.'''
print 'Name:"%s" Age:"%s"' % (self.name, self.age) class Teacher(SchoolMember):
'''Represents a teacher.'''
def __init__(self, name, age, salary):
SchoolMember.__init__(self, name, age)
self.salary = salary
print '(Initialized Teacher: %s)' % self.name def tell(self):
SchoolMember.tell(self)
print 'Salary: "%d"' % self.salary class Student(SchoolMember):
'''Represents a student.'''
def __init__(self, name, age, marks):
SchoolMember.__init__(self, name, age)
self.marks = marks
print '(Initialized Student: %s)' % self.name def tell(self):
SchoolMember.tell(self)
print 'Marks: "%d"' % self.marks t = Teacher('Mrs. Shrividya', 40, 30000)
s = Student('Swaroop', 22, 75)
print # prints a blank line
members = [t, s]
for member in members:
member.tell() # works for both Teachers and Students

异常

try:

except EOFError:

except:

相当于: try .. catch .. catch ..

try:

finally:

eval("2+3")

5

exec 'print "hello world"'

hello world

python简要的更多相关文章

  1. Python简要学习笔记

    1.搭建学习环境 推荐ActivePython,虽然此乃为商业产品,却是一个有自由软件版权保证的完善的Python开发环境,关键是文档以及相关模块的预设都非常齐备. ActivePython下载地址: ...

  2. Python简要标准库(2)

    集合 堆 和 双端队列 1.集合 创建集合 s = set(range(10)) 和字典一样,集合元素的顺序是随意的,因此不能以元素的顺序作为依据编程 集合支持的运算 a = set([1,2,3]) ...

  3. Python简要标准库(1)

    sys sys这个模块让你能够访问与Python解释器联系紧密的变量和函数 其中的一些在下表 F argv 命令行参数,包括脚本名称 exit([arg]) 退出当前的程序,可选参数为给定的返回值或者 ...

  4. Python简要标准库(5)

    hashlib Python的hashlib提供了常见的摘要算法,如MD5,SHA1等等. 基本的生成MD密匙的函数 import hashlib md5 = hashlib.md5() md5.up ...

  5. python 简要小结

    初学python 简单总结部分内置函数 将两个数组合并为元组:zip()   解压:zip(*zip) range(a,b,c) 取值范围 起始:a   结尾:b   间隔:c   (参数不能为空否则 ...

  6. Python简要标准库(3)

    shelve 若只需要一个简单的存储方案,那么shelve模块可以满足你大部分的需要,你所需要的只是为它提供文件名.shelve中唯一有趣的函数是open,在调用的时候他会返回一个Shelf对象 注意 ...

  7. 图解python | 简介

    作者:韩信子@ShowMeAI 教程地址:http://www.showmeai.tech/tutorials/56 本文地址:http://www.showmeai.tech/article-det ...

  8. 各种编程语言功能综合简要介绍(C,C++,JAVA,PHP,PYTHON,易语言)

    各种编程语言功能综合简要介绍(C,C++,JAVA,PHP,PYTHON,易语言) 总结 a.一个语言或者一个东西能火是和这种语言进入某一子行业的契机有关.也就是说这个语言有没有解决社会急需的问题. ...

  9. SPHINX 文档写作工具安装简要指南 - windows 版 - 基于python

    此教程基于本地己安装好 PYTHON 并配置过全局变量:一定具备相应的基础再操作: 上传图片以免产生误导,以下为文字描述,按下列操作即可: 下载 get-pip.py脚本; python get-pi ...

随机推荐

  1. leetcode 131. Palindrome Partitioning----- java

    Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...

  2. spark与storm的对比

    对比点 Storm Spark Streaming 实时计算模型 纯实时,来一条数据,处理一条数据 准实时,对一个时间段内的数据收集起来,作为一个RDD,再处理 实时计算延迟度 毫秒级 秒级 吞吐量 ...

  3. timus 1106 Two Teams(二部图)

    Two Teams Time limit: 1.0 secondMemory limit: 64 MB The group of people consists of N members. Every ...

  4. poj2375 强连通

    题意:有一个 l * w 大小的滑雪场,每个格子都有一个高度,每个格子可以直接通到上下左右四个格子中高度小于等于自己的格子,现在要建立通道,能够连通任意两个格子,问最少建多少通道能够使所有格子能够互相 ...

  5. 论文笔记之:Visual Tracking with Fully Convolutional Networks

    论文笔记之:Visual Tracking with Fully Convolutional Networks ICCV 2015  CUHK 本文利用 FCN 来做跟踪问题,但开篇就提到并非将其看做 ...

  6. javascript输出图的简单路径

    <script> //图的构建 function vnode() { this.visited=0; this.vertex=0; this.arcs=new Array(); } fun ...

  7. PADS Logic 常见错误报告内容

    1.PCB Decal LED0805 not found in Library pcb封装不在库中. 找到原图中的pcb-save to library 未分配PCB时候,右键Edit part-找 ...

  8. Nginx-搭建https服务器

    先看Nginx中的配置 server { listen ; ssl on; ssl_certificate /usr/local/nginx/conf/任意证书名.crt; ssl_certifica ...

  9. Linux-IP地址后边加个/8(16,24,32)是什么意思?

    是掩码的位数        A类IP地址的默认子网掩码为255.0.0.0(由于255相当于二进制的8位1,所以也缩写成“/8”,表示网络号占了8位);    B类的为255.255.0.0(/16) ...

  10. 利用KMeans聚类进行航空公司客户价值分析

    准确的客户分类的结果是企业优化营销资源的重要依据,本文利用了航空公司的部分数据,利用Kmeans聚类方法,对航空公司的客户进行了分类,来识别出不同的客户群体,从来发现有用的客户,从而对不同价值的客户类 ...