Many times the numbers with three or more digits need to be represented suitably using comma. This is a requirement mainly in the accounting industry as well as in the finance domain. In this article we'll see how Python program can be used to insert a comma at a suitable place. We are aiming to insert comma as a thousand separator.Format FunctionThe format function in python can be used with below settings to achieve this requirement.(f"{num:, d}") : is the format specifier D is the thousand separatorExample - Integers Live Demoprint(f'{1445:, d}') print(f'{140045:, d}')OutputRunning the above code gives us the following ... Read More
Permutation of a number of objects is the representation of how the updates can be present in different sequences. But sometimes we may have two objects in a series of given objects which are identical. In that case two sequences will become equal. In this article will see how to represent only the unique sequences from a given list of objects.The module itertools have a method called permutations which help us achieve this. To get the unique permutations we take help of the set method which stores only the distinct elements. But before that we get the elements in the ... Read More
Prime numbers play a central role in many e it applications like cryptography. So it is a necessity to check for prime numbers using Python programs in various applications. A prime number is a number which doesn't have any factors other than one and itself. Below will see programs that can find out if a given number is prime or not.ApproachWe take the following approach to decide whether a number is prime or not.Check at the beginning is positive or not. As only positive numbers can be prime numbers.We divide the number with all the numbers in the range of ... Read More
Mysql is one of the most widely used open source Dbs. Python provides ways to connect to this DB and use the DB to store and retrive the data from it.Install pymysqlDepending on the python environment you are using, pymysql package can be installed using one of the following methods.# From python console pip install pymysql #Using Anaconda conda install -c anaconda pymysql # Add modules using any python IDE pymysqlConnecting to MySqlNow we can connect to the Mysql environment using the following code. After connecting we are finding out the version of the DB.Exampleimport pymysql # Open database connection ... Read More
Sometimes when dealing with the binary representation of numbers we may be needed to find out how many continuous 1’s are present in the number. This article shows two ways how we can find out that.Using Split and MapThe split function in python can be used to split the given string into multiple strings. We split it by zeros and the map function is used to find the maximum length among the splits generated.Example Live Demodata = '11110000111110000011111010101010101011111111' def Max_len_cons_1(data): print ("Maximum Number of consecutive one's: ", max(map(len, data.split('0'))) ) Max_len_cons_1(data)OutputRunning the above code gives us the following result −Maximum Number ... Read More
Python programming language uses both * and ** on different contexts. In this article we will see how these two are used and what the respective useful scenarios.As an Infix OperatorWhen * is used as infix operator, it basically gives the mathematical product of numbers. IN the below example we take integers. Floats and complex numbers to multiply and get the results.Example Live Demo# Integers x = 20 y = 10 z = x * y print(z, "") # Floats x1 = 2.5 y1 = 5.1 z1 = x1 * y1 print(z1, "") # Complex Numbers x2 = 4 ... Read More
Statistics is fundamental to learn ml and AI. As Python is the language of choice for these Technologies, we will see how to write Python programs which incorporate statistical analysis. In this article we will see how to create graphs and charts using various Python modules. This variety of charts help us in analyzing the data quickly and deriving insides are conclusions graphically.Data PreparationWe take the data set containing the data about various seeds. This data set is available at kaggle in the link shown in the program below. It has eight columns which will be used to cerate various ... Read More
Use of escape character (\) would become very essential when we want to insert a comma or any other character between the values of a filed. It can be understood with the help of an example. Suppose we want to import the data from a text file named A.txt, having the following data, into a MySQL table −id, Name, Country, Salary 105, Chum, Marsh, USA, 11000 106, Danny, Harrison, AUS, 12000Here, we can see that the filed name has two values first name, last name separated by a comma. Now, the ... Read More
Excel is the most famous spreadsheet and almost every computer user is comfortable with the idea of managing the data through spreadsheets. Eventually some python program has to interact with excel. Many python libraries are available to create, read and write into excel files. We will see the examples of few such important libraries below.Using openpyxlThis library can read/write Excel 2010 xlsx/xlsm/xltx/xltm files. In the below example we create a excel worksheet, assign data to its cells and finally save the file to a desired location. The module has many in-built methods which can be used for this. We see ... Read More
Back-slash(\) is the by default escape character for the MySQL and when we use it in the text file then we do not need to mention it in the query while importing the data from text file to table. But if we use any other character as escape character then it must be mentioned by using ESCAPED BY option in the query while importing the text file into a table. It can be understood with the help of the following example −Suppose we are using star symbol (‘* ‘) as the escape character in a text file as follows −id, ... Read More
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP