Skip to content

roxey804/codecompare

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

65 Commits
 
 

Repository files navigation

Code Compare

Side-by-side syntax "comparisons" between 3 languages: Python (3), JavaScript(ES6) and Ruby

Note: not every data type or function has a direct comparison in the other language, sometimes these are approximate.

Operation Python JS (ES6)
print print("Hello world!") console.log('Hello World!')
functions def myfunction():
   print("running")
function myFunction()
   { code here }
calling functions my_function() myFunction()
conditionals if condition:
   do sthing
else:
   do sthing else
if (condition) {
    statement }
else {
   statement }
inline conditional val_when_true if condition else val_when_false { condition? 'true':'false'}
safe navigation operator not yet obj.value?.property

In-depth comparison

Conditionals

Function Python JS (ES6) Notes
Conditionals if, elif, else if, else if, else -
Ternary operators val_when_true if condition else val_when_false { condition? 'true':'false'}
Switch/case - switch case expression
when condn
   statement

Comparison (equal/not equal) and Boolean (and/or/not) operators

Operator Python JS (ES6) Notes
Boolean bool(var) var.length
Falsey 0, 0.0, '' false, nil
Equal to == ==/=== ==
Not equal to != !=/!== !=
And and && &&
Or or || ||
Not not ! ! / unless?

Functions

Python JS (ES6) Notes
def myfunction(args) function myFunction() { //code here } JS functions and vars are defined using camelCase
JS Arrow func. syntax => const myFunction = () => { // code here } const myFunction = (args) => { // code here }
JS Arrow func syntax allows for single-line function definition If returning a JS object wrap it in ( ) => ({JS object})

Loops

Operator Python JS (ES6) Notes
For for i in range(x): - For loops iterate a specific no. of times
While while i < 3:``while True: - While loops iterate while a certain condn is true

Built-in functions e.g. length

Function Python JS (ES6) Notes
string str(var) -
int int(var) -
length len(var) -
type type(var) typeof(var) -
user input input("") window.prompt("") input is always a str by default
F and template strings f strings: f"Hello, {var}" Hello, ${name} whitespace is honoured in both
Sleep (delay) sleep(5) setTimeout(myFunc, 2000) where 2000 is in ms

Data structures

Data type Python JS (ES6) Notes
String "string" or 'string' -
Integer 1 -
Boolean True -
Mutable List [1,2,3] -
Unmutable list tuple (1,2,3) -
Key:value Dictionary {"key":"value"} JS Object -

Web development framework comparison

X Python JS (ES6) Ruby
Web development frameworks Flask, Django React, Vue, Angular, NextJS Sinatra, Ruby on Rails
Unit testing Pytest, Unitttest Jest (test runner) + React testing library/Cypress ] RSpec, minitest
Syntax linting pylint ESLint Rubocop
Frontend tests Selenium Cypress Capybara, Cucumber
X Python - Django React JS Ruby on Rails
Package manager pip, pipenv, poetry node, yarn RubyGems => bundler
Package config stored in: requirements.txt package.json/yarn.lock Gemfile.lock
Library installation pip install <lib> npm/yarn install <package> gem install <gem>
Library/package import import library import library / { Component } from require "package"

Frontend

Displaying variables

Framework Syntax name Example Notes
Django Jinja2 <p>{{ person.name }}</p> -
ReactJS JSX/ JS expression <p>{personName}</p> JSX is not readable by the browser, hence uses Babel

Language specific syntax

React Javascript

JSX is not HTML but a combination of JS + XML

React component syntax

<Component prop={} />

<ParentComponent>
 <ChildComponent>
</ParentComponent>

Object destructuring in React component props

Version React component props usage Notes
v1 using props export default function Menu(props) {.. <p>{props.title}</p>
v2 destructured export default function Menu({title, recipe}) {.. <p>{title}</p>

Python

Think of scope as a container

Local scope is temporary, created when a function is called and destroyed when the function returns. A local variable cannot be used in a global scope.

Global scopes are created outside a function

This allows functions to work as individual black boxes and helps isolate code.

variable = 'GLOBAL'

def my_function():
    variable = 'local'

Q In the below, what value of name will be printed out?

def func_1():
    name = 'Maria'
    func_2()
    print(name)
    
def func_2():
    name = 'Bob'

func_1()
Answer

Maria - as the scope of func_2 will be destroyed once it returns

The global keyword can be used to create global variables in a local context (within a function)

Q What will be the output if you run the below?

result = 0 # initial global variable

def add():
    global result
    result = result + 2 # increment by 2
    print("Inside add():", result)

add()
print("In main:", result)
Answer When we run the above script, the output will be: Inside add(): 2 In main: 2

Q In a python function without a return statement, what type does the return value default to?

Answer
NoneType

Standard if/else blocks keep the existing scope and don't destroy them like a function.

Q In the below, what value of number will be printed out?

number = 9

if True:
 number = 15

print(number)
Answer if True means that the indented code underneath will always be executed, 15 - if/else statements do not convert code to local scope, they mantain the existing scope.

Q What are the kwargs (optional args) for the print() function?

Answer
sep='sthing to separate each str by', end="" <--no /n after each print() call```

print("a string", sep=, end=)

def myfunction(arg1, arg2=defaultvalue):
    print("hi")
myfunction(2,3)

Q What wil be the output of the below code snippet? #TODO

def myfunction(arg1, arg2=99):
    print(arg1,arg2)
myfunction(2,3)
Answer
2, 3  as those values will have overridden the default

For Loops

Q how many times will this sentence be printed?

for i in range(3):
    print('hi')
Answer 3 times, the first time the variable i is set to 0, then 1 and 2 not including 3. Total of 3 times - as range(3)

Q Write a for loop to count the sum of all no.s from 1 to 100

Answer
total = 0
for i in range(101):
    total = total + i
print(total)

Q Sum of all no.s from 4 to 99

Answer
total = 0
for i in range(4, 100):
    total = total + i
print(total)

To append items to a list in a for loop, remember to instantiate your list outside the for loop

Functions from built-in modules e.g. random, sys

Remember all python functions have a return value, if no return statement, the return value defaults to a None type.

Getting arguments from the command line import sys

Q What is sys.argv[0] in the following line? python myscript.py jenna 2

Answer myscript.py
sys.argv[0] is the name of the script being run, with the following arguments being sys.argv[1] and [2] and so on

Q What is the type of any arguments passed in to a python script by default? thourgh sys or input()?

Answer string
that's why for integers you must specify input = int(.... or int(sys.argv[1])

Method Description Use ?
__main__ A module’s name is set equal to 'main' when read from standard input if __name__ == __main__: ..
__file__ returns the path specified (usually the filepath) filepath = __file__ ..
__init__ object inittialisation def __init__(self, ..
Method Description Use ?
strip() remove whitespace at start and end of a string myvar.strip() ..
replace() specified value is replaced by another myvar.replace(" ","") ..
join() useful for converting iterables to strings separator.join(iterable) ..
Method Description Example Use
filter() takes a function and an iterable as arguments and constructs a new iterable by applying the function to every element in the list filter even no.s even = filter(lambda x: x % 2 == 0, integers)
map() applies a function to every element in a list. Unlike filter, it leaves the number of elements in the list unchanged myvar.replace(" ","") ..
reduce() Reduce works by applying a function that takes two arguments to the elements in the list, from left to right .. ..
Module usage example notes
random random.randint(var) random.randint(1,10) generate random number between 1 and 10
sys sys.exit() sys.exit() This allows a script to terminate, anything below this code will not be ecxecuted
get even no.s even = [x for x in integers if x % 2 == 0] bla

JavaScript (ES6)

Defining variables

New let, const keywords were introduced. Unlike let variables, const variables cannot be changed.

let x = 2;
const company = 'BT'

Object destructuring

Symbols ( : )

  • An immutable data type
  • Symbols are denoted by a colon (:) and are identifiers (fixed values) unlike strings which are meant to be mutable,
  • Tend to be used for method names or attribute names
my_symbol = :name
my_symbol2 = :name

my_symbol.object_id and my_symbol2.object_id will be the same

Hashes

  • Hashes are as objects in JS

Unit tests - RSpec

describe "scenario name" do
  it "test name" do
    <test code here>
    <expectation here>
  end
  
  decribe "can create a new country" do
   it "create country" do
   

About

Comparing syntax between various languages

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published