Using the below you can see the basics of 10 different languages. For most of these langauges you should be able to try them out by generating a REPL.
How to print text to the console
One of the first things you need to learn in any language is how to print text in the console. Being able to print text to console allows us to…
- print the value of variables to check that they have the right values
- print the return values of functions so we can make sure they return the right value
- be used just to print text to confirm parts of our code are running
Javascript
console.log("Hello World")
Enter fullscreen mode Exit fullscreen mode
Python
print("Hello World")
Enter fullscreen mode Exit fullscreen mode
Ruby
puts "Hello World"
Enter fullscreen mode Exit fullscreen mode
PHP
<?php
echo "Hello World";
?>
Enter fullscreen mode Exit fullscreen mode
GO
import fmt
func main(){
fmt.Println("Hello World")
}
Enter fullscreen mode Exit fullscreen mode
Rust
fn main(){
print!("Hello World");
}
Enter fullscreen mode Exit fullscreen mode
Dart
void main(){
print("Hello, World!");
}
Enter fullscreen mode Exit fullscreen mode
C Sharp
using System;
namespace HelloWorldApp {
class HelloWorld {
static void Main(string[] args){
Console.WriteLine("Hello World");
}
}
}
Enter fullscreen mode Exit fullscreen mode
Java
class HelloWorld {
public static void main(String[] args){
System.out.println("Hello, World");
}
}
Enter fullscreen mode Exit fullscreen mode
Ballerina
import ballerina/io;
public function main() {
io:println("Hello World");
}
Enter fullscreen mode Exit fullscreen mode
Declaring Variables
Storing data to use is pivotal in programming. Data is generally stored in variables that we declare. These variables can hold data like numbers, strings and booleans (true/false).
Javascript
let number = 5
let str = "Hello"
let bool = true
console.log(number, str, bool)
Enter fullscreen mode Exit fullscreen mode
Python
number = 5
string = "hello"
boolean = True
print(number, string, boolean)
Enter fullscreen mode Exit fullscreen mode
Ruby
num = 5
str = "Hello"
bool = true
puts num, str, bool
Enter fullscreen mode Exit fullscreen mode
PHP
<?php
$num = 5;
$str = "Hello";
$bool = true;
echo $num;
echo $str;
echo $bool;
?>
Enter fullscreen mode Exit fullscreen mode
GO
import fmt
func main(){
num := 5
str := "Hello"
boolean := true
fmt.Println(num, str, boolean)
}
Enter fullscreen mode Exit fullscreen mode
Rust
fn main(){
let num = 5;
let string = "Hello";
let boolean = true;
print!("{0} - {1} - {2}", num, string, boolean );
}
Enter fullscreen mode Exit fullscreen mode
Dart
void main (){
var number = 5;
var string = "hello";
var boolean = true;
print(number, string, boolean);
}
Enter fullscreen mode Exit fullscreen mode
C Sharp
using System;
namespace MyProgramApp {
class MyProgram {
static void Main(string[] args){
int num = 5;
string str = "Hello";
bool boolean = true;
Console.WriteLine(num);
Console.WriteLine(str);
Console.WriteLine(boolean);
}
}
}
Enter fullscreen mode Exit fullscreen mode
Java
class Main {
public static void main(String[] args){
int num = 5;
String str = "Hello";
boolean bool = true;
System.out.println(num);
System.out.println(str);
System.out.println(bool);
}
}
Enter fullscreen mode Exit fullscreen mode
Ballerina
import ballerina/io;
public function main(){
int num = 5;
string str = "Hello";
boolean bool = true;
io:println(num);
io:println(str);
io:println(bool);
}
Enter fullscreen mode Exit fullscreen mode
Collection arrays and key/value pairs
Usually you have two main collections you’ll use most of the time.
-
Arrays/Lists that will be used to store data in an order that is referenced by a zero based index
-
A key/value pair structure by which you can reference different values based on a key.
Javascript
const myArray = [1,2,3,4,5]
const myObject = {name: "Alex Merced", age: 35}
console.log(myArray)
console.log(myObject)
Enter fullscreen mode Exit fullscreen mode
Python
my_list = [1,2,3,4,5]
my_dictionary = {"name": "Alex Merced, "age": 35} print(my_list) print(my_dictionary)
Enter fullscreen mode Exit fullscreen mode
Ruby
my_array = [1,2,3,4,5]
my_hash = {name: "Alex Merced", age: 35}
puts my_array
puts my_hash
Enter fullscreen mode Exit fullscreen mode
PHP
<?php
$my_array = [1,2,3,4,5];
$my_associative_array = ["name" => "Alex Merced", "age" => 35];
var_dump($my_array);
var_dump($my_associative_array);
?>
Enter fullscreen mode Exit fullscreen mode
GO
import fmt
func main(){
my_slice := []int{1,2,3,4,5}
my_map := map[string]string{"name":"alex merced", "age":"35"}
fmt.Println(my_slice)
fmt.Println(my_map)
}
Enter fullscreen mode Exit fullscreen mode
Rust
use std::collections::HashMap;
fn main(){
let my_array = [1,2,3,4,5];
let mut my_hashmap = HashMap::new();
my_hashmap.insert("name", "Alex Merced");
my_hashmap.insert("age", "35");
println!("{:?}", my_array);
println!("{:?}", my_hashmap);
}
Enter fullscreen mode Exit fullscreen mode
Dart
void main (){
final my_list = [1,2,3,4,5];
final my_map = {"name": "Alex Merced", "age":"35"}
print(my_list);
print(my_map);
}
Enter fullscreen mode Exit fullscreen mode
C Sharp
using System;
using System.Collections;
namespace MyProgramApp {
class MyProgram {
static void Main(string[] args){
int[] my_array = {1,2,3,4,5};
Hashtable my_ht = new Hashtable();
my_ht.Add("name", "Alex Merced");
my_ht.Add("age", "35");
Console.WriteLine(my_array);
Console.WriteLine(my_ht);
}
}
}
Enter fullscreen mode Exit fullscreen mode
Java
import java.util.*;
class MyProgram {
public static void main(String[] args){
int[] my_array = {1,2,3,4,5};
Hashtable my_ht = new Hashtable();
my_ht.put("name", "Alex Merced");
my_ht.put("age", "35");
System.out.println(my_array);
System.out.println(my_ht);
}
}
Enter fullscreen mode Exit fullscreen mode
Ballerina
import ballerin/io;
public function main(){
int[] my_array = [1, 2, 3, 4, 5];
map<string> my_map = {
"name": "Alex Merced",
"age" : "35"
};
io:println(my_array);
io:println(my_map);
}
Enter fullscreen mode Exit fullscreen mode
Defining Function
Functions allow you define blocks of code you can run on-demand. Pivotal to any programming language! There are two steps:
-
declare/define the function
-
call/invoke the function
Javascript
function helloWorld(){
console.log("Hello World")
}
helloWorld()
Enter fullscreen mode Exit fullscreen mode
Python
def hello_world():
print("Hello World")
hello_world()
Enter fullscreen mode Exit fullscreen mode
Ruby
def hello_world
puts "Hello World"
end
hello_world
Enter fullscreen mode Exit fullscreen mode
PHP
<?php
function helloWorld(){
echo "Hello World";
}
helloWorld();
?>
Enter fullscreen mode Exit fullscreen mode
GO
import fmt
func helloWorld(){
fmt.Println("hello world")
}
func main(){
helloWorld()
}
Enter fullscreen mode Exit fullscreen mode
Rust
fn hello_world(){
println!("Hello World");
}
fn main(){
hello_world();
}
Enter fullscreen mode Exit fullscreen mode
Dart
void hello_world(){
print("Hello World");
}
void main(){
hello_world()
}
Enter fullscreen mode Exit fullscreen mode
C Sharp
using System;
namespace MyProgramApp {
class MyProgram {
static void Main(string[] args){
HelloWorld();
}
static void HelloWorld(){
Console.WriteLine("Hello World");
}
}
}
Enter fullscreen mode Exit fullscreen mode
Java
class MyProgram {
public static void main(String[] args){
hello_world();
}
public static void hello_world(){
System.out.println("Hello, World");
}
}
Enter fullscreen mode Exit fullscreen mode
Ballerina
import ballerina/io;
function hello_world(){
io:println("Hello World");
}
public function main(){
hello_world();
}
Enter fullscreen mode Exit fullscreen mode
Learning More
- Find Video Playlists on All these Languages and more at devNursery
- Find more tutorials and walkthroughs on my blog
原文链接:10 Programming Languages Side by Side (JS, Python, Ruby, PHP, GO, Rust, Dart, C#, Java, Ballerina)
暂无评论内容