JavaScript 就像 Python

javascript 就像 python

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

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

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

你好世界

javascript

// in codeguppy.com environmentprintln('hello, world');// outside codeguppy.comconsole.log('hello, world');

python

print('hello, world')

变量和常量

javascript

let myvariable = 100;const myconstant = 3.14159;

python

myvariable = 100myconstant = 3.14159

字符串插值

javascript

let a = 100;let b = 200;println(`sum of ${a} and ${b} is ${a + b}`);

python

a = 100b = 200print(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 = 18if 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 = 20message = "can vote" if age >= 18 else "cannot vote"print(message)  # output: can vote

数组

javascript

// creating an arraylet myarray = [1, 2, 3, 4, 5];// accessing elementsprintln(myarray[0]);  // access the first element: 1println(myarray[3]);  // access the fourth element: 4// modifying an elementmyarray[2] = 30;  // change the third element from 3 to 30// adding a new elementmyarray.push(6);  // add a new element to the end

python

# creating a list to represent an arraymy_array = [1, 2, 3, 4, 5]# accessing elementsprint(my_array[0])  # access the first element: 1print(my_array[3])  # access the fourth element: 4# modifying an elementmy_array[2] = 30  # change the third element from 3 to 30# adding a new elementmy_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 dictionaryfruit_prices = {    apple: 0.65,    banana: 0.35,    cherry: 0.85};// accessing a value by keyprintln(fruit_prices["apple"]);  // output: 0.65

python

# creating a dictionaryfruit_prices = {    "apple": 0.65,    "banana": 0.35,    "cherry": 0.85}# accessing a value by keyprint(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 + bresult = 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 functionconst [area, circumference] = getcircleproperties(5);println(`the area of the circle is: ${area}`);println(`the circumference of the circle is: ${circumference}`);

python

import mathdef 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 functionradius = 5area, 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 sumprint(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 arrayconst 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 listsquared_numbers = map(lambda x: x**2, numbers)# convert map object to a list to print the resultssquared_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/

以上就是JavaScript 就像 Python的详细内容,更多请关注创想鸟其它相关文章!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/1353144.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2025年12月13日 17:26:42
下一篇 2025年12月13日 17:26:54

相关推荐

  • 如何解决本地图片在使用 mask JS 库时出现的跨域错误?

    如何跨越localhost使用本地图片? 问题: 在本地使用mask js库时,引入本地图片会报跨域错误。 解决方案: 要解决此问题,需要使用本地服务器启动文件,以http或https协议访问图片,而不是使用file://协议。例如: python -m http.server 8000 然后,可以…

    2025年12月24日
    200
  • 使用 Mask 导入本地图片时,如何解决跨域问题?

    跨域疑难:如何解决 mask 引入本地图片产生的跨域问题? 在使用 mask 导入本地图片时,你可能会遇到令人沮丧的跨域错误。为什么会出现跨域问题呢?让我们深入了解一下: mask 框架假设你以 http(s) 协议加载你的 html 文件,而当使用 file:// 协议打开本地文件时,就会产生跨域…

    2025年12月24日
    200
  • HTML、CSS 和 JavaScript 中的简单侧边栏菜单

    构建一个简单的侧边栏菜单是一个很好的主意,它可以为您的网站添加有价值的功能和令人惊叹的外观。 侧边栏菜单对于客户找到不同项目的方式很有用,而不会让他们觉得自己有太多选择,从而创造了简单性和秩序。 今天,我将分享一个简单的 HTML、CSS 和 JavaScript 源代码来创建一个简单的侧边栏菜单。…

    2025年12月24日
    200
  • 前端代码辅助工具:如何选择最可靠的AI工具?

    前端代码辅助工具:可靠性探讨 对于前端工程师来说,在HTML、CSS和JavaScript开发中借助AI工具是司空见惯的事情。然而,并非所有工具都能提供同等的可靠性。 个性化需求 关于哪个AI工具最可靠,这个问题没有一刀切的答案。每个人的使用习惯和项目需求各不相同。以下是一些影响选择的重要因素: 立…

    2025年12月24日
    300
  • 带有 HTML、CSS 和 JavaScript 工具提示的响应式侧边导航栏

    响应式侧边导航栏不仅有助于改善网站的导航,还可以解决整齐放置链接的问题,从而增强用户体验。通过使用工具提示,可以让用户了解每个链接的功能,包括设计紧凑的情况。 在本教程中,我将解释使用 html、css、javascript 创建带有工具提示的响应式侧栏导航的完整代码。 对于那些一直想要一个干净、简…

    2025年12月24日
    000
  • TypeScript 中如何约束对象为 CSS 属性?

    typescript 中如何约束对象为 css 属性 想要约束一个对象为 css 属性,以便在调用函数时得到自动补全提示,可以采用以下方法: 使用 react 的 cssproperties 类型 对于 react 项目,可以使用 react 提供的 cssproperties 类型: 立即学习“前…

    2025年12月24日
    300
  • 如何在 TypeScript 中约束对象为 CSS 属性?

    如何在 typescript 中约束对象为 css 属性? 在 typescript 中,为特定目的而约束对象类型是很重要的。在本文中,我们将探究如何将对象约束为包含 css 属性。 考虑以下函数: function setattrstoelement(el: htmlelement, attr: …

    2025年12月24日
    000
  • 布局 – CSS 挑战

    您可以在 github 仓库中找到这篇文章中的所有代码。 您可以在这里查看视觉效果: 固定导航 – 布局 – codesandbox两列 – 布局 – codesandbox三列 – 布局 – codesandbox圣杯 &#8…

    2025年12月24日
    000
  • 隐藏元素 – CSS 挑战

    您可以在 github 仓库中找到这篇文章中的所有代码。 您可以在此处查看隐藏元素的视觉效果 – codesandbox 隐藏元素 hiding elements hiding elements hiding elements hiding elements hiding element…

    2025年12月24日
    400
  • 居中 – CSS 挑战

    您可以在 github 仓库中找到这篇文章中的所有代码。 您可以在此处查看垂直中心 – codesandbox 和水平中心的视觉效果。 通过 css 居中 垂直居中 centering centering centering centering centering centering立即…

    2025年12月24日 好文分享
    300
  • 如何使用 TypeScript 约束对象以匹配 CSS 属性?

    如何约束 typescript 对象以匹配 css 属性? setattrstoelement 函数接收两个参数,其中第二个参数应为 css 属性。对于 react 项目,可以使用 cssproperties 类型: import { cssproperties } from “react”;fun…

    2025年12月24日
    000
  • 如何在 Laravel 框架中轻松集成微信支付和支付宝支付?

    如何用 laravel 框架集成微信支付和支付宝支付 问题:如何在 laravel 框架中集成微信支付和支付宝支付? 回答: 建议使用 easywechat 的 laravel 版,easywechat 是一个由腾讯工程师开发的高质量微信开放平台 sdk,已被广泛地应用于许多 laravel 项目中…

    2025年12月24日
    000
  • 为什么使用 :global 修改 Antd 样式无效?

    :global 修改 antd 样式为何无效 本文旨在帮助您解决在组件内使用:global修改 antd 全局样式未生效的问题。 问题描述 您在组件内使用:global修改 antd 按钮样式,但没有生效。完整代码可参考 https://codesandbox.io/s/fk7jnl 。 解决方案 …

    2025年12月24日
    000
  • 如何在移动端实现子 div 在父 div 内任意滑动查看?

    如何在移动端中实现让子 div 在父 div 内任意滑动查看 在移动端开发中,有时我们需要让子 div 在父 div 内任意滑动查看。然而,使用滚动条无法实现负值移动,因此需要采用其他方法。 解决方案: 使用绝对布局(absolute)或相对布局(relative):将子 div 设置为绝对或相对定…

    2025年12月24日
    000
  • 移动端嵌套 DIV 中子 DIV 如何水平滑动?

    移动端嵌套 DIV 中子 DIV 滑动 在移动端开发中,遇到这样的问题:当子 DIV 的高度小于父 DIV 时,无法在父 DIV 中水平滚动子 DIV。 无限画布 要实现子 DIV 在父 DIV 中任意滑动,需要创建一个无限画布。使用滚动无法达到负值,因此需要使用其他方法。 相对定位 一种方法是将子…

    2025年12月24日
    000
  • 为什么在 React 组件中无法获得 Tailwind CSS 语法提示?

    为什么在 React 组件中无法获得 Tailwind CSS 语法提示? 你在 VSCode 中编写 HTML 文件时,可以正常获取 Tailwind CSS 语法提示。但当你尝试在 React 组件中编写 Tailwind CSS 时,这些提示却消失不见了。这是什么原因造成的? 解决方案 要解决…

    2025年12月24日
    000
  • 移动端项目中,如何消除rem字体大小计算带来的CSS扭曲?

    移动端项目中消除rem字体大小计算带来的css扭曲 在移动端项目中,使用rem计算根节点字体大小可以实现自适应布局。但是,此方法可能会导致页面打开时出现css扭曲,这是因为页面内容在根节点字体大小赋值后重新渲染造成的。 解决方案: 要避免这种情况,将计算根节点字体大小的js脚本移动到页面的最前面,即…

    2025年12月24日
    000
  • Nuxt 移动端项目中 rem 计算导致 CSS 变形,如何解决?

    Nuxt 移动端项目中解决 rem 计算导致 CSS 变形 在 Nuxt 移动端项目中使用 rem 计算根节点字体大小时,可能会遇到一个问题:页面内容在字体大小发生变化时会重绘,导致 CSS 变形。 解决方案: 可将计算根节点字体大小的 JS 代码块置于页面最前端的 标签内,确保在其他资源加载之前执…

    2025年12月24日
    200
  • Nuxt 移动端项目使用 rem 计算字体大小导致页面变形,如何解决?

    rem 计算导致移动端页面变形的解决方法 在 nuxt 移动端项目中使用 rem 计算根节点字体大小时,页面会发生内容重绘,导致页面打开时出现样式变形。如何避免这种现象? 解决方案: 移动根节点字体大小计算代码到页面顶部,即 head 中。 原理: flexível.js 也遇到了类似问题,它的解决…

    2025年12月24日
    000
  • 如何在 VSCode 中为 React 组件启用 Tailwind CSS 提示?

    在 vscode 中为 react 组件启用 tailwind css 提示 如果你在使用 vscode 编写 react 组件时,发现 tailwind css 提示无法正常显示,这里有一个解决方法: 安装 tailwind css intellisense 插件 这是实现代码提示的关键,确保你已…

    2025年12月24日
    200

发表回复

登录后才能评论
关注微信