1、创建变量
a=10
b=2
c=a+b
print(c)
2、判断语句
# coding=utf-8
a = 90
if a > 80:
print("nice")
elif a > 60:
print("normal")
else:
print("bad")
***# coding=utf-8***用于指定中文的编码格式
3、循环
# coding=utf-8
for i in range(1, 100):
print("iterm {0},{1}".format(i, "hello"))
iterm是静态的拼接字符串,{1}、{0}就是需要动态输入的字符串。
4、函数定义
def hello():
print("hello")
def max(a,b):
if a > b:
print(a)
else:
print (b)
hello()
max(2,3)
5、面向对象
# coding=utf-8
class hello:
#构造函数
def __init__(self,name):
self._name=name
def sayhello(self):
print("hello {0}".format(self._name))
#继承
class saybye(hello):
def __init__(self, name):
hello.__init__(self, name)
def bye(self):
print ("bye {0} ".format(self._name))
h=hello("zhangsan")
h.sayhello()
j=saybye("lisi")
j.bye()
6、导入文件
# import classdemo
#
# h=classdemo.saybye("lisi")
# h.bye()
from classdemo import saybye
j=saybye("wangwu")
j.bye()
7、创建web2py项目
创建web2py创建完成后,右上角确定工程名称,点击运行
设置端口号、管理员密码,点击start server
成功进入项目入口
直接生成了一个完整的web项目,甚至包括了登录注册,很是方便
8、静态文件处理
静态文件html、imag等都存放于static文件夹下,可以通过浏览器url直接访问。
直接访问到文件
9、编写一个控制器(controller)
在web项目中新建一个main包,编写一个hello.py的文件
# coding=utf-8
def index():
return " hello "
通过浏览器:http://127.0.0.1:8000/main/hello可直接访问,index为默认访问方法,也可直接指定指定方法。