
本文对 javascript 和 python 的语法和基本编程结构进行了比较。它旨在强调这两种流行的编程语言在实现基本编程概念方面的相似之处。
虽然两种语言有许多共同点,使开发人员更容易在它们之间切换或理解对方的代码,但也应该注意明显的语法和操作差异。
重要的是要以轻松的角度进行这种比较,而不是过分强调 javascript 和 python 之间的相似或差异。目的不是要声明一种语言优于另一种语言,而是提供一种资源,可以帮助熟悉 python 的程序员更轻松地理解并过渡到 javascript。
javascript
// 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}')
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平台的博客。
本文受到其他编程语言之间类似比较的影响:
以上就是JavaScript 就像 Python的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号