`

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
 

 

 

 

 

 

 

分享到:
评论

相关推荐

    Learn Data Analysis with Python 英文原版

    If you are already using Python for data analysis, you will find a number of things that you wish you knew how to do in Python. You can then take these techniques and apply them directly to your own ...

    Data Wrangling with Python 2016新书 无水印pdf 0分

    How do you take your data analysis skills beyond Excel to the next level? By learning just enough Python to get stuff done. This hands-on guide shows non-programmers like you how to process ...

    Python Advanced Predictive Analytics

    Data is powerful but not in its raw form - It needs to be processed and modeled, and Python is one of the most robust tools out there to do so. It has an array of packages for predictive modeling and...

    Learning Python

    Then, the authors address the mechanics of the language itself, providing illustrations of how Python conceives of numbers, strings, and other objects as well as the operators you use to work with ...

    [machine_learning_mastery系列]Machine_Learning_Mastery_With_Python.pdf

    I am often asked the question: How do you use Python for machine learning? This book is my definitive answer to that question. It contains my very best knowledge and ideas on how to work through ...

    Automate.the.Boring.Stuff.with.Python.Practical.Programming

    In Automate the Boring Stuff with Python, you'll learn how to use Python to write programs that do in minutes what would take you hours to do by hand—no prior programming experience required....

    Geoprocessing-with-Python.pdf

    made me realize how much I enjoyed working with Python, so naturally I had to start using GDAL with it as well. More importantly for this book, my coworker John Lowry suggested that we team- teach a ...

    OpenCV 3.x with Python By Example, 2nd Edition-Packt Publishing(2018).pdf

    Computer vision is found everywhere in ... By the end of this chapter, you will be able to understand how neural networks work and how to apply them to machine learning to build advance images tools.

    笨方法学python3 Learn Python 3 the Hard Way

    As you do, you’ll learn how a computer works; what good programs look like; and how to read, write, and think about code. Zed then teaches you even more in 5+ hours of video where he shows you how ...

    Machine.Learning.in.Python

    Machine Learning in Python shows you how to do this, without requiring an extensive background in math or statistics. Table of Contents Chapter 1 The Two Essential Algorithms for Making Predictions ...

    Python安装包version 3.1.5

    especially how built-in objects like dictionaries and strings work, have changed considerably, and a lot of deprecated features have finally been removed. Build Instructions ------------------ On ...

    Machine Learning in Python 无水印pdf 0分

    Machine Learning in Python: Essential Techniques for Predictive Analysis ... Machine Learning in Python shows you how to do this, without requiring an extensive background in math or statistics.

    Effective Python Development for Biologists pdf

    Environments for development - learn how you can take advantage of different tools for actually writing code, including those designed specifically for scientific work. Organising and sharing code - ...

    Udemy - Deep Learning Convolutional Neural Networks in Python

    We will also do some biology and talk about how convolutional neural networks have been inspired by the animal visual cortex. After describing the architecture of a convolutional neural network, we ...

    Python: Advanced Predictive Analytics: 2017-12 python重量级高级大数据分析预测

    it needs to be processed and modeled, and Python is one of the most robust tools out there to do so. It has an array of packages for predictive modeling and a suite of IDEs to choose from. Using the ...

    Rapid GUI Programming with Python and Qt

    This book teaches how to write GUI applications using the Python programming language and the Qt application development framework. The only prior knowledge assumed is that you can program in some ...

Global site tag (gtag.js) - Google Analytics