菜鸟学PythonPython从入门到大神首页投稿(暂停使用,暂停投稿)

python优先级,文档字符串,模块,数据结构

2017-10-08  本文已影响174人  王中阳

python优先级由高到低排行

  1. 函数调用,寻址,下标
  2. 幂运算 **
  3. 翻转运算 ~
  4. 正负号 -
  5. 乘,除,取余 * / %
  6. 加减 + -
  7. 左右移位 << >>
  8. 按位与,按位或,按位异或 & | ^
  9. 比较运算符 < > <= >=

变量

  1. 默认为局部变量
  2. global是设置全局变量的关键字

文档字符串

  1. 用三引号包含内容
  2. 第一行标题
  3. 第二行空行
  4. 第三行及以后介绍
  5. 首行首字母大写
  6. 每行行尾用句号
  1. help(函数名称)
  2. 函数名称.doc

主模块和非主模块

  1. 如果一个模块的name属性值为main,这个模块是主模块,反之亦然。

dir()函数

数据结构概述

python的栈

# 定义栈

class Stack():
    def __init__(st,size):
        st.stack = [] #列表形式
        st.size = size
        st.top = -1

    # 填充栈
    def push(st,content):
        if st.Full(st):
            return "栈已满"
        else:
            st.stack.append(content)
            st.top = st.top+1
    # 移除栈
    def out(st):
        if st.Empty(st):
            return  "栈已空"
        else:
            st.top = st.top-1

    #判断充满
    def Full(st):
        if st.size == st.top:
            return True #首字母大写
        else:
            return False

    # 判断为空
    def Empty(st):
        if st.top == -1:
            print True
            return True
        else:
            return False

s =Stack(10) #初始化
s.push(s,'a')
s.Empty()
# 输出False
s.out(s)
s.Empty(s)
# 输出True

Python的队列

# 定义队列

class Queue():
    #初始化
    def __init__(qu,size):
        qu.queue = []
        qu.size = size
        qu.head = -1
        qu.tail = -1
    #判断为空
    def isEmpty(qu):
        if qu.tail == qu.head:
            return True
        else:
            return False
    #判断已满
    def isFull(qu):
        if qu.tail-qu.size+1 == qu.size:
            return True
        else:
            return False
    #进队列
    def push(qu,content):
        if qu.isFull():
            return '队列已满'
        else:
            qu.queue.append(content)
            qu.tail= qu.tail+1 #队尾指针加一
    #出队列
    def out(qu):
        if qu.isEmpty():
            return "队列已空"
        else:
            qu.head= qu.head+1 #队首指针加1

q = Queue(5)
print q.isEmpty() #返回True
q.push("a")
print q.isEmpty() #返回False
print q.isFull()  #返回False
q.out()
print  q.isEmpty() #返回True
上一篇 下一篇

猜你喜欢

热点阅读