Python初学1
windows版python下载:
https://pan.baidu.com/s/1dsAPp0C9PJUF73kFDdAzXQ
安装时勾选pip和Add python.exe to Path。
windows版pycharm下载:
https://pan.baidu.com/s/1DV81hxSsodtukUHNW-Bzgg
一些基础知识:
1、if __name__ == '__main__'的意思是:
当.py文件被直接运行时,if __name__ == '__main__'之下的代码块将被运行;当.py文件以模块形式被导入时,if __name__ == '__main__'之下的代码块不被运行。
2、在python执行py文件
- import sys #引入sys库体
- sys.path.append("C://myPython") #往系统路径中加入自己存放py文件的地址
然后就可以开始通过import的方法导入相关的方法和内容了 - from test import * #从test.py文件中加载所有的内容。
文件读写:
with open(r'f:\test\t1.txt','r') as f:
print f.read() 读
f.close() 关闭
with相当于try...catch;
按行读:
with open(r'f:\test\t1.txt','r') as f:
for l in f.readlines():
print l.strip()
f.close()
写文件:
with open(r'f:\test\t1.txt','w') as f:
f.write('abcde')
f.close()
序列化:
import cPickle as pickle 引入包
with open(r'f:\test\t1.txt', 'wb') as f:
dic = 10
pickle.dump(dic, f)
f.close()
with open(r'f:\test\t1.txt', 'rb') as f:
aa = pickle.load(f)
print(aa)
f.close()
多线程:
import random
import time,threading
def thread_run(urls):
for url in urls:
print "%s ---->>>> %s"%(threading.current_thread().name,url)
time.sleep(random.random())
print "thread %s end..."%threading.current_thread().name
t1 = threading.Thread(target=thread_run,name="Thread_1",args=(["url_1","url_2","url_3"],))
t2 = threading.Thread(target=thread_run,name="Thread_1",args=(["url_4","url_5","url_6"],))
t1.start()
t2.start()
t1.join()
t2.join()
print "end....."
//join是主线程等待子线程
import random
import threading
import time
class myThread(threading.Thread):
def __init__(self,name,urls):
threading.Thread.__init__(self,name=name)
self.urls = urls def run(self):
for url in self.urls:
print "%s ---->>> %s" %(threading.current_thread().name,url)
print "%s ended..."%threading.current_thread().name t1 = myThread(name="Thread_1",urls=["url_1","url_2","url_3"])
t2 = myThread(name="Thread_2",urls=["url_4","url_5","url_6"])
t1.start()
t2.start()
t1.join()
t2.join()
print "%s end...." %threading.current_thread().name
线程同步:
import random
import threading
import time
mylock = threading.RLock()
class myThread(threading.Thread):
def __init__(self,name,urls):
threading.Thread.__init__(self,name=name)
self.urls = urls def run(self):
for url in self.urls:
mylock.acquire()
print "%s ---->>> %s --" %(threading.current_thread().name,url)
mylock.release() t1 = myThread(name="Thread_1",urls=["url_1","url_2","url_3"])
t2 = myThread(name="Thread_2",urls=["url_4","url_5","url_6"])
t1.start()
t2.start()
t1.join()
t2.join()
print "%s end...." %threading.current_thread().name
Python初学1的更多相关文章
- 孤荷凌寒自学python第四十五天Python初学基础基本结束的下阶段预安装准备
孤荷凌寒自学python第四十五天Python初学基础基本结束的下阶段预安装准备 (完整学习过程屏幕记录视频地址在文末,手写笔记在文末) 今天本来应当继续学习Python的数据库操作,但根据过去我自 ...
- Python初学笔记之字符串
一.字符串的定义 字符串是就一堆字符,可以使用""(双引号).''(单引号)来创建. 1 one_str = "定义字符串" 字符串内容中包含引号时,可以使用转 ...
- Python初学的易犯错误
当初学 Python 时,想要弄懂 Python 的错误信息的含义可能有点复杂.这里列出了常见的的一些让你程序 crash 的运行时错误. 1)忘记在 if , elif , else , for , ...
- Python初学
经同学推荐,学习了下Python语言,看Python的介绍,它本身是一个面向对象的解释型脚本语言,我初看到这句话的时候就在想,一个脚本语言还搞成面向对象?有这个必要么?原谅我肤浅了一把. 它还被俗称为 ...
- python 初学笔记 (一)
初学python第一天,希望自己真正了解计算机语言,并且做出成效. 写下学习笔记,记录学习进度,娱乐学习,不断成长. python详细介绍: python是什么?运用到哪里?有哪些在使用它? pyth ...
- Python初学(1)
最近在学习python,以后想编写一些工作中用的到的脚本.python的入门我选择了<python从初学到入门>,这篇文章我会跟进我的学习进度.算是一个笔记吧. 我本身是熟悉C语言的,看p ...
- python初学心得之一
昨天开始接触并学习python,对python有了初步印象. 一.python主要应用方向 二.python语言类型 三.python2和3的主要区别 四.常见字符编码 五.Python语法初学 一 ...
- python初学杂记
python常用命令: 1.python 或者 python3 打开交互式python解释器 2.python hello.py 通过命令提示符运行python脚本 交互式python解释器常用 ...
- Mac下python初学之Image库(PIL)
Mac下python 使用Image库 安装PIL,下载http://www.pythonware.com/products/pil/ 解压PIL源码包,阅读README知道需要使用python se ...
- python初学day01
1.执行Python脚本时打印的字符有颜色 1. print "\033[32;1mhello\033[0m" #打印绿色 2. print "\033[31;1mhel ...
随机推荐
- 用XAMPP+Wordpress搭建个人博客
http://biancheng.dnbcw.info/php/456308.html http://jingyan.baidu.com/article/f71d60376ba9571ab641d11 ...
- P1082||T1200 同余方程 codevs|| 洛谷
时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond http://codevs.cn/problem/1200/||https://www.luogu.o ...
- Nginx源码分析:3张图看懂启动及进程工作原理
编者按:高可用架构分享及传播在架构领域具有典型意义的文章,本文由陈科在高可用架构群分享.转载请注明来自高可用架构公众号「ArchNotes」. 导读:很多工程师及架构师都希望了解及掌握高性能服务器 ...
- java(JSP)中几种获取项目路径方式
在jsp和class文件中调用的相对路径不同. 在jsp里,根目录是WebRoot 在class文件中,根目录是WebRoot/WEB-INF/classes 当然你也可以用System.getPro ...
- webview 播放H5视频问题 黑屏 只有声音没有画面
android 用webview 播放网络视频怎控制播放按键? 在代码中加入webview.getSettings().setJavaScriptEnabled(true);//支持jswebview ...
- JSP页面规格化
http://doc.okbase.net/%E4%BA%BA%E7%94%9F%E9%9A%BE%E5%BE%97%E7%B3%8A%E6%B6%82/archive/123084.html htt ...
- B1877 [SDOI2009]晨跑 费用流
其实之前写过一个板子,但是一点印象都没有,所以今天重写了一下,顺便把这个题当成板子就行了. 其实费用流就是把bfs换成spfa,但是中间有一个原则,就是费用优先,在费用(就是c)上跑spfa,顺便求出 ...
- shell脚本-数组
shell脚本-数组 数组 变量:存储单个元素的内存空间. 数组:存储多个元素的连续的内存空间,相当于多个变量的集合. 数组索引:编号从0开始,属于数值索引.索引可支持使用自定义的格式,而不仅是数值格 ...
- AngularJS过滤器filter-保留小数-渲染页面-小数点-$filter
AngularJS 保留小数 默认是保留3位 固定的套路是 {{deom | number:4}} 意思就是保留小数点 的后四位 在渲染页面的时候 加入这儿个代码 用来精确浮点数,指定小数点 ...
- A Few Words on Callbacks and Asynchronous Mechanism In Javascript
It is said that the one of the most routine work a javascript programmer do is writing codes like &q ...