/python-vs-javascript

파이썬과 자바스크립트의 기초 문법을 비교합니다.

Python vs Javascript

  • 파이썬과 자바스크립트의 기초 문법을 비교합니다.
  • 자바스크립트를 먼저 학습한 상태에서 둘의 다른 점 위주로 정리했습니다.
  • 내용 추가 중입니다.

Index


Print

# PY
print()
// JS
console.log();

Input

# PY
value = input('입력: ')
type(value) # <class 'str'>

Type Check

# PY
type('abcde') # <type 'str'>
type(5) # <type 'int'>
type(4.7) # <type 'float'>
// JS
typeof 'abcde'; // 'string'

Type Casting

# PY
a = int('52')
b = float('52.2')
c = int('abc')

print(a) # 52
print(b) # 52.2
print(c) # Value Error

print(type(a)) # <class 'int'>
print(type(b)) # <class 'float'>
// JS
var a = '52';
var b = '52.2';
var c = 'abc';

parseInt(a); // 52
parseFloat(b); // 52.2
parseInt(c); // NaN

String

line break

# PY
multiline = '''
Life is too short
You need python
'''

len('str')

# PY
len('abcde') # 5
// JS
'abcde'.length; // 5

str.slice()

# PY
'abcde'[0:2] # 'ab'
'abcde'[2:] # 'cde'
'abcde'[:2] # 'ab'
'abcde'[3:-1] # 'd'
'abcde'[::2] # 'ace'
'abcde'[::-1] # 'edcba'
// JS
'abcde'.slice(0, 2); // 'ab'

str.format()

# PY
result = '{}'.format(11)
result # '11'
print(type(result)) # <class 'str'>

a = '{}원'.format(3000)
print(a) # '3000원'

b = '{} {} {}'.format(1, 2, 3)
print(b) # '1 2 3'

c = '{} {} {}'.format(1, '문자', True)
print(c) # '1 문자 True'

# '{}' 개수가 format() 함수의 매개변수 개수보다 많으면 에러
'{} {}'.format(1, 2, 3, 4) # '1 2'
'{} {} {}'.format(1, 2) # Index Error

'Hey, {name}. It\'s {num}PM.'.format(name='Hank', num=3)
# "Hey, Hank. It's 3PM."

'Hey, {0}. It\'s {1}PM.'.format('Hank', 3)
# "Hey, Hank. It's 3PM."

f-string (3.6v+)

# PY
name = 'Sarah'
sister = '\'s sister'
age = 27

f'{name} is {age} years old.'
# 'Sarah is 27 years old.'
f'{name} will be {age + 1} years old next year.'
# 'Sarah will be 28 years old next year.'
f'{name + add} will be {age + 3} years old next year.'
# "Sarah's sister will be 30 years old next year."

d = {'name': 'Sarah', 'age': 27} # dictionary
f'{d["name"]} will be {d["age"] + 1} years old next year.'
# 'Sarah will be 28 years old next year.'

str.upper() / str.lower()

# PY
word = 'abcde'
word.upper() # 'ABCDE'
print(word) # 'abcde'

WORD = 'ABCDE'
WORD.lower() # 'abcde'
print(WORD) # 'ABCDE'

# 원본을 변화시키지 않는 '비파괴적 함수'
// JS
var str = 'abc';
str.toUpperCase(); // 'ABC'
console.log(str); // 'abc'

str.capitalize()

# PY
a = 'a duck goes into a bar'
a.capitalize() # 'A duck goes into a bar'

str.title()

# PY
a = 'a duck goes into a bar'
a.title() # 'A Duck Goes Into A Bar'

str.swapcase()

# PY
a = 'abCDEfg'
a.swapcase() # 'ABcdeFG'

str.strip()

# PY
a = '''
  hello        '''
a.strip() # 'hello'
print(a) # '\n  hello        '

b = 'aaabbcca'
b.strip('a') # 'bbcc'
// JS
var str = '    abc  ';
str.trim(); // 'abc'

str.count()

# PY
fruit = 'banana'
fruit.count('a') # 3
// JS
var fruit = 'banana';
var result = fruit.match(/a/g); // ["a", "a", "a"]
result.length; //3

str.find() / str.rfind()

# PY
fruit = 'banana'
fruit.find('n') # 2
fruit.rfind('n') # 4
fruit.find('c') # 없으면 -1

str.index()

# PY
fruit = 'banana'
fruit.index('a') # 1
fruit.index('c') # 없으면 Value Error
// JS
var fruit = 'banana';
fruit.indexOf('n'); // 2
fruit.indexOf('c'); // -1

str.replace()

# PY
a = 'Life is too short'
a.replace('Life', 'Your leg') # 'Your leg is too short'
a.replace('Like', 'Your leg') # 'Life is too short'
// JS
var a = 'Life is too short';
a.replace('Life', 'Your leg'); // 'Your leg is too short'

str.split()

# PY
a = 'Life is too short'
a.split() # ['Life', 'is', 'too', 'short']
a.split( ) # ['Life', 'is', 'too', 'short']
a.split('') # Value Error: empty separator
a.split(' ') # ['Life', 'is', 'too', 'short']
a.split('i') # ['L', 'fe ', 's too short']
// JS
var a = 'Life is too short';
a.split(); // ["Life is too short"]
a.split(''); //  ["L", "i", "f", "e", " ", "i", "s", " ", "t", "o", "o", " ", "s", "h", "o", "r", "t"]
a.split(' '); // ["Life", "is", "too", "short"]
a.split('i'); // ["L", "fe ", "s too short"]

join

# PY
','.join('abcd') # 'a,b,c,d'
' '.join('abcd') # 'a b c d'
','.join(['a', 'b', 'c', 'd']) # 'a,b,c,d'
''.join(['a', 'b', 'c', 'd']) # 'abcd'
// JS
var arr = ['a', 'b' ,'c' ,'d'];
arr.join(); // 'a,b,c,d'
arr.join(''); // 'abcd'
arr.join(' '); // 'a b c d'

str1 in str2

# PY
a = 'Life is too short'
'Life' in a # True
'life' in a # False
'Like' in a # False
// JS
var a = 'Life is too short';
a.includes('Life'); // true
a.includes('life'); // false
a.includes('Like'); // false

str.is~~()

# PY
a = '12ab@#'
b = '12abc'
c = 'abcde'
d = '123'
e = 'aBCdE'
f = 'ABC'

a.isalnum() # False
b.isalnum() # True
c.isalpha() # True
c.isdigit() # False
d.isdigit() # True
e.islower() # False
f.isupper() # True

Number

/

# PY
print(5 / 0) # ZeroDivisionError: division by zero
// JS
console.log(5 / 0); // Infinity

//

# PY
3 // 2 # 1
// JS
var num = 3.23123;
Math.floor(num); // 3

**

# PY
2 ** 3 # 8
pow(2, 3) # 8
// JS
Math.pow(2, 3); // 8

abs

# PY
abs(-1) # 1

import math
math.fabs(-1) # 1.0
// JS
Math.abs(-1); // 1

Boolean

not

# PY
not True # False
not False # True
not not True # True
// JS
!true // false
!false // true
!!true // true

List

create list

# PY
empty_list = []
another_empty_list = list()

weekdays = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri']

list('cat') # ['c', 'a', 't']

t = ('a', 'b', 'c') # tuple
list(t) # ['a', 'b', 'c']
// JS
var empty_array = []
var another_empty_array = new Array()

var weekdays = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri']

index

# PY
weekdays = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri']
weekdays[0] # 'Mon'
weekdays[-1] # 'Fri'
// JS
var weekdays = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri'];
weekdays[0]; // 'Mon'
weekdays[-1]; // undefined
weekdays[weekdays.length - 1]; // 'Fri'

slice

# PY
weekdays = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri']
weekdays[0:2] # ['Mon', 'Tue']
weekdays[::2] # ['Mon', 'Wed', 'Fri']
weekdays[::-1] # ['Fri', 'Thu', 'Wed', 'Tue', 'Mon']

weekdays # ['Mon', 'Tue', 'Wed', 'Thu', 'Fri'] -> 원본 그대로 유지
// JS
var weekdays = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri'];

weekdays.slice(0, 2); // ["Mon", "Tue"]
weekdays // ["Mon", "Tue", "Wed", "Thu", "Fri"] -> 원본 그대로 유지

weekdays.reverse(); // ["Fri", "Thu", "Wed", "Tue", "Mon"]
weekdays // ["Fri", "Thu", "Wed", "Tue", "Mon"] -> 원본 변경

list.append()

# PY
week = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri']
week.append('Sat') 
week # ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat']
// JS
var week = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri'];
week.push('Sat'); // 6
week // ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat"]

list.extend() / +=

# PY
weekdays = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri']
weekend = ['Sat', 'Sun']

weekdays.extend(weekend) 
weekdays # ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']

weekdays += weekend 
weekdays # ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
// JS
var weekdays = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri'];
var weekend = ['Sat', 'Sun'];

weekdays.concat(weekend); // ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]
weekdays; // ["Mon", "Tue", "Wed", "Thu", "Fri"] -> 원본 그대로 유지

weekdays.push(...weekend); // 7
weekdays; // ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]

list.insert()

# PY
alphabet = ['a', 'c', 'd', 'e']
alphabet.insert(1, 'b') 
alphabet # ['a', 'b', 'c', 'd', 'e']
alphabet.insert(10, 'z')
alphabet # ['a', 'b', 'c', 'd', 'e', 'z']
// JS
var alphabet = ['a', 'c', 'd', 'e'];
alphabet.splice(1, 0, 'b'); // []
alphabet; // ["a", "b", "c", "d", "e"]

list.remove()

# PY
alphabet = ['a', 'a', 'b', 'c', 'd', 'e']
alphabet.remove('b') 
alphabet # ['a', 'a', 'c', 'd', 'e']
alphabet.remove('a')
alphabet # ['a', 'c', 'd', 'e']
alphabet.remove('b') # ValueError: list.remove(x): x not in list

list.pop()

# PY
alphabet = ['a', 'b', 'c', 'd', 'e']
alphabet.pop() # 'e'
alphabet # ['a', 'b', 'c', 'd']
alphabet.pop(-1) # 'd'
alphabet # ['a', 'b', 'c']
alphabet.pop(0) # 'a'
alphabet # ['b', 'c']

empty_list = []
empty_list.pop() # IndexError: pop from empty list
// JS
var alphabet = ['a', 'b', 'c', 'd', 'e'];
alphabet.pop(); // "e"
alphabet; // ["a", "b", "c", "d"]
alphabet.shift(); // "a"
alphabet; // ["b", "c", "d"]

var empty_array = [];
empty_array.pop(); // undefined

del (list)

# PY
alphabet = ['a', 'b', 'c', 'd', 'e']
del(alphabet[2])
alphabet # ['a', 'b', 'd', 'e']

alphabet = ['a', 'b', 'c', 'd', 'e']
del(alphabet[0:2])
alphabet # ['c', 'd', 'e']

alphabet = ['a', 'b', 'c', 'd', 'e']
del(alphabet[:])
alphabet # []
// JS
var alphabet = ['a', 'b', 'c', 'd', 'e'];
alphabet.splice(0, 2); // ["a", "b"]
alphabet; // ["c", "d", "e"]

alphabet.splice(0); // ["c", "d", "e"]
alphabet; // []

list.clear()

# PY
alphabet = ['a', 'b', 'c', 'd', 'e']
alphabet.clear()
alphabet # []

list.index()

# PY
names = ['Smith', 'Maria', 'John', 'Tracy']
names.index('Maria') # 1
// JS
var names = ['Smith', 'Maria', 'John', 'Tracy'];
names.indexOf('Maria'); // 1

in (list)

# PY
names = ['Smith', 'Maria', 'John', 'Tracy']
'John' in names # True
'Emily' in names # False
// JS
var names = ['Smith', 'Maria', 'John', 'Tracy'];
names.includes('John'); // true
names.includes('Emily'); // false

list.count()

names = ['Smith', 'Maria', 'John', 'Tracy']
names.count('John') # 1
names.count('Emily') # 0

