0

0

JavaScript 就像 Python

花韻仙語

花韻仙語

发布时间:2024-11-14 16:49:49

|

1196人浏览过

|

来源于dev.to

转载

javascript 就像 python

本文对 javascript 和 python 的语法和基本编程结构进行了比较。它旨在强调这两种流行的编程语言在实现基本编程概念方面的相似之处。

虽然两种语言有许多共同点,使开发人员更容易在它们之间切换或理解对方的代码,但也应该注意明显的语法和操作差异。

重要的是要以轻松的角度进行这种比较,而不是过分强调 javascript 和 python 之间的相似或差异。目的不是要声明一种语言优于另一种语言,而是提供一种资源,可以帮助熟悉 python 的程序员更轻松地理解并过渡到 javascript。

你好世界

javascript

Typeface
Typeface

AI创意内容创作助手

下载
// in codeguppy.com environment
println('hello, world');

// outside codeguppy.com
console.log('hello, world');

python

print('hello, world')

变量和常量

javascript

let myvariable = 100;

const myconstant = 3.14159;

python

myvariable = 100

myconstant = 3.14159

字符串插值

javascript

let a = 100;
let b = 200;

println(`sum of ${a} and ${b} is ${a + b}`);

python

a = 100
b = 200

print(f'sum of {a} and {b} is {a + b}')

if 表达式/语句

javascript

let age = 18;

if (age < 13) 
{
    println("child");
} 
else if (age < 20) 
{
    println("teenager");
} 
else 
{
    println("adult");
}

python

age = 18

if age < 13:
    print("child")
elif age < 20:
    print("teenager")
else:
    print("adult")

条件句

javascript

let age = 20;
let message = age >= 18 ? "can vote" : "cannot vote";
println(message);  // output: can vote

python

age = 20
message = "can vote" if age >= 18 else "cannot vote"
print(message)  # output: can vote

数组

javascript

// creating an array
let myarray = [1, 2, 3, 4, 5];

// accessing elements
println(myarray[0]);  // access the first element: 1
println(myarray[3]);  // access the fourth element: 4

// modifying an element
myarray[2] = 30;  // change the third element from 3 to 30

// adding a new element
myarray.push(6);  // add a new element to the end

python

# creating a list to represent an array
my_array = [1, 2, 3, 4, 5]

# accessing elements
print(my_array[0])  # access the first element: 1
print(my_array[3])  # access the fourth element: 4

# modifying an element
my_array[2] = 30  # change the third element from 3 to 30

# adding a new element
my_array.append(6)  # add a new element to the end

对于每个

javascript

let fruits = ["apple", "banana", "cherry", "date"];

for(let fruit of fruits)
    println(fruit);

python

fruits = ["apple", "banana", "cherry", "date"]

for fruit in fruits:
    print(fruit)

词典

javascript

// creating a dictionary
fruit_prices = {
    apple: 0.65,
    banana: 0.35,
    cherry: 0.85
};

// accessing a value by key
println(fruit_prices["apple"]);  // output: 0.65

python

# creating a dictionary
fruit_prices = {
    "apple": 0.65,
    "banana": 0.35,
    "cherry": 0.85
}

# accessing a value by key
print(fruit_prices["apple"])  # output: 0.65

功能

javascript

function addnumbers(a, b) 
{
    return a + b;
}

let result = addnumbers(100, 200);
println("the sum is: ", result);

python

def add_numbers(a, b):
    return a + b

result = add_numbers(100, 200)
print("the sum is: ", result)

元组返回

javascript

function getcircleproperties(radius) 
{
    const area = math.pi * radius ** 2;
    const circumference = 2 * math.pi * radius;

    return [area, circumference];  // return as an array
}

// using the function
const [area, circumference] = getcircleproperties(5);

println(`the area of the circle is: ${area}`);
println(`the circumference of the circle is: ${circumference}`);

python

import math

def getcircleproperties(radius):
    """calculate and return the area and circumference of a circle."""
    area = math.pi * radius**2
    circumference = 2 * math.pi * radius
    return (area, circumference)

# using the function
radius = 5
area, circumference = getcircleproperties(radius)

print(f"the area of the circle is: {area}")
print(f"the circumference of the circle is: {circumference}")

可变数量的参数

javascript

function sumnumbers(...args) 
{
    let sum = 0;
    for(let i of args)
        sum += i;
    return sum;
}

println(sumnumbers(1, 2, 3));
println(sumnumbers(100, 200));

python

def sum_numbers(*args):
    sum = 0
    for i in args:
        sum += i
    return sum

print(sum_numbers(1, 2, 3))
print(sum_numbers(100, 200))

拉姆达斯

javascript

const numbers = [1, 2, 3, 4, 5];

// use map to apply a function to all elements of the array
const squarednumbers = numbers.map(x => x ** 2);

println(squarednumbers);  // output: [1, 4, 9, 16, 25]

python

numbers = [1, 2, 3, 4, 5]

# use map to apply a function to all elements of the list
squared_numbers = map(lambda x: x**2, numbers)

# convert map object to a list to print the results
squared_numbers_list = list(squared_numbers)

print(squared_numbers_list)  # output: [1, 4, 9, 16, 25]

课程

javascript

class book 
{
    constructor(title, author, pages) 
    {
        this.title = title;
        this.author = author;
        this.pages = pages;
    }

    describebook() 
    {
        println(`book title: ${this.title}`);
        println(`author: ${this.author}`);
        println(`number of pages: ${this.pages}`);
    }
}

python

class book:
    def __init__(self, title, author, pages):
        self.title = title
        self.author = author
        self.pages = pages

    def describe_book(self):
        print(f"book title: {self.title}")
        print(f"author: {self.author}")
        print(f"number of pages: {self.pages}")

类的使用

javascript

// creating an instance of the book class
// this is actually a real book (see curriculum section for more info)
const mybook = new book("illustrated javascript", "adrian", 684);
mybook.describebook();

python

# Creating an instance of the Book class
# This is actually a real book (see Curriculum section for more info)
my_book = Book("Illustrated JavaScript", "Adrian", 684)
my_book.describe_book()

结论

我们鼓励您参与完善此比较。您的贡献,无论是更正、增强还是新增内容,都受到高度重视。通过合作,我们可以创建更准确、更全面的指南,让所有有兴趣学习 javascript 和 python 的开发人员受益。

立即学习Java免费学习笔记(深入)”;


制作人员

本文转载自免费编码平台https://codeguppy.com平台的博客。

本文受到其他编程语言之间类似比较的影响:

  • kotlin 就像 c# https://ttu.github.io/kotlin-is-like-csharp/
  • kotlin 就像 typescript https://gi-no.github.io/kotlin-is-like-typescript/
  • swift 就像 kotlin https://nilhcem.com/swift-is-like-kotlin/
  • swift 就像 go http://repo.tiye.me/jiyinyiyong/swift-is-like-go/
  • swift 就像 scala https://leverich.github.io/swiftislikescala/

热门AI工具

更多
DeepSeek
DeepSeek

幻方量化公司旗下的开源大模型平台

豆包大模型
豆包大模型

字节跳动自主研发的一系列大型语言模型

WorkBuddy
WorkBuddy

腾讯云推出的AI原生桌面智能体工作台

腾讯元宝
腾讯元宝

腾讯混元平台推出的AI助手

文心一言
文心一言

文心一言是百度开发的AI聊天机器人,通过对话可以生成各种形式的内容。

讯飞写作
讯飞写作

基于讯飞星火大模型的AI写作工具,可以快速生成新闻稿件、品宣文案、工作总结、心得体会等各种文文稿

即梦AI
即梦AI

一站式AI创作平台,免费AI图片和视频生成。

ChatGPT
ChatGPT

最最强大的AI聊天机器人程序,ChatGPT不单是聊天机器人,还能进行撰写邮件、视频脚本、文案、翻译、代码等任务。

相关专题

更多
Swift iOS架构设计与MVVM模式实战
Swift iOS架构设计与MVVM模式实战

