笨办法学Python
打印:
%r
%r 与 %s 的区别就好比 repr() 函数处理对象与 str() 函数处理对象的差别。
%s => str(),比较智能
%r => repr(),处理较为简单和直接
from datetime import datetime
s = "world"
print("hello %s (str)" %s)
print("hello %r (repr)" %s)
timeinfo = datetime.today()
print(str(timeinfo))
print("time is %s (str)" %timeinfo)
print(repr(timeinfo))
print("time is %r (repr)" %timeinfo)
结果:
hello world (str)
hello 'world' (repr)
2016-10-28 11:37:28.554000
time is 2016-10-28 11:37:28.554000 (str)
datetime.datetime(2016, 10, 28, 11, 37, 28, 554000)
time is datetime.datetime(2016, 10, 28, 11, 37, 28, 554000) (repr)
%r, repr就是对string的一个格式化处理,即,返回的是一个格式化处理过的string(新对象)
argv,可变参数:
#argv,可变参数(argument variable)
from sys import argv
#参数解包(unpack),将参数放进对应的变量中
script, first, secind, third = argv
print("The script is called:",script)
print("Your first variable is:",first)
print("Your second variable is:",secind)
print("Your third variable is:",third)
终端输入:python test1.py a b c
结果:
('The script is called:', 'test1.py')
('Your first variable is:', 'a')
('Your second variable is:', 'b')
('Your third variable is:', 'c')
argv的第一个参数是文件名,所以接收一个参数都是argv[1]
读写文件
open( )打开文件
close( )关闭兼保存文件
read( )读取文件内容
readline( )读取一行内容
write( )写入内容
#with,简化了try…expect…finally,并且可以自动关闭文件
with open("xx.txt", "r") as f:
#read(),一次性将文件独进一个string中
print(f.read())
print(type(f.read()))
with open("xx.txt", "r") as f:
for line in f:
print(line)
#一行一行读取问价内容
列表
list_1 = ['a','b','b','c','d','e']
#pop(),删除最后一个元素
list_1.pop()
print(list_1)
#count(),统计这个元素
print(list_1.count("b"))
#remove(),删除指定元素
list_1.remove('b')
print(list_1)
#extend(),添加元素组或者当作两个列表相加,添加在原列表最后
list_1.extend(['q','t'])
print(list_1)
#append(),在列表最后添加元素
list_1.append('w')
print(list_1)
</span><span style="font-size:24px;">#insert(),元素插入指定位置
list_1.insert(1,'s')
print(list_1)
#reverse(),反向列表元素
list_1.reverse()
print(list_1)
#sort(),排序列表元素,可以有三个参数sorted(data, cmp=None, key=None, reverse=False),reverse参数是升序降序
list_1.sort()
print(list_1)
结果:
['a', 'b', 'b', 'c', 'd']
2
['a', 'b', 'c', 'd']
['a', 'b', 'c', 'd', 'q', 't']
['a', 'b', 'c', 'd', 'q', 't', 'w']
['a', 's', 'b', 'c', 'd', 'q', 't', 'w']
['w', 't', 'q', 'd', 'c', 'b', 's', 'a']
['a', 'b', 'c', 'd', 'q', 's', 't', 'w']
web.py
目录结构:
gothonweb
|----bin
|----app.py
第一个程序
编写app.py:
import web
urls = (
'/','index',
)
app = web.application(urls, globals())
class index:
def GET(self):
greeting = "Hello World"
return greeting
if __name__=="__main__":
app.run()
运行app.py
打开浏览器,http://127.0.0.1:8080/
控制台显示:
浏览器通过网络连接到127.0.0.1,连接成功后发起一个GET请求,访问URL / ,app.py中有URL与类的匹配关系,urls = ('/','index',) 中将'/'与index类联系在一起,web.py接收到访问/的请求会通过匹配关系找到index类,因为是一个GET请求所以会找到index.GET函数,程序执行这个函数返回响应给浏览器!
创建基本的模板文件
目录结构:
gothonweb
|----bin
|----app.py
|----templates
|----index.html
编写index.html
$def with(greeting)
<html>
<head>
<meta charset="UTF-8">
<title>Test</title>
</head>
<body>
$if greeting:
I just wanted to say<em style="color:green;font-size: 2em;">$greeting</em>.
$else:
<em>Hello</em>,World!
</body>
</html>
修改app.py
import web
urls = (
'/','index',
)
app = web.application(urls, globals())
render = web.template.render('templates/')
class index(object):
def GET(self):
greeting = "Hello World"
return render.index(greeting=greeting)
if __name__=="__main__":
app.run()
打开浏览器,http://127.0.0.1:8080/
在app.py中添加一个web.template.render对象,用来指定模板目录位置,当浏览器出发index.GET后,返回一个模板,render.index(greeting=greeting)是render.index是找模板目录下的index.html文件,将参数传入模板,模板中有一个对参数的判断,又来决定显示什么内容,再最终内容返回响应,显示到浏览器
表单(form)
单个参数:
修改app.py
import web
urls = (
'/','index',
)
app = web.application(urls, globals())
render = web.template.render('templates/')
class index(object):
def GET(self):
form = web.input(name="Nobody")
greeting = "Hello %s"%form.name
return render.index(greeting=greeting)
if __name__=="__main__":
app.run()
打开浏览器,http://127.0.0.1:8080/
打开浏览器,http://127.0.0.1:8080?name=web
多个参数;
修改app.py
import web
urls = (
'/','index',
)
app = web.application(urls, globals())
render = web.template.render('templates/')
class index(object):
def GET(self):
form = web.input(name="Nobody", greet=None)
if form.greet:
greeting = "Hello %s, %s"%(form.name, form.greet)
return render.index(greeting=greeting)
else:
return "ERROR:GREET IS REQUIRED!"
if __name__=="__main__":
app.run()
浏览器访问:http://127.0.0.1:8080/?name=web
浏览器访问:http://127.0.0.1:8080/?name=web&greet=python
创建HTML表单
目录结构:
gothonweb
|----bin
|----app.py
|----templates
|----index.html
|----hello_form.html
新建一个hello_form.html
<html>
<head>
<meta charset="UTF-8">
<title>Hello</title>
</head>
<body>
<form action="/" method="POST">
A Greeting:<input type="text" name="greet">
<br/>
Your Name:<input type="text" name="name">
<br/>
<input type="submit">
</form>
</body>
</html>
修改app.py
import web
urls = (
'/','index',
)
app = web.application(urls, globals())
render = web.template.render('templates/')
class index(object):
def GET(self):
return render.hello_form()
def POST(self):
form = web.input(name="Nobody", greet="Hello")
greeting = "%s, %s"%(form.name, form.greet)
return render.index(greeting=greeting)
if __name__=="__main__":
app.run()
浏览器访问http://127.0.0.1:8080/
控制台:
其实这里还能看出HTTP的连接是短连接,每次都是客户端的随机端口重新发起一次连接
浏览器访问/,发起一次GET请求,app.py中index.GET返回了hello_form.html,当填好表单以后,通过form,将POST请求发给了index.POST做处理,再返回响应给浏览器
创建布局模板(类似其它的模板继承)
目录结构:
gothonweb
|----bin
|----app.py
|----templates
|----index.html
|----hello_form.html
|----layout.html
修改index.html
def with(greeting)
$if greeting:
I just wanted to say<em style="color:green;font-size: 2em;">$greeting</em>.
$else:
<em>Hello</em>,World!
修改hello_form.html
<form action="/" method="POST">
A Greeting:<input type="text" name="greet">
<br/>
Your Name:<input type="text" name="name">
<br/>
<input type="submit">
</form>
新建layout.html
$def with(content)
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
$:content
</body>
</html>
修改app.py
import web
urls = (
'/','index',
)
app = web.application(urls, globals())
render = web.template.render('templates/', base="layout")
class index(object):
def GET(self):
return render.hello_form()
def POST(self):
form = web.input(name="Nobody", greet="Hello")
greeting = "%s, %s"%(form.name, form.greet)
return render.index(greeting=greeting)
if __name__=="__main__":
app.run()
笨办法学Python的更多相关文章
- 笨办法学 Python (Learn Python The Hard Way)
最近在看:笨办法学 Python (Learn Python The Hard Way) Contents: 译者前言 前言:笨办法更简单 习题 0: 准备工作 习题 1: 第一个程序 习题 2: 注 ...
- 笨办法学 Python (第三版)(转载)
笨办法学 Python (第三版) 原文地址:http://blog.sina.com.cn/s/blog_72b8298001019xg8.html 摘自https://learn-python ...
- 笨办法学Python - 习题1: A Good First Program
在windows上安装完Python环境后,开始按照<笨办法学Python>书上介绍的章节进行练习. 习题 1: 第一个程序 第一天主要是介绍了Python中输出函数print的使用方法, ...
- 笨办法学python 13题:pycharm 运行
笨办法学python 13题 代码: # -*- coding: utf-8 -*- from sys import argv # argv--argument variable 参数变量 scrip ...
- 笨办法学python - 专业程序员的养成完整版PDF免费下载_百度云盘
笨办法学python - 专业程序员的养成完整版PDF免费下载_百度云盘 提取码:xaln 怎样阅读本书 由于本书结构独特,你必须在学习时遵守几条规则 录入所有代码,禁止复制粘贴 一字不差地录入代码 ...
- 笨办法学Python 3|百度网盘免费下载|新手基础入门书籍
点击下方即可百度网盘免费提取 百度网盘免费下载:笨办法学Python 3 提取码:to27 内容简介: 本书是一本Python入门书,适合对计算机了解不多,没有学过编程,但对编程感兴趣的读者学习使用. ...
- 《笨办法学 Python(第四版)》高清PDF|百度网盘免费下载|Python编程
<笨办法学 Python(第四版)>高清PDF|百度网盘免费下载|Python编程 提取码:jcl8 笨办法学 Python是Zed Shaw 编写的一本Python入门书籍.适合对计算机 ...
- 笨办法学python 第四版 中文pdf高清版|网盘下载内附提取码
笨办法学 Python是Zed Shaw 编写的一本Python入门书籍.适合对计算机了解不多,没有学过编程,但对编程感兴趣的朋友学习使用.这本书以习题的方式引导读者一步一步学习编 程,从简单的打印一 ...
- 《笨办法学Python 3》python入门书籍推荐|附下载方式
<笨办法学Python 3>python入门书籍免费下载 内容简介 本书是一本Python入门书,适合对计算机了解不多,没有学过编程,但对编程感兴趣的读者学习使用.这本书以习题的方式引导读 ...
- 笨办法学Python (exercise1-15)
#exercise1print "Hello world!"print "Hello Again"print "I like typing this. ...
随机推荐
- xe.10.2的下载路径
为了这个玩意,我折腾了一天,为了以后自己还用到 官网地址: http://altd.embarcadero.com/download/radstudio/10.2/delphicbuilder10_2 ...
- php 函数篇
1.array_values($data); 注:将关联数组转化为索引数组 <?php $a=array("Name"=>"Bill"," ...
- JAVA如何实现中式排名和美式排名
根据公司需求,需要编写中式和美式排名算法,根据具体业务编写的,代码如下,看不懂留言,欢迎探讨,求高手指教更高效稳定的方法.private static int[] datas = {9,9,10,10 ...
- Python实现欧几里得算法
欧几里得算法的目标是找到两个数的最大公约数. 计算两个非负整数p和q的最大公约数:若q是0,则最大公约数为p.否则,将p除以q得到余数r,p和q的最大公约数即为q和r的最大公约数. def eucli ...
- Python知识点总结篇(四)
递归 特性 必须有明确的结束条件: 每进入深一层递归,问题规模比上层应有所减少: 递归效率不高,层次更多会导致栈溢出: def calc(n): print(n) if n // 2 > 0: ...
- vim安装 YCM 过程记录
YCM(YouComplateMe) 属于Vim中大神级的插件,提供了类似于巨硬爸爸的VS中的代码补全,但是其安装方式也是比较复杂,因此特意写下一篇记录,记录下我自己如何安装这一插件的过程: 检查自己 ...
- 阿里巴巴 Java 开发手册 (十二)安全规约
1. [强制]隶属于用户个人的页面或者功能必须进行权限控制校验. 说明:防止没有做水平权限校验就可随意访问.修改.删除别人的数据,比如查看他人的私信 内容.修改他人的订单. 2. [强制]用户敏感数据 ...
- 用lua求两个数组的交集、并集和补集。
-- 克隆 function Clone(object) local lookup_table = { } local function _copy(object) if type(object) ~ ...
- wamp环境下配置https证书后,网站内容访问受限
wamp环境下配置https证书后,网站内容访问受限,点击首页链接标签后报错,大致意思是没有权限进行操作. 解决方法:打开apache的http.conf(位置大致如下:项目所在目录\bin\apac ...
- Celery:First Steps
参考文档:http://docs.celeryproject.org/en/latest/getting-started/first-steps-with-celery.html#first-step ...