笨办法学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. ...
随机推荐
- Python之路【第十二篇】:Python面向对象高级
一.反射 1 什么是反射 反射的概念是由Smith在1982年首次提出的,主要是指程序可以访问.检测和修改它本身状态或行为的一种能力(自省).这一概念的提出很快引发了计算机科学领域关于应用反射性的研究 ...
- hadoop功能与用途
1.hadoop是什么? 开源的分布式存储和分布式计算平台. 2.hadoop组成? HDFS:分布式文件存储系统,存储海量数据. Mapreduce:并行处理框架,实现任务分配和调度. 3.hado ...
- golang基础学习-strings包常用函数学习
package main import ( "fmt" "strings" ) //StrFunc 字符串说明 func main() { var testSt ...
- linux端口映射
参考文章: http://jingyan.baidu.com/article/ed15cb1b2a332e1be36981ed.html http://www.myhack58.com/Article ...
- SpringBootSecurity学习(07)网页版登录整合JDBC
数据库中定义用户 前面我们定义用户是在配置文件和代码中定义死的默认用户,一般在开发中是不会这样做的,我们的用户都是来自我们的用户表,存储在数据库中.操作数据库的技术有很多,spring securit ...
- java之hiberante之集合映射之list映射
这篇讲解 集合映射之List映射 1.通常对于集合,在hibernate中的处理都是使用set来完成.但是hibernate也提供了对于其他几种集合的映射. 在这里实现List的映射,List是有序的 ...
- asp.net core 核心对象解析
首先声明这篇文章的所有内容均来自https://www.cnblogs.com/artech/p/inside-asp-net-core-framework.html ----感谢大内老A(artec ...
- ASP.NET MVC 页面静态化操作的思路
本文主要讲述了在asp.net mvc中,页面静态化的几种思路和方法.对于网站来说,生成纯html静态页面除了有利于seo外,还可以减轻网站的负载能力和提高网站性能.在asp.net mvc中,视图的 ...
- ASP.NET SignalR 系列(五)之群组推送
在上一章介绍了 一对一推送的方式,这章重点介绍下群组推送和多人推送 群组主要就是用到了方法:Groups.Add(Context.ConnectionId, groupName); 将不同的连接id加 ...
- go install -v github.com/gopherjs/gopherjs报错提示go cannot find package "golang.org/x/crypto/ssh/terminal" 解决方案
1前言 方法一:go get 方法二: github clone 2 方法方法一:go get go get golang.org/x/crypto/ssh/terminal 但是这种方法容易被墙,出 ...