Python基础知识与js,c#,ts类比
# Python变量与数据结构
变量与数据类型 :变量可用来存储数据,Python 中有多种数据类型,像整数(int)、浮点数(float)、字符串(str)、布尔值(bool)等。
# 整数
age = 25
# 浮点数
height = 1.75
# 字符串
name = "John"
# 布尔值
is_student = True
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
# 类比js
let age=25;
const height=1.75;
let name ="john"
is_student=true
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
# 类比ts
let age: number = 25;
// 浮点数
let height: number = 1.75;
// 字符串
let name: string = "John";
// 布尔值
let is_student: boolean = true;
1
2
3
4
5
6
7
2
3
4
5
6
7
# 类比C
using System;
class Program
{
static void Main()
{
// 整数
int age = 25;
// 浮点数
double height = 1.75;
// 字符串
string name = "John";
// 布尔值
bool is_student = true;
// 输出变量的值,可根据需求保留或删除
Console.WriteLine($"年龄: {age}");
Console.WriteLine($"身高: {height}");
Console.WriteLine($"姓名: {name}");
Console.WriteLine($"是否为学生: {is_student}");
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# python运算符
运算符:包括算术运算符(+、-、*、/ 等)、比较运算符(==、!=、>、< 等)和逻辑运算符(and、or、not)。
# 算术运算
result = 5 + 3
# 比较运算
is_equal = (5 == 3)
# 逻辑运算
is_valid = (age > 18) and is_student
1
2
3
4
5
6
7
2
3
4
5
6
7
# 类比js运算符
// 算术运算
let result = 5 + 3;
// 比较运算
let is_equal = (5 === 3);
// 假设 age 和 is_student 已经定义
let age = 25;
let is_student = true;
// 逻辑运算
let is_valid = (age > 18) && is_student;
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
# python 控制结构
包含条件语句(if - elif - else)和循环语句(for、while)。
# 条件语句
if age >= 18:
print("成年人")
else:
print("未成年人")
# for循环
fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
print(fruit)
# while循环
count = 0
while count < 5:
print(count)
count = count + 1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# 类比js控制结构
// 假设 age 已经定义
let age = 25;
// 条件语句
if (age >= 18) {
console.log("成年人");
} else {
console.log("未成年人");
}
// for循环
const fruits = ["apple", "banana", "cherry"];
for (let fruit of fruits) {
console.log(fruit);
}
// while循环
let count = 0;
while (count < 5) {
console.log(count);
count = count + 1;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# python 函数和模块
函数:函数是一段可重复使用的代码块,能接收参数并返回值。
模块:模块是包含 Python 代码的文件,可导入并使用其中的函数和变量。
def add_numbers(a, b):
return a + b
sum_result = add_numbers(3, 5)
print(sum_result)
import math
# 使用 math 模块的函数
square_root = math.sqrt(16)
print(square_root)
1
2
3
4
5
6
7
8
9
10
11
12
2
3
4
5
6
7
8
9
10
11
12
# 类比js 函数和模块
function add_numbers(a,b){
return a+b
}
const sum_result=add_numbers(3,5)
console.log(sum_result)
import math from math
const square_root = math.sqrt(16)
console.log(square_root)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# python 类和对象
类和对象:类是对象的蓝图,对象是类的实例。
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def introduce(self):
print(f"我叫 {self.name},今年 {self.age} 岁。")
# 创建对象
person = Person("John", 25)
person.introduce()
1
2
3
4
5
6
7
8
9
10
11
12
2
3
4
5
6
7
8
9
10
11
12
# 类比js 类和对象
class Person {
constructor(name, age) {
this.name = name;
this.age = age;
}
introduce() {
console.log(`我叫 ${this.name},今年 ${this.age} 岁。`);
}
}
// 创建对象
const person = new Person("John", 25);
person.introduce();
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
编辑 (opens new window)
上次更新: 2025/03/19, 14:21:39