Categories
PROGRAMMING TOOLS TUTORIALS

What Is A Curl Command & How To Use Curl?

Did you just hear about curl and want to know what a curl command is? What it does and how to use it? Then you have come to right place. Because in this article, we will go through each of these questions and answer it in an easy way.

So if you are ready to learn more about it, let us get started!

What Is A Curl Command?

Curl is an open source project that mainly has two modules in it. These modules are:

Curl Command Line Interface Tool

Curl CLI tool is the tool that you have most probably heard of and want to use. This is a command line interface tool. So you need to type in the curl commands in your computer’s command prompt. But while this might seem tedious to you, it has one great advantage for being a CLI tool.

Can you guess what it is?

Curl logo
Curl logo

It is the fact that you can then use it in your scripts as well! Now that is always welcome, right? But you may now be wondering what this curl CLI tool even do, Right? Well, you will have to wait a bit to get answer for that. As we will learn about it only in the next section.

But for now just know that this is one of the main module of Curl. Alright? So then what is the other module of Curl? Let us take a look at that now!

libcurl C library

So along with the Curl CLI tool, we also have a C library called libcurl. In fact, the CLI tool uses libcurl under the hood. But why have two separate modules – curl CLI and libcurl, you ask?

Well, here is the thing. While you can use the curl tool, wouldn’t it be great if you could also build a curl GUI application if you want? Or want to call it programmatically from other programming languages like Python, Java etc.?

libcurl is created for exactly that reason. It gives us a bunch of C language based APIs that you can use to create applications on top of that! Or call it from other programming languages if you want to!

Alright! So now that you know what a curl tool is, it is now time to learn what it does.

What Does A Curl Command Do?

The curl command is used to transfer data between a computer and a server. That’s all it does at it’s core! It is used to exchange information between a client (your computer) and a server.

Now, that server can be of any type such as a web server, ftp server, email server, ssh server anything. It doesn’t matter. You can always use the curl command to download or upload data from or to these servers.

Wait a second! So how is that possible for a single tool like curl to talk to these many types of servers, you ask? Well, it is possible because the libcurl C library that the curl CLI tool uses under the hood can talk to any of these servers. Because libcurl library has full understanding of all the different protocols that these servers use to talk!

So because of this, if you want to talk to a web server, libcurl can make use of HTTP protocol. But on the other hand, if you want to talk to an email server, it can use SMTP, POP3 and other email protocols.

So you may then be wondering as to what all protocols does this libcurl C library support, right? Well, here is a list of protocols that the curl tool supports.

List Of Protocols Supported By Curl

  • HTTP
  • HTTPS
  • SMTP
  • SMTPS
  • POP3
  • POP3S
  • DICT
  • FILE
  • GOPHER
  • FTP
  • FTPS
  • IMAP
  • IMAPS
  • LDAP
  • LDAPS
  • MQTT
  • RTMP
  • RTSP
  • SCP
  • SFTP
  • SMB
  • SMBS
  • TELNET
  • TFTP

So as you can see, libcurl has support for a lot of protocols and this list is still growing! Because, the maintainers of curl keep adding new protocols as and when the need arises!

Alright! Now that we know what a curl command is and what it is used for, it is time to learn how to use it.

But as we saw that curl is a versatile tool that can talk with many different protocols, it’s use cases are also many. So trying to list all the ways you can use curl in this article will not be possible. So we will be creating a separate post for that.

But we do not want to leave you hanging without showing you how to use it at least at a basic level. So here is a simple example of how you can use curl to fetch a web page from a web server.

A Simple Curl Command Example

If you have curl installed on your computer, just issue the following command:

curl -X GET https://www.google.com

When you enter this command on your computer’s command prompt, you should see a bunch of text running along your console. But it is not some gibberish text. But rather it is the HTML code for the google.com home page!

So what exactly has happened here? When you issued the above curl command, the curl CLI asked google.com web server to send it’s HTML home page. You just issued a HTTP GET request to Google’s server and as a result it sent back the home page!

Now how did Google know if it was a GET or a POST HTTP request? How did it know it had to send the home page back to your computer? These are all the questions that has answer’s in the command that we issued above.

But we will discuss about this curl GET command and many other related commands in our next article. So you will have to wait until you read the next article to get an answer for that. Alright?

