发布于 2026-01-06 1 阅读
0

一篇博客文章即可学会 Python、PHP、Ruby 和 Javascript

一篇博客文章即可学会 Python、PHP、Ruby 和 Javascript

入门

下面我们将介绍四种不同编程语言中所有主要编程概念的模式。要尝试这些模式,请访问 repl.it 网站,创建一个免费的练习环境。

我们将遵循学习一门新语言时应该始终遵循的步骤:

  1. 学习如何将值打印到控制台。
  2. 学习如何赋值变量
  3. 条件
  4. 循环
  5. 收藏
  6. 函数

掌握了这些基础知识后,你就可以开始进一步挑战自己,并在 CodeWars.com 等网站上进行挑战。

将值打印到控制台

通常情况下,你会通过命令行 shell(Mac/Linux 上的 Bash,Windows 上的 cmd/powershell/gitBash)与编程语言进行交互。一旦你的电脑上安装了这些语言,你就可以编写文件并通过命令运行它们。除了偶尔在控制台打印一些值来确保程序正在执行指令之外,我们无法直观地看到代码的运行过程。

语言 文件扩展名 运行脚本命令 包装经理 部门清单
Javascript .js node 文件.js npm package.json
Python .py python 文件.py 管道 requirements.txt
红宝石 .rb ruby 文件.rb 宝石 Gemfile
PHP .php php 文件.php 作曲家 composer.json

这就是为什么你创建的第一个程序总是“Hello World”。

Javascript(使用NodeJS)

console.log("Hello World")
Enter fullscreen mode Exit fullscreen mode

红宝石

puts "hello world"
Enter fullscreen mode Exit fullscreen mode

Python

print("Hello World")
Enter fullscreen mode Exit fullscreen mode

PHP

<?php

echo "hello world";
?>
Enter fullscreen mode Exit fullscreen mode

变量

编程的本质在于编写动态代码。我们常常会遇到一些未知或随时间变化的值。与其将这些值硬编码到代码中,使代码变得死板僵化,不如使用变量来存储和引用这些值。

数据类型有多种:

  1. 数字——可用于数学表达式的数值数据
  2. 字符串——通常用引号括起来的字母、文本和符号序列
  3. 布尔值——一个可以表示真或假的值
  4. 无——表示没有值,称为 null、nil、undefined 等。

我们可以将这些值存储在称为变量的小格子里,然后我们可以用变量来引用这些值。

Javascript(使用NodeJS)

let myString = "Hello World"
let myNumber = 5
let myBool = true

console.log(myString)
console.log(myNumber)
console.log(myBool)
Enter fullscreen mode Exit fullscreen mode

红宝石

my_string = "Hello World"
my_number = 5
my_boolean = true

puts my_string
puts my_number
puts my_boolean
Enter fullscreen mode Exit fullscreen mode

Python

my_string = "Hello World"
my_number = 5
my_boolean = True

print(my_string)
print(my_number)
print(my_boolean)
Enter fullscreen mode Exit fullscreen mode

PHP

<?php

$my_string = "Hello World";
$my_number = 5;
$my_boolean = true;

echo $my_string;
echo $my_number;
echo $my_boolean;
?>
Enter fullscreen mode Exit fullscreen mode

条件

脚本的实用之处不仅在于能够概述一系列操作,还在于能够根据相关数据的状态来调整这些操作。if 语句允许我们询问“这个条件是否为真?”,然后根据其真假来决定脚本的执行步骤。

Javascript(使用NodeJS)

let number = 6

if (number > 5){
    console.log(true)
} else {
    console.log(false)
}

Enter fullscreen mode Exit fullscreen mode

红宝石

number = 6

if number > 5
    puts true
else
    puts false
end
Enter fullscreen mode Exit fullscreen mode

Python

number = 6

if (number > 5):
    print(True)
else:
    print(False)
Enter fullscreen mode Exit fullscreen mode

PHP

<?php
$number = 6

if ($number > 5){
    echo true;
} else {
    echo false;
}
?>
Enter fullscreen mode Exit fullscreen mode

循环

如果需要多次重复执行某个任务,一遍又一遍地输入指令会非常繁琐。针对这种情况,我们可以使用循环,只要某个表达式为真,循环就会重复执行一组指令,一旦表达式变为假,循环就会停止。

