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. 2层Folder删除问题,父文件夹删不掉

    在此用的是由内向外删除.文件结构是:父文件夹/子文件夹/文件.用的是java1.6的java.io.FIle#deleteFile(); 在删除的过程中,发现,文件删除的时候没有问题,但是在子文件夹删 ...

  2. Linux网络管理概述

    概述:计算机基础知识.网络基础知识其实是所有的程序员所必须的,甚至已经不仅仅是程序员的专利,而是每一个人都应该掌握的计算机知识. 主要内容: 一.网络基础 二.Linux网络配置 三.Linux网络命 ...

  3. POJ1459 Power Network(网络最大流)

                                         Power Network Time Limit: 2000MS   Memory Limit: 32768K Total S ...

  4. wordpress(一)wordpress环境的搭建

    搭建wordpress环境因为自动安装的脚本不提供创建数据库的功能,所以先要创建数据库. 1.使用如下命令创建数据库(都是在已经登陆的mysql界面中的命令) ①CREATE DATABASE 数据库 ...

  5. HDU-1255 覆盖的面积 (扫描线)

    题目大意:给若干个矩形,统计重叠次数不为0的面积. 题目分析:维护扫描线的长度时,只需要只统计覆盖次数大于1的区间即可.这是个区间更新,不过不能使用懒标记,但是数据规模不大,不用懒惰标记仍可以AC. ...

  6. GCC编译器

    详见<gcc中文手册> 编译过程 预处理器cpp 编译器gcc 汇编器as 链接器linker file.c   -------------> file.i  ----------- ...

  7. 代码备份:处理 SUN397 的代码,将其分为 80% 训练数据 以及 20% 的测试数据

    处理SUN397 的代码,将其分为80% 训练数据以及20% 的测试数据 2016-07-27 1 %% Code for Process SUN397 Scene Classification 2 ...

  8. tomcat 源码解析

    how_tomcat_works https://www.uzh.ch/cmsssl/dam/jcr:00000000-29c9-42ee-0000-000074fab75a/how_tomcat_w ...

  9. kubernetes centos 安装

    1.  安装       yum install -y  etcd  kubernetes   2.  配置         docker             /etc/sysconfig/doc ...

  10. 【转】PHP简单拦截器的实现

    最近在看Yii的源代码,收获了不少,这里就是从中得到的启发,而写的一个简单拦截器的实现下面看例子: <?phpclass A{    private $_e = array();       p ...