Search my blog:

Browse the full archives:

How to Send Emails With Python

Python is a highly versatile programming language. It consists of many standard libraries that enable many useful functionalities, sending emails is just one of them.

Although it may sound difficult, sending an email with a Python script is quite easy. It is time-saving in the long run and has lots of real-world applications. Whether you need to send bulk emails, send confirmation or verification codes to users, or even automate regular emailing tasks, all that is possible with Python scripts and we’ll be covering them in this article.

We will be using the SMTP (Simple Mail Transfer Protocol) to send emails.

Read More

How to Clean Text in Python for Machine Learning Models

Textual data plays a huge role in machine learning. They are also one of the key types of data in addition to numerical data values.

Text is used for various applications in machine learning such as language translation, sentiment analysis, content detection, and even developing chatbots. All these require massive amounts of textual data in order to produce successful results.

As much as textual data is rich in useful content, most of them are highly disorganized, unstructured, and often contain noise. This is why we are required to clean texts before utilizing them to train our machine learning models.

This tutorial will teach you how to clean texth in Python for use in machine learning models.

Read More

How to Write C++ Comments

Commenting is an integral part of programming languages.

Although it does not add to the functionality of code, it is critical to the understanding of it. Both programmers and non-programmers need comments to make sense of the code at their own level. A comment in a programming language can be defined as a line written in a common language that explains what the code does or the purpose behind the code. It is a note that is inserted directly into the source code. Comments are written using proper syntax so that the compilers and interpreters skip it because they are not the part of code that functions. The purpose of writing comments in code is manifold. It is helpful not just for non-programmers in understanding the code but also for programmers long after they are done writing the code. Therefore, it is critical to include comments in the code as you write it and not postpone it to be done at the end of coding as you are likely to forget the thought process later.

Table of Contents

You can skip to a specific section of this C++ tutorial using the table of contents below:

Types of C++ Comments

There are two main comment types in C++: single-line comments and multi-line comments. There is another kind of single-line comment called an inline comment. These are explained below along with examples.

1. Single-line Comments

Single-line comments are the ones that last up to one line. These types of comments start with a double forward-slash (//).

Syntax:

// this is a single-line comment in C++

Here’s a simple ‘Hello World!’ program to illustrate single-line comments.

// Cpp single-line comment example
#include <iostream>
int main()
{
 	// Hello World program in cpp  
	cout << Hello World! << endl;
	return 0;
}

Output:

Hello World!

2. Multi-line Comments

The comments that go on for multiple lines are called multi-line comments. There are two ways of writing multi-line comments. One way of doing it is by starting each line of comment by a double forward-slash (//).

// This is an example of multi-line
// comment using single-line
// commenting format.

However, this format can become tedious when the comment is longer. For a comment that goes on for a paragraph or multiple paragraphs, a different syntax is used. A multi-line comment starts with /* and ends with a */.

/*
This is how a multi-line comment is made
in C++.  All the lines wrapped in a pair of
forwarding slash and a star are ignored. This
line is ignored by the compiler and so is
this line.
*/

A single-line comment can be nested inside a multi-line comment. However, a multi-line comment cannot be nested inside another multi-line comment.

/* The comment starts here /* and */ this is outside the comment */
// The comment in above line ends at first */ and not the second */

3. Inline Comments

This is type of single-line comment. When a line of code is followed by a comment within same line, it is called inline comment. Below is the example to make it clearer:

include <iostream>
int main()
{
	cout << Welcome to lessons in Cpp. << endl; 	// this is an inline comment
	return 0;
}

Output:

Welcome to lessons in Cpp.

Documentation Comments

Before each file or function begins, block comments are used. These comments contain information pertaining to the file that follows. They are created as follows:

  • The first line is indented to align with code below the comment.
  • Include details of the purpose of class or function that follows.
  • Leave one line blank between description and the list of tags.
/*
	CS-11 Asn 2, checks.cpp
	Purpose: Calculate the total of 10 checks
 
	@author Nick McCullum
	@version 2.3 03/05/2020
*/

A properly documented source code has such block comments that contain information on files and functions. Below is an example:

/**
	CS-11 Asn 19
	spherecircle.cpp
	Purpose: Calculates the area of a circle and the volume
	of a sphere.
	@author Nick McCullum
	@version 1.1 5/12/2020
*/
 
#include <iostream>
#include <cmath>
using namespace std;
const double PI = 3.14159;
 
/*
	Returns the area of a circle with the given radius.
	@param radius The radius of the circle.
	@return The area of the circle.
*/
double area(double radius);
/*
	Returns the volume of a sphere with the given radius.
	@param radius The radius of the circle.
	@return The volume of the sphere.
*/
double volume(double radius);
// Controls the program.
int main(void) {
	double radius_of_circlesphere, area_of_circle, volume_of_sphere;
 
	cout << "Enter the radius of a circle\n"
        	<< "and a sphere (in centimetres): ";
	cin >> radius_of_ circlesphere;
 
	area_of_circle = area(radius_of_ circlesphere);
	volume_of_sphere = volume(radius_of_ circlesphere);
 
	cout << "Radius = " << radius_of_ circlesphere << " centimetres\n"
        	<< "Area of circle = " << area_of_circle
        	<< " square inches\n"
        	<< "Volume of sphere = " << volume_of_sphere
        	<< " cubic centimetres\n";
 
	return 0;
}
 
// Returns the area of a circle with the given radius.
double area(double radius) {
	return (PI * pow(radius, 2));
}
// Returns the volume of a sphere with the given radius.
double volume(double radius) {
	return ((4.0 / 3.0) * PI * pow(radius, 3));
}

Why Write Comments in C++?

It might seem to you that comments are not that important. However, commenting is a significant aspect of programming. Now that you have learned the syntax of single and multi-line comments, let me tell you more about what makes C++ comments critical to good code.

1. Easier to Understand your Code at Any Point in Time

For a programmer, it becomes a little difficult to remember what the code does, especially when the codes are long and time to finish it is less.

At times when a programmer is in rush, he might give variables a temporary name which he has to correct later. In such cases, if you do not leave a comment, it can be a disaster because you will not remember later that what is what.

Some programmers may think of completing the code first and writing comments once that is done. This almost never works because by the time code is complete, they will have lost track of it and they are tired. The best way to go about commenting is by doing it as you write the code.

2. Others can Understand the Code without Reaching out to Developer

Most of the programs are quite long of about 20,000 lines and a single developer cannot complete the task. Multiple developers come together to complete the task. Different developers work on different parts of code and need to refer to each other’s work. In such a scenario, comments are important for them to make sense of the code that they have not developed. Using comments, they can skim through the code and understand it. Also, it helps in a smooth transition if a project has to be handed over to another person completely.

Best Commenting Practices

I wanted to conclude this tutorial by going through some widely-held best practices related to placing useful comments within a codebase.

1. Keep Your Comments Readable

The purpose of comments is simply to explain the purpose of code to programmers and non-programmers alike. Therefore, it is important that C++ comments are readable. Avoid using jargon in comments and keep the language simple and readable.

2. Whitespace Between Code and Comments is Important

This applies to both inline and separate comments. For inline comments, leave two to three blank spaces between the code and comment. For separate comment lines of blocks, leave a single line blank to separate code from comment. This is important to keep the code easy to read when skimming through it.

3. Comment as You Go

As explained earlier, make comments as you go on writing the code. Do not postpone comment writing until the code is done to avoid confusion and blunders that can result in buggy code.

4. Do No Explain the Obvious

Do not write comments for when code is clear. Some code lines and self-explanatory and comments should be omitted in such places, as shown below.

cout<< Hello World!;  // Prints Hello World!

In the code above, it is obvious that it prints Hello World! to console. Mentioning the same in the comment was unnecessary.

5. Do Not Use Comments to Explain the Problematic Code

Such comments are also called smelly comments. These are the comments that give a longer explanation of code when, in fact, code needs fixing. No amount of commenting can make it right when the problem is with code and will only make the code bulky.

Final Thoughts

A well-documented code is one with informative and concise comments. All languages allow commenting in some form. Without comments, a person reading the code might get confused. Therefore, good programmers are good at writing comments too.

Read More

Python vs. JavaScript: The 11 Major Differences

Software development arena is moving at a fast pace and has reached an advanced stage today. Among other programming languages, Python and JavaScript have been making significant contributions since decades. They are considered as the most popular and widely used languages today.

Stack Overflow which is a question-answering platform for software professionals and developers surveys more than 100,000 software developers annually to find out the programming trends, job opportunities, and challenges. In a survey in 2018, they found out that Python was the most popular programming language among the developer community and 25.1% developers wanted to learn it and increase their programming skills.

While, JavaScript ranked second after Python with a 19% popularity rate. In the past few years, it is reported by many studies and software developers that Python and JavaScript were the most rapidly growing languages. These languages are particularly prevalent among young developers and tech startups due the best features they are providing especially in data science.

Being a developer, it is very difficult to decide which one to leave and the other one to choose, because both provide the best of programming features. Hence, this brings up the topic: “Python Vs JavaScript: The Major 11 Differences”. In this article, we will briefly discuss the 10 differences between Python and JavaScript so that it may help you in deciding which one to choose for learning, practicing, and earning.

Python and JavaScript have many features in common, however, there are some significant differences. Before discussing their differences, let’s start their brief introduction so that you may easily understand them.

Read More

How to Run Python Scripts

Python is one of the most popular programming languages in the world. It is extremely powerful, has easy syntax and lots of cool libraries that you can use for various tasks.

If you’re starting to learn Python, then knowing how to run your Python scripts is an important skill to learn.

If you don’t know how to run your scripts, you can’t verify whether they actually work. It is the only way to really test your programs.

In this step-by-step guide, you’ll learn everything you need to know about the basics of running Python scripts. We’ll discuss how the techniques used for running scripts differs based on the kind of environment, your operating system, and even your skills as a developer.

Read More

Python 3 Installation & Setup Tutorial

Python is one of the easiest to learn and the most versatile programming languages available.

It is highly popular among data science enthusiasts and researchers.

This tutorial will teach you how to install Python 3 on your computer, including guidance for several different operating systems.

Read More

How to Write Anonymous Functions in JavaScript

There are a number of different ways to write functions in JavaScript.

One of the types of JavaScript functions is called anonymous functions.

As its name suggests, an anonymous function is a function that is declared without a name. Said differently, an anonymous function does not have an identifier. Anonymous functions are usually arguments passed to higher-order functions.

In this tutorial, I will teach you how to write an anonymous function in JavaScript. You will also learn the many uses of anonymous functions and the advantages and disadvantages of using this function type in JavaScript.

Read More

How to Use Python Pointers

Have you ever worked in programming languages like C++ or C?

If yes, you would have definitely come across the term pointers.

Every variable has a corresponding memory location and every memory location has a corresponding address defined for it. Pointers store the address of other variables.

Surprisingly, pointers don't really exist in Python. If that is the case, what am I writing about here?

Everything is an object in Python. In this article, we will look at the object model of Python and see how we can fake pointers in Python.

Read More

Python Pass - Doing Nothing, and Why That's Useful

The Python pass keyword allows you to skip over a block of Python code.

Said differently, it allows you to create a block (whether in a function, loop, or something else) and have that block run with no functionality.

This tutorial will teach you how to use the Python pass statement. Specifically, we will discuss how the Python pass statement is useful during the development stage and how it differs from different flow control keywords in Python.

Read More

Python Speech Recognition - a Step-by-Step Guide

Have you used Shazam, the app that identifies music that is playing around you?

If yes, how often have you wondered about the technology that shapes this application?

How about products like Google Home or Amazon Alexa or your digital assistant Siri?

Many modern IoT products use speech recognition. This both adds creative functionality to the product and improves its accessibility features.

Python supports speech recognition and is compatible with many open-source speech recognition packages.

In this tutorial, I will teach you how to write Python speech recognition applications use an existing speech recognition package available on PyPI. We will also build a simple Guess the Word game using Python speech recognition.

Read More

How to Use JavaScript onclick Events

When writing code in JavaScript, you will often want to execute code when the user interacts with the webpage.

For example, you want to do something when a user clicks a button. You might also want something to happen when a user clicks an image.

How can such functionality be implemented in JavaScript?

You can use the onclick() event, which is a very popular event in JavaScript. It allows you to execute code when a user clicks an element on a webpage (such as a button).

In this article, I will teach you how to use JavaScript onclick events to make your webpages more interactive.

Read More

How to Write a Bubble Sort Algorithm in JavaScript

The bubble sort algorithm is a simple comparison-based sorting algorithm.

In this algorithm, each element in an array is compared to the adjacent element in the array and then swapped, if required, until all the elements of the array are properly sorted.

In this article, you will learn the basics of bubble sort algorithm and how to write a bubble sort algorithm in JavaScript. I will also discuss the time complexity of the bubble sort algorithm.

Read More

How to Create a Beautiful Soup Web Scraper in Python

The internet is brimming with data about any topic you can imagine.

Much of this data exists on websites on public domains that you can access through a web browser. To harness this data, you’ll need to learn the valuable skill of Python web scraping.

This tutorial will teach you about the basics of web scraping by using a practical example. More specifically, we’ll scrape job postings from the PythonJobs website using Python’s BeautifulSoup library.

Read More

Creating a Python GUI using Tkinter

Tkinter is the native Python GUI framework that comes bundled with the standard Python distribution.

There are numerous other Python GUI frameworks. However, Tkinter is the only one that comes bundled by default.

Tkinter has some advantages over other Python GUI frameworks. It is stable and offers cross-platform support. This enables developers to quickly develop python applications using Tkinter that will work on Windows, macOS, and Linux.

Another benefit is that the visual elements created by Tkinter are rendered using the operating system’s native elements. This ensures that the application is rendered as though it belongs to the platform where it is running.

Tkinter is not without its flaws. Python GUIs built using Tkinter appear outdated in comparison to other more modern GUIs. If you’re looking to build attractive applications with a modern look, then Tkinter may not be the best option for you.

On the other hand, Tkinter is lightweight and simple to use. It requires no installation and is less of a headache to run as compared to other GUI frameworks. These properties make Tkinter a solid option for when a robust, cross-platform supporting application is required without worrying about modern aesthetics.

Because of its low aesthetic appeal and easy of use, Tkinter is often used as a learning tool. In this tutorial, you will learn how to build a Python GUI using the Tkinter library.

Read More

Object-Oriented Programming (OOP) in Python

Python inherently an object-oriented programming language, which makes it easy to work with classes and objects in Python.

Object-oriented programming is an example of a programming paradigms, which are a way to distinguish between different programming languages. Languages can belong to multiple paradigms.

In this tutorial, you will learn about object-oriented programming (OOP) in Python. We’ll discuss the broad characteristics of object-oriented programming as well as several examples of how you can implement this paradigm in practice.

Read More

The Python `turtle` Library - A Step-by-Step Tutorial

The Python programming language, like most languages, is purely text-based. This can make it quite boring for young learners.

Visual tools can be important educational resources. More specifically, the turtle library is a great tool to help elementary and middle school students to master the language.

The turtle library comes with a collection of commands that you can use to create artwork and games.

If you’re an elementary or a middle school student with the desire to learn Python (or an educator who is serving that audience), you’ve come to the right place.

This tutorial will help you start your Python programming journey by creating shapes and images using the Python turtle library.

Read More

The 10 Best JavaScript Libraries and Frameworks for 2020 (and Beyond!)

There has never been a better time to be a JavaScript developer.

Not only is there a multitude of frameworks, libraries, and tools available to developers, it's evident that the popularity of web applications is going to grow for a long time.

Add to this the fact that many JavaScript frameworks allow you to build native apps, and it is easy to see why it has become the go-to language for so many developers.

We truly are spoiled for choice with the number of sophisticated frameworks out there, which can make it somewhat difficult to choose one for your project.

In this article, I'll cover the 10 most important JavaScript libraries (with the best listed at the end) to help you make a more informed decision.

Read More

How to Write Java Variables

Java is a robust language and is used to build many programs.

Variables are the building blocks of any language. Variables in Java have a huge role to play in making it a widely accepted language.

What is a variable? A variable can be defined as a name given to a certain memory location, which is a basic unit in a program. All languages use variables to store data and the value of a variable can be changed during the execution of a program. Any operation done on the variable will affect the memory location.

It’s critical that you understand how variables work if you’re learning to become a Java programmer. In this tutorial, you will learn how to write Java variables.

Read More

The 10 Best Java Frameworks for 2020 (and Beyond)

Java is one of the most robust programming languages that has ever existed.

Combined with frameworks, Java can provide excellent solutions for domains like IT, cloud computing, banking, or even e-commerce.

In this article, I will discuss Java frameworks and why it is beneficial to use them. Also, I will briefly discuss the 10 best Java frameworks for building applications in 2020 (and beyond).

Read More

SyntaxError in Python: How to Handle Invalid Syntax in Python

Syntax errors are the single most common error encountered in programming.

Regardless of the language used, programming experience, or the amount of coffee consumed, all programmers have encountered syntax errors many times.

Syntax problems manifest themselves in Python through the SyntaxError exception. In this tutorial, I will teach you how to handle SyntaxError in Python, including numerous strategies for handling invalid syntax in Python.

Read More

How To Write Java Comments The Right Way

Commenting is an integral part of any programming language. Tt is a guide that helps in better understanding the functionality of a program. Good programmers tend to comment as they go while writing the code.

Most of the time, when you write code, you will need to go back and make changes later. Without Java comments, it’s easy to get lost.

This is why I specifically make sure to make comments as I write code. Not only does it help me keep track of the code as it grows more complicated, but it also makes the code more understandable for anyone that needs to reference the code at a later date.

Comments in Java are the statements in the Java code that are not executed when the program is run. Think of them as markers that guide the reader.

Said differently, comments provide information about the code without changing the code’s functionality. Some programmers can also use it to disable certain portions of code during development.

In this tutorial, I will how to write Java comments and what are some of the best ways to use them.

Read More

Python vs. C++: A Comprehensive Comparison

The debate between Python and C++ has been hot in the programming world for as long as anyone can remember.

Both languages have valuable use cases. When it comes to choosing between them, one must be aware of the syntax, usage and overall programming approach of each language.

There are many factors that will weigh on the Python vs. C++ debate. In this article, I will discuss both languages and highlight the key differences between Python and C++ that should make it easy for you to pick the right language for your project.

Read More

How To Write Java Loops

Loops are an important part of any computer application.

Similarly, learning the syntax for writing loops is a crucial step in learning a new programming language. Java is no exception.

In this tutorial, you’ll learn about the different types of loops in Java and how to write loops in the Java programming language.

Read More

How To Handle KeyError Exceptions in Python

KeyError exceptions are a common error encountered by junior Python developers.

Unfortunately, it’s not always clear how to handle these exceptions.

You’ll be glad to hear that handling KeyError messages is likely easier than you think. In this article, I will teach you how to handle KeyError exceptions in Python.

Read More

np.append() - How To Use NumPy Append in Python

The NumPy programming library is considered to be a best-of-breed solution for numerical computing in Python.

NumPy stands out for its array data structure. NumPy arrays are excellent for handling ordered data. Moreover, they allow you to easily perform operations on every element of th array - which would require a loop if you were using a normal Python list.

One of the core capabilities available to NumPy arrays is the append method. In this tutorial, I will explain how to use the NumPy append method to add data to a NumPy array.

Read More

np.reshape - How to Manipulate Arrays Using NumPy's Reshape Function

NumPy is the most popular Python library for numerical and scientific computing.

NumPy’s most important capability is the ability to use NumPy arrays, which is its built-in data structure for dealing with ordered data sets.

The np.reshape function is an import function that allows you to give a NumPy array a new shape without changing the data it contains. In this tutorial, I will teach you how to use the NumPy reshape function to manipulate arrays in NumPy.

Read More

How To Use the Python Square Root Function

There are many situations in which you will want to find the square root of a function in a Python application.

Fortunately, Python includes some very powerful functionality for calculating square roots.

In this article, I will teach you how to use the Python square root function. I will also show you how to calculate square roots without this function, and how to calculate the square root of every element in an outside data structure.

Read More

np.arange() - How To Use the NumPy arange() Method

NumPy is a Python library widely considered to be the most important library for numerical computing.

Within NumPy, the most important data structure is an array type called np.array().

NumPy contains a number of methods that are useful for creating boilerplate arrays that are useful in particular circumstances. One of these methods is np.arange().

In this article, I will teach you everything you need to know about the np.arange() method. By the end of this article, you will know np.arange()’s characteristics, uses, and properties, and will feel comfortable integrating this method into your Python applications.

Read More

How To Write C++ Variables

Variables are an important part of any software application.

However, every programming language has different variable characteristics. Some languages have special syntax.

There is also the question of typing - some languages are statically typed (which means you must specify a variable’s data type before value assignment) while others are dynamically typed (which means a variable can assume any value, with no specification required).

There is much to understand about a language’s variables before starting to write code. With that in mind, this article will teach you how to write C++ variables properly, which will save you time and grief later on in your programming journey.

Read More

The Best PHP Frameworks For 2020 (and Beyond)

PHP has come a long way in the last 25 years. What used to be a relatively under-powered scripting language has now become a powerful tool to build complex applications.

It’s no wonder, then, that PHP is one of the most popular choices for web applications around the world and that a whole slew of PHP frameworks have emerged over the past decade and a half.

In this article, I’ll go over the 10 best PHP frameworks for 2020 and some of their defining features. Use this as a guide to selecting the right framework for your project.

Read More

How To Write Python Classes: Code, Examples, & More

Python is an object-oriented programming (OOP) language.

OOP is methodology used by programmers that enables a system or program to be defined as a set of objects, which can be manipulated and controlled in a piece-by-piece manner.

It is important for you to understand why object oriented languages are necessary in programming. When creating complex code, object-oriented programming allows you to re-use code, making it easy to maintain and scale. It also puts emphasis on objects, unlike procedure oriented programming languages that stress the importance of functions.

In this article, I am going to teach you how to write Python classes - which are special constructs that programmers use to organize related items in an application.

Read More

How To Write Python Comments: The 2020 Guide

For programmers, it is important that your code is easily understood by outside users.

While professionals might not find it difficult to understand, for someone who is trying to learn Python, including comments in the code can be very helpful.

I have seen many bad practices within Python comments over the years.

In this article, I will explain the best way to write Python comments for developers in 2020 (and beyond!).

Read More

How To Write React JSX Comments

Writing detailed comments is an important component of writing software.

However, every language and framework has its own syntax for writing comments. This can make it difficult for developers to know how to write comments when they’re learning a new language or library.

The JavaScript library React has some notoriously bizarre syntax for writing comments. In this article, I’m going to explain how to write comments in React JSX.

This article starts by explaining what React and JSX are; if you’re already familiar with React ecosystem and only want to learn about how to write React JSX comments, click here to skip to the relevant section of this article.

Read More

The Essays of Warren Buffett: Quotes From the Best Investor of All Time

Warren Buffett is unquestionably one of the best investors to have ever lived.

Moreover, he has been very willing to share his writing with the world through his annual shareholder letters.

I have personally learned a great deal from reading Buffett’s writing over time. In this article, I will share a collection of Warren Buffett’s most insightful writings about the art and science of business and investing.

Read More

How to Profile Python Code: Accurately Measure the Performance of your Software

As software engineers, we are paid to create high-quality code that performs.

Performance can be measured in a number of ways: the problems it solves, its uptime, or a slew of quantitative metrics.

Speed is unquestionably an important component of software performance. In this article, I’ll show you how to measure the speed of Python code using a process called “profiling”, step-by-step.

Read More

Coronavirus Tests, Cases, and Deaths by US State

The coronavirus pandemic continues to worsen, both in terms of the actual health impact of patients and the broader effect it has had on our economy and psychology. The purpose of this post is to list some real statistics on the coronavirus - withoutthe sensationalism that we have seen from some major news outlets.

Read More