Categories
Data Science

Data Visualization using Matplotlib – Part 1

Matplotlib is one of the most popular library for data visualization and that’s for a reason. It has so many features to offer and can be used without any external software except python and the matplotlib library.

In this article, you will learn how to use matplotlib to visualize data that will also enable you to better understand the data, extract information, and make more effective decisions.

Before going further into matplotlib, let’s talk about Data Visualization

What is Data Visualization?

“A picture is worth a thousand words.” We are all familiar with this expression. It especially applies when trying to explain the insights obtained from the analysis of increasingly large datasets. Data visualization plays an essential role in the representation of both small and large-scale data.

Data visualization is the graphical representation of information and data. Graphical representation(in the form of charts, graphs, and maps etc) allows us to better understand the relationship in the data, data visualization tools provide an accessible way to see and understand trends, outliers, and patterns in data

One of the key skills of a data scientist is the ability to tell a compelling story, visualizing data and findings in an approachable and stimulating way.

Prerequisites: – Python and Data Cleaning with Python Pandas

Exploring Datasets With Pandas

The dataset contains annual data on the flow of international immigrants as recorded by the countries of destination. The data presents both inflows and outflows according to the place of birth, citizenship or place of previous / next residence both for foreigners and nationals. The current version presents data pertaining to 45 countries.The dataset is available here.

Downloading and Preparing Data

import pandas as pd           #For reading the data

#Read the dataset skipping top 20 rows(irrelevant) and second last row
df_canada = pd.read_excel("./data/canada.xlsx",skiprows = range(20),skipfooter=2) df_canada.head() #View top 5 rows
#Check the dimensions of our canada dataset
df_canada.shape              # (195, 43) 
#Let gets insight of our dataset
df_canada.describe()

Cleaning the Dataset

Remove columns that are not informative to us for visualization(eg., type, area, reg)

df_canada.drop(["AREA","REG","DEV","Type","Coverage"],axis = 1, inplace = True)

#View columns of our dataset
df_canada.columns

Renaming Columns

df_canada.rename(columns={"OdName":"Country","AreaName":"Continent","RegName":"Region"},inplace = True)

#View columns of our dataset 
df_canada.columns

Check if column labels are string

For consistency, make sure that all column labels are of type string.

# let's examine the types of the column labels
all(isinstance(column, str) for column in df_canada.columns)
#False

df_canada.columns = list(map(str,df_canada.columns))
# let's examine the types of the column labels 
all(isinstance(column, str) for column in df_canada.columns)
#True

Set the country name as index – useful for quickly looking up countries using .loc method.

df_canada.set_index("Country",inplace=True)

Add total column

#Add a column Total in our dataset containing 
#total numbers of immigrants 
df_canada["Total"] = df_canada.sum(axis=1)

#View top 5 rows 
df_canada.head()

Visualizing Data using matplotlib

Now after we have cleaned the dataset, its time to draw some plots. Plotting data using Matplotlib is quite easy. Generally, while plotting they follow the same steps in each and every plot. Matplotlib has a module called pyplot which aids in plotting figure. The Jupyter notebook is used for running the plots. We import matplotlib.pyplot as plt for making it call the package module.

Installing matplotlib

Type !pip install matplotlib in the Jupyter Notebook or if it doesn’t work in cmd type conda install -c conda-forge matplotlib . This should work in most cases.

#import necessary modules
import matplotlib.pyplot as plt

# use the inline backend to generate the plots within the browser
%matplotlib inline

LINE PLOTS

What is Line Plot? When to use Line Plot?

A line chart or line plot is a type of plot which displays information as a series of data points called ‘markers’ connected by straight line segments. It is a basic type of chart common in many fields. Use line plot when you have a continuous data set. These are best suited for trend-based visualizations of data over a period of time.

Let’s Start with a Case Study

In 2010, Haiti suffered a catastrophic magnitude 7.0 earthquake. The quake caused widespread devastation and loss of life and aout three million people were affected by this natural disaster. As part of Canada’s humanitarian effort, the Government of Canada stepped up its effort in accepting refugees from Haiti. We can quickly visualize this effort using a Line plot:

#First, we will extract the data series for Haiti.
years = list(map(str, range(1980, 2014)))
haiti = df_canada.loc["Haiti",years ]

# passing in years 1980 - 2013 to exclude the 'total' column
haiti.head()

pandas automatically populated the x-axis with the index values (years), and the y-axis with the column values (population). However, notice how the years were not displayed because they are of type string. Therefore, let’s change the type of the index values to integer for plotting.

haiti.index = list(map(int,haiti.index))

Also, let’s label the x and y axis using plt.title()plt.ylabel(), and plt.xlabel() as follows:

haiti.plot(kind = "Line")     #Plotting the line
plt.title("Immigration of Haiti")
plt.xlabel("Years")
plt.ylabel("Number of immigrants")

plt.show() # need this line to show the updates made to the figure

We can clearly notice how number of immigrants from Haiti spiked up from 2010 as Canada stepped up its efforts to accept refugees from Haiti. Let’s annotate this spike in the plot by using the plt.text() method.

haiti.plot(kind = "Line")     #Plotting the line
plt.title("Immigration of Haiti")
plt.xlabel("Years")
plt.ylabel("Number of immigrants")

plt.text(2000,6000,"2010 Earthquake")
plt.show() # need this line to show the updates made to the figure

We can easily add more countries to line plot to make meaningful comparisons immigration from different countries.

Lets Compare the trend of top 5 countries that contributed the most to immigration to Canada.

#Sorting the dataset using sort_values() method
df_canada.sort_values(by = "Total",ascending  = False, inplace = True)

#Extract the data for top 5 countries 
years = list(map(str,range(1980,2014))) 
df_top = df_canada.head()

#Transpose the dataset 
df_top=df_top[years].transpose()
df_top.head()

Now create a line plot and visualize the data

df_top.plot(kind='line')            #Plotting line

plt.title('Immigration Trend of Top 5 Countries')
plt.ylabel('Number of Immigrants')
plt.xlabel('Years')

plt.show()

Final Words

Thank you for the read. I hope that you have enjoyed the article. If you like it, share it with your friends. Also, I have a quick task for you to see how much you have learned.
You can think of it as an assignment.

Task.
COMPARE THE TREND OF LAST 5 COUNTRIES THAT CONTRIBUTED THE MOST TO IMMIGRATION TO CANADA.

I’ll be happy to hear your feedback. If you have some questions, feel free to ask them. 😉

Categories
Data Science

What is Data Science? How to Become a Data Scientist?

Hey Guys, this article is all about Data Science. You will get to know what is data science, what you have to learn to be a data scientist, what skills do you need, the job opportunities, and how you can become one. It will help you decide whether the Data Science field is for you or not.

So, without wasting any time, let’s dive in.