hellos = ['hello', 'hello', 'hello']
hellos.count('hello') # 3
// JS
var names = ['Smith', 'Maria', 'John', 'Tracy'];
var count = 0;
for (var i = 0; i < names.length; i++) {
	if (names[i] === 'John') {
		count++;
	}
}
console.log('count', count); # 1

list.sort() / sorted(list)

# PY
alphabet = ['d', 'a', 'b', 'c']
alphabet.sort()
alphabet # ['a', 'b', 'c', 'd'] -> 원본 배열 수정

alphabet = ['d', 'a', 'b', 'c']
sorted(alphabet) # ['a', 'b', 'c', 'd'] -> 복사본 반환
alphabet # ['d', 'a', 'b', 'c']

numbers = [10, 1, 13, 1.3, 2, 2.222]
numbers.sort
numbers # [1, 1.3, 2, 2.222, 10, 13]

numbers = [10, 1, 13, 1.3, 2, 2.222]
numbers.sort(reverse=True)
numbers # [13, 10, 2.222, 2, 1.3, 1]
// JS
var alphabet = ['d', 'a', 'b', 'c'];
alphabet.sort(); // ["a", "b", "c", "d"]
alphabet; // ["a", "b", "c", "d"] -> 원본 배열 수정

var numbers = [10, 1, 13, 3, 2, 22];
numbers.sort(); // [1, 10, 13, 2, 22, 3]
numbers.sort((a, b) => a - b); // [1, 2, 3, 10, 13, 22]
numbers.sort((a, b) => b - a); // [22, 13, 10, 3, 2, 1]

len(list)

# PY
names = ['Smith', 'Maria', 'John', 'Tracy']
len(names) # 4
// JS
var names = ['Smith', 'Maria', 'John', 'Tracy'];
names.length // 4 

list.copy()

# PY
a = [1, 2, 3]
b = a 
b # [1, 2, 3]
a[0] = 'a'
a # ['a', 2, 3]
b # ['a', 2, 3]

a = [1, 2, 3]
b = a.copy()
c = list(a)
d = a[:]
a[0] = 'a'
a # ['a', 2, 3]
b # [1, 2, 3]
c # [1, 2, 3]
d # [1, 2, 3]
// JS
var a = [1, 2, 3];
b = a;
b; // [1, 2, 3]
a[0] = 'a';
a; // ['a', 2, 3]
b; // ['a', 2, 3]

var a = [1, 2, 3];
b = a.slice();
c = [...a];
a[0] = 'a';
a; // ['a', 2, 3]
b; // [1, 2, 3]
c; // [1, 2, 3]

Tuple

create tuple

# PY
empty_tuple = ()
names = ('Emily', 'Sally', 'Tom')

unpacking

# PY
names = ('Emily', 'Sally', 'Tom')
a, b, c = names
a # 'Emily'
b # 'Sally'
c # 'Tom'
d # NameError: name 'x' is not defined

a, b, c = c, a, b
a # 'Tom'
b # 'Emily'
c # 'Sally'

Dictionary

create dictionary

# PY
empty_dict = {}
another_empty_dict = dict()

lol = [['a', 'b'], ['c', 'd'], ['e', 'f']]
dict(lol) # {'a': 'b', 'c': 'd', 'e': 'f'}

lot = [('a', 'b'), ('c', 'd'), ('e', 'f')]
dict(lot) # {'a': 'b', 'c': 'd', 'e': 'f'}

tol = (['a', 'b'], ['c', 'd'], ['e', 'f'])
dict(tol) # {'a': 'b', 'c': 'd', 'e': 'f'}

los = ['ab', 'cd', 'ef']
dict(los) # {'a': 'b', 'c': 'd', 'e': 'f'}

tos = ('ab', 'cd', 'ef')
dict(tos) # {'a': 'b', 'c': 'd', 'e': 'f'}
// JS
var empty_obj = {};
var another_empty_obj = new Object();
var obj = {'a': 'b', 'c': 'd', 'e': 'f'}

dict.update()

# PY
dict1 = {'a': 1}
dict2 = {'b': 2}
dict1.update(dict2)

