打印:
%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的更多相关文章

  1. 笨办法学 Python (Learn Python The Hard Way)

    最近在看:笨办法学 Python (Learn Python The Hard Way) Contents: 译者前言 前言:笨办法更简单 习题 0: 准备工作 习题 1: 第一个程序 习题 2: 注 ...

  2. 笨办法学 Python (第三版)(转载)

    笨办法学 Python (第三版) 原文地址:http://blog.sina.com.cn/s/blog_72b8298001019xg8.html   摘自https://learn-python ...

  3. 笨办法学Python - 习题1: A Good First Program

    在windows上安装完Python环境后,开始按照<笨办法学Python>书上介绍的章节进行练习. 习题 1: 第一个程序 第一天主要是介绍了Python中输出函数print的使用方法, ...

  4. 笨办法学python 13题:pycharm 运行

    笨办法学python 13题 代码: # -*- coding: utf-8 -*- from sys import argv # argv--argument variable 参数变量 scrip ...

  5. 笨办法学python - 专业程序员的养成完整版PDF免费下载_百度云盘

    笨办法学python - 专业程序员的养成完整版PDF免费下载_百度云盘 提取码:xaln  怎样阅读本书 由于本书结构独特,你必须在学习时遵守几条规则 录入所有代码,禁止复制粘贴 一字不差地录入代码 ...

  6. 笨办法学Python 3|百度网盘免费下载|新手基础入门书籍

    点击下方即可百度网盘免费提取 百度网盘免费下载:笨办法学Python 3 提取码:to27 内容简介: 本书是一本Python入门书,适合对计算机了解不多,没有学过编程,但对编程感兴趣的读者学习使用. ...

  7. 《笨办法学 Python(第四版)》高清PDF|百度网盘免费下载|Python编程

    <笨办法学 Python(第四版)>高清PDF|百度网盘免费下载|Python编程 提取码:jcl8 笨办法学 Python是Zed Shaw 编写的一本Python入门书籍.适合对计算机 ...

  8. 笨办法学python 第四版 中文pdf高清版|网盘下载内附提取码

    笨办法学 Python是Zed Shaw 编写的一本Python入门书籍.适合对计算机了解不多,没有学过编程,但对编程感兴趣的朋友学习使用.这本书以习题的方式引导读者一步一步学习编 程,从简单的打印一 ...

  9. 《笨办法学Python 3》python入门书籍推荐|附下载方式

    <笨办法学Python 3>python入门书籍免费下载 内容简介 本书是一本Python入门书,适合对计算机了解不多,没有学过编程,但对编程感兴趣的读者学习使用.这本书以习题的方式引导读 ...

  10. 笨办法学Python (exercise1-15)

    #exercise1print "Hello world!"print "Hello Again"print "I like typing this. ...

随机推荐

  1. golang(二)

    基本结构和基本数据类型 指针 不像 Java 和 .NET,Go 语言为程序员提供了控制数据结构的指针的能力:但是,你不能进行指针运算.通过给予程序员基本内存布局,Go 语言允许你控制特定集合的数据结 ...

  2. Android apk逆向:反编译,回编译,签名,打包。

    Android apk逆向:反编译,回编译,签名,打包流程. 第一步: apk 反编译. 1) 打开命令行窗口,输入java -version, 检测当前java版本,若版本较低, 则下载JAVA S ...

  3. emmet 配置文件

    snippets.json(添加自己的或更新现有的片段) preferences.json(更改某些Emmet过滤器和操作的行为) SyntaxProfiles.json(定义生成的HTML / XM ...

  4. (转)消息队列 Kafka 的基本知识及 .NET Core 客户端

    原文地址:https://www.cnblogs.com/savorboard/p/dotnetcore-kafka.html 前言 最新项目中要用到消息队列来做消息的传输,之所以选着 Kafka 是 ...

  5. 百度搜索常用api

    http://www.baidu.com/s?wd=关键字 wd(Keyword):查询的关键词:http://www.baidu.com/s?wd=关键字&cl=3 cl(Class):搜索 ...

  6. python程序打包exe文件

    第一步:安装依赖 pip install pyinstaller 第二步:移动文件 把python文件和一个exe拿来当图标的ico文件移动到同一文件中 注意:文件路径不用用中文!!!!!!!! 第三 ...

  7. 自学Python编程的第\七天----------来自苦逼的转行人

    2019-09-17-23:09:48 今天学的内容是有关小数据池的,学的有点懵逼,感觉越来越难学了,但是得坚持下去 明天学习下一个课程时,感觉要跟不上,看来明天得先看好几遍今天的内容 不然肯定会听的 ...

  8. 1+X证书学习日志——javascript基础

    js javascript js的组成: ECMAscript DOM BOM js放置的位置 <script></script> <script src="路 ...

  9. FlaskCBV视图类

    路由视图类 from flask import Flask app = Flask(name) 视图类 Views文件 看views源码 继承最后一个类 导入CBV的视图基类 from flask i ...

  10. 魅族手机使用应用沙盒一键修改imsi数据

    较早前文章介绍了怎么在安卓手机上安装激活XPosed框架,XPosed框架的牛逼之处功能各位都介绍过,可以不修改apk的前提下,修改系统内核的参数,打比方在某些应用领域,各位需要修改手机的某个系统参数 ...