Skip to content

Commit

Permalink
Started js and discordbot notes
Browse files Browse the repository at this point in the history
  • Loading branch information
5urajdas committed Oct 26, 2021
1 parent ecac17a commit 1632d28
Show file tree
Hide file tree
Showing 6 changed files with 340 additions and 1 deletion.
15 changes: 15 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Python: Current File",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal"
}
]
}
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"python.pythonPath": "C:\\Python39\\python.exe"
}
267 changes: 267 additions & 0 deletions JsNotes.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,267 @@
# Javascript Notes
to learn:
- [ ] dom manip
- [ ] api stuff
- [ ] React

## Intro
- Javascript is a dynamic lang that runs on both browsers(Spidermonkey and v8) and also outside the browser in the node runtime environment
- Standardised by ECMA and standard is called ECMAscript

## Bestpractices
- If your writing js in the html file itself then write it in the bottom most part of the body tag, that way the html content is loaded first and the script proccessed after that plus we actually need some elements to work(manip) with first.
- Use Camel notation to name identifiers eg. firstName

## Basics
- all **statements** need to be terminated by `;` in js
- Except statements ending with blocks:
```js
function foo() {
// ...
}
if (y >0) {
// ···
}
```
- Code without semicolons is legal but not preferred
- comment is made by using `//`