dict1 # {'a': 1, 'b': 2}
dict2 # {'b': 2}
// JS
var obj1 = {'a': 1};
var obj2 = {'b': 2};
Object.assign(obj1, obj2);

obj1; // {a: 1, b: 2}
obj2; // {b: 2}

del (dict)

# PY
dict1 = {'a': 1, 'b': 2}
del dict1['a']
dict1 # {'b': 2}

del dict1['c'] # KeyError: 'c'
// JS
var obj = {'a': 1, 'b': 2}
delete obj.a // true
obj // {b: 2}

delete obj.c // true
obj // {b: 2}

dict.clear()

# PY
dict1 = {'a': 1, 'b': 2}
dict1.clear()
dict1 # {}

dict1 = {'a': 1, 'b': 2}
dict1 = {}
dict1 # {}

in (dict)

# PY
dict1 = {'a': 1, 'b': 2, 'c': 3}
'a' in dict1 # True
'd' in dict1 # False
// JS
var obj = {'a': 1, 'b': 2, 'c': 3}
Object.keys(obj).includes('a') // true
Object.keys(obj).includes('d') // false

dict.get()

# PY
dict1 = {'a': 1, 'b': 2, 'c': 3}

dict1['a'] # 1
dict1['c'] # KeyError: 'd'

dict1.get('a') # 1
dict1.get('d') # 아무것도 출력 안됨
dict1.get('d', 'Nope') # 'Nope' -> 옵션값 지정 가능
// JS
var obj = {'a': 1, 'b': 2, 'c': 3};
obj.a; // a
obj.d; // undefined

dict.keys()

# PY
dict1 = {'a': 1, 'b': 2, 'c': 3}
dict1.keys() # dict_keys(['a', 'b', 'c'])
// JS
var obj = {'a': 1, 'b': 2, 'c': 3};
Object.keys(obj); // ["a", "b", "c"]

dict.values()

# PY
dict1 = {'a': 1, 'b': 2, 'c': 3}
dict1.values() # dict_values([1, 2, 3])
// JS
var obj = {'a': 1, 'b': 2, 'c': 3};
Object.values(obj); // [1, 2, 3]

dict.items()

# PY
dict1 = {'a': 1, 'b': 2, 'c': 3}
dict1.items() # dict_items([('a', 1), ('b', 2), ('c', 3)])

dict.copy()

# PY
dict1 = {'a': 1, 'b': 2}
dict2 = dict1
dict1['b'] = 3
dict1 # {'a': 1, 'b': 3}
dict2 # {'a': 1, 'b': 3}

dict1 = {'a': 1, 'b': 2}
dict2 = dict1.copy()
dict1['b'] = 3
dict1 # {'a': 1, 'b': 3}
dict2 # {'a': 1, 'b': 2}
// JS
var obj1 = {'a': 1, 'b': 2};
obj2 = obj1;
obj1.b = 3;
obj1; // {a: 1, b: 3}
obj2; // {a: 1, b: 3}

var obj1 = {'a': 1, 'b': 2};
obj2 = {...obj1};
obj1.b = 3;
obj1; // {a: 1, b: 3}
obj2; // {a: 1, b: 2}

Conditions

If statement

# PY
a = 200
b = 33

if b > a:
  print("b is greater than a")
elif a == b:
  print("a and b are equal")
else:
  print("a is greater than b")

short hand If

# PY
a = 200
b = 33

if a > b: print('a is greater than b') # 'a is greater than b'
// JS
var a = 200;
var b = 33;

if (a > b) console.log('a is greater than b'); // 'a is greater than b'

short hand If ... Else

# PY
a = 200
b = 33

print('A') if a > b else print('B') # A
// JS
var a = 200;
var b = 33;

a > b ? console.log('A') : console.log('B'); // A

Ternary

# PY
a = 200
b = 33

print('A') if a > b else print('=') if a == b else print('B')
// JS
var a = 200;
var b = 33;

a > b ? console.log('A') : a === b ? console.log('=') : console.log('B');

Loop

While loop

# PY
i = 1
while i < 6:
  print(i)
  i += 1

# 1 2 3 4 5