So see you in the next article. Until then, take care!

Categories
FFMPEG PROGRAMMING TOOLS TUTORIALS

Display FFmpeg output without saving to file

So if you have been editing video files using FFmpeg, you know that we need to save the output to a file displaying viewing it, right? But would it not be great if we could display FFmpeg output without having to save it to a file?

Well, there is good news for you! Because we can actually do this! There is this special command that you can use to display the ffmpeg output without having to save it to a file first. Want to know what that command is? Well, here we go!

Here is the command we normally use to display output by saving it to an output file:

ffmpeg -i input_file ... options ... output_file
Display ffmpeg output directly to screen using ffplay command

But if you want to display the output directly to the screen, we will let go off the ffmpeg command in the above command line. But instead, we will use the other available ffmpeg tool called “ffplay”.

What Is ffplay?

We have already seen what ffplay is in our earlier article on ffmpeg commands list. But just to re-iterate, ffplay is another tool from the ffmpeg command line tool set. It is used to display the content of an ffmpeg operation directly on to the screen without having to save it to a file!

Just what we needed, right?

So how can we use the ffplay command line tool?

Display FFmpeg output to a screen

The syntax of ffplay command line tool is pretty straight forward and it looks somewhat like this:

ffplay -i input_file .... test_options

So it looks pretty straight forward, right?

All you have to do is to pass in the input media file along with any optional test parameters. So, if we have an input file called input.mp4, we can run the above command as follows:

ffplay -i input.mp4

That is it! By running the above command, you should now be able to display ffmpeg output onto the screen.

So that is all there is to it guys! You just run the command ffplay in place of ffmpeg and let it display the contents directly on your screen!

If you still have any doubts on this, do let me knw in the comments below!

Categories
FFMPEG PROGRAMMING TOOLS TUTORIALS

FFmpeg Command Line Syntax With Example

In this article, we will take a look at the FFmpeg command line syntax. We will explore all the options made available in the FFmpeg tool while using it to transform one or more input video files.

But before we take a look at the syntax for FFmpeg commands, let us first try to understand what an FFmpeg command line tool is.

What Is An FFmpeg Command Line Tool?

FFMpeg is an open source command line tool used for multimedia file processing.

So now that looks like quite a complicated and technically rich definition. But what it really means is that we can use the FFmpeg tool to encode or decode audio or video files at a high speed and quality. But not just that, FFmpeg also helps us in achieving these at a very small file size.

Learn About FFmpeg Command Line Syntax
Learn About FFmpeg Command Line Syntax

So in general, we can say that FFmpeg is an efficient media file processing tool that is also open source!

What does FFmpeg stands for?

FFmpeg stands for Fast Forward Moving Pictures Expert Group.

But generally when we talk about FFmpeg, we talk about a library of tools that goes along with FFmpeg tool in video or audio file processing. So, this FFmpeg library is generally made up of these command line tools and libraries.

List Of FFmpeg Software Library

NameSoftware TypeFunctionality
ffmpegCommand line toolFast audio and video encoder/decoder
ffplayCommand line toolMedia Player
ffserverCommand line toolHTTP or RTSP based multimedia streaming server
ffprobeCommand line toolDisplays Media file information
libavcodecFFmpeg software libraryContains various multimedia codec
libavdeviceFFmpeg software libraryLibrary for various device handling
libavutilFFmpeg software libraryContains a bunch of multimedia utilities
libavformatFFmpeg software libraryContains various media formats
libavfilterFFmpeg software libraryContains various filters
libswscaleFFmpeg software libraryUsed for media scaling
libswresampleFFmpeg software libraryUsed for audio resampling
libpostprocFFmpeg software libraryUsed for post processing
List Of FFmpeg Software Library

So now that we know what an FFmpeg tool is and its associated set of software libraries, let us take a look at the syntax for using FFmpeg tool.

FFmpeg Command Line Syntax

FFmpeg is a command line tool that comes with a highly complex set of options available to pass as both input and output parameters. So as a result it’s generic syntactical structure will also look quite complex at the first glance. Here is how a generic ffmpeg command syntax looks like:

ffmpeg global_options input1_options -i input1 input2_options -i input2....output1_options output1 output2_options output2

