Freekode
FreeKode | Snippets

[email protected]

Slice A NumPy Array

# Imports

import numpy as np

# Create a list

lst = [1, 2, 3, 4, 5]

# Convert lst to an array

arr = np.array(lst)

# Slice arr

sliced_arr = arr[:3] # sliced_arr should contain the first 3 elements of arr

print(sliced_arr)

[email protected]

JavaScript Countdown Timer

<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="UTF-8" />

<title>Countdown timer using HTML and JavaScript</title>

</head>

<body>

Registration closes in <span id="timer">05:00<span> minutes!

 

<!-- custom js -->

<script>

window.onload = function () {

var minute = 0;

var sec = 5;

setInterval(function () {

document.getElementById("timer").innerHTML =

minute + " : " + sec;

if (sec != 00) {

sec--;

}

if (sec == 00) {

minute = 0;

sec = 0;

if (minute == 0) {

minute = 0;

sec = 0;

}

}

}, 1000);

};

</script>

</body>

</html>

[email protected]

Indexing A NumPy Array

# To index a NumPy array, you can use the same method you use to index Python lists.

import numpy as np

lst = [1, 2, 3, 4, 5]

# Convert lst to a NumPy array

arr = np.array(lst)

# Print out the element at the first index of arr, or, print out the element of arr which is at the 0th index, which is 1.

print(arr[0])

 

 

[email protected]

Convert List To An Array NumPy

# To convert a list to an array in the NumPy module of Python, use the numpy.array() function and pass in the list as an argument.

import numpy as np

lst = [1, 2, 3, 4, 5, 6, 7]

arr = np.array(lst)



First 2 3 4