1644302547
Do you want to mint thousands of NFTs? If so, this walkthrough is for you! Learn how to easily mint 10,000 NFTs with Moralis in mere minutes!
In this article, we’re going to break down the process of how to mint 10,000 NFTs. To make the tutorial more accessible, we’ll be using an already prepared Moralis template for an NFT minting engine. So, if you’d rather skip the tutorial and immediately take a closer look at the GitHub repository, check out the following link:
Full NFT Engine Documentation – https://morioh.com/p/50f79d2c67aa
NFTs (non-fungible tokens) are some of the most exciting features of Web3 development, and innovative new ideas have been generated as a result of the utilization of these tokens. One of the hottest trends within the world of crypto is digital collectibles. These are NFTs generated in bulk to create collections consisting of thousands of tokens. The two most prominent examples are Bored Ape Yacht Club and CryptoPunks. These collections contain around 10,000 entirely unique NFTs, some of which are selling for astronomical amounts. Due to this sector’s incredible success, we’ll take a closer look at how to mint 10,000 NFTs and create your own series of tokens with Moralis.
#nft #nonfungibletoken #blockchain #web3 #gamedev #gamefi #moralis
1643336880
If you have a website, it's helpful to have a loader so users can tell something is happening once they've clicked a link or button.
You can use this loader component in a lot of places, and it should be as simple as possible.
In this post, we will see how to build two types of loaders with only one <div>
and a few lines of CSS code. Not only this but we will make them customizable so you can easily create different variations from the same code.
Here's what we'll build:
CSS-only Spinner and Progress Loader
Below is a demo of what we are building:
https://codepen.io/t_afif/pen/PoJyaNy
<div class="loader"></div>
<div class="loader" style="--b: 15px;--c: blue;width: 120px;--n: 8"></div>
<div class="loader" style="--b: 5px;--c: green;width: 80px;--n: 6;--g: 20deg"></div>
<div class="loader" style="--b: 20px;--c: #000;width: 80px;--n: 15;--g: 7deg"></div>
.loader {
--b: 10px; /* border thickness */
--n: 10; /* number of dashes*/
--g: 10deg; /* gap between dashes*/
--c: red; /* the color */
width: 100px; /* size */
aspect-ratio: 1;
border-radius: 50%;
padding: 1px;
background: conic-gradient(#0000,var(--c)) content-box;
-webkit-mask:
repeating-conic-gradient(#0000 0deg,
#000 1deg calc(360deg/var(--n) - var(--g) - 1deg),
#0000 calc(360deg/var(--n) - var(--g)) calc(360deg/var(--n))),
radial-gradient(farthest-side,#0000 calc(98% - var(--b)),#000 calc(100% - var(--b)));
mask:
repeating-conic-gradient(#0000 0deg,
#000 1deg calc(360deg/var(--n) - var(--g) - 1deg),
#0000 calc(360deg/var(--n) - var(--g)) calc(360deg/var(--n))),
radial-gradient(farthest-side,#0000 calc(98% - var(--b)),#000 calc(100% - var(--b)));
-webkit-mask-composite: destination-in;
mask-composite: intersect;
animation: load 1s infinite steps(var(--n));
}
@keyframes load {to{transform: rotate(1turn)}}
We have 4 different loaders using the same code. By only changing a few variables, we can generate a new loader without needing to touch the CSS code.
The variables are defined like below:
--b
defines the border thickness.--n
defines the number of dashes.--g
defines the gap between dashes. Since we're dealing with a circular element, this one is an angle value.--c
defines the color.Here is an illustration to see the different variables.
CSS Variables of the Spinner loader
Let's tackle the CSS code. We will use another figure to illustrate a step-by-step construction of the loader.
Step-by-Step illustration of the Spinner Loader
We first start by creating a circle like this:
.loader {
width: 100px; /* size */
aspect-ratio: 1;
border-radius: 50%;
}
Nothing complex so far. Note the use of aspect-ratio
which allows us to only modify one value (the width
) in order to control the size.
Then we add a conic gradient coloration from transparent to the defined color (the variable --c
):
.loader {
width:100px; /* size */
aspect-ratio: 1;
border-radius: 50%;
background: conic-gradient(#0000,var(--c));
}
In this step, we introduce the mask
property to hide some parts of the circle in a repetitive manner. This will depend on the --n
and --d
variables. If you look closely at the figure, we will notice the following pattern:
visible part
invisible part
visible part
invisible part
etc
To do this, we use repeating-conic-gradient(#000 0 X, #0000 0 Y)
. From 0
to X
we have an opaque color (visible part) and from X
to Y
we have a transparent one (invisible part).
We introduce our variables:
g
between each visible part so the formula between X
and Y
will be X = Y - g
.n
visible part so the formula of Y
should be Y = 360deg/n
. A full circle is 360deg
so we simply divide it by n
Our code so far is:
.loader {
width: 100px; /* size */
aspect-ratio: 1;
border-radius: 50%;
background: conic-gradient(#0000,var(--c));
mask: repeating-conic-gradient(#000 0 calc(360deg/var(--n) - var(--g)) , #0000 0 calc(360deg/var(--n))
}
This next step is the trickiest one, because we need to apply another mask to create a kind of hole in order to get the final shape. To do this we will logically use a radial-gradient()
with our variable b
:
radial-gradient(farthest-side,#0000 calc(100% - var(--b)),#000 0)
A full circle from where we remove a thickness equal to b
.
We add this to the previous mask:
.loader {
width: 100px; /* size */
aspect-ratio: 1;
border-radius: 50%;
background: conic-gradient(#0000,var(--c));
mask:
radial-gradient(farthest-side,#0000 calc(100% - var(--b)),#000 0),
repeating-conic-gradient(#000 0 calc(360deg/var(--n) - var(--g)) , #0000 0 calc(360deg/var(--n))
}
We have two mask layers, but the result is not what we want. We get the following:
It may look strange but it's logical. The "final" visible part is nothing but the sum of each visible part of each mask layer. We can change this behavior using mask-composite
. I would need a whole article to explain this property so I will simply give the value.
In our case, we need to consider intersect
(and destination-out
for the prefixed property). Our code will become:
.loader {
width: 100px; /* size */
aspect-ratio: 1;
border-radius: 50%;
background: conic-gradient(#0000,var(--c));
mask:
radial-gradient(farthest-side,#0000 calc(100% - var(--b)),#000 0),
repeating-conic-gradient(#000 0 calc(360deg/var(--n) - var(--g)) , #0000 0 calc(360deg/var(--n));
-webkit-mask-composite: destination-in;
mask-composite: intersect;
}
We are done with the shape! We are only missing the animation. The latter is an infinite rotation.
The only thing to note is that I am using a steps
animation to create the illusion of fixed dashes and moving colors.
Here is an illustration to see the difference
A Linear Animation vs a Steps Animation
The first one is a linear and continuous rotation of the shape (not what we want) and the second one is a discrete animation (the one we want).
Here is the full code including the animation:
<div class="loader"></div>
<div class="loader" style="--b: 15px;--c: blue;width: 120px;--n: 8"></div>
<div class="loader" style="--b: 5px;--c: green;width: 80px;--n: 6;--g: 20deg"></div>
<div class="loader" style="--b: 20px;--c: #000;width: 80px;--n: 15;--g: 7deg"></div>
.loader {
--b: 10px; /* border thickness */
--n: 10; /* number of dashes*/
--g: 10deg; /* gap between dashes*/
--c: red; /* the color */
width: 100px; /* size */
aspect-ratio: 1;
border-radius: 50%;
padding: 1px;
background: conic-gradient(#0000,var(--c)) content-box;
-webkit-mask:
repeating-conic-gradient(#0000 0deg,
#000 1deg calc(360deg/var(--n) - var(--g) - 1deg),
#0000 calc(360deg/var(--n) - var(--g)) calc(360deg/var(--n))),
radial-gradient(farthest-side,#0000 calc(98% - var(--b)),#000 calc(100% - var(--b)));
mask:
repeating-conic-gradient(#0000 0deg,
#000 1deg calc(360deg/var(--n) - var(--g) - 1deg),
#0000 calc(360deg/var(--n) - var(--g)) calc(360deg/var(--n))),
radial-gradient(farthest-side,#0000 calc(98% - var(--b)),#000 calc(100% - var(--b)));
-webkit-mask-composite: destination-in;
mask-composite: intersect;
animation: load 1s infinite steps(var(--n));
}
@keyframes load {to{transform: rotate(1turn)}}
You will notice a few differences with the code I used in the explanation:
padding: 1px
and setting the background to content-box
+/1deg
between the colors of the repeating-conic-gradient()
radial-gradient()
Those are some corrections to avoid visual glitches. Gradients are known to produce "strange" results in some cases so we have to adjust some values manually to avoid them.
Like the previous one loader, let's start with an overview:
https://codepen.io/t_afif/pen/bGoNddg
<div class="loader"></div>
<div class="loader" style="--s:10px;--n:10;color:red"></div>
<div class="loader" style="--g:0px;color:darkblue"></div>
<div class="loader" style="--s:25px;--g:8px;border-radius:50px;color:green"></div>
.loader {
--n:5; /* control the number of stripes */
--s:30px; /* control the width of stripes */
--g:5px; /* control the gap between stripes */
width:calc(var(--n)*(var(--s) + var(--g)) - var(--g));
height:30px;
padding:var(--g);
margin:5px auto;
border:1px solid;
background:
repeating-linear-gradient(90deg,
currentColor 0 var(--s),
#0000 0 calc(var(--s) + var(--g))
) left / calc((var(--n) + 1)*(var(--s) + var(--g))) 100%
no-repeat content-box;
animation: load 1.5s steps(calc(var(--n) + 1)) infinite;
}
@keyframes load {
0% {background-size: 0% 100%}
}
We have the same configuration as the previous loader. CSS variables that control the loader:
--n
defines the number of dashes/stripes.--s
defines the width of each stripe.--g
defines the gap between stripes.Illustration of the CSS Variables
From the above figure we can see that the width of the element will depend on the 3 variables. The CSS will be as follows:
.loader {
width: calc(var(--n)*(var(--s) + var(--g)) - var(--g));
height: 30px; /* use any value you want here */
padding: var(--g);
border: 1px solid;
}
We use padding
to set the gap on each side. Then the width will be equal to the number of stripes multiplied by their width and the gap. We remove one gap because for N
stripes we have N-1
gaps.
To create the stripes we will use the below gradient.
repeating-linear-gradient(90deg,
currentColor 0 var(--s),
#0000 0 calc(var(--s) + var(--g))
)
From 0
to s
is the defined color and from s
to s + g
a transparent color (the gap).
I am using currentColor
which is the value of the color
property. Note that I didn't define any color inside border
so it will also use to the value of color
. If we want to change the color of the loader, we only need to set the color
property.
Our code so far:
.loader {
width: calc(var(--n)*(var(--s) + var(--g)) - var(--g));
height: 30px;
padding: var(--g);
border: 1px solid;
background:
repeating-linear-gradient(90deg,
currentColor 0 var(--s),
#0000 0 calc(var(--s) + var(--g))
) left / 100% 100% content-box no-repeat;
}
I am using content-box
to make sure the gradient doesn't cover the padding area. Then I define a size equal to 100% 100%
and a left position.
It's time for the animation. For this loader, we will animate the background-size
from 0% 100%
to 100% 100%
which means the width of our gradient from 0%
to 100%
Like the previous loader, we will rely on steps()
to have a discrete animation instead of a continuous one.
A Linear Animation vs a Steps Animation
The second one is what we want to create, and we can achieve it by adding the following code:
.loader {
animation: load 1.5s steps(var(--n)) infinite;
}
@keyframes load {
0% {background-size: 0% 100%}
}
If you look closely at the last figure, you will notice that the animation is not complete. We are missing one stripe at the end, even if we have used N
. This is not a bug but how steps()
is supposed to work.
To overcome this, we need to add an extra step. We increase the background-size
of our gradient to contain N+1
stripes and use steps(N+1)
. This will get us to the final code:
.loader {
width: calc(var(--n)*(var(--s) + var(--g)) - var(--g));
height: 30px;
padding: var(--g);
margin: 5px auto;
border: 1px solid;
background:
repeating-linear-gradient(90deg,
currentColor 0 var(--s),
#0000 0 calc(var(--s) + var(--g))
) left / calc((var(--n) + 1)*(var(--s) + var(--g))) 100%
content-box no-repeat;
animation: load 1.5s steps(calc(var(--n) + 1)) infinite;
}
@keyframes load {
0% {background-size: 0% 100%}
}
Note that the width of the gradient is equal to N+1
multiplied by the width of one stripe and a gap (instead of being 100%
)
I hope you enjoyed this tutorial. If you are interested, I have made more than 500 CSS-only single div loaders. I also wrote another tutorial to explain how to create the Dots loader using only background properties.
Find below useful links to get more detail about some properties I have used that I didn't explain thoroughly due to their complexity:
mask-composite
: https://css-tricks.com/mask-compositing-the-crash-course/steps()
: https://developer.mozilla.org/en-US/docs/Web/CSS/easing-function#the_steps_class_of_easing_functionsThank you for reading!
Link: https://www.freecodecamp.org/news/how-to-create-a-css-only-loader/
1642923600
Webサイトがある場合は、ローダーを使用すると、ユーザーがリンクまたはボタンをクリックすると何かが起こっていることを知ることができるので便利です。
このローダーコンポーネントは多くの場所で使用でき、可能な限りシンプルにする必要があります。
<div>
この投稿では、1行と数行のCSSコードで2種類のローダーを構築する方法を説明します。これだけでなく、同じコードからさまざまなバリエーションを簡単に作成できるようにカスタマイズできるようにします。
これが私たちが構築するものです:
CSSのみのスピナーとプログレスローダー
以下は、私たちが構築しているもののデモです。
https://codepen.io/t_afif/pen/PoJyaNy
<div class="loader"></div>
<div class="loader" style="--b: 15px;--c: blue;width: 120px;--n: 8"></div>
<div class="loader" style="--b: 5px;--c: green;width: 80px;--n: 6;--g: 20deg"></div>
<div class="loader" style="--b: 20px;--c: #000;width: 80px;--n: 15;--g: 7deg"></div>
.loader {
--b: 10px; /* border thickness */
--n: 10; /* number of dashes*/
--g: 10deg; /* gap between dashes*/
--c: red; /* the color */
width: 100px; /* size */
aspect-ratio: 1;
border-radius: 50%;
padding: 1px;
background: conic-gradient(#0000,var(--c)) content-box;
-webkit-mask:
repeating-conic-gradient(#0000 0deg,
#000 1deg calc(360deg/var(--n) - var(--g) - 1deg),
#0000 calc(360deg/var(--n) - var(--g)) calc(360deg/var(--n))),
radial-gradient(farthest-side,#0000 calc(98% - var(--b)),#000 calc(100% - var(--b)));
mask:
repeating-conic-gradient(#0000 0deg,
#000 1deg calc(360deg/var(--n) - var(--g) - 1deg),
#0000 calc(360deg/var(--n) - var(--g)) calc(360deg/var(--n))),
radial-gradient(farthest-side,#0000 calc(98% - var(--b)),#000 calc(100% - var(--b)));
-webkit-mask-composite: destination-in;
mask-composite: intersect;
animation: load 1s infinite steps(var(--n));
}
@keyframes load {to{transform: rotate(1turn)}}
同じコードを使用する4つの異なるローダーがあります。いくつかの変数を変更するだけで、CSSコードに触れることなく新しいローダーを生成できます。
変数は次のように定義されます。
--b
境界線の太さを定義します。--n
ダッシュの数を定義します。--g
ダッシュ間のギャップを定義します。円形の要素を扱っているので、これは角度の値です。--c
色を定義します。これは、さまざまな変数を確認するための図です。
スピナーローダーのCSS変数
CSSコードに取り組みましょう。別の図を使用して、ローダーの段階的な構成を説明します。
スピナーローダーのステップバイステップの図
まず、次のような円を作成します。
.loader {
width: 100px; /* size */
aspect-ratio: 1;
border-radius: 50%;
}
これまでのところ複雑なことはありません。これを使用すると、サイズを制御するためにaspect-ratio
1つの値()のみを変更できることに注意してください。width
次に、透明から定義された色(変数--c
)に円錐曲線の色を追加します。
.loader {
width:100px; /* size */
aspect-ratio: 1;
border-radius: 50%;
background: conic-gradient(#0000,var(--c));
}
このステップでmask
は、円の一部を繰り返し非表示にするプロパティを紹介します。--n
これはと--d
変数に依存します。図をよく見ると、次のパターンに気付くでしょう。
visible part
invisible part
visible part
invisible part
etc
これを行うには、を使用しますrepeating-conic-gradient(#000 0 X, #0000 0 Y)
。から0
までX
は不透明な色(可視部分)があり、からX
までY
は透明な色(不可視部分)があります。
変数を紹介します。
g
ように、各可視部分の間に等しいギャップが必要です。XYX = Y - g
n
目に見える部分が必要なので、の式Y
はY = 360deg/n
です。完全な円は360deg
、単純にで割ったものです。n
これまでのコードは次のとおりです。
.loader {
width: 100px; /* size */
aspect-ratio: 1;
border-radius: 50%;
background: conic-gradient(#0000,var(--c));
mask: repeating-conic-gradient(#000 0 calc(360deg/var(--n) - var(--g)) , #0000 0 calc(360deg/var(--n))
}
この次のステップは最も難しいステップです。最終的な形状を取得するために、別のマスクを適用して一種の穴を作成する必要があるためです。これを行うにはradial-gradient()
、変数で論理的にaを使用しますb
。
radial-gradient(farthest-side,#0000 calc(100% - var(--b)),#000 0)
に等しい厚さを削除するところから完全な円b
。
これを前のマスクに追加します。
.loader {
width: 100px; /* size */
aspect-ratio: 1;
border-radius: 50%;
background: conic-gradient(#0000,var(--c));
mask:
radial-gradient(farthest-side,#0000 calc(100% - var(--b)),#000 0),
repeating-conic-gradient(#000 0 calc(360deg/var(--n) - var(--g)) , #0000 0 calc(360deg/var(--n))
}
2つのマスクレイヤーがありますが、結果は私たちが望むものではありません。次のようになります。
奇妙に見えるかもしれませんが、それは論理的です。「最終的な」可視部分は、各マスクレイヤーの各可視部分の合計に他なりません。この動作は、を使用して変更できますmask-composite
。このプロパティを説明するために記事全体が必要になるので、単純に値を示します。
intersect
私たちの場合、 (そしてdestination-out
接頭辞付きのプロパティについて)考慮する必要があります。コードは次のようになります。
.loader {
width: 100px; /* size */
aspect-ratio: 1;
border-radius: 50%;
background: conic-gradient(#0000,var(--c));
mask:
radial-gradient(farthest-side,#0000 calc(100% - var(--b)),#000 0),
repeating-conic-gradient(#000 0 calc(360deg/var(--n) - var(--g)) , #0000 0 calc(360deg/var(--n));
-webkit-mask-composite: destination-in;
mask-composite: intersect;
}
形が出来上がりました!アニメーションが欠けているだけです。後者は無限回転です。
注意すべき唯一のことは、steps
アニメーションを使用して、固定されたダッシュと動く色の錯覚を作成しているということです。
これが違いを見るためのイラストです
線形アニメーションとステップアニメーション
最初のものは形状の線形で連続的な回転であり(私たちが望むものではありません)、2番目のものは離散アニメーション(私たちが望むもの)です。
アニメーションを含む完全なコードは次のとおりです。
<div class="loader"></div>
<div class="loader" style="--b: 15px;--c: blue;width: 120px;--n: 8"></div>
<div class="loader" style="--b: 5px;--c: green;width: 80px;--n: 6;--g: 20deg"></div>
<div class="loader" style="--b: 20px;--c: #000;width: 80px;--n: 15;--g: 7deg"></div>
.loader {
--b: 10px; /* border thickness */
--n: 10; /* number of dashes*/
--g: 10deg; /* gap between dashes*/
--c: red; /* the color */
width: 100px; /* size */
aspect-ratio: 1;
border-radius: 50%;
padding: 1px;
background: conic-gradient(#0000,var(--c)) content-box;
-webkit-mask:
repeating-conic-gradient(#0000 0deg,
#000 1deg calc(360deg/var(--n) - var(--g) - 1deg),
#0000 calc(360deg/var(--n) - var(--g)) calc(360deg/var(--n))),
radial-gradient(farthest-side,#0000 calc(98% - var(--b)),#000 calc(100% - var(--b)));
mask:
repeating-conic-gradient(#0000 0deg,
#000 1deg calc(360deg/var(--n) - var(--g) - 1deg),
#0000 calc(360deg/var(--n) - var(--g)) calc(360deg/var(--n))),
radial-gradient(farthest-side,#0000 calc(98% - var(--b)),#000 calc(100% - var(--b)));
-webkit-mask-composite: destination-in;
mask-composite: intersect;
animation: load 1s infinite steps(var(--n));
}
@keyframes load {to{transform: rotate(1turn)}}
説明で使用したコードとの違いに気付くでしょう。
padding: 1px
背景を追加して設定していますcontent-box
+/1deg
の色の間にありますrepeating-conic-gradient()
radial-gradient()
これらは、視覚的な不具合を回避するためのいくつかの修正です。グラデーションは「奇妙な」結果を生成することが知られているため、それらを回避するためにいくつかの値を手動で調整する必要があります。
前のローダーと同様に、概要から始めましょう。
https://codepen.io/t_afif/pen/bGoNddg
<div class="loader"></div>
<div class="loader" style="--s:10px;--n:10;color:red"></div>
<div class="loader" style="--g:0px;color:darkblue"></div>
<div class="loader" style="--s:25px;--g:8px;border-radius:50px;color:green"></div>
.loader {
--n:5; /* control the number of stripes */
--s:30px; /* control the width of stripes */
--g:5px; /* control the gap between stripes */
width:calc(var(--n)*(var(--s) + var(--g)) - var(--g));
height:30px;
padding:var(--g);
margin:5px auto;
border:1px solid;
background:
repeating-linear-gradient(90deg,
currentColor 0 var(--s),
#0000 0 calc(var(--s) + var(--g))
) left / calc((var(--n) + 1)*(var(--s) + var(--g))) 100%
no-repeat content-box;
animation: load 1.5s steps(calc(var(--n) + 1)) infinite;
}
@keyframes load {
0% {background-size: 0% 100%}
}
以前のローダーと同じ構成になっています。ローダーを制御するCSS変数:
--n
ダッシュ/ストライプの数を定義します。--s
各ストライプの幅を定義します。--g
ストライプ間のギャップを定義します。CSS変数の図
上の図から、要素の幅が3つの変数に依存することがわかります。CSSは次のようになります。
.loader {
width: calc(var(--n)*(var(--s) + var(--g)) - var(--g));
height: 30px; /* use any value you want here */
padding: var(--g);
border: 1px solid;
}
padding
両側にギャップを設定するために使用します。その場合、幅はストライプの数に幅とギャップを掛けたものに等しくなります。N
ストライプにはギャップがあるため、1つのギャップを削除しN-1
ます。
ストライプを作成するには、以下のグラデーションを使用します。
repeating-linear-gradient(90deg,
currentColor 0 var(--s),
#0000 0 calc(var(--s) + var(--g))
)
From 0
tos
は定義された色であり、from s
tos + g
は透明色(ギャップ)です。
currentColor
プロパティの値であるwhichを使用していcolor
ます。内部に色を定義しなかったためborder
、の値にも使用されることに注意してくださいcolor
。ローダーの色を変更したい場合は、color
プロパティを設定するだけです。
これまでのコード:
.loader {
width: calc(var(--n)*(var(--s) + var(--g)) - var(--g));
height: 30px;
padding: var(--g);
border: 1px solid;
background:
repeating-linear-gradient(90deg,
currentColor 0 var(--s),
#0000 0 calc(var(--s) + var(--g))
) left / 100% 100% content-box no-repeat;
}
content-box
グラデーションがパディング領域をカバーしないようにするために使用しています。100% 100%
次に、左の位置に等しいサイズを定義します。
アニメーションの時間です。このローダーでは、background-size
from 0% 100%
toをアニメーション化します。これは、fromから toへ100% 100%
のグラデーションの幅を意味します。0%100%
steps()
以前のローダーと同様に、連続的なアニメーションではなく、個別のアニメーションを使用することに依存します。
線形アニメーションとステップアニメーション
2つ目は作成したいもので、次のコードを追加することで実現できます。
.loader {
animation: load 1.5s steps(var(--n)) infinite;
}
@keyframes load {
0% {background-size: 0% 100%}
}
最後の図をよく見ると、アニメーションが完全ではないことがわかります。を使用したとしても、最後に1つのストライプがありませんN
。これはバグではありませんが、どのように機能するsteps()
はずです。
これを克服するには、追加のステップを追加する必要があります。background-size
グラデーションを増やしてN+1
ストライプを含め、を使用しますsteps(N+1)
。これにより、最終的なコードが表示されます。
.loader {
width: calc(var(--n)*(var(--s) + var(--g)) - var(--g));
height: 30px;
padding: var(--g);
margin: 5px auto;
border: 1px solid;
background:
repeating-linear-gradient(90deg,
currentColor 0 var(--s),
#0000 0 calc(var(--s) + var(--g))
) left / calc((var(--n) + 1)*(var(--s) + var(--g))) 100%
content-box no-repeat;
animation: load 1.5s steps(calc(var(--n) + 1)) infinite;
}
@keyframes load {
0% {background-size: 0% 100%}
}
グラデーションの幅は、N+1
(ではなく100%
) 1つのストライプとギャップの幅を掛けたものに等しいことに注意してください。
このチュートリアルを楽しんでいただけたでしょうか。興味があれば、私は500以上のCSSのみのシングルdivローダーを作成しました。また、バックグラウンドプロパティのみを使用してドットローダーを作成する方法を説明する別のチュートリアルを作成しました。
以下の便利なリンクを見つけて、複雑さのために完全には説明しなかった、私が使用したいくつかのプロパティの詳細を確認してください。
mask-composite
:https ://css-tricks.com/mask-compositing-the-crash-course/steps()
:https ://developer.mozilla.org/en-US/docs/Web/CSS/easing-function#the_steps_class_of_easing_functions読んでくれてありがとう!
リンク:https ://www.freecodecamp.org/news/how-to-create-a-css-only-loader/
1642484580
Si tiene un sitio web, es útil tener un cargador para que los usuarios puedan saber que algo está sucediendo una vez que hayan hecho clic en un enlace o botón.
Puede usar este componente del cargador en muchos lugares y debería ser lo más simple posible.
En esta publicación, veremos cómo construir dos tipos de cargadores con solo una <div>
y unas pocas líneas de código CSS. No solo esto, sino que los haremos personalizables para que pueda crear fácilmente diferentes variaciones del mismo código.
Esto es lo que construiremos:
Spinner y Progress Loader solo para CSS
A continuación se muestra una demostración de lo que estamos construyendo:
https://codepen.io/t_afif/pen/PoJyaNy
<div class="loader"></div>
<div class="loader" style="--b: 15px;--c: blue;width: 120px;--n: 8"></div>
<div class="loader" style="--b: 5px;--c: green;width: 80px;--n: 6;--g: 20deg"></div>
<div class="loader" style="--b: 20px;--c: #000;width: 80px;--n: 15;--g: 7deg"></div>
.loader {
--b: 10px; /* border thickness */
--n: 10; /* number of dashes*/
--g: 10deg; /* gap between dashes*/
--c: red; /* the color */
width: 100px; /* size */
aspect-ratio: 1;
border-radius: 50%;
padding: 1px;
background: conic-gradient(#0000,var(--c)) content-box;
-webkit-mask:
repeating-conic-gradient(#0000 0deg,
#000 1deg calc(360deg/var(--n) - var(--g) - 1deg),
#0000 calc(360deg/var(--n) - var(--g)) calc(360deg/var(--n))),
radial-gradient(farthest-side,#0000 calc(98% - var(--b)),#000 calc(100% - var(--b)));
mask:
repeating-conic-gradient(#0000 0deg,
#000 1deg calc(360deg/var(--n) - var(--g) - 1deg),
#0000 calc(360deg/var(--n) - var(--g)) calc(360deg/var(--n))),
radial-gradient(farthest-side,#0000 calc(98% - var(--b)),#000 calc(100% - var(--b)));
-webkit-mask-composite: destination-in;
mask-composite: intersect;
animation: load 1s infinite steps(var(--n));
}
@keyframes load {to{transform: rotate(1turn)}}
Tenemos 4 cargadores diferentes usando el mismo código. Con solo cambiar algunas variables, podemos generar un nuevo cargador sin necesidad de tocar el código CSS.
Las variables se definen como sigue:
--b
define el grosor del borde.--n
define el número de guiones.--g
define el espacio entre guiones. Como estamos tratando con un elemento circular, este es un valor de ángulo.--c
define el color.Aquí hay una ilustración para ver las diferentes variables.
Variables CSS del cargador Spinner
Abordemos el código CSS. Usaremos otra figura para ilustrar una construcción paso a paso del cargador.
Ilustración paso a paso del cargador giratorio
Primero comenzamos creando un círculo como este:
.loader {
width: 100px; /* size */
aspect-ratio: 1;
border-radius: 50%;
}
Nada complejo hasta ahora. Tenga en cuenta que su uso aspect-ratio
nos permite modificar solo un valor (el width
) para controlar el tamaño.
Luego agregamos una coloración de degradado cónico de transparente al color definido (la variable --c
):
.loader {
width:100px; /* size */
aspect-ratio: 1;
border-radius: 50%;
background: conic-gradient(#0000,var(--c));
}
En este paso, introducimos la mask
propiedad para ocultar algunas partes del círculo de forma repetitiva. Esto dependerá de las variables --n
y . --d
Si observa detenidamente la figura, notaremos el siguiente patrón:
visible part
invisible part
visible part
invisible part
etc
Para hacer esto, usamos repeating-conic-gradient(#000 0 X, #0000 0 Y)
. De 0
a X
tenemos un color opaco (parte visible) y de X
a Y
tenemos uno transparente (parte invisible).
Introducimos nuestras variables:
g
entre cada parte visible por lo que la fórmula entre X
y Y
será X = Y - g
.n
la parte visible, por lo que la fórmula de Y
debería ser Y = 360deg/n
. Un círculo completo es 360deg
así que simplemente lo dividimos porn
Nuestro código hasta ahora es:
.loader {
width: 100px; /* size */
aspect-ratio: 1;
border-radius: 50%;
background: conic-gradient(#0000,var(--c));
mask: repeating-conic-gradient(#000 0 calc(360deg/var(--n) - var(--g)) , #0000 0 calc(360deg/var(--n))
}
El siguiente paso es el más complicado, porque necesitamos aplicar otra máscara para crear una especie de agujero para obtener la forma final. Para ello usaremos lógicamente a radial-gradient()
con nuestra variable b
:
radial-gradient(farthest-side,#0000 calc(100% - var(--b)),#000 0)
Un círculo completo del que quitamos un espesor igual a b
.
Añadimos esto a la máscara anterior:
.loader {
width: 100px; /* size */
aspect-ratio: 1;
border-radius: 50%;
background: conic-gradient(#0000,var(--c));
mask:
radial-gradient(farthest-side,#0000 calc(100% - var(--b)),#000 0),
repeating-conic-gradient(#000 0 calc(360deg/var(--n) - var(--g)) , #0000 0 calc(360deg/var(--n))
}
Tenemos dos capas de máscara, pero el resultado no es el que queremos. Obtenemos lo siguiente:
Puede parecer extraño pero es lógico. La parte visible "final" no es más que la suma de cada parte visible de cada capa de máscara. Podemos cambiar este comportamiento usando mask-composite
. Necesitaría un artículo completo para explicar esta propiedad, así que simplemente daré el valor.
En nuestro caso, debemos considerar intersect
(y destination-out
para la propiedad prefijada). Nuestro código se convertirá en:
.loader {
width: 100px; /* size */
aspect-ratio: 1;
border-radius: 50%;
background: conic-gradient(#0000,var(--c));
mask:
radial-gradient(farthest-side,#0000 calc(100% - var(--b)),#000 0),
repeating-conic-gradient(#000 0 calc(360deg/var(--n) - var(--g)) , #0000 0 calc(360deg/var(--n));
-webkit-mask-composite: destination-in;
mask-composite: intersect;
}
¡Hemos terminado con la forma! Solo nos falta la animación. Esta última es una rotación infinita.
Lo único a tener en cuenta es que estoy usando una steps
animación para crear la ilusión de guiones fijos y colores en movimiento.
Aquí hay una ilustración para ver la diferencia.
Una animación lineal frente a una animación de pasos
La primera es una rotación lineal y continua de la forma (no la que queremos) y la segunda es una animación discreta (la que queremos).
Aquí está el código completo, incluida la animación:
<div class="loader"></div>
<div class="loader" style="--b: 15px;--c: blue;width: 120px;--n: 8"></div>
<div class="loader" style="--b: 5px;--c: green;width: 80px;--n: 6;--g: 20deg"></div>
<div class="loader" style="--b: 20px;--c: #000;width: 80px;--n: 15;--g: 7deg"></div>
.loader {
--b: 10px; /* border thickness */
--n: 10; /* number of dashes*/
--g: 10deg; /* gap between dashes*/
--c: red; /* the color */
width: 100px; /* size */
aspect-ratio: 1;
border-radius: 50%;
padding: 1px;
background: conic-gradient(#0000,var(--c)) content-box;
-webkit-mask:
repeating-conic-gradient(#0000 0deg,
#000 1deg calc(360deg/var(--n) - var(--g) - 1deg),
#0000 calc(360deg/var(--n) - var(--g)) calc(360deg/var(--n))),
radial-gradient(farthest-side,#0000 calc(98% - var(--b)),#000 calc(100% - var(--b)));
mask:
repeating-conic-gradient(#0000 0deg,
#000 1deg calc(360deg/var(--n) - var(--g) - 1deg),
#0000 calc(360deg/var(--n) - var(--g)) calc(360deg/var(--n))),
radial-gradient(farthest-side,#0000 calc(98% - var(--b)),#000 calc(100% - var(--b)));
-webkit-mask-composite: destination-in;
mask-composite: intersect;
animation: load 1s infinite steps(var(--n));
}
@keyframes load {to{transform: rotate(1turn)}}
Notarás algunas diferencias con el código que usé en la explicación:
padding: 1px
y configurando el fondo paracontent-box
+/1deg
entre los colores de larepeating-conic-gradient()
radial-gradient()
Esas son algunas correcciones para evitar fallas visuales. Se sabe que los degradados producen resultados "extraños" en algunos casos, por lo que debemos ajustar algunos valores manualmente para evitarlos.
Al igual que el cargador anterior, comencemos con una descripción general:
https://codepen.io/t_afif/pen/bGoNddg
<div class="loader"></div>
<div class="loader" style="--s:10px;--n:10;color:red"></div>
<div class="loader" style="--g:0px;color:darkblue"></div>
<div class="loader" style="--s:25px;--g:8px;border-radius:50px;color:green"></div>
.loader {
--n:5; /* control the number of stripes */
--s:30px; /* control the width of stripes */
--g:5px; /* control the gap between stripes */
width:calc(var(--n)*(var(--s) + var(--g)) - var(--g));
height:30px;
padding:var(--g);
margin:5px auto;
border:1px solid;
background:
repeating-linear-gradient(90deg,
currentColor 0 var(--s),
#0000 0 calc(var(--s) + var(--g))
) left / calc((var(--n) + 1)*(var(--s) + var(--g))) 100%
no-repeat content-box;
animation: load 1.5s steps(calc(var(--n) + 1)) infinite;
}
@keyframes load {
0% {background-size: 0% 100%}
}
Tenemos la misma configuración que el cargador anterior. Variables CSS que controlan el cargador:
--n
define el número de guiones/rayas.--s
define el ancho de cada franja.--g
define el espacio entre las rayas.Ilustración de las variables CSS
De la figura anterior podemos ver que el ancho del elemento dependerá de las 3 variables. El CSS será el siguiente:
.loader {
width: calc(var(--n)*(var(--s) + var(--g)) - var(--g));
height: 30px; /* use any value you want here */
padding: var(--g);
border: 1px solid;
}
Usamos padding
para establecer el espacio en cada lado. Entonces el ancho será igual al número de rayas multiplicado por su ancho y el espacio. Eliminamos un espacio porque para N
las rayas tenemos N-1
espacios.
Para crear las rayas usaremos el siguiente degradado.
repeating-linear-gradient(90deg,
currentColor 0 var(--s),
#0000 0 calc(var(--s) + var(--g))
)
De 0
a s
es el color definido y de s
a s + g
un color transparente (la brecha).
Estoy usando currentColor
cuál es el valor de la color
propiedad. Tenga en cuenta que no definí ningún color dentro border
, por lo que también se usará para el valor de color
. Si queremos cambiar el color del cargador, solo necesitamos establecer la color
propiedad.
Nuestro código hasta ahora:
.loader {
width: calc(var(--n)*(var(--s) + var(--g)) - var(--g));
height: 30px;
padding: var(--g);
border: 1px solid;
background:
repeating-linear-gradient(90deg,
currentColor 0 var(--s),
#0000 0 calc(var(--s) + var(--g))
) left / 100% 100% content-box no-repeat;
}
Estoy usando content-box
para asegurarme de que el degradado no cubra el área de relleno. Luego defino un tamaño igual a 100% 100%
y una posición izquierda.
Es hora de la animación. Para este cargador, animaremos el background-size
de 0% 100%
a 100% 100%
lo que significa el ancho de nuestro degradado de 0%
a100%
Al igual que el cargador anterior, confiaremos en steps()
tener una animación discreta en lugar de una continua.
Una animación lineal frente a una animación de pasos
El segundo es el que queremos crear, y lo podemos lograr agregando el siguiente código:
.loader {
animation: load 1.5s steps(var(--n)) infinite;
}
@keyframes load {
0% {background-size: 0% 100%}
}
Si observa detenidamente la última figura, notará que la animación no está completa. Nos falta una raya al final, incluso si hemos usado N
. Esto no es un error, sino cómo steps()
se supone que funciona.
Para superar esto, necesitamos agregar un paso adicional. Aumentamos el background-size
de nuestro degradado para contener N+1
rayas y usar steps(N+1)
. Esto nos llevará al código final:
.loader {
width: calc(var(--n)*(var(--s) + var(--g)) - var(--g));
height: 30px;
padding: var(--g);
margin: 5px auto;
border: 1px solid;
background:
repeating-linear-gradient(90deg,
currentColor 0 var(--s),
#0000 0 calc(var(--s) + var(--g))
) left / calc((var(--n) + 1)*(var(--s) + var(--g))) 100%
content-box no-repeat;
animation: load 1.5s steps(calc(var(--n) + 1)) infinite;
}
@keyframes load {
0% {background-size: 0% 100%}
}
Tenga en cuenta que el ancho del degradado es igual a N+1
multiplicado por el ancho de una franja y un espacio (en lugar de ser 100%
)
Espero que disfrutes este tutorial. Si está interesado, he creado más de 500 cargadores div únicos solo para CSS . También escribí otro tutorial para explicar cómo crear el cargador de puntos usando solo propiedades de fondo .
Encuentre a continuación enlaces útiles para obtener más detalles sobre algunas propiedades que he usado y que no expliqué a fondo debido a su complejidad:
mask-composite
: https://css-tricks.com/mask-compositing-the-crash-course/steps()
: https://developer.mozilla.org/en-US/docs/Web/CSS/easing-function#the_steps_class_of_easing_functions¡Gracias por leer!
Enlace: https://www.freecodecamp.org/news/how-to-create-a-css-only-loader/
1624294740
0:00 Intro
0:15 Tokenmetrics
0:49 BTC Price Analysis [WATCH ALL!]
6:07 Crypto News
9:39 Join Tokenmetrics
📺 The video in this post was made by K Crypto
The origin of the article: https://www.youtube.com/watch?v=DtTd120OWfY
🔺 DISCLAIMER: The article is for information sharing. The content of this video is solely the opinions of the speaker who is not a licensed financial advisor or registered investment advisor. Not investment advice or legal advice.
Cryptocurrency trading is VERY risky. Make sure you understand these risks and that you are responsible for what you do with your money
🔥 If you’re a beginner. I believe the article below will be useful to you ☞ What You Should Know Before Investing in Cryptocurrency - For Beginner
⭐ ⭐ ⭐The project is of interest to the community. Join to Get free ‘GEEK coin’ (GEEKCASH coin)!
☞ **-----CLICK HERE-----**⭐ ⭐ ⭐
Thanks for visiting and watching! Please don’t forget to leave a like, comment and share!
#bitcoin #blockchain #bitcoin's next #-$10,000 #bitcoin's next -$10,000 dump imminent!!!!!!!!!! last chance or else!!!!!! ( hot new!!! ) #-$10,000 dump
1666082925
This tutorialvideo on 'Arrays in Python' will help you establish a strong hold on all the fundamentals in python programming language. Below are the topics covered in this video:
1:15 What is an array?
2:53 Is python list same as an array?
3:48 How to create arrays in python?
7:19 Accessing array elements
9:59 Basic array operations
- 10:33 Finding the length of an array
- 11:44 Adding Elements
- 15:06 Removing elements
- 18:32 Array concatenation
- 20:59 Slicing
- 23:26 Looping
Python Array Tutorial – Define, Index, Methods
In this article, you'll learn how to use Python arrays. You'll see how to define them and the different methods commonly used for performing operations on them.
The artcile covers arrays that you create by importing the array module
. We won't cover NumPy arrays here.
Let's get started!
Arrays are a fundamental data structure, and an important part of most programming languages. In Python, they are containers which are able to store more than one item at the same time.
Specifically, they are an ordered collection of elements with every value being of the same data type. That is the most important thing to remember about Python arrays - the fact that they can only hold a sequence of multiple items that are of the same type.
Lists are one of the most common data structures in Python, and a core part of the language.
Lists and arrays behave similarly.
Just like arrays, lists are an ordered sequence of elements.
They are also mutable and not fixed in size, which means they can grow and shrink throughout the life of the program. Items can be added and removed, making them very flexible to work with.
However, lists and arrays are not the same thing.
Lists store items that are of various data types. This means that a list can contain integers, floating point numbers, strings, or any other Python data type, at the same time. That is not the case with arrays.
As mentioned in the section above, arrays store only items that are of the same single data type. There are arrays that contain only integers, or only floating point numbers, or only any other Python data type you want to use.
Lists are built into the Python programming language, whereas arrays aren't. Arrays are not a built-in data structure, and therefore need to be imported via the array module
in order to be used.
Arrays of the array module
are a thin wrapper over C arrays, and are useful when you want to work with homogeneous data.
They are also more compact and take up less memory and space which makes them more size efficient compared to lists.
If you want to perform mathematical calculations, then you should use NumPy arrays by importing the NumPy package. Besides that, you should just use Python arrays when you really need to, as lists work in a similar way and are more flexible to work with.
In order to create Python arrays, you'll first have to import the array module
which contains all the necassary functions.
There are three ways you can import the array module
:
import array
at the top of the file. This includes the module array
. You would then go on to create an array using array.array()
.import array
#how you would create an array
array.array()
array.array()
all the time, you could use import array as arr
at the top of the file, instead of import array
alone. You would then create an array by typing arr.array()
. The arr
acts as an alias name, with the array constructor then immediately following it.import array as arr
#how you would create an array
arr.array()
from array import *
, with *
importing all the functionalities available. You would then create an array by writing the array()
constructor alone.from array import *
#how you would create an array
array()
Once you've imported the array module
, you can then go on to define a Python array.
The general syntax for creating an array looks like this:
variable_name = array(typecode,[elements])
Let's break it down:
variable_name
would be the name of the array.typecode
specifies what kind of elements would be stored in the array. Whether it would be an array of integers, an array of floats or an array of any other Python data type. Remember that all elements should be of the same data type.elements
that would be stored in the array, with each element being separated by a comma. You can also create an empty array by just writing variable_name = array(typecode)
alone, without any elements.Below is a typecode table, with the different typecodes that can be used with the different data types when defining Python arrays:
TYPECODE | C TYPE | PYTHON TYPE | SIZE |
---|---|---|---|
'b' | signed char | int | 1 |
'B' | unsigned char | int | 1 |
'u' | wchar_t | Unicode character | 2 |
'h' | signed short | int | 2 |
'H' | unsigned short | int | 2 |
'i' | signed int | int | 2 |
'I' | unsigned int | int | 2 |
'l' | signed long | int | 4 |
'L' | unsigned long | int | 4 |
'q' | signed long long | int | 8 |
'Q' | unsigned long long | int | 8 |
'f' | float | float | 4 |
'd' | double | float | 8 |
Tying everything together, here is an example of how you would define an array in Python:
import array as arr
numbers = arr.array('i',[10,20,30])
print(numbers)
#output
#array('i', [10, 20, 30])
Let's break it down:
import array as arr
.numbers
array.arr.array()
because of import array as arr
.array()
constructor, we first included i
, for signed integer. Signed integer means that the array can include positive and negative values. Unsigned integer, with H
for example, would mean that no negative values are allowed.Keep in mind that if you tried to include values that were not of i
typecode, meaning they were not integer values, you would get an error:
import array as arr
numbers = arr.array('i',[10.0,20,30])
print(numbers)
#output
#Traceback (most recent call last):
# File "/Users/dionysialemonaki/python_articles/demo.py", line 14, in <module>
# numbers = arr.array('i',[10.0,20,30])
#TypeError: 'float' object cannot be interpreted as an integer
In the example above, I tried to include a floating point number in the array. I got an error because this is meant to be an integer array only.
Another way to create an array is the following:
from array import *
#an array of floating point values
numbers = array('d',[10.0,20.0,30.0])
print(numbers)
#output
#array('d', [10.0, 20.0, 30.0])
The example above imported the array module
via from array import *
and created an array numbers
of float data type. This means that it holds only floating point numbers, which is specified with the 'd'
typecode.
To find out the exact number of elements contained in an array, use the built-in len()
method.
It will return the integer number that is equal to the total number of elements in the array you specify.
import array as arr
numbers = arr.array('i',[10,20,30])
print(len(numbers))
#output
# 3
In the example above, the array contained three elements – 10, 20, 30
– so the length of numbers
is 3
.
Each item in an array has a specific address. Individual items are accessed by referencing their index number.
Indexing in Python, and in all programming languages and computing in general, starts at 0
. It is important to remember that counting starts at 0
and not at 1
.
To access an element, you first write the name of the array followed by square brackets. Inside the square brackets you include the item's index number.
The general syntax would look something like this:
array_name[index_value_of_item]
Here is how you would access each individual element in an array:
import array as arr
numbers = arr.array('i',[10,20,30])
print(numbers[0]) # gets the 1st element
print(numbers[1]) # gets the 2nd element
print(numbers[2]) # gets the 3rd element
#output
#10
#20
#30
Remember that the index value of the last element of an array is always one less than the length of the array. Where n
is the length of the array, n - 1
will be the index value of the last item.
Note that you can also access each individual element using negative indexing.
With negative indexing, the last element would have an index of -1
, the second to last element would have an index of -2
, and so on.
Here is how you would get each item in an array using that method:
import array as arr
numbers = arr.array('i',[10,20,30])
print(numbers[-1]) #gets last item
print(numbers[-2]) #gets second to last item
print(numbers[-3]) #gets first item
#output
#30
#20
#10
You can find out an element's index number by using the index()
method.
You pass the value of the element being searched as the argument to the method, and the element's index number is returned.
import array as arr
numbers = arr.array('i',[10,20,30])
#search for the index of the value 10
print(numbers.index(10))
#output
#0
If there is more than one element with the same value, the index of the first instance of the value will be returned:
import array as arr
numbers = arr.array('i',[10,20,30,10,20,30])
#search for the index of the value 10
#will return the index number of the first instance of the value 10
print(numbers.index(10))
#output
#0
You've seen how to access each individual element in an array and print it out on its own.
You've also seen how to print the array, using the print()
method. That method gives the following result:
import array as arr
numbers = arr.array('i',[10,20,30])
print(numbers)
#output
#array('i', [10, 20, 30])
What if you want to print each value one by one?
This is where a loop comes in handy. You can loop through the array and print out each value, one-by-one, with each loop iteration.
For this you can use a simple for
loop:
import array as arr
numbers = arr.array('i',[10,20,30])
for number in numbers:
print(number)
#output
#10
#20
#30
You could also use the range()
function, and pass the len()
method as its parameter. This would give the same result as above:
import array as arr
values = arr.array('i',[10,20,30])
#prints each individual value in the array
for value in range(len(values)):
print(values[value])
#output
#10
#20
#30
To access a specific range of values inside the array, use the slicing operator, which is a colon :
.
When using the slicing operator and you only include one value, the counting starts from 0
by default. It gets the first item, and goes up to but not including the index number you specify.
import array as arr
#original array
numbers = arr.array('i',[10,20,30])
#get the values 10 and 20 only
print(numbers[:2]) #first to second position
#output
#array('i', [10, 20])
When you pass two numbers as arguments, you specify a range of numbers. In this case, the counting starts at the position of the first number in the range, and up to but not including the second one:
import array as arr
#original array
numbers = arr.array('i',[10,20,30])
#get the values 20 and 30 only
print(numbers[1:3]) #second to third position
#output
#rray('i', [20, 30])
Arrays are mutable, which means they are changeable. You can change the value of the different items, add new ones, or remove any you don't want in your program anymore.
Let's see some of the most commonly used methods which are used for performing operations on arrays.
You can change the value of a specific element by speficying its position and assigning it a new value:
import array as arr
#original array
numbers = arr.array('i',[10,20,30])
#change the first element
#change it from having a value of 10 to having a value of 40
numbers[0] = 40
print(numbers)
#output
#array('i', [40, 20, 30])
To add one single value at the end of an array, use the append()
method:
import array as arr
#original array
numbers = arr.array('i',[10,20,30])
#add the integer 40 to the end of numbers
numbers.append(40)
print(numbers)
#output
#array('i', [10, 20, 30, 40])
Be aware that the new item you add needs to be the same data type as the rest of the items in the array.
Look what happens when I try to add a float to an array of integers:
import array as arr
#original array
numbers = arr.array('i',[10,20,30])
#add the integer 40 to the end of numbers
numbers.append(40.0)
print(numbers)
#output
#Traceback (most recent call last):
# File "/Users/dionysialemonaki/python_articles/demo.py", line 19, in <module>
# numbers.append(40.0)
#TypeError: 'float' object cannot be interpreted as an integer
But what if you want to add more than one value to the end an array?
Use the extend()
method, which takes an iterable (such as a list of items) as an argument. Again, make sure that the new items are all the same data type.
import array as arr
#original array
numbers = arr.array('i',[10,20,30])
#add the integers 40,50,60 to the end of numbers
#The numbers need to be enclosed in square brackets
numbers.extend([40,50,60])
print(numbers)
#output
#array('i', [10, 20, 30, 40, 50, 60])
And what if you don't want to add an item to the end of an array? Use the insert()
method, to add an item at a specific position.
The insert()
function takes two arguments: the index number of the position the new element will be inserted, and the value of the new element.
import array as arr
#original array
numbers = arr.array('i',[10,20,30])
#add the integer 40 in the first position
#remember indexing starts at 0
numbers.insert(0,40)
print(numbers)
#output
#array('i', [40, 10, 20, 30])
To remove an element from an array, use the remove()
method and include the value as an argument to the method.
import array as arr
#original array
numbers = arr.array('i',[10,20,30])
numbers.remove(10)
print(numbers)
#output
#array('i', [20, 30])
With remove()
, only the first instance of the value you pass as an argument will be removed.
See what happens when there are more than one identical values:
import array as arr
#original array
numbers = arr.array('i',[10,20,30,10,20])
numbers.remove(10)
print(numbers)
#output
#array('i', [20, 30, 10, 20])
Only the first occurence of 10
is removed.
You can also use the pop()
method, and specify the position of the element to be removed:
import array as arr
#original array
numbers = arr.array('i',[10,20,30,10,20])
#remove the first instance of 10
numbers.pop(0)
print(numbers)
#output
#array('i', [20, 30, 10, 20])
And there you have it - you now know the basics of how to create arrays in Python using the array module
. Hopefully you found this guide helpful.
Thanks for reading and happy coding!
#python #programming