本专题聚焦 Swift 在 iOS 应用架构设计中的实践,系统讲解 MVVM 模式的核心思想、数据绑定机制、模块拆分策略以及组件化开发方法。内容涵盖网络层封装、状态管理、依赖注入与性能优化技巧。通过完整项目案例,帮助开发者构建结构清晰、可维护性强的 iOS 应用架构体系。

357

2026.03.03

TypeScript工程化开发与Vite构建优化实践
TypeScript工程化开发与Vite构建优化实践

本专题面向前端开发者,深入讲解 TypeScript 类型系统与大型项目结构设计方法,并结合 Vite 构建工具优化前端工程化流程。内容包括模块化设计、类型声明管理、代码分割、热更新原理以及构建性能调优。通过完整项目示例,帮助开发者提升代码可维护性与开发效率。

49

2026.02.13

TypeScript全栈项目架构与接口规范设计
TypeScript全栈项目架构与接口规范设计

本专题面向全栈开发者,系统讲解基于 TypeScript 构建前后端统一技术栈的工程化实践。内容涵盖项目分层设计、接口协议规范、类型共享机制、错误码体系设计、接口自动化生成与文档维护方案。通过完整项目示例,帮助开发者构建结构清晰、类型安全、易维护的现代全栈应用架构。

199

2026.02.25

TypeScript类型系统进阶与大型前端项目实践
TypeScript类型系统进阶与大型前端项目实践

本专题围绕 TypeScript 在大型前端项目中的应用展开,深入讲解类型系统设计与工程化开发方法。内容包括泛型与高级类型、类型推断机制、声明文件编写、模块化结构设计以及代码规范管理。通过真实项目案例分析,帮助开发者构建类型安全、结构清晰、易维护的前端工程体系,提高团队协作效率与代码质量。

89

2026.03.13

Kotlin协程编程与Spring Boot集成实践
Kotlin协程编程与Spring Boot集成实践

本专题围绕 Kotlin 协程机制展开,深入讲解挂起函数、协程作用域、结构化并发与异常处理机制,并结合 Spring Boot 展示协程在后端开发中的实际应用。内容涵盖异步接口设计、数据库调用优化、线程资源管理以及性能调优策略,帮助开发者构建更加简洁高效的 Kotlin 后端服务架构。

131

2026.02.12

java基础知识汇总
java基础知识汇总

java基础知识有Java的历史和特点、Java的开发环境、Java的基本数据类型、变量和常量、运算符和表达式、控制语句、数组和字符串等等知识点。想要知道更多关于java基础知识的朋友,请阅读本专题下面的的有关文章,欢迎大家来php中文网学习。

1570

2023.10.24

if什么意思
if什么意思

if的意思是“如果”的条件。它是一个用于引导条件语句的关键词,用于根据特定条件的真假情况来执行不同的代码块。本专题提供if什么意思的相关文章,供大家免费阅读。

847

2023.08.22

js 字符串转数组
js 字符串转数组

js字符串转数组的方法:1、使用“split()”方法;2、使用“Array.from()”方法;3、使用for循环遍历;4、使用“Array.split()”方法。本专题为大家提供js字符串转数组的相关的文章、下载、课程内容,供大家免费下载体验。

761

2023.08.03

TypeScript类型系统进阶与大型前端项目实践
TypeScript类型系统进阶与大型前端项目实践

本专题围绕 TypeScript 在大型前端项目中的应用展开,深入讲解类型系统设计与工程化开发方法。内容包括泛型与高级类型、类型推断机制、声明文件编写、模块化结构设计以及代码规范管理。通过真实项目案例分析,帮助开发者构建类型安全、结构清晰、易维护的前端工程体系,提高团队协作效率与代码质量。

69

2026.03.13

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
最新Python教程 从入门到精通
最新Python教程 从入门到精通

共4课时 | 22.5万人学习

Django 教程
Django 教程

共28课时 | 5万人学习

SciPy 教程
SciPy 教程

共10课时 | 2万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号