一、模块初识:

Python有大量的模块,从而使得开发Python程序非常简洁。类库有包括三中:

  • Python内部提供的模块
  • 业内开源的模块
  • 程序员自己开发的模块

1、Python内部提供一个 sys 的模块,

① 其中的 sys.argv 用来捕获执行执行python脚本时传入的参数:

import sys

strings = sys.argv
print(strings) # 所有参数 类型为列表
# ['start.py', 'hello', 'world']
print(strings[0]) # 脚本名本身
# start.py
print(strings[1]) # 第一个参数
# hello
print(strings[2]) # 第二个参数
# world
print(strings[-1]) # 倒数第一个参数
# world $ python test.py helo world 执行脚本

② sys.stdin信息输入

读取的文件:

我越无所适从
越会事与愿违
在交错的时空
灵魂加速下坠
Here we are, here we are, here we are

lzl.txt

程序start.py:

import sys
li = []
file = sys.stdin
# <_io.TextIOWrapper name='<stdin>' mode='r' encoding='cp936'> file
for line in file:
li.append(line)
print(li)
print(line.strip()) # 这个很重要 strip去除末尾的换行符 本身print跟print直接回产生换行符
# 如果不进去去除末尾的换行符的话 会再多打印一行换行符
li =[]

执行程序: < 输入的文件  > 脚本打印的输出

python start.py < lzl.txt >test.txt

生成的文件test.txt:

['我越无所适从\n']
我越无所适从
['越会事与愿违\n']
越会事与愿违
['在交错的时空\n']
在交错的时空
['灵魂加�\udc80�下�\udca0\n']
灵魂加速下坠
['Here we are, here we are, here we are']
Here we are, here we are, here we are

test.txt

sys.stdout 重定向输出

import sys

sys.stdout.write("asdgf") # 当我们在 Python 中打印对象调用 print obj 时候,事实上是调用了 sys.stdout.write(obj+'\n')
# asdgf

④ sys.stdout.flush 强制刷新标准输出缓存,举网上泛传的一个例子,python3中楼主没有看出其中的效果,单本楼主之前学多进程时看到过效果,那个例子找不到了;就暂且认为下面的例子看出效果了吧

import time
import sys
for i in range(5):
print(i),
sys.stdout.flush()
time.sleep(1)
# 这样设计是为了打印一个数每秒五秒钟,但如果您运行它,因为它是现在(取决于您的默认系统缓冲),
# 你可能看不到任何输出 CodeGo.net,直到再一次全部,你会看到0 1 2 3 4打印到屏幕上。
# 这是输出被缓冲,除非你sys.stdout之后每print你不会看到从输出中取出sys.stdout.flush()网上看到的差别

  

2、os模块,os模块与系统进行交互

① os.dir、os.popen调用当前系统命令:

#!/usr/bin/env python
# -*- coding:utf-8 -*-
#-Author-Lian
import os #调用os模块
res = os.system("dir") #只执行命令,不保存结果
#������ C �еľ�û�б�ǩ�� #打印dir命令的结果
#�������� 50FA-4D57
#
#C:\Users\L\PycharmProjects\s14\Day2 ��Ŀ¼
#2016/08/04 10:53 <DIR> .
#2016/08/04 10:53 <DIR> ..
#2016/08/04 10:53 147 module.py
# 1 ���ļ� 147 �ֽ�
# 2 ��Ŀ¼ 35,291,541,504 �����ֽ�
print(res) #res只返会执行命令的成功与否,成功为0 反之为1
#0 res = os.popen("dir") #不打印输出,保存执行结果
print(res) #打印res内存地址信息
#<os._wrap_close object at 0x009BE570>
res = os.popen("dir").read() #通过read去读取内存地址记录的信息
print(res)
#驱动器 C 中的卷没有标签。
#卷的序列号是 50FA-4D57
#
#C:\Users\L\PycharmProjects\s14\Day2 的目录
#
#2016/08/04 11:00 <DIR> .
#2016/08/04 11:00 <DIR> ..
#2016/08/04 11:00 1,002 module.py
# 1 个文件 1,002 字节
# 2 个目录 35,291,017,216 可用字节 os.mkdir("new_dir") #在当前目录下创建新的目录new_dir

②、两者结合使用: 

import os,sys

os.system(''.join(sys.argv[1:])) #把用户的输入的参数当作一条命令交给os.system来执行

3、platform识别当前运行的系统

① 根据不同的系统创建文件的绝对路径(linux系统路径为/,windows系统路径为\)

import os,sys
import platform if platform.system() == 'Windows': #windows系统
BASE_DIR = '\\'.join(os.path.abspath(os.path.dirname(__file__)).split('\\')[:-1]) # 取当前文件目录的父目录
#school_dbpaths = os.path.join(BASE_DIR,'school_db') # 如果只要当前目录则不用split
sys.path.insert(0, BASE_DIR) #加入到系统路径 else:
BASE_DIR = '/'.join(os.path.abspath(os.path.dirname(__file__)).split('/')[:-1])
school_dbpaths =os.path.join(BASE_DIR, 'school_db') print(school_dbpaths)
# C:\Users\L\PycharmProjects\s14\homework\Day_11\rpc_client\school_db

  

二、运算符:

1、算数运算:

2、比较运算:

3、赋值运算:

4、逻辑运算:

5、成员运算:

6、身份运算:

7、位运算:

