Python,how do you work

想写这样的文章有好长时间了,反正最近也没什么任务,所以就来说说Python。

 

Python其实是很简单的一门编程语言了。翻遍所有的书籍,虽然很少,但一看大家都能明白。简简单单的语句就能干出大事情的,我想Python,非你莫属了。

 

这里不想将关于Python的基本语法,感觉太简单,只要学过编程的都会明白这个。什么Hello World啊,大家到网上也会学到很多的。

这里提供一个网站

 

http://sebug.net/paper/python/index.html 对于Python 2.x版本有很细的讲解。大概看个一天左右基本都会了。

 

我这个主要讲解Python的小例子和看到的精髓代码,和大家分享一下。

 

第一个:上面网站提供

 

关于Python和window的联合应用问题

 

其实Python是很轻松的调用系统的函数,做一些事情,比如说查看注册表了,打打包什么的,下面的例子就是这样的

 

 

import os
import time

# 1. The files and directories to be backed up are specified in a list.
source = [r'D:\home\bin']
# If you are using Windows, use source = [r'C:\Documents', r'D:\Work'] or something like that

# 2. The backup must be stored in a main backup directory
target_dir = 'd:/' # Remember to change this to what you will be using

# 3. The files are backed up into a zip file.
# 4. The current day is the name of the subdirectory in the main directory
today = target_dir + time.strftime('%Y%m%d')
# The current time is the name of the zip archive
now = time.strftime('%H%M%S')

# Create the subdirectory if it isn't already there
if not os.path.exists(today):
    os.mkdir(today) # make directory
    print 'Successfully created directory', today

# The name of the zip file
target = today + os.sep + now + '.zip'

# 5. We use the zip command (in Unix/Linux) to put the files in a zip archive
zip_command = "zip -qr %s %s" % (target, ' '.join(source))

# Run the backup
if os.system(zip_command) == 0:
    print 'Successful backup to', target
else:
    print 'Backup FAILED'

 

 上面的例子说明:

 

source 为需要打包的目录,target为我想打包的位置和文件名。zip_command就是我要执行的命令,通过os.system来调用系统来执行。os.path来判断文件是否存在。

 

如果看不懂上面的可以看看下面的

 

 

import os
import time

# 1. The files and directories to be backed up are specified in a list.
source = [r'D:\home\bin']
# If you are using Windows, use source = [r'C:\Documents', r'D:\Work'] or something like that

# 2. The backup must be stored in a main backup directory
target_dir = 'd:/' # Remember to change this to what you will be using

# 3. The files are backed up into a zip file.
# 4. The name of the zip archive is the current date and time
target = target_dir + time.strftime('%Y%m%d%H%M%S') + '.zip'

# 5. We use the zip command (in Unix/Linux) to put the files in a zip archive
zip_command = "zip -qr %s %s" % (target, ' '.join(source))

# Run the backup
if os.system(zip_command) == 0:
    print 'Successful backup to', target
else:
    print 'Backup FAILED'

 

 这个更简单,更好理解

 

 

对于一些刚刚接触Python的人来说上面会很难,不知道是干什么的,那么你应该从基础学习一下。查看我给你的网站,进行分析,然后在来看这篇文章,就会发现,有一种上升的感觉哦。

 

对于Python的面向对象,我这里还是会说明一下

 

Python是怎么创建类的

 

 

class Person:
    pass
p = Person()
print p

 

 

类的内部函数

 

 

class Person:
    def sayHi(self,message):
        print message

p = Person()
p.sayHi("Hello,how the python work")
 

 


类的方法与普通的函数只有一个特别的区别——它们必须有一个额外的第一个参数名称,但是在调用这个方法的时候你不为这个参数赋值,Python会提供这个值。这个特别的变量指对象本身,按照惯例它的名称是self。

 

类的构造方法的定义

 

class Person:
    def __init__(self, name):
        self.name = name
    def sayHi(self):
        print 'Hello, my name is', self.name

p = Person('chenhailong')
p.sayHi()

 

 类的析构函数的定义

 

class Person:
    '''Represents a person.'''
    population = 0

    def __init__(self, name):
        '''Initializes the person's data.'''
        self.name = name
        print '(Initializing %s)' % self.name

        # When this person is created, he/she
        # adds to the population
        Person.population += 1

    def __del__(self):
        '''I am dying.'''
        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):
        '''Greeting by the person.

        Really, that's all it does.'''
        print 'Hi, my name is %s.' % self.name

    def howMany(self):
        '''Prints the current population.'''
        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()

kalam = Person('Abdul Kalam')
kalam.sayHi()
kalam.howMany()

swaroop.sayHi()
swaroop.howMany()

del swaroop
del kalam

 

类的继承关系好例子

 

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
 

 

 

 

 

 

 

你可能感兴趣的:(python)