But do not worry for looking that complex, as we can easily understand what it does by breaking it down to simpler parts.

  • global_options: So this option set will affect all inputs and outputs.
  • inputx_options: But this will affect the specific input x.
  • outputx_options: This will affect the specific output x.

So as you can see, we can pass certain options that are applicable to all or specific inputs and outputs.

So now that we have seen how a generic ffmpeg command looks like, let us take a look at how it is used using an example:

ffmpeg -y -i vidIn.mp4 -s vga vidOut.mp4

In the above example, the option “-y” passed initially is a global option, but the option “-s vga” passed before the output file name is an output specific option.

Conclusion

So there you have it! That is how a generic ffmpeg syntax looks like. But this may still be very looking very theoretical to you. So in the next article, we will take a look at all the available options that can be passed to both inputs and outputs in the next tutorial. But for now, this should give you a good idea on how FFmpeg command works.

But if you still have any doubts regarding this tutorial, do let me know in the comments below and I will be happy to answer them. So until next time, take care!

But before you go, do you want to know how to edit videos for WhatsApp? Check this out!

Categories
JAVASCRIPT PROGRAMMING TUTORIALS WEB DEVELOPMENT

How To Enable Or Disable Console.log For Debugging

So you are working on your Javascript project and you want to know how to enable or disable console.log messages? Then read on to learn how you can do it quite easily.

Whenever you are working with Javascript, be it on the front-end side with vanilla Javascript or frameworks like React, Vuejs etc. Or on the backend with Node JS, Express JS etc for your servers, you want a way to debug the code. Right?

One of the easiest way we try to debug our code is by using simple print statements. So on the Javascript end, this usually boils down to using console.log( ) messages.

But here is the thing. When you use a programming language that will be compiled – such as C, C++ or Java, we can use compiler flags to enable or disable debug messages.

However, for an interpreted language like Javascript, we dont have a built in method for how to Enable or Disable console.log( ) messages.

So what do we do in that case? Well, if we don’t have a built-in method to enable or disable Javascript console.log( ), then we need to create one!

And that is what we are going to do here. We will be writing a simple Javascript module that we can use to enable or disable Javascript’s console.log( ) messages.

So how do we design this piece of code?

Here is what we are going to do. Instead of using console.log( ) to print our debug messages, we will use our own custom function called debugLog( ). But within this debugLog( ) function, we will have a simple Debug Enable (or) Disable flag.

Now when this flag is set, we will call the console.log( ) in turn and if not set, we will skip printing the log messages. It is as simple as that!

Sounds good? Great! So here is the piece of code that does just that!

How to Enable or Disable console.log
How to Enable or Disable console.log using a switch?

Create a file called “utils.js” and add the following piece of code into it:

DEBUG = true;

function debugLog(msg) {
    if (DEBUG) {
        console.log(msg);
    }
}

//For express.js, we need to export this module
module.exports = debugLog;

So that is it! From now on, call debugLog( ) instead of console.log( ) to write your debug message. An example piece of such a debug message is shown below:

debugLog("[app] Connect to mongoDB...");

So whenever you want to switch on the debug logging, set the flag as below:

DEBUG = true;

But the moment you want to turn these debug messages OFF, just set it to false:

DEBUG = false;

And that is all there is to it! This is how you can enable or disable console.log( ) messages for debugging!

Categories
HTML PROGRAMMING TUTORIALS WEB BROWSER WEB DEVELOPMENT

HTML Meter Tag Explained

HTML Meter Tag is a new type of HTML tag introduced in the HTML 5 specification. So chances are, you are not so familiar with it. Right? But do not worry, as we will start exploring everything there is to the HTML meter tag in this article. So you will soon be able to start using it in your websites and web apps.

Sounds good? Great! Then let us get started!

What Is An HTML Meter Tag?

So consider this situation. You are creating a dashboard in your web app that, among many other things, also shows the storage space left in your hard disk. Now how will you go about designing it?

So the chances are, you might create a custom HTML element that is styled using its CSS properties to display the storage space graphically. But even if it seems a valid solution, problem is you need to keep creating such custom HTML elements frequently. And if you are designing a web app having a dashboard showing multiple data, these custom elements will only increase. Right?

HTML5 Meter tag explained with example
HTML5 Meter tag explained

