环境自行官网下载安装。
前提说明:
python是自上向下执行代码,有严格的缩进要求。
- #1.变量t
-
- t = 2 int类型
- t = '2' 字符串类型
- t = 2.2 float类型
- t = [2,3] 数组(列表)类型
- t = {'s':'v'} 字典类型
- python 声明变量不用指定类型 根据值得类型 具体详细解释自行百度
-
- #2.循环
-
- t = [2,3,4,5]
- #循环内容
- for i in t:
- print(i)
-
- #根据长度循环 len(t)可以替换成数字
- for i in range(len(t)):
- print(t[i])
-
- t = {'s':'v'}
- for k in t.keys():
- print(t.get(k))
-
- #3.条件判断
-
- t = 3
- if str(t).isdigit(): # str() 把t转成字符串,因为.isdigit()是字符串类型的函数
- print('t 是数字!')
-
- if t > 2:
- print('t 大于2')
-
差不多就这样了,之所以学python因为python开发效率太高了,同样的功能java声明一个类的代码行数就够python完成功能了。当然各有千秋,类似小工具的python效率极高,因为python封装了好多模块,直接调用模块就能完成大部分功能,模块其实就是提取出来的工具类。
例子:
- #发送post请求
- # !/usr/bin/python
- # -*- coding: utf-8 -*-
-
- import requests
-
- url = 'http://localhost:8083'
- body = '{"test":"test_value"}'
- header = {'Content-Type':'application/json','Host':'www.test.com','User-Agent':'Java/1.8.0'}
- response = requests.post(url=post_url,headers=header,json=body)
- print(response.status_code)
-
-
- #接受post请求
- # !/usr/bin/python
- # -*- coding: utf-8 -*-
- import json
-
- import uvicorn
- from fastapi import FastAPI, Body, Request
- from fastapi.middleware.cors import CORSMiddleware
-
- app = FastAPI()
-
-
- app.add_middleware(
- CORSMiddleware,
- allow_origins='*',
- allow_credentials=True,
- allow_methods=["GET", "POST"],
- allow_headers=["*"],
- )
-
- @app.post('/test')
- async def programInfo(request:Request):
- data = await request.body()
- print(json.loads(data)) # 转json格式
-
-
- if __name__ == '__main__':
- uvicorn.run(app=app,
- host="0.0.0.0",
- port=8083,
- workers=1)
-