#!/usr/bin/python

a = 60            # 60 = 0011 1100
b = 13 # 13 = 0000 1101
c = 0 c = a & b; # 12 = 0000 1100
print "Line 1 - Value of c is ", c c = a | b; # 61 = 0011 1101
print "Line 2 - Value of c is ", c c = a ^ b; # 49 = 0011 0001
print "Line 3 - Value of c is ", c c = ~a; # -61 = 1100 0011
print "Line 4 - Value of c is ", c c = a << 2; # 240 = 1111 0000
print "Line 5 - Value of c is ", c c = a >> 2; # 15 = 0000 1111
print "Line 6 - Value of c is ", c

8、运算符优先级:

点击查看更多内容:Python运算符

Python开发【第二章】:Python模块和运算符的更多相关文章

  1. [Python笔记][第二章Python序列-复杂的数据结构]

    2016/1/27学习内容 第二章 Python序列-复杂的数据结构 堆 import heapq #添加元素进堆 heapq.heappush(heap,n) #小根堆堆顶 heapq.heappo ...

  2. [Python笔记][第二章Python序列-tuple,dict,set]

    2016/1/27学习内容 第二章 Python序列-tuple tuple创建的tips a_tuple=('a',),要这样创建,而不是a_tuple=('a'),后者是一个创建了一个字符 tup ...

  3. [python笔记][第二章Python序列-list]

    2016/1/27学习内容 第二章 Python序列-list list常用操作 list.append(x) list.extend(L) list.insert(index,x) list.rem ...

  4. 路飞学城-Python开发-第二章

    ''' 数据结构: menu = { '北京':{ '海淀':{ '五道口':{ 'soho':{}, '网易':{}, 'google':{} }, '中关村':{ '爱奇艺':{}, '汽车之家' ...

  5. Python笔记·第二章—— Python的编码问题(一)

    一.什么是编码 可以说,计算机是一个即聪明又笨蛋的家伙.说它聪明,是因为他可以做很多事情,它的强大无需多说,大家应该都有所了解以及感受.但是为什么说它又是个笨蛋呢,因为我们在电脑上写出的每一个字,保存 ...

  6. Python自学:第二章 Python之禅

    >>print import <Python之禅>,提姆·彼得斯著 美胜于丑. 显式优于隐式. 简单胜于复杂. 复杂总比复杂好. 平的比嵌套的好. 稀疏胜于稠密. 可读性计数. ...

  7. Python第五章__模块介绍,常用内置模块

    Python第五章__模块介绍,常用内置模块 欢迎加入Linux_Python学习群  群号:478616847 目录: 模块与导入介绍 包的介绍 time &datetime模块 rando ...

  8. 简学Python第五章__模块介绍,常用内置模块

    Python第五章__模块介绍,常用内置模块 欢迎加入Linux_Python学习群  群号:478616847 目录: 模块与导入介绍 包的介绍 time &datetime模块 rando ...

  9. 第二章Python入门

    第二章 Python入门 2.1.简介 Python是著名的"龟叔"(Guido van Rossum)在1989年圣诞节期间,为了打发无聊的圣诞节而编写的一个编程语言 Pytho ...

  10. visual studio 2015 搭建python开发环境,python入门到精通[三]

    在上一篇博客Windows搭建python开发环境,python入门到精通[一]很多园友提到希望使用visual studio 2013/visual studio 2015 python做demo, ...

随机推荐

  1. ajax、post、get实例

    html代码: <!DOCTYPE HTML><html lang="en-US"><head> <meta charset=" ...

  2. 利用mysqldump 将一个表按条件导出数据

    mysqldump -uroot -pdsideal -t dsideal_db t_resource_info --where="res_type=1 and group_id=1 and ...

  3. Coder-Strike 2014 - Finals (online edition, Div. 2) B. Start Up

    需要满足的条件是 (1)每个字母是对称的 (2)每个字符串是对称的 #include <iostream> #include <algorithm> #include < ...

  4. [BZOJ2791][Poi2012]Rendezvous

    2791: [Poi2012]Rendezvous Time Limit: 25 Sec  Memory Limit: 128 MBSubmit: 95  Solved: 71[Submit][Sta ...

  5. UpdatePanel完成后调用js

    引言: asp.net 微软引入了UpdatePanel 使用起来很方便 如果 我们想UpdatePanel加载完成后做一些事情 需要使用js <script type="text/j ...

  6. Flash与JS之间相互调用以及参数传递

    [AS3]ExternalInterface.call传多个参数的写法代码示例 import flash.text.TextField; ; ; var result:uint = ExternalI ...

  7. 【转】在C#中使用SendMessage

    SendMessage是一个在user32.dll中声明的API函数,在C#中导入如下: using System.Runtime.InteropServices; [DllImport(" ...

  8. 内网网段划分ciso交换机配置

    内网3750交换机配置: vlan 192 192.168.101.0/24 端口: 1--8vlan 10 10.10.10.0/24 端口: 9--16vlan 172 172.16.172.2/ ...

  9. vue 一些开发姿势

    .vue : <template></template><script></script> .js :import Vue from 'vue'

  10. PHP如何通过SQL语句将数据写入MySQL数据库呢?

    1,php和MySQL建立连接关系 2,打开 3,接受页面数据,PHP录入到指定的表中 1.2两步可直接使用一个数据库链接文件即可:conn.php <?phpmysql_connect(&qu ...