So then what is an easier solution for this problem than that? Well for one, would it not be easier if we had a built in HTML element to display such measurements? With the growing number of web apps with dashboards, having such a prebuilt HTML element makes sense, right?

Well, that is precisely what the HTML5 specification guys decided to do! And that is how the HTML Meter tag came into picture!

So What Does The Meter Tag Do?

The idea was to create an HTML element that can be used to display any measured values within it. So that is what a Meter tag does!

A Meter tag can be used to display a measured value within a given range of values.

Sounds good? Great! Then let us move on to take a look at an example piece of code that uses the Meter tag. Shall we?

HTML Meter Tag Example

So for this example, let us assume that we want to display the size of a cloud storage space. For brevity, let us say we want to display the size of our mail box which is currently at 66% full. Alright?

So how do we go about using the Meter tag to display this value? And how will the displayed result look like?

To answer these questions, let us start writing our code as shown below:

<!DOCTYLE html>

<html>
  <head>
  </head>
  <body>
    <label for="disk">Disk Usage: </label>
    <meter id="disk" value="0.7">70%</meter>
  </body>
</html>

Now, if you copy the above piece of coded into a new file and save it as meter.html, what do you see?

Go ahead and try it out! I want you to do it yourself so that you become familiar with how to use the HTML meter tags.

Did you try it? If you did, you should see that the result looks something like this:

70%

Conclusion

So that is it! That is how the Meter tag works! You just feed the right value into the “value” property of the Meter tag and it will display the meter gauge accordingly. Go ahead and test it out with few more different values and see how the gauge changes yourself.

Finally, if you have any questions or suggestions, let me know in the comment below! So until next time, take care!

Categories
PROGRAMMING TUTORIALS

Typescript Type Conversion

In this article, we will learn how to do type conversion in Typescript. So are you working on Typescript and now want to convert a value from string to number? Or want to convert a number to string in Typescript? Well, then read through this article to learn how to do that!

So here is the thing. You have a Typescript value stored in a variable and you want to use that. But the thing is that, you can not use it as is. Why? Because this number is stored as a string type. But you want to use it as a number. So how can we do that?

Typescript Type Conversion
Typescript Programming Language Logo

That is when the Typescript type conversion comes to your rescue! Using the Typescript type conversion, you can turn that variable’s value from a string to a number!

Sounds great! Right? But then how do we do that in our code? To learn that, take a look at the example code given below:

Example Of Type Conversion from a String to Number

let myStringOfNum: string = "10";
let myNumber: number = Number(myStringOfNum);
console.log(typeof myStringOfNum);
console.log(typeof myNumber);

So if you look at the above code, we have a variable called myStringOfNum that holds a string that reads “10”. But we want that string to be used as a number. So what do we do for that? We use Type Conversions!

So in Line 2, we declared another variable called myNumber. But this time, this variable is of type number. What we do next is that we will take the value of the vairable myStringOfNum and convert its type to Number. We do that by using the typecast Number(). The resulting value is then assigned to our variable myNumber.

So in that case, our console output will look like this:

string
number

Example Of Type Conversion from a Number to String

In a similar way, we can convert a number to string in Typescript. So when we take a look at this code:

let myNumber: number = 10
let myString: string = String(number)
console.log(typeof myNumber);
console.log(typeof myString);

What do you think how to output will look like? I think your guess will be:

number
string

Summary

So in summary, we can convert a number to string or a string to number using Typescript’s Type conversion feature. I guess it could not have been any more simpler than this. What do you guys think? It is, Right?

Learn this difference between an expression and a statement here

Categories
ANDROID APP COMPUTER HARDWARE PROGRAMMING SMARTPHONES TUTORIALS

Turn Android Phone Into A Bootable USB Drive

In this article, we will learn how we can turn our Android phone into a Bootable USB drive. So by doing so, we can just use our Android smartphone to install OS on our computers.

But are you now wondering why do we even want to do that in the first place? What are we going to gain by doing so? Well I agree! These are all valid questions you are asking. So let me first explain to you about the advantages of that.

Turn Your Android Phone Into A Bootable USB
Turn Your Android Phone Into A Bootable USB

Sounds good? Great! Then let us get started!

