笨办法学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之路【第十五篇】开发FTP多线程程序
要求: 1.用户加密认证 2.允许同时多用户登录 3.每个用户有自己的家目录,且只能访问自己的家目录 4.对用户进行磁盘配额,每个用户的可用空间不同 5.允许用户在ftp server上随意切换目录 ...
- 【JVM】3、jvm参数和main方法参数
在实际应用中,我们经常会使用一些额外的参数定义不通的环境下jvm的启动设置 特别是springCloud的项目,因为yml配置文件的问题,如果我们要做负载的话,会同时启动一个jar当做2个服务 那么这 ...
- Actions require unique method/path combination for Swagger
原文:Actions require unique method/path combination for Swagger services.AddSwaggerGen (c => { c.Re ...
- 为 WPF 程序添加 Windows 跳转列表的支持
原文:为 WPF 程序添加 Windows 跳转列表的支持 Windows 跳转列表是自 Windows 7 时代就带来的功能,这一功能是跟随 Windows 7 的任务栏而发布的.当时应用程序要想用 ...
- 利用HashMap计算一个字符串中每个字符出现的次数
问题描述:计算一个字符串中每个字符出现的次数 问题分析:每个字符串对应着它的次数,且字符串唯一不重复,这让我们想到了HashMap中的键值对. 1.使用Scanner获取字符串 2.遍历字符串,获取每 ...
- hadoop细节 -> 持续更新
Hdfs: hdfs写流程: 客户端通过DistributedFileSystem请求namenode上传文件 Namenode进行检查,比如父路径 文件本身,是否允许上传 Namenode相应信 ...
- 【开发工具】- myeclipse安装主题
你想用IDEA那样炫酷的符合90后气质的主题吗?废话不多说,按照下边步骤就可以安装像IDEA一样超级炫酷的主题. 下载主题 1.进入插件官网(http://eclipsecolorthemes.org ...
- 82.使用vue后怎么针对搜索引擎做SEO优化?
什么是SEO 搜索引擎优化(Search engine optimization,简称SEO),指为了提升网页在搜索引擎自然搜索结果中(非商业性推广结果)的收录数量以及排序位置而做的优化行为,是为了从 ...
- Java 之 MyBatis(一)入门
一.Mybatis 框架概述 (1)mybatis 是一个优秀的基于 java 的持久层框架,它内部封装了 jdbc,使开发者只需要关注 sql 语句本身,而不需要花费精力去处理加载驱动.创建连接.创 ...
- C# EF框架 频繁连接性能损耗
目的 测试EF框架在一次连接中多次保存和多次连接的耗时对比 测试环境 数据库SqlServer 2012 R2 EF框架6.2.0版本 数据库内容 ID UserName Password Creat ...