尧图建网站 尧图建网站 YAOTU WEB BUILD 免费咨询
ARTICLE DETAIL

资讯详情

深耕网站建设与建站编程的一线实战洞察。

Triton前置——Python基础语法

Triton前置——Python基础语法 代码块缩进 vs 大括号Python缩进决定层级 缩进def hello(): print(你好) # 缩进4空格属于函数 print(世界) # 缩进4空格属于函数 if x 0: print(正数) # 缩进4空格属于if print(哈哈) # 缩进4空格属于if print(结束) # 不缩进不属于ifC大括号决定层级void hello() { cout 你好; // 在大括号内属于函数 cout 世界; // 在大括号内属于函数 } if (x 0) { cout 正数; // 在大括号内属于if cout 哈哈; // 在大括号内属于if } cout 结束; // 在大括号外不属于if语句结束换行 vs 分号Python换行即可x 5 y 10 z x y print(z)C必须加分号int x 5; // 必须有分号 int y 10; // 必须有分号 int z x y; // 必须有分号 cout z; // 必须有分号变量类型动态 vs 静态Python不需要声明类型x 5 # 自动推断为int x hello # 可以随意改变类型 x 3.14 # 再次改变类型 def add(a, b): # 不需要指定a,b的类型 return a b add(3, 5) # 可以传整数 add(hi, !) # 也可以传字符串C必须声明类型int x 5; // 声明为int x 10; // 可以还是int x hello; // ❌ 错误不能改变类型 int add(int a, int b) { // 必须指定类型 return a b; } add(3, 5); // ✅ 可以 add(hi, !); // ❌ 错误类型不匹配函数定义def vs 类型Pythondef add(a, b): return a b def greet(name世界): # 默认参数 print(f你好, {name}) def multiple_return(): # 返回多个值 return 1, 2, 3Cint add(int a, int b) { // 必须指定返回类型和参数类型 return a b; } void greet(string name 世界) { // 默认参数 cout 你好, name; } tupleint, int, int multiple_return() { // 返回多个值需要tuple return {1, 2, 3}; }循环语法Python# for循环 for i in range(10): # 0-9 print(i) for item in [1, 2, 3, 4]: # 遍历列表 print(item) # while循环 i 0 while i 10: print(i) i 1C// for循环 for (int i 0; i 10; i) { // 初始化;条件;更新 cout i; } for (int item : {1, 2, 3, 4}) { // C11 范围for cout item; } // while循环 int i 0; while (i 10) { cout i; i; }条件语句Pythonif x 0: print(正数) elif x 0: print(零) else: print(负数) # 简洁写法 result 正数 if x 0 else 负数Cif (x 0) { cout 正数; } else if (x 0) { cout 零; } else { cout 负数; } // 三元运算符 string result (x 0) ? 正数 : 负数;数组/列表Python# 列表动态数组可增长 list1 [1, 2, 3] list1.append(4) # 添加元素 list1.pop() # 删除元素 list1[0] # 访问 # 列表推导式 squares [x*x for x in range(10)] # 元组不可变 tuple1 (1, 2, 3) # 字典 dict1 {name: 小明, age: 18}C// 数组固定大小 int arr[3] {1, 2, 3}; arr[0] 10; // vector动态数组 vectorint vec {1, 2, 3}; vec.push_back(4); // 添加 vec.pop_back(); // 删除 vec[0]; // 访问 // map字典 mapstring, int dict; dict[name] 小明; dict[age] 18;字符串操作Pythonname 小明 greeting f你好, {name} # f-string简单 greeting 你好, name # 拼接 # 切片 s hello s[1:3] # el s[::-1] # olleh反转Cstring name 小明; string greeting 你好, name; // 拼接 // C11 需要额外库 string greeting 你好, name; // 切片需要substr string s hello; s.substr(1, 2); // el reverse(s.begin(), s.end()); // olleh内存管理Python自动垃圾回收def create_list(): return [1, 2, 3, 4, 5] # 自动管理内存 # 不需要担心内存泄漏 x [1, 2, 3] x None # 旧列表自动被回收C手动管理或智能指针int* create_array() { int* arr new int[5]; // 手动分配 return arr; } int main() { int* arr create_array(); // 必须手动释放 delete[] arr; } // 智能指针现代C unique_ptrint[] arr make_uniqueint[](5); // 自动释放类面向对象Pythonclass Person: def __init__(self, name, age): # 构造函数 self.name name self.age age def say_hello(self): print(f你好, 我是{self.name}) classmethod def create_child(cls, name): return cls(name, 0) # 继承 class Student(Person): def __init__(self, name, age, grade): super().__init__(name, age) self.grade gradeCclass Person { public: string name; int age; // 构造函数 Person(string n, int a) : name(n), age(a) {} void say_hello() { cout 你好, 我是 name; } // 静态方法 static Person create_child(string name) { return Person(name, 0); } }; // 继承 class Student : public Person { public: int grade; Student(string n, int a, int g) : Person(n, a), grade(g) {} };异常处理Pythontry: x 10 / 0 except ZeroDivisionError: print(除以零了) except Exception as e: print(f其他错误: {e}) else: print(没有错误) finally: print(总是执行)Ctry { int x 10 / 0; } catch (const runtime_error e) { cout 错误: e.what(); } catch (...) { cout 其他错误; } // 没有else和finally常用操作对比操作PythonC打印print(hello)cout hello输入input()cin x注释# 注释// 注释多行注释 注释 /* 注释 */逻辑与and逻辑或or||逻辑非not!比较, !, , , !, , 自增x 1x或x 1
返回列表