Advantages of turning Android smartphone into a bootable USB drive

So here are some of the advantages I can think of in turning our Android into a bootable USB drive:

  • As we carry our smartphones with us every where we go, a bootable USB drive will be there with us all the time.
  • Chances of losing a bootable USB drive becomes less – If we created a separate bootable OS USB drive, there is a high change of us losing it. But if we have it on our smartphones, chances of losing it reduces greatly. Right?
  • Download new OS ISO images on the fly – Now what do we mean by that? You see, if we are using a USB pen drive as a bootable device, we can boot only those OS images that are already stored in it. But if we want to install a new OS, we cant, right? But on the other hand, if we use our Android smartphone as a bootable drive, we can download new OS ISO images from the internet on the fly, directly onto our smartphone. Now, that is a cool thing to be able to do, right? 😉

So now that we know how an Android smartphone bootable USB drive is beneficial, let us see how we can create one.

How To Turn Your Android Phone Into A Bootable USB?

There are many Android apps on the Google Playstore that we can use to turn our Android Phone into a bootable USB drive. But the most famous one is called the DriveDroid.

The main advantage of this app is that it provides you with many ISO images that you can download directly. Some of the ISO images that DriveDroid provides includes – Arch Linux, Debian, Fedora, FreeBSD ISOs and many more.

So we will be using this Android app to use our smartphone as a bootable USB drive.

Here are the steps you need to take to turn Android Phone into a Bootable USB:

  1. Install The App From The Google Playstore: Now, as we discussed earlier in the article, go ahead and install the app from the Android Playstore.
  2. Connect The Android Smartphone Into Your Computer Using USB: Make sure that your smartphone is connected in USB MTP mode.
  3. Open the App On Your Smartphone: Click and open the Android app on your smartphone.
  4. Click On the ‘+’ button and select “Create Blank Image”: This is where you will be creating your bootable USB Drive space.
  5. Set A Bootable Image Name & Its Size: This is where you will be setting the name and size of the boot space. Make sure that the size you set is equal to or greater than the ISO file size. Click the Create button at last.
  6. Select An OS ISO Image To Burn Into The USB Space: This is the final step where we choose the OS image we want to burn into our bootable USB space.

Once you have followed the above steps, you should a bootable USB drive as part of your Android smartphone.

How To Boot An ISO Image From Android Smartphone

So that is it! Those were the only steps you had to take to create a bootable OS on your smartphone. Now anytime you want to boot a computer using this USB bootable drive, just follow these steps:

  • Plug in your smartphone to the computer you want to boot, using a USB cable.
  • Power ON the computer and go to its BIOS menu by pressing F2 or the F10 key as mentioned on the computer screen.
  • Go to the Boot Priority and change it to Boot From USB to select your Android smartphone.

That is it. From here on, your PC should boot just like how it would from any USB bootable device.

Conclusion

So these days, your Android smartphone has become the swiss army knife of technology. You can use it to do many things that required several gadgets earlier.

In this blog, we try to explore all the different ways we can use our smartphones to make our lives better. So if you liked what you read, bookmark muddoo.com on your browser and visit it regularly to read more content like this.

Also, if you have any questions or suggestions for our website, please do leave a comment below. We will be delighted to know what you think! So until next time, cya and take care!

Categories
ELECTRONICS EMBEDDED EMBEDDED COMPUTERS EMBEDDED PROGRAMMING IOT PROGRAMMING RASPBERRY PI TUTORIALS

WS2811 Vs WS2812 Difference In LED Strip

Are you planning to connect LED strips to your board? Then you would want to know the difference between WS2811 Vs WS2812. So read below to know more!

Most of the articles on Arduino or Raspberry Pi asks you to hook up an WS2811 or WS2812 LED strip to your board. But have you ever tried to know why these LED strips? Why not any other LED strips? What makes them so special? And what about the WS2811 Vs WS2812 Difference?

WS2811 Vs WS2812 Difference In LED
WS2811 Vs WS2812 Difference In LED

Not many tutorials out there have tried to answer these questions. So in this article, we will do just that! So brace yourself. Sit tight and join the ride to learn all about these LED strips. Alright? Cool! Here we go!

What Is An LED Strip?

So before we dwell into learning the WS2811 Vs WS2812 Difference, let us try to answer this question first.

