• Python核心编程读笔 1


    第一章 欢迎来到Python世界

    1 Python特点

      高级的可进行系统调用的解释性语言

      面向对象

      可升级、扩展、移植

      自动内存管理器(内存管理由Python解释器负责)

    2 安装

       Windows的安装只用从官网上下载一个安装包然后一步一步next到底即可,最后不要忘记勾选“添加到path”,或者自己后面手动添加path环境变量。安装好后在cmd中输入命令python即可检查有没有安装成功

    3 运行

      win版本安装好后,默认有一个python的IDE叫IDLE,可以直接在里面敲代码。或者也可以自己写好.py格式的程序,然后用cmd中去运行该.py文件

    第二章 Python起步

    (1) print

      print 'hello world' 或者  print( "hello world" ) 或者 myString=“hello world”  print myString

      print的格式化输出: print “%s is number %d!!!” % ( "python", 1 )   输出:python is number 1

      print支持重定向:

        import sys

        print >> sys.stderr, 'some message!'  将输出重定向到标准错误输出

        logfile = open( '/tmp/mylog.txt', 'a' )

        print  >> logfile, 'some message!'  将输出重定向到日志文件

        logfile.close();  

      注意:print默认会给每一行添加一个换行符,只要在print语句结尾添加一个逗号,即可改变此行为

     (2)raw_input() 读取标准输入

      >>>user = raw_input( 'Enter login name: ' )

       Enter login name: root

      >>>user

        'root'

      >>>num = raw_input( 'enter a number: ' )

           enter a number: 1024

      >>>print 'doubling your number: %d' % ( int(num)*2 )

      doubling your number: 2048

     

    (3) 运算符

      // 浮点除法(地板除,总是舍去小数部分)

      ** 乘方

      <> “不等于”操作符,但目前慢慢被淘汰,用!=较好

      and or not 逻辑运算符

    (4) 数字

    python支持5种基本类型:

      int

      long

      bool

      float

      complex(复数)

    (5)字符串

    单引号、双引号、三引号

      [:] 得到子字符串

      + 字符串连接

      * 字符串重复

    (6)列表和元组(理解成“数组”)

      列表元素用[],元素的个数和内容可变;元组是只读的列表,元素用(),元素个数不变,元素内容可变

      通过切片[:]可以得到子集

    (7)字典

      字典元素用大括号{}包裹:

      >>> a = { "abc":2, "cde":3 }

      >>>a["abc"]

        2

      >>>a["def"] = 7  此时a中会多一个元素!

    (8)if语句

      if x>0:   #x>0并不需要用括号!

        ...

      elif sth:

        ...

      else: 

        ...

     (9)for语句、range()函数、enumerate()函数

      for item in ['a', 'b', 'c']

      for eachNum in range(3)

      

      foo = 'abc'

      for eachChar in foo

      

      foo = 'abc'

      for i in range( len(foo) ):

        print foo[i], '%d' % i

      foo = 'abc'

      for i, ch in enumerate( foo ):

        print ch, '%d' % i

    (10) 列表解析

      squared = [ x**2 for x in range(4) ]   #现在squared列表中的元素有 0 1 4 9

      sqdEvens = [ x**2 for x in range(8) if not x%2 ]  #现在sqdEvens列表中的元素有 0 4 16 36

    (11) 文件操作open()、file()

      fobj = open( filename , 'r' )

      for eachLine in fobj:

        print eachLine,  

      fobj.close()

      file()是python是最近才添加的函数,等同于open()

    (12)错误和异常

    使用try-except语句:

      try:

        filename = raw_input( 'Enter file name: ' )

        fobj = open( filename, 'r' )

        for eachLine in fobj:

          print eachLine,

        fobj.close()

      except IOError, e:

        print 'file open error: ', e

     

    (13) 函数

    如何定义函数:

      def addMe2Me( x ):

        'apply + operation to argument'  #这是对函数功能描述的注释

        return x+x;

    调用函数:

      >>>addMe2Me(4.25)

    默认参数:

      def foo( debug = True ):

        if debug:

          print 'in debug mode'

    (14) 类

     类是数据和逻辑的容器!!!

    类定义实例:

    class FoolClass( object ):
        "My first class: FoolClass"    #类说明,注释
        version = 0.1                #静态成员!!!
        
        #构造函数!!!
        def __init__( self, nm='John' ):    #self类似于this
            "constructor"
            self.name = nm
            print 'create a class instance for ', nm
        
        def showname(self):
            print 'Your name is ', self.name
            print 'My name is ', self.__class__.__name__
            
        def shower(self):
            print self.version
            
        def addMe2Me( self, x ):
            return x+x

    类使用实例:

      fool = FoolClass( "hansonwang" )  #非常像调用一个函数

      fool.shower()    #调用成员函数

    (15)模块

    导入模块: import module_name

    一旦模块导入完成,一个模块的属性(函数和变量)可以通过.号访问:

      import sys

      sys.stdout.write( "Hello World" )

      sys.platform

    (16) 实用的函数

     dir( obj ) 显示对象的属性

    help( obj ) 以一种整齐美观的形式显示对象的文档字符串

    int( obj ) 对象→整数

    str( obj ) 对象→字符串

    len( obj ) 返回对象长度

    open( filename, mode ) 以mode('r'=读,'w'=写)方式打开文件

    range( [start,]stop[,step] ) 返回一个整数列表,start默认0,step默认1

    raw_input( str ) str是提示信息

    type( obj ) 返回对象的类型(返回值本身是一个type对象!)

      

  • 相关阅读:
    NopCommerce4.2 常见错误及异常处理
    使用NopCommerce微信电商系统
    简单理解Socket
    html5 postMessage解决跨域、跨窗口消息传递
    html5 Web Workers
    node.js module初步理解
    node.js调试
    最简单的JavaScript模板引擎
    简单JavaScript模版引擎优化
    容易被忽略CSS特性
  • 原文地址:https://www.cnblogs.com/hansonwang99/p/4934623.html
Copyright © 2020-2023  润新知