[PYTHON] Frequently used syntax memorandum for each language

Introduction

I'll forget it soon. I will write it down little by little. We are dealing with C #, Java, Scala, JavaScript, Python3, PHP. Scala Because I'm a beginner, I thought I'd put it together anyway.

The link destination to the sample is "JSFiddle" for JavaScript, and "Ideone" for others. Refer to here for environment construction: Environment construction for each language, simple confirmation and skill test --Qiita

Input / output

Hello World

C#


using System;
class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("Hello " + Console.ReadLine() + "!");
    }
}

Java


import java.util.*;
public class Main {
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        System.out.println("Hello " + sc.next() + "!");
    }
}

Scala


object Main extends App {
    println("Hello " + readLine + "!");
}

JavaScript


function Main(input) {
  alert('Hello ' + input + '!');
}
Main('World');

Python3


print("Hello " + input() + "!")

PHP


<?php
fscanf(STDIN, "%s", $s);
echo "Hello " . $s . "!";

Array manipulation

Initialization

-C #: Array initialization

C#


int[] ary1 = {1, 2, 3};
int[,] ary2 = {{11, 12, 13}, {21, 22, 23}, {31, 32, 33}};
int[][] ary3 = {new int[]{11, 12, 13}, new int[]{21, 22, 23}, new int[]{31, 32, 33}};

-Java: Array initialization

Java


int ary1[] = {1, 2, 3};
int ary2[][] = {{11, 12, 13}, {21, 22, 23}, {31, 32, 33}};

-Scala: Array initialization

Scala


var ary1 = Seq(1, 2, 3);
var ary2 = Seq(Seq(11, 12, 13), Seq(21, 22, 23), Seq(31, 32, 33));

-JavaScript: Array initialization

JavaScript


var ary1 = [1, 2, 3];
var ary2 = [[11, 12, 13], [21, 22, 23], [31, 32, 33]];

-Python3: Array initialization

Python3


ary1 = [1, 2, 3]
ary2 = [[11, 12, 13], [21, 22, 23], [31, 32, 33]]

-PHP: Array initialization

PHP


$ary1 = [1, 2, 3];
$ary2 = [[11, 12, 13], [21, 22, 23], [31, 32, 33]];

File operations

Input / output

--C #: File input / output

C#


using System.IO;
using System.Text;

var text = File.ReadAllText(@"C:\hoge1.txt", Encoding.UTF8);
var lines = File.ReadAllLines(@"C:\hoge1.txt", Encoding.UTF8);

File.WriteAllText(@"C:\hoge2.txt", "Hoge", Encoding.UTF8);
File.WriteAllLines(@"C:\hoge2.txt", lines, Encoding.UTF8);

--Java: File input / output

Java


import java.util.List;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.charset.StandardCharsets;

List<String> lines = Files.readAllLines(Paths.get("C:\\hoge1.txt"), StandardCharsets.UTF_8);

Files.write(Paths.get("c:\\hoge2.txt"), lines, StandardCharsets.UTF_8);
Files.write(Paths.get("c:\\hoge3.txt"), "Hoge".getBytes(StandardCharsets.UTF_8));

--Scala: File input / output

For now I don't know of a better way than the same as Java.

-JavaScript: File input / output

Let's see various things.

html


<form>
  <input type="file" id="file"/><br>
  <textarea id="write"></textarea><br>
  <a id="download" href="#" download="hoge.txt">download</a>
</form>

JavaScript


document.getElementById("file").addEventListener('change', f => {
  var reader = new FileReader();
  reader.readAsText(f.target.files[0]);
  reader.addEventListener( 'load', () => {
    document.getElementById("write").value = reader.result;
  });
});
document.getElementById("download").addEventListener('click', () => {
  var text = document.getElementById("write").value;
  var blob = new Blob([ text ], { "type" : "text/plain" });
  document.getElementById("download").href = window.URL.createObjectURL(blob);
});

--Python3: File input / output

Python3


text = open('C:\hoge1.txt', 'r', encoding='utf-8').read()
open('C:\hoge2.txt', 'w', encoding='utf-8').write('Hoge')

--PHP: File input / output

PHP


$text = file_get_contents('C:\hoge1.txt');
file_put_contents('C:\hoge2.txt', 'Hoge');

Added at any time

It's quite troublesome, so let's add it little by little ...

-C #: Add at any time

C#


-Java: Add as needed

Java


-Scala: Add as needed

Scala


-JavaScript: added at any time

JavaScript


-Python3: Added at any time

Python3


-PHP: Add as needed

PHP


Recommended Posts

Frequently used syntax memorandum for each language
Frequently used Linux commands (for beginners)
Create execution environment for each language with boot2docker
[Linux command] A memorandum of frequently used commands
[For beginners] Django Frequently used commands and reference collection
Qiita Syntax Highlighting Memorandum
pyenv Frequently used commands
[For recording] Pandas memorandum
Frequently used tmux commands
Frequently used Linux commands
Frequently used Linux commands
Frequently used linux commands
Freecad memorandum (for myself)
Frequently used pip commands
Behavior in each language when coroutines are reused with for
Environment construction, simple confirmation and skill test for each language