What is an LED?

An LED is a simple semiconductor device that will emit light when electric current flows through it. Now notice how I used the term “semiconductor device“. But what type of device is it? Well to be precise, it is a semiconductor diode.

So in short, LED stands for Light Emitting Diode. So what do we mean by that? It means that when electricity passes through this diode, it emits light!

So now that we know what an LED is, it is time to answer our next question.

What Is WS2811 LED Strip?

A WS2811 LED strip uses WS2811 LED driver chip to control the LED strip. Uhh… Got confused by what I just said?

Well, let me clear that for you by answering this question.

What is an LED strip?

You see, an LED strip is just a fancy way of connecting a bunch of LED lights together along a strip of wire. So an LED strip will have more than one LED lights in them.

But what makes them great is that you can address each of these LED lights separately.

Wait what? How can we do that, you ask?

Well that is where LED driver IC like WS8211 comes into picture. It is these chips that will help us connecting to each of the LED and controlling them to turn then ON or OFF.

Are you with me until here? Great! Then let us find answer to the next question

How Does WS8211 Chip Control LED?

So here is the thing. To control individual LEDs in a strip, LED driver chips like WS8211 is used. These chips sit alongside the LED and control their light intensity.

So by makes use of serial protocol in your microcontroller and assigning unique addresses to each of the LED driver chip you can control each of the LEDs.

So this way, when you want to control an LED, your board’s microcontroller will send out the address of the LED driver chip to be controlled. This will make the respective LED chip accept the next set of commands to be sent for itself.

And this way, your board will be able to control every LED to be turned ON or OFF!

Pretty neat, isn’t it? 😉

So now that we know what an LED driver chip like WS8211 does, it is finally time to ask the important question of this article.

WS2811 Vs WS2812 Difference

While both WS2811 & WS2812 are LED controller ICs, there are a few notable differences between them. Here are few of them that we listed out for you:

WS2811 Vs WS2812 Difference In Power

WS2811 LED chip uses 12V to work. Where as a WS2812 chip which uses only 5V. But then how will this impact you?

Well you see, if you are going to use along LED strip with many LEDs in them, you are going to see quite a bit of voltage drop along the strip.

Which means that the last few LEDs that is present at the far end of the strip from the board will get lower voltage. Now, if this drop is significant, your last few LEDs might not be glowing as well as its should be.

You do not want that. Right?

So for example you have 30 LEDs in your strip and the voltage drop is 2V towards the end of the strip. Well in that case, if you are using a WS2811 chip, the last few LEDs will get around 10V. This is still reasonably OK for an LED supposed to operate at 12V.

But on the other hand, if you are using WS2812 that works at 5V. the same voltage drop will be down to 3V from the supposed 5V. Now that is quite a significant voltage drop there. Right?

So you need to keep this in mind while choosing between WS2811 Vs WS2812.

WS2811 Vs WS2812 Difference In LEDs Controlled

WS2811 actually do not control one LED. But instead it controls a cluster of 3 LEDs together. So that means that you cannot go below a granularity level of 3 LEDs to control them.

But on the other hand, WS2812 can control 1 LED each. So, if you need a control granularity of 1 LED, you should be going for WS2812 instead of WS2811.

WS2811 Vs WS2812 Difference In Power Consumption

As WS8211 operates at 12V, the amount of power consumed by it is significantly more. On the other hand, as WS8212 uses 5V, it can operate at a much lower power consumption.

WS2811 Vs WS2812 – Pros & Cons Of Each

WS8211 Pros

  • Operates at 12V. So it can control more number of LEDs in a strip.
  • Ease of use when dealing with mass LED controls.

WS8211 Cons

  • It cannot control lower than 3 LEDs at a time. So its lower granularity level of control lies at 3 LEDs.
  • Consumes significantly higher power because of 12V of operation.

WS8212 Pros

  • Can control single indiviudal LEDs.
  • LED control graularity lies at 1 LED.
  • Lower power consumption.

WS8212 Cons

  • As it operates at 5V, its voltage drop becomes significant as the number of LEDs in the strip increases.

Conclusion

So in conclusion, whether to choose WS2811 or WS2812 entirely depends upon the type of use cases you are dealing with. If you want to control a long LED strip then WS2811 is the obvious choice for you.

