{ "cells": [ { "cell_type": "code", "execution_count": 4, "id": "c7b3e794-2bb7-41a6-9b90-28a8ad61b644", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n", "1\n", "2\n", "3\n", "4\n", "5\n", "6\n", "7\n", "8\n", "9\n" ] } ], "source": [ "for i in range(10):\n", " print(i)" ] }, { "cell_type": "markdown", "id": "c748202a-4a95-4b1b-8ba1-7ebb106c2e99", "metadata": {}, "source": [ "## 2.1 嵌套" ] }, { "cell_type": "markdown", "id": "e02f6691-4293-4d8a-895b-6618fc51f9ae", "metadata": {}, "source": [ "- fo嵌套\n", "- for if嵌套" ] }, { "cell_type": "code", "execution_count": 17, "id": "eaf26468-451a-43d4-b947-1064f8d1ae47", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " \u001b[31m*\u001b[0m \n", " \u001b[31m*\u001b[0m\u001b[31m*\u001b[0m\u001b[31m*\u001b[0m \n", " \u001b[31m*\u001b[0m\u001b[31m*\u001b[0m\u001b[31m*\u001b[0m\u001b[31m*\u001b[0m\u001b[31m*\u001b[0m \n", " \u001b[31m*\u001b[0m\u001b[31m*\u001b[0m\u001b[31m*\u001b[0m\u001b[31m*\u001b[0m\u001b[31m*\u001b[0m\u001b[31m*\u001b[0m\u001b[31m*\u001b[0m \n", " \u001b[31m*\u001b[0m\u001b[31m*\u001b[0m\u001b[31m*\u001b[0m\u001b[31m*\u001b[0m\u001b[31m*\u001b[0m\u001b[31m*\u001b[0m\u001b[31m*\u001b[0m\u001b[31m*\u001b[0m\u001b[31m*\u001b[0m \n", " \u001b[31m*\u001b[0m\u001b[31m*\u001b[0m\u001b[31m*\u001b[0m\u001b[31m*\u001b[0m\u001b[31m*\u001b[0m\u001b[31m*\u001b[0m\u001b[31m*\u001b[0m\u001b[31m*\u001b[0m\u001b[31m*\u001b[0m\u001b[31m*\u001b[0m\u001b[31m*\u001b[0m \n", " \u001b[31m*\u001b[0m\u001b[31m*\u001b[0m\u001b[31m*\u001b[0m\u001b[31m*\u001b[0m\u001b[31m*\u001b[0m\u001b[31m*\u001b[0m\u001b[31m*\u001b[0m\u001b[31m*\u001b[0m\u001b[31m*\u001b[0m\u001b[31m*\u001b[0m\u001b[31m*\u001b[0m\u001b[31m*\u001b[0m\u001b[31m*\u001b[0m \n", " \u001b[31m*\u001b[0m\u001b[31m*\u001b[0m\u001b[31m*\u001b[0m\u001b[31m*\u001b[0m\u001b[31m*\u001b[0m\u001b[31m*\u001b[0m\u001b[31m*\u001b[0m\u001b[31m*\u001b[0m\u001b[31m*\u001b[0m\u001b[31m*\u001b[0m\u001b[31m*\u001b[0m\u001b[31m*\u001b[0m\u001b[31m*\u001b[0m\u001b[31m*\u001b[0m\u001b[31m*\u001b[0m \n", " \u001b[31m*\u001b[0m\u001b[31m*\u001b[0m\u001b[31m*\u001b[0m\u001b[31m*\u001b[0m\u001b[31m*\u001b[0m\u001b[31m*\u001b[0m\u001b[31m*\u001b[0m\u001b[31m*\u001b[0m\u001b[31m*\u001b[0m\u001b[31m*\u001b[0m\u001b[31m*\u001b[0m\u001b[31m*\u001b[0m\u001b[31m*\u001b[0m\u001b[31m*\u001b[0m\u001b[31m*\u001b[0m\u001b[31m*\u001b[0m\u001b[31m*\u001b[0m \n", " \u001b[31m*\u001b[0m\u001b[31m*\u001b[0m\u001b[31m*\u001b[0m\u001b[31m*\u001b[0m\u001b[31m*\u001b[0m\u001b[31m*\u001b[0m\u001b[31m*\u001b[0m\u001b[31m*\u001b[0m\u001b[31m*\u001b[0m\u001b[31m*\u001b[0m\u001b[31m*\u001b[0m\u001b[31m*\u001b[0m\u001b[31m*\u001b[0m\u001b[31m*\u001b[0m\u001b[31m*\u001b[0m\u001b[31m*\u001b[0m\u001b[31m*\u001b[0m\u001b[31m*\u001b[0m\u001b[31m*\u001b[0m \n", "\u001b[31m*\u001b[0m\u001b[31m*\u001b[0m\u001b[31m*\u001b[0m\u001b[31m*\u001b[0m\u001b[31m*\u001b[0m\u001b[31m*\u001b[0m\u001b[31m*\u001b[0m\u001b[31m*\u001b[0m\u001b[31m*\u001b[0m\u001b[31m*\u001b[0m\u001b[31m*\u001b[0m\u001b[31m*\u001b[0m\u001b[31m*\u001b[0m\u001b[31m*\u001b[0m\u001b[31m*\u001b[0m\u001b[31m*\u001b[0m\u001b[31m*\u001b[0m\u001b[31m*\u001b[0m\u001b[31m*\u001b[0m\u001b[31m*\u001b[0m\u001b[31m*\u001b[0m\n", " \u001b[31m*\u001b[0m\u001b[31m*\u001b[0m\u001b[31m*\u001b[0m\u001b[31m*\u001b[0m\u001b[31m*\u001b[0m\u001b[31m*\u001b[0m\u001b[31m*\u001b[0m\u001b[31m*\u001b[0m\u001b[31m*\u001b[0m\u001b[31m*\u001b[0m\u001b[31m*\u001b[0m\u001b[31m*\u001b[0m\u001b[31m*\u001b[0m\u001b[31m*\u001b[0m\u001b[31m*\u001b[0m\u001b[31m*\u001b[0m\u001b[31m*\u001b[0m\u001b[31m*\u001b[0m\u001b[31m*\u001b[0m \n", " \u001b[31m*\u001b[0m\u001b[31m*\u001b[0m\u001b[31m*\u001b[0m\u001b[31m*\u001b[0m\u001b[31m*\u001b[0m\u001b[31m*\u001b[0m\u001b[31m*\u001b[0m\u001b[31m*\u001b[0m\u001b[31m*\u001b[0m\u001b[31m*\u001b[0m\u001b[31m*\u001b[0m\u001b[31m*\u001b[0m\u001b[31m*\u001b[0m\u001b[31m*\u001b[0m\u001b[31m*\u001b[0m\u001b[31m*\u001b[0m\u001b[31m*\u001b[0m \n", " \u001b[31m*\u001b[0m\u001b[31m*\u001b[0m\u001b[31m*\u001b[0m\u001b[31m*\u001b[0m\u001b[31m*\u001b[0m\u001b[31m*\u001b[0m\u001b[31m*\u001b[0m\u001b[31m*\u001b[0m\u001b[31m*\u001b[0m\u001b[31m*\u001b[0m\u001b[31m*\u001b[0m\u001b[31m*\u001b[0m\u001b[31m*\u001b[0m\u001b[31m*\u001b[0m\u001b[31m*\u001b[0m \n", " \u001b[31m*\u001b[0m\u001b[31m*\u001b[0m\u001b[31m*\u001b[0m\u001b[31m*\u001b[0m\u001b[31m*\u001b[0m\u001b[31m*\u001b[0m\u001b[31m*\u001b[0m\u001b[31m*\u001b[0m\u001b[31m*\u001b[0m\u001b[31m*\u001b[0m\u001b[31m*\u001b[0m\u001b[31m*\u001b[0m\u001b[31m*\u001b[0m \n", " \u001b[31m*\u001b[0m\u001b[31m*\u001b[0m\u001b[31m*\u001b[0m\u001b[31m*\u001b[0m\u001b[31m*\u001b[0m\u001b[31m*\u001b[0m\u001b[31m*\u001b[0m\u001b[31m*\u001b[0m\u001b[31m*\u001b[0m\u001b[31m*\u001b[0m\u001b[31m*\u001b[0m \n", " \u001b[31m*\u001b[0m\u001b[31m*\u001b[0m\u001b[31m*\u001b[0m\u001b[31m*\u001b[0m\u001b[31m*\u001b[0m\u001b[31m*\u001b[0m\u001b[31m*\u001b[0m\u001b[31m*\u001b[0m\u001b[31m*\u001b[0m \n", " \u001b[31m*\u001b[0m\u001b[31m*\u001b[0m\u001b[31m*\u001b[0m\u001b[31m*\u001b[0m\u001b[31m*\u001b[0m\u001b[31m*\u001b[0m\u001b[31m*\u001b[0m \n", " \u001b[31m*\u001b[0m\u001b[31m*\u001b[0m\u001b[31m*\u001b[0m\u001b[31m*\u001b[0m\u001b[31m*\u001b[0m \n", " \u001b[31m*\u001b[0m\u001b[31m*\u001b[0m\u001b[31m*\u001b[0m \n", " \u001b[31m*\u001b[0m \n" ] } ], "source": [ "r = 10 \n", "for y in range(2 * r + 1):\n", " for x in range(2 * r + 1):\n", " if y >= -x + r and y >= x -r and y <= x + r and y <= -x + 3 * r:\n", " print(\"\\033[31m*\\033[0m\", end=\"\")\n", " else:\n", " print(\" \", end=\"\")\n", " print()" ] }, { "cell_type": "code", "execution_count": 28, "id": "31f66a43-f3c0-40eb-afc7-97ea41d6be9e", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import cv2\n", "import numpy\n", "\n", "wh = 500\n", "\n", "# 生成空的图像 【矩阵】【二维数组】\n", "\n", "g = [[[0, 0, 255] for y in range(2*wh)] for x in range(2*wh)] # BGR RGB\n", "g = numpy.array(g)\n", "\n", "for y in range(2*wh):\n", " for x in range(2*wh):\n", " if y >= -x + wh and y >= x -wh and y <= x + wh and y <= -x + 3 * wh:\n", " g[y, x, :] = [0, 255, 0]\n", " else:\n", " g[y, x, :] = [0, 0, 0]\n", "cv2.imwrite(\"tu.png\", g)" ] }, { "cell_type": "code", "execution_count": 30, "id": "de60f33d-a19f-4294-be96-29edd58dfb67", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import cv2\n", "import numpy\n", "\n", "wh = 500\n", "\n", "# 生成空的图像 【矩阵】【二维数组】\n", "\n", "g = [[[0, 0, 255] for y in range(2*wh)] for x in range(2*wh)] # BGR RGB\n", "g = numpy.array(g)\n", "\n", "for y in range(2*wh):\n", " for x in range(2*wh):\n", " if ((x-250) ** 2 + (y-250)**2)<= 250**2:\n", " g[y, x, :] = [0, 255, 0]\n", " else:\n", " g[y, x, :] = [0, 0, 0]\n", "cv2.imwrite(\"tu.png\", g)" ] }, { "cell_type": "code", "execution_count": null, "id": "1aaa9bb9-fa74-4fbb-8431-982adfdeccdc", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "32f1c62b-5c0b-4b86-9efb-bf4cc419a6f9", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.9.13" } }, "nbformat": 4, "nbformat_minor": 5 }