## Operators
### Comparison operators
- a == b : Loose equality (doesn't check types)
- a != b : loose inequality
- a === b : strict equality (checks for same type)
- a !== b : strict inequality
- a < b / a > b / a >= b / a <= b : greater than and less than
> Note: There's no strict greater than/ less than operator
### Logical Operators
- expr1 && expr2 : And operator
- expr1 || expr2 : Or operator
- !expr : Not operator

### Variables
- Define a variable using the `let` keyword
```js
let x;
console.log(x);

let y = 2;
y = 'hi'
console.log(y);
```
- This will print undefined and hi in the console
- Variables can be undefined in js and be defined later in the code
- Identifier names can't start with a number and -

### Constants
- Constants as their names suggest are variables that have a fixed value and can't be reassigned
```js
const pi = 3.1459;
console.log(pi);
```

## Data types
- Primitives | Reference types

### Primitives
- string, number, bool(true/false), undefined, null
```js
let name = 'Suraj'; //string
let age = 69; //number
let feelingGood = true; //bool
let purposeInLife; //undefined
let energyMeter = null; //null
```

- the type of an object can be checked by using `typeof object` eg. `typeof name => string`

#### String Properties and Methods
- string.length => returns the length of the string
- string.slice(start, end)
- string.replace(toReplace, newString) => The replace() method replaces a specified value with another value in a string
```js
let text = "Please visit Mcdonalds";
let newText = text.replace("Mcdonalds", "Dominos");
```
- string.toUpperCase() and string.toLowerCase()
- string.trim() => trims off leading and ending whitespaces

#### Template Strings
```js
let name = 'Gandumon';
let age = 1649;
console.log(`${name} is turning ${age} this year`);
```

### Reference types
- objects, arrays, functions

### Objects
- A JavaScript object is a collection of named values that have properties and methods to them
- They are declared inside `{}`
- Everything in js is an object except primitives

```js
let person = {firstName:'Suraj', lastName:'Das', age:69};
```

- The properties of an object can be accessed in 2 ways :- dot notation or squre bracket notation
- Use the dot notation in most cases as its more concise and compact and the square notation can be used when we don't know what property we are going to access in advanced (like when user selects a property)
- Note json is based on js objects and can be converted to and fro but keep in mind keys and strings must be in double quotes only (no single quotes allowed)
- To convert from object to json pass in the array of objects inside `JSON.stringify()`

```js
let person = {firstName:'Suraj', socialPoints:-90000};
// dot notation
person.socialPoints -= 42000;
person.judgement = "Labor Camp"; // will create a new property of judgement

// square bracket
let summary = person["firstName"] + ' will be given ' + person["judgement"];
let selection = 'firstName'; // user selects a property
person[selection] = 'Kalia';

//Converting to json
personJSON = JSON.stringify(person)
console.log(personJSON)
// in json format
/* person = {
"firstName":"Suraj",
"socialPoints":-90000
}
*/
```

## Equality comparisons and sameness
### Strict equality using `===`
- Returns true if the operands are equal and of the same type.
- Strict equality is almost always the correct comparison operation to use.

### Loose equality using `==`
- Loose equality compares two values for equality after converting both values to a common type.
- undefined and null are loosely equal; that is, undefined == null is true, and null == undefined is true.

```js
if (1=='1') {
console.log("1=='1'")
}

if (1==='1') {
console.log("1==='1'")
}
/* Output
1=='1'
*/
```

### Arrays
- Arrays in js are dynamic in size and type of elements it can hold
- Its is made using `[]`
- An element can be accessed by its index, array[i]

```js
let favThings = ['blue', 'manga', 2];
favThings[0] = 'turqoise blue';
favThings[2] = [180, 360];
```
- Arrays are objects and hence have properties and methods to them
```js
console.log(favThings.length)
```

## Functions
- A function in js is defined by using the function keyword + indentifier + (parameters in bracket) + {function body in curly braces}
```js
function calcDaysRem(startDate, endDate) {
daysRem = endDate - startDate + 1;
console.log(daysRem);
}

calcDaysRem(2,10);
```

- Values can be retured using the return keyword

```js
function sqaure(num) {
return num * num;
}
console.log(square(1230));
```

## Loops
### For loop
- Loops in js are a bit like in c/c++ I think
```js
for(initial value; condition till which the loop will iterate; increment) {
statement;
}
```
```js
for(i = 0 ; i < 69; i++) {
console.log(`loops are weird in js times ${i}`);
}
```

### While Loop
- While loop only takes in the condition till which to iterate
- remember to increment the initial value tho otherwise it will go on infinitly
```js
i = 0;
while(i < 10) {
console.log(i);
i++
}
```

## Higher Order Array methods
- forEach, map, filter

### forEach
- It is better to use this array method to iterate/traverse through an array than simply using a for/while loop
- The forEach method is called on the array and a callback function needs to be passed as the argument. This callback function handles/operates upon the incoming elements from the array.
- Multiple parameters can be passed to the callback fn but the first parameter must be the one that represents the incoming elements from the array
```js
const letters = ['r', 's', 't'];

letters.forEach( function(letter) {
console.log(letter);
});
```

### Map
- Map returns the elements of an array
```js
let mappedLs = letters.map( function(letter) {
return letter;
});
```

### Filter
- Filters and returns elements of an array according to a condition
```js
const letters = ['r', 's', 't', 3];

let filteredLs = letters.filter( function(letter) {
return typeof(letter) == 'number';
});
console.log(filteredLs)
// output:- Array [ 3 ]
```

## Conditionals
### If, else if, else
- If conditional can be nested in this order:- if (condition) {do}-> else if (condition) {do} -> else {do}
```js
let person = {firstName:'Suraj', socialPoints:-90000};
if (person.socialPoints < 0) {
console.log("To the gulag you go 👿");
} else if (person.socialPoints >= 1000) {
console.log("Keep up the good work citizen");
} else {
console.log("Welcome to the mainland citizen")
}
```

###
38 changes: 37 additions & 1 deletion PythonNotes.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
- [Misc Python notes](#misc-python-notes)
- [Tips](#tips)
- [Naming Conventions](#naming-conventions)
- [Standard library](#standard-library)
- [Zip()](#zip)
- [Packing and Unpacking variables](#packing-and-unpacking-variables)
- [Venv](#venv)
- [Pyinstaller](#pyinstaller)
- [Threading](#threading)
Expand Down Expand Up @@ -34,7 +37,8 @@
- [Filter](#filter)
- [Genrators in python](#genrators-in-python)

## Tips
## Tips
- Use [pyp](https://www.pypy.org/) when doing memory intensive tasks (web frameworks, graphics, etc) or need speed (optimisation)
- Error: UnicodeEncodeError: 'charmap' codec can't encode character '\u2640' in position 23: character maps to <undefined>
- Fix: with open("mangas.txt",'w',encoding='utf-8') as f:
- You just change the encoding of the text to utf-8
Expand All @@ -60,6 +64,38 @@ PEP 8 tells that names should be short; this following gives a good overview of
- keep short scopes for quick lookup;
- spend time thinking about readability.

## Standard library
### Zip()
- Returns an iterator of tuples, where the i-th tuple contains the i-th element from each of the argument sequences or iterables. The iterator stops when the shortest input iterable is exhausted.
- With a single iterable argument, it returns an iterator of 1-tuples. With no arguments, it returns an empty iterator.
```
>>> x = [1, 2, 3]
>>> y = [4, 5, 6]
>>> zipped = zip(x, y) # zip() Creates a iterator
>>> list(zipped) # Iterator converted to list
[(1, 4), (2, 5), (3, 6)]
```
### Packing and Unpacking variables
- You can assign multiple values to multiple variables by separating variables and values with commas `,`.
- values can be of diff types
`a, b, c = 1, 2, 3`
- If there is one variable on the left side, it is assigned as a tuple.
`a = 1, 2, 3`
- If the number of variables on the left and the number of values on the right do not match, a ValueError will occur, but you can assign the rest as a list by appending * to the variable name.
```py
# a, b = 100, 200, 300
# ValueError: too many values to unpack (expected 2)

# a, b, c = 100, 200
# ValueError: not enough values to unpack (expected 3, got 2)

a, *b = 100, 200, 300

# a -> 100, b -> (200, 300)
```
- You can assign the same value to multiple variables by using = consecutively.
`a = b = 1`

## Venv
- Use venv to isolate packages from the main py installation when doing projects
```
Expand Down
17 changes: 17 additions & 0 deletions checkout.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
x = int(input())
y = int(input())
z = int(input())
n = int(input())
pos_coords = list(zip([i for i in range(x+1)], [j for j in range(y+1)], [k for k in range(z+1)]))
print(pos_coords)
check_sum = 0
result_coord = []
for coords in pos_coords:
for axes in coords:
# print(axes)
check_sum += axes
if sum == n:
continue
else:
result_coord.append(coords)
# print(result_coord)
1 change: 1 addition & 0 deletions discordbot.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Notes on making a discord bot from scratch

0 comments on commit 1632d28

Please sign in to comment.