But on the other hand, if you want to control LEDs at the granular level of a single LED, then WS2812 is the option for you to go for.

So hope this article was helpful for you in understanding the WS2811 Vs WS2812 difference. If you still have any more questions in your mind, do let me know in the comments below and I will try to give you my best help.

So with that, I will end this article here. Until next time, take care and happy learning! 🙂

Categories
100DaysOfCode DATA MINING DATA SCIENCE DJANGO EMBEDDED PROGRAMMING IOT MACHINE LEARNING PROGRAMMING PYTHON TUTORIALS

Python Init Function Example, Syntax & Best Practices

In this article, we will take a look at the Python Init Function using an example. At first, we learn about it’s syntax and how we can use it. But once we are comfortable with it, we will then learn the best practices to use when we want to call it!

So, does this plan sound good? Yes? Then let’s do it! 😉

Python Init Function Example and its syntax
Python Init Function Example and its syntax explained in 5 minutes

What Is The Syntax Of Python Init Function?

So first, let us start with how the syntax of a Python Init function looks like. Now, for us to know about it, we will have to take a look at the Python’s official document. From this, we now get to know that the init function is a method of a class and it looks like:

def __init__(self)

From the above code, we can see that the init function takes at least one argument. This is called as ‘self‘ and it has got a special meaning to it. But before we learn about it, let us first know how Python’s Class works.

You see, in Python a Class can have many methods in it. But it will have one method in it for sure. It is called the init() method. Now this method will be present in a Python Class even if you have not declared it yourself!

So, in other words, init() is a function that you will always find in any Class of Python. If you declared it yourself, it is an explicit init() function. But if you did not, it will be an implicit init() function!

Alright, so we now that an init() function is always present in any Python Class, what does it do? We will answer this question next!

Why do we need an init() function in Python?

So to answer this, we need you to have some knowledge about Object Oriented Programming (OOP). So if you are not so familiar with it, please go and learn about constructors in OOP. But for now, I will assume that you do know about OOP.

So as you know in OOP, we have a constructor method in every Class. So whenever we create an object using a Class, its constructor method will get called. Right?

In the same way, in Python the init function acts as a constructor. So it will get called whenever we create a Python’s Class object.

So this is why I said that the init() function is always present in a Python Class. So, the syntax of the init() that you saw above is how it looks like when it is implicit.

What does the self parameter do?

In order to answer this, let us take a look at the syntax once again

class Test:
    def __init__(self)

Here, we are looking at the code of an example Python class called Test. So here we can see that the init function has one argument called self.

Now, in order to for us to understand what this self argument does, let us first create an object of the Test class type.

t = Test()

So in the above code, we created a new object instance of the Test class. Now, as soon as this code runs, the init() function of the Test class will get called. But what is getting passed as the self argument here the variable t itself.

So, we now see that the self argument of the init() function points to the instance of the object itself!

Are you clear until now? Good! Then let us now look into an example of Python Init() function.

Python Init Function Example

So, let us take a look at this Python Init function example code

class Point:
    def __init__(self, x, y):
        _x = x
        _y = y

p = Point(1,2)

From the above code, we can see that we now have a new Class called Point. We also have an init() function that we have defined explicitly. This init() function takes in three arguments self, x & y.

Is it all clear? Good!

So we can quickly see the advantage of using init() function here. With the help of this function, we are able to set the initial values of x & y variables!

So when we created an instance of Point class using the code:

p = Point(1,2)

we were able to set the p object’s x & y values to 1 & 2 right away!

So, if things are clear until this point, we will then move on to the last part of this article.

Best Practices With Python Init Function

So here is a list of best practices you should follow when writing an init function:

  • Always use Init() function to initialize values to object parameters
  • Reduce the number of parameters passed to the init() if possible. Do not over use it.
  • Keep the init() function simple. Do not abuse it!

Conclusion

So that is about it! That is all you need to know about Python init() function. We have discussed everything there is to know about Python’s init() function with an example. We also went through the best practice we should follow when making use of it.

So with this, I will end this article here. If you still have any more questions, please ask it in the comment section below!

Know the difference between an expression Vs statement in Python

So until next time, have a nice day! 🙂

