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. C++ Primer : 第十二章 : 动态内存之shared_ptr与new的结合使用、智能指针异常

    shared_ptr和new结合使用 一个shared_ptr默认初始化为一个空指针.我们也可以使用new返回的指针来初始化一个shared_ptr: shared_ptr<double> ...

  2. centos6.3 + db2v9.7的数据库移行

    工作内容如题,我要做的事情大体如下: 1,正确备份可用数据库: 2,安装64位的cent os 6.3: 3,将1备份的数据恢复到新的cent os 6.3系统上. 第一件事情,就是备份一个可用的数据 ...

  3. HTML---常见标签与插入背景音乐;

     插入背景音乐 (一).基本语法: embed src=url 说明:embed可以用来插入各种多媒体,格式可以是 Midi.Wav.AIFF.AU.MP3等等, Netscape及新版的IE 都支持 ...

  4. MongoDB副本集搭建及备份恢复

    一.MongoDB副本集(repl set)介绍 早起版本使用master-slave,一主一从和MySQL类似,但slave在此架构中为只读,当主库宕机后,从库不能自动切换为主: 目前已经淘汰了ma ...

  5. Codeforces Round #132 (Div. 2)

    A. Bicycle Chain 统计\(\frac{b_j}{a_i}\)最大值以及个数. B. Olympic Medal \(\frac{m_{out}=\pi (r_1^2-r_2^2)hp_ ...

  6. Ci分开配置网站前台后台的方法

    CodeIgniter 是一个简单快速的PHP MVC框架.EllisLab 的工作人员发布了 CodeIgniter.许多企业尝试体验过所有 PHP MVC 框架之后,CodeIgniter 都成为 ...

  7. java的nio之:java的nio系列教程之Scatter/Gather

    一:Java NIO的scatter/gather应用概念 ===>Java NIO开始支持scatter/gather,scatter/gather用于描述从Channel(译者注:Chann ...

  8. (转)一文学会用 Tensorflow 搭建神经网络

    一文学会用 Tensorflow 搭建神经网络 本文转自:http://www.jianshu.com/p/e112012a4b2d 字数2259 阅读3168 评论8 喜欢11 cs224d-Day ...

  9. Teaching Your Computer To Play Super Mario Bros. – A Fork of the Google DeepMind Atari Machine Learning Project

    Teaching Your Computer To Play Super Mario Bros. – A Fork of the Google DeepMind Atari Machine Learn ...

  10. git checkout 和 git checkout --merge <branch_name>使用

    一.git checkout //查看当前分支$ git branch master *t2 testing //checkout会覆盖当前工作区文件和覆盖暂存区内容,所以发现分支有未提交的警告,执行 ...