python-javascript基礎構文

26944 ワード

List/arrayカウント


python
list1 = [1,2,3,4]
len(list1)
javascript
let arr = [1,2,3,4];
arr.length;

検査結果


python
list1 = [1,2,3,4]
print(list1)
javascript
let arr = [1,2,3,4];
console.log(arr);

データ型の検証


python
list1 = [1,2,3,4]
print(type(list1))
javascript
let arr = [1,2,3,4];
console.log(typeof arr);

データ型変換


Str --> Num


python
str1 = "3"
int(str1)
float(str1)
javascript
var str1     = "3";
var strInt   = parseInt(str1);
var strFloat = parseFloat(str1);
演算子「-」を使用して、数値上の点のみを変換します.

Num --> Str


python
num1 = 3
str(num1)
javascript
var num1     = 3;
var str1   = num1.toString();
数値+文字=>文字に変換された点

for loop list/array結果出力


python
list1 = [1,2,3,4]
for i in list1:
	print (list1)
javascript
//일반적 for loop
let arr = [1,2,3,4];
for (i =0; i<arr.length; i++){
  console.log(arr[i])
};
//for each 
let startWithNames = [];
let names = ['a', 'ab', 'cbb', 'ada'];
names.forEach(el => {   
  if (el.startsWith('a')) {     
   startWithNames.push(el);   
  } 
});

list expression

javascript
var cat = [
  {
    name: "russian blue",
    height: 120,
  },
  {
    name: "spynk",
    height: 80,
  },
  {
    name: "perssian",
    height: 70,
  },
];
let names = cat.map(({name})=>name)
for (const name of names) {
   console.log(name);
 }
 for (const element of array1) {
   console.log(element);
 }
const catobj = cat[0]{name}
console.log(catobj)

loop + index


python
list1 = [1,2,3,4]
for i in enumerate(list1):
	print (i)
javascript
let idxOfC = -1;
let arr = ['a', 'b', 'c', 'd'];
 
arr.forEach((el, idx) => {
  if (el === 'c') {
    idxOfC = idx;
    return console.log(el, idxOfc);
  }
});

f-string/templateテキスト-変数をテキストに挿入


python
age = 20
text = f'only age over {age} can drink liquor'
javascript
let age = 20;
let text = `only age over ${age} can drink liquor`;
`(backticで表す)

処理日


python
import datetime
dt = datetime.datetime.today()
year = dt.year
month = dt.month 
day = dt.day
hour = dt.hour
minute = dt.minute
second = dt.second
javascript
let rightNow = new Date();
let year = rightNow.getFullYear();
let month = rightNow.getMonth()+1;
let date = rightNow.getDate();
let day = rightNow.getDay();
let currentHour = rightNow.getHours();
let currentMin = rightNow.getMinutes();
getmonthは今の月より1ヶ月少ないです!だから+1を作る

整数端数処理


python
num = 3.4
#올림 
round(num + 0.5 )  # python round even (올리는 쪽이 짝수인 경우만 올림 적용 
# 내림 
int(num)
javascript
let num = 3.2
let roundedNum = Math.round(num);
let roundDown = Math.floor(num);
let roundUp = Math.ceil(num);

random


python
import random
print(random.randrange(3,10)) # 범위 내에 랜덤정수 
print(random.random())  # 0~1 사이 난수 생성
javascript
let rndInt = Math.floor(Math.random() * 6) + 1 //1~6 사이 랜덤정수
let randomNumber = Math.random();// 0~1 사이 난수 생성

関数宣言


python
def new_function(variable1):
	variable2 = variable1 +10
    return variable2
javascript
//기존 문법 (ES5)
function newFunction(variable1){
  var variable2 = variable2 + 10;
  return variable2;
};
//ES6 arrow function
const newFunction = variable1 => {   //매개변수가 1개일 때 표현가능한 방법
  const variable2 += 10;
  return variable2;
};
//매개변수가 2개  일 때 표현방식
const functionWithTwo =(variable1, variable2) ==> {
    return variable1 +variable2
}