Categories
ARDUINO ARM ARM ARCHITECTURE ARM PROGRAMMING COMPUTER HARDWARE ELECTRONICS EMBEDDED EMBEDDED COMPUTERS EMBEDDED PROGRAMMING HARDWARE IOT MACHINE LEARNING PROGRAMMING TUTORIALS

Buy Arduino Nano Online At A Low Price

In this article, we will learn how you can buy an Arduino Nano online. We will learn how to buy it online in India at a low price. But before we do that, let us first learn what an Arduino Nano is in the first place. So are you ready to dive in? Then let us go!

What Is An Arduino Nano Board?

If you are still a beginner in the embedded world, chances are you may have not heard about Arduino Nano until now. So what is it?

Well, the Arduino Nano is a small computer board that one can buy to learn more about computers. It is a computer on a single PCB board that contains all the circuitry needed to run the software code on it.

Oh wait a second here! So we are learning that it is a single board computer, right? Cool! But then you may be wondering how does this Arduino Nano look like?

I am glad you asked this question! Here is a picture of an Arduino Nano board I will share with you for your pleasure. Take a look at it.

Arduino Nano Board Available To Buy Online
Arduino Nano Board You Can Buy Online

Woah! That is one weird looking single board computer out there! But what are all those pins coming out of it?! Looks scary isn’t it?

But don’t worry. Even though the Arduino Nano board looks like that, it is actually easy to use. There are many tutorials we have that will teach how to use it. So learning how to work with it will never be a problem at all.

So with this introduction to Arduino Nano, it is time for us to move on to know how we can buy it online.

How To Buy Arduino Nano Online?

There are many sellers who are selling the Arduino Nano hardware board. They are selling these Arduino Nano boards in online stores as well as in the offline stores. However, when you try to buy the Arduino Nano board, you will realize that the price differs a lot from one seller to another!

Now this is happening because there they are all not original Arduino boards. You see, the thing is Arduino boards are all launched as open source hardware boards. So, their circuit as well as their board designs are all available for free for anyone to use.

So, what happened because of this is that there are many Arduino Nano clones as well. Since these Arduino clones are coming from China, they will be of cheaper cost.

So in that case, who is the original manufacturer of Arduino Nano?

Arduino is the company that designs and manufactures the original Arduino boards. But as this company is from Italy, the products it makes are costlier than their clones.

So now this begs us with our next question which is:

Who manufactures the Arduino clones?

Most of the Arduino clones manufacturers are from China. So since the manufacturing cost of these boards are cheaper in China as compared to Italy, the clones are always cheaper.

So here is the real reason then! This clearly tells us why we see so much difference in the price for the same Arduino Nano boards across different online websites!

So depending on how much you can spend, decide which Arduino Nano board you want to go for.

What is the difference between an original Arduino Nano Vs a Nano clone?

Now as I told earlier, both the original and the cloned Nano boards use the same circuit design. So nothing is different over here. But there is one difference between the two. And this difference is in terms of the quality of components used.

So if you want a higher quality board, you pay more and go for original Nano module. But on the other hand, if you want to spend less, just go for a Nano clone. It really doesn’t matter at all. Trust me!

What Is The Price Of Arduino Nano Online In India?

If you are from India, you can buy the original Arduino Nano online at a low price of Rs.1449. But if you are OK to buy a cheap Arduino Nano clone, you can buy it at a low cost of Rs.200!

What Is The Price Of Arduino Nano Online In The US?

In the US, you can buy an original Arduino Nano at a cost of $22.00. But on the other hand, a cloned Arduino Nano will only cost you around $9! It’s as cheap as that!

What Is The Online Price Of Arduino Nano In The UK?

But if you are in the UK, you can buy the Nano board at just GBP20!

Have you looked at the new Arduino Portenta?

Conclusion

So there you have it. We just looked into the price of an Arduino Nano in 3 different regions. I also explained to you why its price changes so much when you want to buy it online. It is because of the clones that has reduced the price of these boards.

I hope you have got what you were looking for in this article. If you still have any more questions on it, ask me right away and I will answer you.

I hope you will have fun working with them. These are the best things you can work on to gain good knowledge. It is also one the best hobby that will keep you busy for some time! So enjoy with your new toys! Until next time, ciao! 🙂