Javascript(使用NodeJS)

let counter = 0

while (count < 10){
    console.log(count)
    count = count + 1
}
Enter fullscreen mode Exit fullscreen mode

红宝石

counter = 0

while counter < 10
    puts counter
    counter = counter + 1
end
Enter fullscreen mode Exit fullscreen mode

Python

counter = 0

while (counter < 10):
    print(counter)
    counter = counter + 1
Enter fullscreen mode Exit fullscreen mode

PHP

<?php
$counter = 0;

while($counter < 10){
    echo counter;
    $counter = $counter + 1
}
?>
Enter fullscreen mode Exit fullscreen mode

收藏

集合是编程语言中的一种数据结构,它可以存储多个值。它们通常分为两类。

  • 一个有序的值列表,可通过从 0 开始的数值索引访问。
  • 通过“键”(通常是字符串/符号)访问的值列表
语言 使用数值索引 使用按键
Javascript 数组 对象
红宝石 数组 哈希
Python 列表 字典
PHP 数组 关联数组

Javascript(使用NodeJS)

let myArray = ["Alex Merced", 35]
console.log(myArray[0])
console.log(myArray[1])

let myObject = {name: "Alex Merced", age: 35}
console.log(myObject.name)
console.log(myObject.age)
Enter fullscreen mode Exit fullscreen mode

红宝石

my_array = ["Alex Merced", 35]
puts my_array[0]
puts my_array[1]

my_hash = {name: "Alex Merced", age: 35}
puts my_hash[:name]
puts my_hash[:age]
Enter fullscreen mode Exit fullscreen mode

Python

my_list = ["Alex Merced", 35]
print(my_list[0])
print(my_list[1])

my_dictionary = {"name": "Alex Merced", "age": 35}
print(my_dictionary["name"])
print(my_dictionary["age"])
Enter fullscreen mode Exit fullscreen mode

PHP

<?php
$my_array = ["Alex Merced", 35];
echo $my_array[0];
echo $my_array[1];

$my_associative = ["name" => "Alex Merced", "age" => 35];
echo $my_associative["name"];
echo $my_associative["age"];
?>
Enter fullscreen mode Exit fullscreen mode

函数

函数就像魔法咒语,它们执行特定的操作,并且可以随时随地重复使用。信息可以以参数的形式传递给函数,这些参数存储在称为参数变量的变量中。函数是实现强大编程功能的关键。

Javascript(使用NodeJS)

// x is parameter variable that will receive the first argument
function addOne(x){
    //the return value is what the function gives back when its done
    return x + 1
}

//Several invocations of the function passing different arguments
const result1 = addOne(1)
console.log(result1)

const result2 = addOne(2)
console.log(result2)
Enter fullscreen mode Exit fullscreen mode

红宝石

def addOne x
    return x + 1
end

result1 = addOne 1
puts result1

result2 = addOne 2
puts result2
Enter fullscreen mode Exit fullscreen mode

Python

def addOne(x):
    return x + 1

result1 = addOne(1)
print(result1)

result2 = addOne(2)
print(result2)
Enter fullscreen mode Exit fullscreen mode

PHP

<?php
function addOne(x){
    return x + 1;
}

$result1 = addOne(1);
echo $result1;

$result2 = addOne(2);
echo $result2;

?>
Enter fullscreen mode Exit fullscreen mode

结论

希望这能为你学习这些语言打下良好的基础。以下是一些你可以查找和尝试的主题,以提升你的语言能力。

  • 字符串操作(连接、插值等)
  • 数组/列表操作(添加、删除、查找元素)
  • 类和面向对象编程

当你真正熟练掌握任何一种编程语言后,就可以尝试使用 Web 框架构建 Web 应用程序了。这些框架有极简型和功能型两种。下表列出了每种编程语言的主要框架。

语言 极简主义 Web 框架 有主见的 Web 框架
Python Flask、FastAPI Django,Masonite
红宝石 辛纳屈 Ruby on Rails
PHP 苗条的 Laravel、Symphony
Javascript Express、Koa、Fastify FoalTS、NestJS、Sails
文章来源:https://dev.to/alexmercedcoder/learn-python-php-ruby-and-javascript-in-one-blog-post-2n0p