What is Data Science?

Data Science, in simple words, is a study of data. You use various tools and technologies to generate useful insights and information from the raw data. Though, it is not as simple as it sounds. To become a data scientist, you need to know your way around mathematics, programming, statistics, and patience.

What does a Data Scientist Do?

A data scientist has to identify issues use data to solve them to help the company in decision making. They typically analyze, process, and model data to interpret the results.

Source: Springboard

As a data scientist, you have to gain insights into the data, and for that, you may have to build algorithms, do statistical analysis, data mining, and data visualization so that you will be able to create solutions to increase business performance.

As a data scientist you will be making programs that include creating various machine learning-based tools or processes within the company, such as suggestion engines or lead scoring systems. You should also be able to perform statistical analysis.

The Path to Data Science

There are various things that you need to learn to become a data scientist.

Programming Languages

First, you need to learn Programming Language to communicate with the computer.

Source: Assignment Expert

Python, R, and SQL are mainly used in Data Science. You can also use other languages for this, but these are widely used languages because they have a large number of libraries especially for this purpose. Like for python, there’s NumPy, Pandas, Matplotlib, Seaborn, SciPy, SkLearn, etc. These libraries make the job ten times easier. There’s a library for python for everything you need. You need graphs to visualize data? There’s matplotlib and seaborn. You need DataFrames, there’s Pandas. You need to perform vectorized operations on a big data set really fast? There’s NumPy for that.

The same is the case with R. With other languages, there are not many libraries and hence, Python and R are preferred over other languages.

We also have free python tutorials. Learn Python

Collecting Data

To do anything with data first you need to have data.

Data can be obtained through various sources. You will have to find them yourself but gathering and converting that raw data into useful data is something that you need to need to learn how to do. Sometimes, data is directly provided to you by the company/client you are working for, but most of the times you have to gather the data yourself.

There are some python libraries which can make the process easier. For example, suppose a website is providing public data in json format and you need that data to do work, now to use that data you first need to convert that data into the format that can be used for manipulation. To do that you need requests library. It helps you read and work with Json Data.

Source: OctoParse

Similarly, there are cases where the website isn’t just giving out the data for free and you have to fetch the data, this is the part where web scraping comes into play. For example, suppose you have to fetch articles from Wikipedia, now Wikipedia doesn’t just give data in JSON format or RSS feeds, so to read the data, you need libraries like Scrapy, Beautiful Soup, etc. Learning how to automate the browser can also be handy to fully automate the process of fetching and converting the data for future uses.

Data Analysis and Visualization

Once you have the data you can start analyzing the data. Convert the raw data into dataframes using Pandas library.

Source: Stanford University

Use libraries like Matplotlib and Seaborn to draw graphs. Make heatmaps, bar graphs, scatter plots. These graphs will help you understand the relationships in the data. What values are relevant, what values are irrelevant, what values can help related to the output you need. There are also tools like Power Bi by Microsoft and Tableau which can make the data visualization process easier.

You can check out these articles on NumPy and Pandas to get started with Data Science.

Python: Beginners Guide to NumPy Arrays

Introduction In this tutorial, you will learn the basics of NumPy Arrays from creating and working with NumPy Arrays to Indexing, Slicing, performing operations, joining and splitting of arrays. But first up we have a question that What is NumPy? NumPy is an open-source fundamental library for data science with Python. It stands for ‘Numerical […]

Python Pandas: Overview of DataFrames and Series

Introduction Hey Guys, this article will give you a quick introduction of pandas as to what is Pandas, why you should use Pandas, what can you do with Pandas, and the supported Pandas data types. It can help you make sure if Pandas is what you are looking for or you should learn pandas or […]

Machine Learning

Data Science doesn’t involve hardcore machine learning. That’s the job of a machine learning engineer. But you will be expected to know the basics and be perfect with algorithms. Having a great understanding of Neural Networks is, however, a plus point.

Source: DataCamp

You have the data and you have already analyzed it. Now you need to make models that can be used to make predictions or perform particular tasks based on the data. All this comes under the machine learning territory. In most cases, as a data scientist, you will be required to make models that can predict some values based on the previous data that you provided. For that, you need to learn how to identify the type of problem(what type of problem you are solving) and the working of various algorithms. There are numerous algorithms that can be used to solve a particular problem but you need to choose the best one and for that, you need to be 100% sure about the type of problem and the category to which it belongs.

There’s no particular algorithm that works for all types of problems. An algorithm that works well with one dataset may not work with other datasets even if they are of the same category. Now I can go on and on about Machine Learning and algorithms but the post is not about that. We will go into detail when we talk exclusively about machine learning.

Mathematics

Yes, you need mathematics to become a full-fledged data scientist. Some people may argue that you don’t need mathematics to be a Data Scientist. It’s halfway true. You don’t need mathematics at the start of your career but you will need it as you grow. Simply put, to just learn and apply things, you don’t need mathematics but to understand the data on a deeper level you need to know mathematics. It will help you understand the relationship between the variables, whether they are linear or not, whether they will affect the outcome or not.

You need to be good in Statistics and Multivariate Calculus & Linear Algebra.

Mathematics allows you to understand what is happening with your data once you apply an algorithm. It can help you understand the working of the algorithm better and make the algorithm selection process easier.

Career Opportunities in Data Science

The world is generating 2.5 quintillion bytes of data everyday! This simply means that the need of data scientists is only going to increase in future.

Data Science Trends

There’s a requirement of Data Scientist in almost every industry.

According to Glassdoor, a Data Scientist can make anywhere around $113,000/Year in US or ₹985K/Year in India on an average which is still one of the highest paying career paths.

Data Scientist isn’t the only job role in the data science industry. Here are some of the most popular data science job titles along with their average salary (According to Indeed and Glassdoor).

Final Thoughts

Data Science is not only the most lucrative career but also an interesting one to go after. As a data scientist, you will be able to experiment with data and machine learning techniques. It is really a fun job which also requires critical thinking skills and a business mind. Yes, it requires you to have certain skills like statistics, calculus, machine learning, etc, that may seem like a daunting task but once you perfect them, it is one of the best career paths you can take.

I guess this is at least enough to help you decide whether data science is for you or not.

Categories
Python

Statements, Comments, and Python Blocks

Hey guys, this is the fourth tutorial of the Python for Beginners Series. If you haven’t checked out our previous tutorials, you should go and check them for a better understanding. In this tutorial, we are going to discuss the statements and escape sequences. This tutorial is focused on statements so you will learn about statements in general and then comments, what are comments, how to write comments in Python, docstrings, escape sequences, and the python blocks.

So, without further ado, let’s start now.

Statements

Any instruction that you give to python to execute is a statement. You say

print("Hello World")

This is a print statement because you are printing some data, which in this case is a string.
Anything you write is a statement. You assign a value to a variable, that’s a declaration/assignment statement. Now some of you might have heard about if, else, for, while, etc… These all are statements.

Now statements can be of single-line or multi-line. Till now, you may have only used single line statements like print(“Hello World”) but there are multi-line statements as well.

Multi-Line Statements

You can extend a single statement to multiple lines. Try making a multi-line statement like this one.

a = "Coding
Ground"
print(a)

It will throw an error like

    a = "Coding
              ^
SyntaxError: EOL while scanning string literal

To extend the statement to multiple lines, all you have to do is put a backslash ( \ ) at the end of the statement. For example,

a = "Coding\
 Ground"
print(a)

And the output will be

Coding Ground

You can also put your statement under brackets for some data types and you won’t need to use backslash but it is not true for strings. For example

a = ['item1',
'item2']
print(a)

The output will be

['item1', 'item2']

It will work with tuples as well.

Now you might be wondering why should we even use it when we can just write it in one line. Well, it is done to make your code cleaner and increase its readability.

Comments

You must have noticed that in some code there is a text written beside it which describes what the code is for. That text is called a comment. There are two types of comments, single-line comments which are normal comments and multi-line comments which are also known as docstrings

Single Line Comments

These are written in a single line for example,

a = "Coding Ground" #Assigns value to variable a 

Single Line comments start with a hashtag. They are written to describe what the code is about, it’s working what the variable stores and so on. Comments are only for user understanding and are never executed with the code.

You can write as many comments as you would like and it’s a good practice to write comments so that others can understand your code without much hassle. Not only for other people, but it will also help you in reading your code after a long time.

Docstrings or Multi-Line Comments

There are times when you have to add a full documentation about a function or a class you create or sometimes, it’s just that you have to write multi-line comments, for that we use docstring.

Docstring is any text written under triple quotes like ”’ or “””. For example,

"""This is a 
docstring"""
'''This 
is 
also a
docstring'''

If you want to add a documentation of a function you can do this like

def codinground():
    """prints Coding Ground"""
    return ("Coding Ground")

Docstring for functions, classes, or modules is written right after their declaration before any other statement.

Now if you want to access the documentation of a function/class/module, all you have to do is call the attribute __doc__.

You can, in this case, do this by printing function.__doc__. For example,

print(codinground.__doc__)

It will print out whatever is written in the docstring of that function or module or class. It will be really helpful whether you are experienced or beginner in python.

The output in this case will be

prints Coding Ground

So, docstrings will help you write documentations in an efficient and a standard way for everyone to follow.

Now, this isn’t a norm but a personal preference that you should write docstrings when you are writing a function or a class or module and single-line comments when you are writing loops or variables.

You might be confused about functions, classes, or modules but don’t worry, we’ll be covering them in our future tutorials in this series. But if you are confused about variables, I recommend checking out our tutorial on Datatypes and Variables in Python

Coming over to our next topic which is Escape Sequences.

Escape Sequences

There are some characters in python which have a different meaning. These are called escape sequences and are written after the escape character backslash( \ ).

For example, if I told you to print out a string whose output is “Coding Ground”. How would you do it? simply using print(“”Coding Ground””) will throw a Syntax Error.

There are many such cases where we want our text to be under quotes or in a new line without actually writing it in another line etc., in these cases, we use escape sequences.

To print a text under quotes just put a backslash before the quotes.

print("\"Coding Ground\"")

The output of this will be “Coding Ground”. This is because the double quotes are written after the escape character backslash. If you try to put backslash without any other character, it will leave whitespace. The backslash defines that the character after after the escape character has a different meaning than the normal one.

TIP
If it’s just printing the text under double or single quotes you can just do it like this print(‘”Coding Ground”‘). What I have done is, I started the string with a single quote and wrote the word under double quote so python interpreter knows that ok, the main string is whatever text is under single quotes. So, it treats double quotes as an ordinary string character.
But this is not recommended

You can also use \n to make it the next line. I know this is not clear so let’s consider this example, You are making a menu for your program and you want to list multiple items in it.
Then you can do this by using the \n like

print("1. Item One\n2. Item Two\n3. Item Three")

The output of this will be

1. Item One
2. Item Two
3. Item Three

You can see that it is much more efficient than writing it in multiple lines.

Similarly, there are many such escape sequences, you can find a list of them below.

Escape SequenceMeaning
\newlineBackslash and newline ignored
\\Backslash (\)
\'Single quote (')
\"Double quote (")
\aASCII Bell (BEL)
\bASCII Backspace (BS)
\fASCII Formfeed (FF)
\nASCII Linefeed (LF)
\rASCII Carriage Return (CR)
\tASCII Horizontal Tab (TAB)
\vASCII Vertical Tab (VT)
\oooCharacter with octal value ooo
\xhhCharacter with hex value hh
List of Python supported Escape Sequences

These are all the escape sequences available in Python. Play with them and you will have a better understanding.

Python Blocks

In our previous example on docstring, we made a function and then wrote a docstring.
If you have noticed carefully then you must be wondering why we put a colon after the function definition and then we leave two spaces in the statement we wrote under that function?

This is because unlike other languages, which use curly braces {} to define a block, python uses colon and indentation which is usually two or four spaces.

According to Python’s Official Documentation,

block is a piece of Python program text that is executed as a unit. The following are blocks: a module, a function body, and a class definition. Each command typed interactively is a block.

All conditional statements, functions, classes and modules use a python block.

Let us look at a if statement to make it more clear.

 a = 10
 if a == 10:
    print("This code executes under if block")
print("This executes anyway")

Since the condition is true, which is a is equal to 10, if block will run and the output will be

This code executes under if block
This executes anyway

If we tweak the value of variable a, suppose 1, then the if condition will become false and if block will not run and the output will simply be

This executes anyway

So basically, a block is a piece of code that executes as a unit whenever called.

Hopefully it should have given you some basic insight on loops as well.

We will discuss python blocks in detail when we cover control flow statements.

This is it for this tutorial, hope you like it. Tell me what you think of this tutorial in the comments section below. Also, if you have any doubts, comment down below and I’ll help you.

References

Categories
Python

DataTypes and Variables

Hey guys, this is the third tutorial of the Python for Beginners Series. If you haven’t checked out our previous tutorials, first go and read them (especially Tokens in Python) because we have discussed keywords and identifiers in those tutorials that you should know before learning about variables.

What are variables?

Variable is a name that points to a location where your data is stored. In simple language, the variable holds your data. You can assign a value to a variable using = sign with the variable on left and value on the right.
Consider this example,

age = 10

In this example, age is a variable that points to the memory location where the data i.e., 10 is stored. Now whenever you want to use 10, you can also use variable age.

For example, if you do this

print(age)

The output will be 10

Also, it is important to know that the data inside the variable can be changed whenever you need. For example, you could store a value 20 in the same variable.

age = 20

Now the variable age holds value 20. The old value 10 is now replaced by the new value 20.

You can store all sorts of data in a variable like

name = "Coding Ground"

the variable name holds the string value Coding Ground.

It is important to remember that variables do not store values they are the pointers. They give reference to the memory location where the data is stored.

Before we dive any deeper, let us discuss the different type of data supported in python.

DataTypes

A datatype is simply the attribute of data that tells the interpreter what type of data it is going to process.

There are multiple data types supported in Python. Also, you can check the type of data using the type() function.

Python Data Types

Numeric Data

The numeric data type includes integers, floating-point numbers, and complex numbers.
They are used as int, float, and complex. Consider the examples below,

Integers

These are the whole numbers which also include negative numbers. For example, 0,1,2,3,150, -200 etc are all integers. They don’t have a fraction part.

a = 10
print(type(a))

It will return

<class 'int'>

Floating Point Numbers

Floating Point numbers have a decimal part in them.

b = 10.2
print(type(b))

The output will be

<class 'float'>

Complex Numbers

These numbers are in the form of a + bj. They are as <real part> + <imaginary part>j

c = -1 + 5j
print(type(c))

The Output will be

<class 'complex'>

Now you see class because the data is an object or instance of a class.
For example, 10 is an instance of int class.

If you don’t understand it then don’t panic, We will be discussing classes and objects in future lectures.

Sequence

Sequences include lists, tuples and strings.
Basically every data type in which slicing can be done falls under this category.

Lists

Python lists contains elements within square brackets [ ].

a = ["Coding Ground",1,1.2,True]
print(type(a))

The Output would be

<class 'list'>

In the above example, the variable a holds a list of 4 elements, each of different data type.

Now each element of list has an index value and it starts from 0.

For example, the index value of the element “Coding Ground” in the above code will be 0.

We will discuss indexing and slicing in future tutorials.

Tuples

Tuples contain elements in round brackets or parenthesis ( ).

An example of a tuple would be

a = ("Coding Ground",1,1.2,True)
print(type(a))

The output would be

<class 'tuple'>

Now you might be wondering, what is the difference between list and tuple?

The answer is that Lists are mutable while tuples are not. That means you can change the elements of a list without replacing the whole variable value but you can not do that with tuple. You can not perform operations on them.

String

Anything within single or double quotes in Python is a string.

a = "Coding Ground"
print(type(a))

The output would be

<class 'str'>

Boolean

Boolean on values can only be either True or False.

a = True
print(type(a))

The Output will be

<class 'bool'>

Remember that True is a keyword and T is capital.

Mapping

There are 2 types of Data Types that fall under this category. These are Dictionary and Sets.

Dictionary

Dictionary is an unordered collection of key-value pairs. It uses curly braces.
If you’ve ever worked with JSON data then you must be familiar with Dictionary.

a = {'name' : "Coding Ground"}
print(type(a))

The Output will be

<class 'dict'>

Dictionaries provide a little more readability as compared to lists and tuples. This is because of the key-value pair. And this is the reason they are used when we have to work with a large amount of data.

Sets

Like dictionaries, Sets are also unordered collections with curly braces. But the difference is that they contain only unique values.

a = {1,2,3,4,5,1,2,3}
print(a)
print(type(a))

The output would be

{1, 2, 3, 4, 5}
<class 'set'>

Here, you can clearly see that we got only unique values as the output.

We cannot filter data from sets as sets are unordered pair. We can however use the classic set functions like union, intersection etc on two set values.

Now that we have discussed the data types, lets continue where we left the variables.

Type Casting or Type Conversion

Now that you have learnt about the different data types and how to assign a value to a variable. It is time to learn how you can convert the data type of some data stored in a variable.

Consider this example,

a = 10
print(a)
print(type(a))
a = float(a)
print(a)
print(type(a))

The output will be

10
<class 'int'>
10.0
<class 'float'>

As you can see we have just converted an integer value to a float one. But remember that you can not do this with all data. You can only do this with data having similar data type.

Now this is all common sense, I don’t need to explain this. If you are thinking how about converting complex to int or float, it won’t work because complex got j in it which isn’t used in any int value or float value.

But you can convert string into a list.

a = "Coding Ground"
a = list(a)
print(a)
print(type(a))

The output will be

['C', 'o', 'd', 'i', 'n', 'g', ' ', 'G', 'r', 'o', 'u', 'n', 'd']
<class 'list'>

So, guys, this is it for today. I hope you like it. If you have any problems or if anything is left unclear then tell us in the comment section below.

Happy Coding!

References

You can read more about additional data types at official python documentation

Categories
Python

Tokens in Python

Hey guys, this is the second tutorial of the Python for Beginners Series. If you haven’t read the first one i.e, Getting Started with Python yet then I suggest checking that out first. Also, this is going to be mostly theoretical with almost no coding involved but is very important.
Do not skip this if you don’t know about tokens in Python.

The Python Character Set

It is the set of valid characters that python understands. Python uses the Unicode character set.
It includes numbers from numbers like
Digits: 0-9,
Letters: a-z, A-Z
Operators: +,-,/,*,//,**
Punctuators like : (colon), (), {}, []
Whitespaces like space, tabs, etc

What are tokens?

Tokens are building blocks of a language. They are the smallest individual unit of a program. There are five types of tokens in Python and we are going to discuss them one by one.

Types of Tokens

So the five types of tokens supported in Python are Keywords, Identifiers, Literals, Punctuators, and Operators. Coming over to the first one we have

Keywords

Keywords are the pre-defined set of words in a language that perform their specific function. You cannot assign a new value or task to them other than the pre-defined one.

You cannot use them as a variable, class, function, object or any other identifier.

For example: if, elif, while, True, False, None, break etc

These have their special task that you cannot change. For example break will only end the loop you cannot make it start the loop. (We’ll be covering loops in future lectures).

Identifiers

Now identifiers are the names that you can assign a value to. An identifier can be anything for example,

a = 10

Here, a is a valid identifier name. Any name you give your variable, function, or class is an identifier of that particular thing. Now there are certain rules that you have to follow to define a valid identifier name.

Rules for valid identifier name

  • A valid identifier name can have letters, digits, and underscore sign.
  • It can start with an alphabet or underscore but can never start with a digit.
  • It can never be a keyword name.
  • An identifier name can be of variable length.
  • The only special symbol that can be used in identifier name is underscore( _ ).

One more thing that you should remember that python is case sensitive i.e.,

a = 10
A = 5

These two hold two different values. a holds the value 10 and A holds the value 5.

Examples of valid identifier names include: a, _a, a12, etc
Examples of invalid identifier names include: 1a, $a, elif, print.
If you don’t understand why they are valid/invalid, read the rules again.

Literals

Literals are the fixed or constant values. They can either be string, numeric or boolean.

For example, anything within single or double quotes is classified as a string and is literal because it is a fixed value i.e, “Coding Ground” is literal because it is a string.

Another example is, 10. It is a simple number but is a fixed value. It is constant and it will remain constant. You can perform operations like addition or subtraction but the value of these two characters 1 and 0 put together in a correct order gives them a value equal to ten and that cannot be changed.

Boolean only consist of 2 values, True and False. Remember that “T” of True is capital. Python is case sensitive and if you write True with small “t” like true it will hold a different meaning. It will act as another variable.

It might seem a bit confusing for now but you’ll definitely understand it in our future lectures on boolean and other data types.

Punctuators or Separators

Punctuators, also known as separators give a structure to code. They are [mostly] used to define blocks in a program. We will be covering code blocks in control flow statements when we discuss how to apply conditions in Python.
Some examples of punctuators include
single quotes – ‘ ‘ , double quote – ” ” , parenthesis – ( ), brackets – [ ], Braces – { },
colon – ( : ) , comma ( , ), etc.
Punctuators and operators go hand in hand are used everywhere. For example,

name = "coding ground"

Here, an assignment operator ( = ) and punctuator, (” “) is used.

And now for the last type of token is Operators

Operators

Operators are the symbols which are used to perform operations between operands.

Unary Operators: Operators having single operand.
Eg. +8, -7, etc
Binary Operators: Operators working on 2 operands.
Eg. 2+2, 4-3, 8*9, etc
Similarly, there are Ternary Operators that work on 3 operands and so on.
These are just basics and not so important to know but the operators listed below are very important

  • Arithmetic operators ( +, -, /, * etc)
  • Assignment operators ( = )
  • Comparison operators ( >, <, >=, <=, ==, !=)
  • Logical operators ( and, or, not)
  • Identity operators ( is, is not)
  • Membership operators ( in, not in)
  • Bitwise operators ( &, |, ^ etc)

There’s so much about operators that it cannot be written here as it will be out of the scope of this topic. A new detailed article on tokens will be there very soon.

Reference to Punctuators:
https://www.ibm.com/support/knowledgecenter/ssw_ibm_i_74/rzarg/punctuators.htm
https://download.mikroe.com/documents/compilers/mikroc/pic/help/punctuators.htm

Categories
Python

Getting Started with Python

Hey Guys, if you ever wanted to learn python but you don’t have any past experience then continue reading. This is the first tutorial of Python for Beginners series. In this tutorial we will cover the introduction of python following with its uses and applications. You will install and set up python on your computer and write your first program in Python.

Introduction to Python

Python is an interpreted*, high-level, object-oriented, and open-source language developed by  Guido van Rossum and first released in 1991. According to a survey conducted by Stackoverflow, Python is the most sought after programming language by developers.

Compiler – It converts the source code into a machine-dependent code completely all at once.
Interpreter – Python runs and checks the code line by line. So if an error is detected at suppose line 2, then it won’t run all the way to the end of the script. Unlike Java, which compiles the whole code at once.

Why Python?

  1. Python is an interpreted language, not a compiled language. Due to this, python is easy to debug.
  2. Python is a cross-platform language, it can run on a variety of platforms.
  3. Python is open-source and free.
  4. Python is best for Artificial Intelligence and Machine Learning.

Setting Up

Installing Python is extremely easy, it’s the same as you install any other software. Just go over to the official Python Website and download the latest version or click here.

Remember to check “Add Python 3.X to PATH“. It adds PIP to environment path variables.

PIP is a Python Package installer that helps in installing libraries and modules. You will need it one day or another, so it’s better to install it now.

Selecting the Python IDE(Integrated Development Environment)

Different people suggest different IDE, it all comes down to the personal preferences.

In this series we will be using the default python idle because a large number of people use that and it is generally advised to start with that.

What I recommend is IDLE or Sublime Text. These are more than enough or you can try VS Code. Though there are many other good IDE’s but I believe an IDE should be lightweight and fast because oftentimes you have to use a web browser to search for the problems. So these 3 are the best in terms of speed and efficiency.

Out of these I recommend Sublime Text because it has a large number of color schemes which look fantastic while coding and some people prefer a darker theme which is really great in Sublime.

Working with IDLE

Writing Your First Program

After installation open up Python Shell and write this code.

print("Hello World")

print() function tells Python to print/echo whatever data is in the parenthesis.

In python, string values are written under double quotes. We will be learning more about that in the future lessons.

Congratulations, you wrote your first program in python!

As an assignment, Try printing your name in python.

Working with Files

Most beginners make this mistake of continue writing their whole program in Python Shell. You shouldn’t do it unless you want to perform a quick task.

Now in your Python Shell click on File and select New File

Now type the same code in the file and save it. The default extension of python files is .py.

After Saving the file press F5 or click on Run and select Run Module.

It should look something like this. If this is what you got then congrats, you successfully learned how to use python files.

Categories
Python SQL

Introduction to MySQL with Python

Introduction

Whether you are building a game or even a simple app with user interaction you need to store the data. There are many methods in Python which are used to store data but by far the most popular one is using MySQL Database. The reason is that it is really easy to use and you can use the SQL file anywhere with any language you like and it is more secure than the others.

What exactly is MySQL?

Databases store the data in tabular form. MySQL is one such Relational Database Management System(RDBMS) for SQL.

Prerequisites

One should have a basic knowledge of both Python and SQL for better understanding.

What’s in this tutorial?

In this tutorial, you will learn how to use MySQL Database with Python. We will make a simple school database consisting of data of students. We’ll store student’s information and retrieve it using Python. So by the end of this tutorial, you will be able to make a full python application that uses MySQL database to store and retrieve data using Python.

Setting Up

MySQL doesn’t come pre-installed with the default windows installation so we’ll need to set it up ourselves. As usual, we’ll be using the default IDLE for python for the sake of simplicity. You can use any IDE.

About MySQL server, you might find it a bit confusing to install and use it if you are a first timer.

So if you are an absolute beginner I’d recommend downloading the command line interface of SQL. Even though it is an older version, it can help you to understand better. You can download it from here.

After installing MySQL now it’s time to install the MySQL driver for Python.

For that we’ll be using python mysql connector.

To install mysql connector just open pip and type

pip install mysql-connector

With this you have everything installed correctly. Now let’s get started with the main thing.

Working With MySQL

Connecting Python with MySQL

Now that we have everything installed, we have to make a connection between your Python file and the MySQL database.

First import the module in your python file as

import mysql.connector

This will import the MySQL Driver in python file. Next up connect your file with server.

mycon = mysql.connector.connect(
host="localhost",
user="username",
passwd="password"
)

Where username and password are defined by you. Generally both of them are root.

Now that you’ve established the connection with the server, all you have to do is execute the database commands.

Creating Database

Let us create a database named school. Since my username and password is root, I’ll be using that.

import mysql.connector

#Establishing Connection with MySQL Server
conn = mysql.connector.connect(
    host = "localhost",
    user = "root",
    password = "root"
)
cursor = conn.cursor()
cursor.execute("CREATE DATABASE School")

Now open the MySQL Interface, and type show databases; and you will find the school database listed.

Creating Tables

Now that we have database set up, we have to create a table and configure it.

Creating a table is almost similar to the way you create a database. We will make a table named student and store his id, name, email id and username.

cursor.execute("USE SCHOOL")
cursor.execute("CREATE TABLE Student(id int primary key auto_increment,name varchar(20) not null,email varchar(30) unique)")

This will create a sample table Student in the School database.

Structure of Student Table

Here we have,

ID – which is kind of a serial number of the registered student. I’ve made it primary key because two students cannot have same serial numbers.

Name – It is the name of the student. Obviously it is a varchar and cannot be null.

Email – It is the email id of the student, it is unique because two students cannot have same email id and they cannot leave it empty.

Now if we ever want to pull specific user data then we can pull it using both id and email since they are unique and will fetch only one result.

Now that we have a table all we need is to make functions for registering and pulling user data.

Storing Data in Tables

First Up, we’ll make a register function

def register():
    name = input("Please Enter Your Name: ")
    query = "SELECT * from Student where email="

    #Checks if the email is already registered
    while True:
    	#Checks if @ is present in email
    	while True:
    		email = input("Enter your email: ")
    		if '@' not in email:
    			print("Invalid Email")
    		else:
    			break
    	tmp_query=query+'"'+email+'"'
    	cursor.execute(tmp_query)
    	result = cursor.fetchall()
    	if len(result) > 0:
    		print("email already registered. Please try again.")
    	else:
    		break
    cmd = "insert into Student(name,email) values (%s,%s)"
    data = (name,email)
    try:
        cursor.execute(cmd,data)
        conn.commit()
        print("Registration Successful")
    except:
        conn.rollback()
        print("Unable to register")

So the code is pretty much self explanatory if you are familiar with python but don’t worry if you are not. We will go through the code

So first we take the student’s name as an input in the name variable then we make a query and store it in the query variable. This is totally optional but it makes string formatting easy to understand.

Now what we did is we applied 2 conditions in the email input. First we check if @ is present in the email because you cannot create an email without @ sign and then we check if the email is already regitered or not.

To retrieve rows from the database we basically have 3 functions.

fetchone() – It fetches the first record that comes up in the query.

fetchmany(number_of_records) – It fetches the specified number of records

fetchall() – It fetches all the records available.

fetchall() and fetchmany() returns a list of tuple whereas fetchone() returns a single tuple. So remember which one you use as it might create error in indexing.

Now after retrieving the records, I stored them into a new variable result and then i checked if the number of records is greater than 0 means there is already one record with that email.

If email is already registered, I prompt user to enter email again as it is already registered using the while loop and breaks if there is no record with that email.

Now there are a lot of ways to check the existence of a particular record but this one just seems to work with me.

In the end, if everything works out fine, I insert the data into the database and commit the data. If by any chance an error occurs, we rollback to previous data.

So, I registered with my name and email as suyash@codinground.com

sample table

Quick Tip: While experimenting with the data you might mess up the auto-increment. To reset the auto-increment value run this query:
ALTER TABLE Student AUTO_INCREMENT = Value;

You can also run the alter table commands with python this way but you won’t need them very often. So we are not going to use each and every command here instead let us try to retrieve the user data.

Fetching Data From Tables

It will be similar to storing data since we are going to apply same conditions on email check.

def retrieve_info():
	query = "SELECT * from Student where email="
	while True:
		email = input("Enter your email: ")
		tmp_query=query+'"'+email+'"'
		cursor.execute(tmp_query)
		result = cursor.fetchone()
		if len(result) > 0:
			userid = result[0]
			name = result[1]
			print("User ID:",userid)
			print("Name:",name)
			print("Email:",email)
			break
		else:
			print("Email does not exist")

What we did here is we fetched the whole record belonging to that email. Since there were 3 columns in that record(id, name, and email), therefore I stored them into different variables and displayed. It would look like this.

Closing Connection

After completing the script it is important to close the connection with the database or the user could modify it.

Now closing is done in the same order as establishing the connection. First, we close the cursor and then we close the connection. It is done by using the close() function.

cursor.close()
conn.close()

Full Code

import mysql.connector

#Establishing Connection with MySQL Server
conn = mysql.connector.connect(
    host = "localhost",
    user = "root",
    password = "root"
)
cursor = conn.cursor()
try:
	cursor.execute("CREATE DATABASE SCHOOL")
	cursor.execute("USE SCHOOL")
	cursor.execute("CREATE TABLE Student(id int primary key auto_increment,name varchar(20) not null,email varchar(30) unique not null)")
except:
	cursor.execute("USE SCHOOL")

query = "SELECT * from Student where email="

#Registration Function    
def register():
    name = input("Please Enter Your Name: ")

    #Checks if the email is already registered
    while True:
    	#Checks if @ is present in email
    	while True:
    		email = input("Enter your email: ")
    		if '@' not in email:
    			print("Invalid Email")
    		else:
    			break
    	tmp_query=query+'"'+email+'"'
    	cursor.execute(tmp_query)
    	result = cursor.fetchall()
    	if len(result) > 0:
    		print("email already registered. Please try again.")
    	else:
    		break
    cmd = "insert into Student(name,email) values (%s,%s)"
    data = (name,email)
    try:
        cursor.execute(cmd,data)
        conn.commit()
        print("Registration Successful")
    except:
        conn.rollback()
        print("Unable to register")

def retrieve_info():
	query = "SELECT * from Student where email="
	while True:
		email = input("Enter your email: ")
		tmp_query=query+'"'+email+'"'
		cursor.execute(tmp_query)
		result = cursor.fetchone()
		if len(result) > 0:
			userid = result[0]
			name = result[1]
			print("User ID:",userid)
			print("Name:",name)
			print("Email:",email)
			break
		else:
			print("Email does not exist")
while True:
    getorsave = input("1. Register User \n2.Get User Info\n")
    if getorsave == "1":
        register()
        break
    elif getorsave == "2":
        retrieve_info()
        break
    else:
        print("Invalid Input")

#Closing Connection with Database
cursor.close()
conn.close()

I’d like to end this one here. I could literally go on writing on this for hours but this should be enough to get you started.

Now with this you can even build a secure login and registration program. I’ll be showing that in future tutorials. If you have any doubts then don’t hesitate and hit the comment section.

Happy Coding!

Categories
Python Artificial Intelligence

Python: The Complete Guide To Speech Recognition

Speech Recognition is a hit in the market. It’s a flashy technology that is used mainly in voice assistants like Apple’s Siri, Amazon’s Alexa, Microsoft’s Cortana, Google’s Allo, etc. It can massively increase the user interaction in your project and will make it look amazing. Writing a program that uses speech recognition is far easier than you think. We will be covering everything from installing to implementation of speech recognition.
Though this program will also work with Python 2, we recommend using python 3.

There’s a lot that you can do with this package but I’ll try to keep it short and cover only the most important things needed to make a simple program or to implement vocal recognition in your own program.
NOTE: If you are looking just for the code then the final code is in the Conclusion section.
You can view the code for Working with microphone and Working with Audio Files directly. But if you want to know about the working of the code then you should read the article.

Introduction to Speech Recognition

Exactly What is speech recognition? In simple words, it is a technology that takes voice as an input and converts it into computer understandable language.

Setting Up

There are many modules that can be used for speech recognition like google cloud speech, apiai, SpeechRecognition, watson-developer-cloud, etc., but we will be using Speech Recognition Module for this tutorial because it is easy to use since you don’t have to code scripts for accessing audio devices also, it comes pre-packaged with many well-known API’s so you don’t have to signup for any kind of service which you may have to while using any other module. And, it gets the job done pretty well.

Requirements

  • Python 3.3+
  • Speech Recognition
  • *PyAudio 0.2.11
  • *PocketSphinx (offline use)
  • FLAC encoder (required only if the system is not x86-based Windows/Linux/OS X)

We will be using SpeechRecognition and PyAudio Module.
*PyAudio: This module is only required if you want to take the user’s voice as an input and not use pre-recorded audio files.
*PocketSphinx: Only use PocketSphinx if you have to use your program offline. I don’t personally recommend using PocketSphinx because of low accuracy.

The Speech Recognition Module

The Speech Recognition engine has support for various APIs. The most common API is Google Speech Recognition because of its high accuracy. In this tutorial though, we will be making a program using both Google Speech Recognition and CMU Sphinx so that you will have a basic idea as to how offline version works as well. However, if you want to use any other API, its pretty easy to switch, you just have to change the recognizer method(we will discuss it later in this tutorial)

Installation

I’m assuming you have python 3 properly installed. Open command prompt and type

­­
pip install speechrecognition
­pip install pyaudio
pip install pocketsphinx
­

NOTE: PyAudio is not available for python versions greater than 3.6.
If you are using python 3.7 or greater then download PyAudio wheel from here.

After downloading you can install wheel file as:

­
pip install C:/some-dir/some-file.whl
­

here some-dir and some-file.whl is the directory and the file name of that wheel respectively.
After installing, its always better to check the installation, open python terminal, and type

>>import speech_recognition as sr
>>sr.__version__
'3.6.0'

If it returns ‘3.6.0’ or higher then you are good to go (3.6.0 is the version that I was using because some of the libraries were not compatible with 3.7+ that I needed in my other project)

Working with Speech Recognition

After installing all the packages we are finally ready to start writing our first voice-based program. We will first be discussing the working of the speech recognition package and then we will start coding the program. You can skip to the code directly but I recommend reading the working as well because that will give you the idea about the options available that you can experiment with.

How Does it Work?

Speech Recognition has an instance named recognizer and as the name suggests it recognizes the speech(whether from an audio file or microphone). You can create a recognizer instance as follows:

import speech_recognition as sr
r = sr.Recognizer()

Note that we imported the Speech Recognition package as speech_recognition whereas we installed it as SpeechRecognition. This is a common mistake that many users make while importing this package.

Now Each Recognizer instance has eight methods by which it can recognize speech those are:

Out of these if you want to make a trigger word for your program so that it only starts and stops listening speech at a particular word(like Hey Siri or Ok Google) then you can go for Snowboy Hotword Detection.
Now with speech recognition, you can do two things:

  1. Take voice as an input from the user or
  2. Use a pre-recorded audio file

Now for most of the people option two would be pretty useless but we will be discussing both of them in this tutorial. You can skip to the desired part of the article from the sidebar.

Using Microphone

The most awaited and most fun section finally. Writing the above part was kind of boring but it was a complete guide afterall so i had to do it.

Now for working with microphones, you need PyAudio as I mentioned above.
After installing everything, you just need to type in this code.

import speech_recognition as sr
r = sr.Recognizer()

with sr.Microphone() as source:
	print("start")
	audio = r.listen(source)

	try:
		data = r.recognize_google(audio)
		print(data)
	except:
		print("Please try again")

Note: If you are using microphones other than the default one then please refer to configuring microphone settings at the bottom.

Now what we did here is we made the microphone as a source using the Microphone() method. I print start so that I would know when to start speaking. Though a symbol of mic appears in the taskbar when the program is using microphones, I did it just out of convenience.

Then we declared new variable audio that records the input. Till now the audio is only recorded through the microphone and not converted into text.

Now here comes the recognize_google() method. The recognize google method sends the audio data to the google web speech server and retrieves response.

Please note that recognize_google() uses google web speech api which is free but it is provided for testing purposes only. So I advice you not to use it for real world project.

Now there might be a time when the audio recorded just couldn’t be converted to text because of noise or other problems. If that happens then you will encounter a traceback error like this.

Traceback (most recent call last):
speech_recognition.UnknownValueError

To prevent this from happening we put a try and except block. This checks if there is some data in the audio variable that can be converted into text. If there is some data then it completes the try block and skips the except block. If there is no data or there is some problem in parsing the data then it runs the except block.

Also, you can raise multiple exception blocks for different errors but the detailed working of try and except blocks is beyond the scope of this tutorial, so we are going to skip it.

If everything is clear to this point then changing methods is simple like if you want to use CMU Sphinx instead of google then all you have to do is replace the recognize_google() with the recognize_sphinx() method. The code would be

import speech_recognition as sr
r = sr.Recognizer()

with sr.Microphone() as source:
	print("start")
	audio = r.listen(source)

	try:
		data = r.recognize_sphinx(audio)
		print(data)
	except:
		print("Please try again")

The main reason why I recommend google web speech instead of CMU sphinx is because of the accuracy

For the same sentence “test speech recognition” both outcomes are different and it is clear the google does it better.

Using Audio Files

Working with audio files is quite similar to working with microphones. The only difference is that instead of taking voice input from the users all you have to do is pass an audio file as a source.

We will be using the sample audio that you can download from here

After downloading, place the audio file in the same folder as your program file. After that, all you have to do is instead of sr.Microphone() use sr.AudioFile(‘filename.extension’) and change the r.listen() method to r.record() method.
That’s it, that’s all there is to it. You can check out the code below

import speech_recognition as sr
r = sr.Recognizer()

with sr.AudioFile('english.wav') as source:
	audio = r.record(source)
	try:
		data = r.recognize_google(audio)
		print(data)
	except:
		print("Please try again")

Conclusion

Congratulations on making it this far! This has been a long journey( at least for me haha :D)
Below is the quick recap of what we did so far( cause who’s got the time to read the freakin thing again and again)

If PyAudio doesn’t work properly, read the installation part again

The Speech Recognition has an instance named recognizer and as the name suggests it recognizes the speech(whether from an audio file or microphone). Depending on what method you use, it process audio data accordingly. There are both online and offline methods. In online methods like recognize_google() it sends data to Google Web Speech Server whereas in recognize_sphinx() method it process data within your device.

In order to take user’s voice as an input, you need PyAudio installed. Below is the complete code for processing audio data with microphone. Note that it uses sr.Microphone() and r.listen() methods

import speech_recognition as sr
r = sr.Recognizer()

with sr.Microphone() as source:
        print("start")
        r.adjust_for_ambient_noise(source, duration=0.5)­
	audio = r.listen(source)
	try:
		data = r.recognize_google(audio)
		print(data)
	except:
		print("Please try again")

Working with Audio files is similar to working with Microphones. Place the audio file that you want to process in the same directory as the program file.Below is the complete code for processing audio file. Note that it uses sr.AudioFile() and r.record() methods. Suppose you have a file name ‘english.wav’ then the code would be

import speech_recognition as sr
r = sr.Recognizer()
 
with sr.AudioFile('english.wav') as source:
    audio = r.record(source)
    try:
        data = r.recognize_google(audio)
        print(data)
    except:
        print("Please try again")

I tried to keep it as short and informative as possible but if I missed anything please tell me in the comments section below also if you have any doubts you can comment them down as well. I will try my best to answer you!

Miscellaneous

Configuring Microphone Settings

Note: If you are going to use your system’s default microphones then you can skip this section as there is no need to configure any microphone settings. If you want to use the microphone other than the default one then you have to select it manually.To get a list of microphones available type:

>>sr.Microphone.list_microphone_names() 
['Microsoft Sound Mapper - Input', 
'Microphone (2- Realtek Audio)',
 'Microsoft Sound Mapper - Output', 
'Speakers / Headphones (2- Realt', 'Primary Sound Capture Driver',
 'Microphone (2- Realtek Audio)', 
'Primary Sound Driver',
 'Speakers / Headphones (2- Realtek Audio)',
 'Realtek ASIO', 'Speakers / Headphones (2- Realtek Audio)',
 'Microphone (2- Realtek Audio)',
 'Stereo Mix (Realtek HD Audio Stereo input)',
 'Microphone (Realtek HD Audio Mic input)',
 'Speakers (Realtek HD Audio output)']

Note that the list of available microphones may vary. Now that you have a list of microphones all you have to do is select the index number of microphone. For example if you want to use Microsoft Sound Mapper which is at index number zero then all you have to do is

import speech_recognition as sr
r = sr.Recognizer()

with sr.Microphone(device_index=0) as source:
	print("start")
	audio = r.listen(source)

Reducing Noise Levels

Though you can not remove the noise completely from the background, there is adjust_for_ambient_noise() which can help you lower the noise levels in audio. For Example:

with sr.Microphone() as source: #You can also use this with audio files
	r.adjust_for_ambient_noise()
	print("start")
	audio = r.listen(source)

adjust_for_ambient_noise() reads the audio file for one second and adjusts the recognizer for the noise of that level. You can reduce the time it takes to read the audio file but it is recommended to let it read file for at least one second. But if one second seems like too much time then you can lower it by using the duration keyword. Example:

­
r.adjust_for_ambient_noise(source, duration=0.5)
­

In the above example, I reduced the duration to 0.5 seconds. You can increase or decrease according to your needs. The more time you give it the better it does its work. But if you are reducing the time limit then keep it at least 0.5 seconds (the SpeechRecognition documentation recommends that)

Working with Multiple Languages

If you are using Google Web Speech then you don’t need to do anything else as it already works for almost all languages. But if you are using CMU Sphinx and need any other language pack installed then you can install addition language packs from here.
All instructions for installation are mentioned on that page as well.

Writing to an Audio File

If you want to record and store user’s voice for further use(for building neural networks or maybe some other purposes) then this option is very useful for you. And this step is really easy. All you have to do is add the write command after using the r.listen() method. For example:

import speech_recognition as sr
r = sr.Recognizer()

with sr.Microphone() as source:
	print("start")
	audio = r.listen(source)

with open("microphone-results.wav","wb") as f:
	f.write(audio.get_wav_data())

This will create a file named microphone-results.wav in the same directory as the program file. With some string formatting, you can save different audio files with different names but we will cover string formatting in some other articles.

So this sums it up. Thank you for reading it till here. This article took a lot of time and energy so if you like it please tell me in the comments below. I’ll be posting more awesome articles so in order to get notified sign up for our newsletter below.

Have a nice day!

Categories
Python

Armstrong Numbers

 What are Armstrong Numbers?

The numbers in which the sum of the digits raised to the power of the number of digits is equal to the number itself are called Armstrong numbers. All the programs on Armstrong numbers that I’ve seen to date only work on the specific number of digits. Like they won’t work for an indefinite number of digits. So my approach is to write a program that works on any input. However, you can customize it according to your needs.

This Code is Written in Python 3!

A positive integer is called an Armstrong number of order n if

abcd….. = an + bn + c+ dn……. 

In the case of an Armstrong number of 3 digits, the sum of cubes of each digit is equal to the number itself.

There are four 3 digits Armstrong numbers 153, 370, 371, 407.

For example:  153 = 13 + 53 + 33

Similarly, 370 = 33 + 73 + 03

To Check if a number is Armstrong or not, we’ll first take input from user as

num = input("Enter a number: ")

*Note that we took input as string.

Now that we have input stored in variable num, we’ll create another variable to store the sum of numbers. Let’s name it sum. So,

sum = 0

Now if we modify our original variable num, we won’t have any real input from the user to check the output. So we will create another variable and store the value of  ‘num’ in it. We will perform operations on this temp variable.

temp = int(num)

Now we will initialize a while loop while loop which will work till the number becomes 0.

while temp > 0:

Now we’ll extract each digit in the digit variable as

digit = temp % 10

After extracting each digit we’ll raise it to power of number of digits and then adding it in sum variable.

sum += digit ** len(num)

*len() only works with string values.

After taking the sum, we’ll remove the last digit from the number.

temp = temp//10

All this will continue till the number becomes 0, after it becomes 0, while loop will break and the final result will be stored in ‘sum’.

We’ll use this sum to verify the user input using the if statement. If it returns True, we’ll print “Number is Armstrong”, if not, we will print “Number is not Armstrong”

Complete Code