Commit b94b09e9 by Leo

upload code

parent 3fb53737
{
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## python基础入门"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"hello woorld\n"
]
}
],
"source": [
"print(\"hello woorld\")"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1 2 3 4 5\n"
]
}
],
"source": [
"print(1,2,3,4,5)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1 2\n"
]
}
],
"source": [
"acc=1\n",
"epoch=2\n",
"print(acc,epoch) # acc为深度学习模型训练的精度指标,epoch为轮数"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1 2 3 4 5\n",
"1->2->3->4->5\n"
]
}
],
"source": [
"print(1,2,3,4,5)\n",
"print(1,2,3,4,5,sep=\"->\") # sep指定输出的分隔"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1 2 3 4 5\t1 2 3 4 5\n"
]
}
],
"source": [
"print(1,2,3,4,5,end=\"\\t\") # \\t为制表符,默认一个tab\n",
"print(1,2,3,4,5)\n"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1 2 3 4 5\n",
"1 2 3 4 5\n"
]
}
],
"source": [
"print(1,2,3,4,5) # \\n为换行\n",
"print(1,2,3,4,5)"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [],
"source": [
"f = open(\"test.txt\",\"a\") # 若没有此txt文件则先创建,然后将内容追加在末尾\n",
"print(\"\\n\",file=f)\n",
"print(\"中文报错,print后面括号为中文()\",file=f)\n",
"f.close()"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0.30000000000000004\n"
]
}
],
"source": [
"print(0.1+0.2)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1690473024512\n",
"1690473024512\n"
]
}
],
"source": [
"list1 = [1,2,3,5]\n",
"print(id(list1))\n",
"list1[1] = 5\n",
"print(id(list1))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 格式化输出"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"我叫qianyu,今年18岁啦\n"
]
}
],
"source": [
"print(\"我叫%s,今年%d岁啦\" % (\"qianyu\",18)) # %s为字符串 %d为整型"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"学校的名称是点头教育,学习的网站是wwww.diantouedu.cn\n"
]
}
],
"source": [
"print(\"学校的名称是{},学习的网站是{}\".format(\"点头教育\",\"wwww.diantouedu.cn\"))"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"我叫做qianyu,今年19岁啦\n",
"我叫做qianyu,今年19岁啦\n",
"我叫做qianyu,今年19岁啦\n",
"我叫做qianyu,今年19岁啦\n"
]
}
],
"source": [
"# f表达式格式化输出\n",
"name = \"qianyu\"\n",
"age = 19\n",
"print(f\"我叫做{name},今年{age}岁啦\")\n",
"print(f\"我叫做{name},今年{age}岁啦\")\n",
"print(f\"我叫做{name},今年{age}岁啦\")\n",
"print(f\"我叫做{name},今年{age}岁啦\")"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"我叫做qianyu,今年19岁啦\n",
"我叫做qianyu,今年19岁啦\n",
"我叫做qianyu,今年19岁啦\n",
"我叫做qianyu,今年19岁啦\n"
]
}
],
"source": [
"print(\"我叫做qianyu,今年19岁啦\")\n",
"print(\"我叫做qianyu,今年19岁啦\")\n",
"print(\"我叫做qianyu,今年19岁啦\")\n",
"print(\"我叫做qianyu,今年19岁啦\")"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"训练轮数为1\n"
]
}
],
"source": [
"epoch = 1\n",
"print(f\"训练轮数为{epoch}\")"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['False', 'None', 'True', '__peg_parser__', 'and', 'as', 'assert', 'async', 'await', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise', 'return', 'try', 'while', 'with', 'yield']\n"
]
}
],
"source": [
"import keyword\n",
"print(keyword.kwlist)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 变量"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"2437147683120\n",
"2437147683152\n"
]
}
],
"source": [
"a = 1\n",
"print(id(a)) \n",
"# print(type(a)) ctrl+/为整体注释\n",
"a = 2\n",
"print(id(a))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 输入函数"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"hello\n",
"<class 'str'>\n"
]
}
],
"source": [
"age = input() # 无论输入的是什么 都会被转化为字符串类型\n",
"print(age)\n",
"print(type(age))"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "test",
"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.18"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
# Python基础编程
# Python基础编程
这个仓库包含了关于第一和第二节课的Python基础编程代码。
## 第一节课
- 代码文件: `python基础入门.ipynb`
- 文本文件: `test.txt` (包含一些常见的报错,是第一节课中用到的文本文件)
## 第二节课
- 代码文件: `六大数据类型2.ipynb`(六大数据类型.ipynb为往期代码,可做参考)
\ No newline at end of file
# Python基础报错例子
# Python基础报错例子
SyntaxError(语法错误):
print "Hello, World!"
# 缺少括号,应该是 print("Hello, World!")
IndentationError(缩进错误):
def my_function():
print("Hello, World!")
# 函数体缩进不正确
NameError(名称错误):
print(x)
# x未定义
TypeError(类型错误):
x = "5"
y = 2
z = x + y
# 字符串和整数不能直接相加
IndexError(索引错误):
my_list = [1, 2, 3]
print(my_list[3])
# 索引超出了列表范围
KeyError(键错误):
my_dict = {"name": "Alice", "age": 30}
print(my_dict["gender"])
# 字典中没有"gender"这个键
ValueError(数值错误):
int("abc")
# 无法将非数字字符串转换为整数
FileNotFoundError(文件未找到错误):
with open("nonexistent_file.txt", "r") as f:
content = f.read()
# 文件不存在
ImportError(导入错误):
import nonexistent_module
# 未安装或导入的模块不存在
ZeroDivisionError(除零错误):
result = 10 / 0
# 除数为零
AttributeError(属性错误):
x = 5
print(x.append(1))
# 整数对象没有append()方法
KeyboardInterrupt(键盘中断):
while True:
pass
# 执行这段代码后,按下Ctrl+C将会中断程序执行hello world
中文报错,print后面括号为中文()
### Python进阶知识
### Python进阶知识
这里是Python部分进阶的知识,主要包括以下内容:
#### 1. 三大程序结构
- **顺序:** 指程序从上到下按照顺序执行的结构。
- **分支:** 涉及条件判断,根据条件不同执行不同的代码块。
- **循环:** 允许代码块多次执行,直到满足退出条件为止。
#### 2. 函数的定义和使用
函数是一段可重复使用的代码块,可以接受参数并返回值。
#### 3. 经典编程案例
- **递归求n阶乘:** 通过递归方式计算给定数的阶乘。
- **求解水仙花数:** 寻找指定范围内的水仙花数,即每位数字的立方和等于该数本身的数。
## 本章节讲解的是 Python 面向对象编程的相关内容,包括类和对象的定义和使用、封装、继承、多态。在学习本章之前需要先看环境配置、Python基础、进阶课程
## 本章节讲解的是 Python 面向对象编程的相关内容,包括类和对象的定义和使用、封装、继承、多态。在学习本章之前需要先看环境配置、Python基础、进阶课程
[
[
{
"id": "001",
"name": "点头教育",
"url": "www.diantouedu.cn",
"age": 10
},
{
"id": "002",
"name": "Google",
"url": "www.google.com",
"age": 100
},
{
"id": "003",
"name": "淘宝",
"url": "www.taobao.com",
"age": 50
}
]
\ No newline at end of file
This source diff could not be displayed because it is too large. You can view the blob instead.
++ "b/\345\275\225\346\222\255/2-AI\345\267\245\345\205\267\345\214\205/2.5-Pandas\346\225\260\346\215\256\345\244\204\347\220\206/test.py"
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment