zoukankan      html  css  js  c++  java
  • python and Django(二)

    走马观花识python

    一、python中的提示符

      主提示符 >>>  等待输入下一个语句

      次提示符 …    等待输入当期语句的其他部分

    二、编程方式

      语句:通知给解释器的一个命令。

    >>> print 'hipython'
    hipython

         表达式:可以是一个算式运算表达式或者是一个函数调用。

    >>> abs(-5)
    5

    三、常用函数

      1、程序输出(print  

    >>> mystring='hipython'
    (1)
    直接输出
    >>> mystring 
    'hipython'
    (2)
    print 输出
    >>> 
    print mystring 
    Hipython
    (3)
    输出最后一个表达式的值
    >>> _   
    'hipython'
    (4)
    实现字符串替换
    >>> 
    print "%s is string;%d is int,%f is float" %("mystr",100,1.23)
    mystr 
    is string;100 is int,1.230000 is float
    5)重定向输出
    >>> logfile=open(
    'd:/lyp/python/tmp.txt','a')
    >>> 
    print >>logfile,'Hello PythonO(_)O哈哈~'
    >>> logfile.close()

      2、程序输入(raw_input

    接收一个输入
    >>> str=raw_input(
    'Please input')
    >>> 
    print str
    hi python!

      3、函数帮助(help

    >>> help(raw_input)
    Help on function Win32RawInput 
    in module pywin.framework.app:

    Win32RawInput(prompt=None)
        Provide raw_input() 
    for gui apps

      4、显示对象的属性(dir

     dir(raw_input)
    [
    '__call__''__class__''__delattr__''__dict__''__doc__''__get__''__getattribute__''__hash__''__init__''__module__''__name__''__new__''__reduce__''__reduce_ex__''__repr__''__setattr__''__str__''func_closure''func_code''func_defaults''func_dict''func_doc''func_globals''func_name']

      5、显示对象类型(type

    >>> type(raw_input)
    <type 
    'function'>

      6、打开文件(open

    >>> myfile = open('D:/lyp/python/tmp.txt')
    >>> myfile.read()
    '\xef\xbb\xbfHello Python.\n'

      7、得到一个整数列表(range

    >>> mylist = range(4,10,2)
    >>> 
    print mylist
    [4, 6, 8]

      8、得到对象长度(len

    >>> mystr='hipython'
    >>> len(mystr)
    8

      9、类型转换(int,string)

    >>> myint=100
    >>> str(myint)
    '100'
    >>> int(str(myint))
    100

    四、注释

    1#直到行末
    >>> print 
    123 #输出123
    123
    2)文档字符串
    >>> def myfun():
         
    "print abc"
         print 
    'abc'
         
    >>> myfun.func_doc
    'print abc'

    五、运算符

    1)算术运算符
    + - * / 
    // % **
    加、减、乘、除、浮点除法、区余、乘方
    >>> print (
    1+3-1)*4/2
    6
    >>> print 
    3**2
    9
    2)比较运算符
    < <= > >= == != <>
    >>> 
    2>4
    False
    3)逻辑运算符
    and or not
    >>> 
    2<4 and 3<6
    True
    >>> not 
    2<4
    False

    六、python中类型

          1变量

        1)字母,下划线,数字。数字不能做开头。

        2)大小写敏感。

        3)变量不需要预声明,变量类型赋值后初始化。

        2、数字

            int (有符号整数)

            long (长整数)

            bool (布尔值)

            float (浮点值)

            complex (复数)

        3、字符串

        单引号、双引号、三引号
    >>> mystring=
    'hipython'
    >>> mystring[
    0]
    'h'
    >>> mystring[
    1:3]
    'ip'
    >>> mystring[-
    1]
    'n'
    >>> mystring*
    3
    'hipythonhipythonhipython'
    >>>

          4列表和元组

    理解为数组
    列表用[],长度和元素可变;
    元组用(),长度和元素不可变;
    >>> mylist=[
    'abc',1,'d','123']
    >>> mylist.append(
    5)
    >>> mylist[
    4]
    5
    >>> myTuple=(
    'abc',1,'d','123')
    >>> myTuple[
    0]
    'abc'
    >>> myTuple[
    2]=3
    Traceback (most recent call last):
      File 
    "<interactive input>", line 1in <module>
    TypeError: 
    'tuple' object does not support item assignment
    >>>

          5字典

    理解为hashtablekey--value
    字典用{}
    >>> myDict={
    'name':'lyp','age':27}
    >>> myDict[
    'sex']='man'
    >>> myDict[
    'age']
    27
    >>> myDict[
    'sex']
    'man'

    七、流程控制

          1、条件判断

               if exp:

               elif  exp:

               else:

    >>> x=1
    >>> 
    if x>0:
         print 
    'true'
     
    else:
         print 
    'false'
         
    true

    >>> 
    if x>1:
         print 
    'x>1'
     elif x==
    1:
         print 
    'x=1'
     
    else:
         print 
    'x<1'
         
    x=
    1

          2while循环

    >>> while x<5:
         print x
         x+=
    1
         
    1
    2
    3
    4

          3for迭代 

           理解为foreach

    >>> for item in [1,2,3]:
         print item
         
    1
    2
    3
    >>> 
    for item in range(3):
         print item
         
    0
    1
    2
    >>> mystr=
    'hi'
    >>> 
    for item in range(len(mystr)):
         print mystr[item]
         
    h
    i

          4NB功能,列表解析

    >>> mylist=[x*2 for x in range(4)]
    >>> 
    for i in mylist:
         print i
         
    0
    2
    4
    6

    八、函数和类

          1、函数定义 def

    1)函数定义 def
    >>> def myFun1():
         print 
    'abc'
         
    >>> myFun1()
    abc
    >>> def myFun2(x,y):
         print x+y
         
    >>> myFun2(
    2,3)
    5
    2)使用默认参数
    >>> def myFun3(s=
    'str'):
         print s
         
    >>> myFun3()
    str
    >>> myFun3(
    '123')
    123
    >>>

          2、类定义 class

    (1)定义类
    >>> 
    class myClass:
         def show(self):
             print self.name
         def __init__(self):
             show(self)
    2)实例化类
    >>> myobj = myClass
    >>> myobj
    <
    class __main__.myClass at 0x012E9D80>
    3__init__相当与构造函数

    九、模块

    代码的组织形式,我理解为namespace

          1、导入一个模块

         
    >>> import sys

             2、访问模块函数

    >>> sys.stdout.write('hi python!')
    hi python!

     

             3、访问模块变量

    >>> sys.api_version
    1013

     

  • 相关阅读:
    查看unity打来的包在手机上面查看日志
    Unity 打包出来动态加载图片丢失问题
    嵌套列表拖拽事件冲突问题
    游戏中实现鼠标拖尾效果
    2048
    面试知识点积累
    ARM处理器架构理论知识
    計算機網絡知識點總結:
    collection
    demo002.链表操作
  • 原文地址:https://www.cnblogs.com/tenghoo/p/Python_django_summary.html
Copyright © 2011-2022 走看看