Found 10476 Articles for Python

Programs for printing pyramid patterns in Python

Pradeep Elance
Updated on 04-Feb-2020 07:35:31

2K+ Views

Taking advantage of the for loop and range function in python, we can draw a variety of for pyramid structures. The key to the approach is designing the appropriate for loop which will leave both vertical and horizontal space for the position of the symbol we choose for drawing the pyramid structure.Pattern -1We draw a right angle based pattern.Example Live Demodef pyramid(p):    for m in range(0, p):       for n in range(0, m+1):          print("* ", end="")       print("\r") p = 10 pyramid(p)OutputRunning the above code gives us the following result −* * ... Read More

Program to make Indian Flag in Python

Pradeep Elance
Updated on 04-Feb-2020 07:22:08

3K+ Views

Python’s libraries to draw graphs has very extensive features which can not only give us charts but also give us flexibility to draw other diagrams like flags. In that sense those modules have an artistic touch. In this article we will see how to draw the Indian flag using the libraries numpy and matplotlib.AppraochWe create three rectangles of same width and draw them with appropriate colours and borders.Use pyplot function to draw the circle of the Ashok Chakra at the center of the middle rectangle.Use numpy and matplotlib to draw the 24 lines inside the Ashok Chakra.In all the above ... Read More

Program to create grade calculator in Python

Pradeep Elance
Updated on 04-Feb-2020 07:15:38

2K+ Views

In Academics it is a common requirement to find the grades of students after an assessment. In this article we will create a Python program which will assign the grades based on the grading criteria. Will call it A grade calculator.Grading criteriaBelow is the grading criteria we have chosen for the program.score >= 90 : "O" score >= 80 : "A+" score >= 70 : "A" score >= 60 : "B+" score >= 50 : "B" score >= 40 : "C"Program ApproachInitialize variables and array to hold the student details including the marks obtained by individual subjects.Define a function to ... Read More

Print number with commas as 1000 separators in Python

Pradeep Elance
Updated on 04-Feb-2020 07:05:34

488 Views

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

Print first n distinct permutations of string using itertools in Python

Pradeep Elance
Updated on 04-Feb-2020 07:01:25

387 Views

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 or not in Python

Pradeep Elance
Updated on 04-Feb-2020 06:52:09

805 Views

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

MySqldb Connection in Python

Pradeep Elance
Updated on 04-Feb-2020 06:44:47

302 Views

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

Maximum length of consecutive 1’s in a binary string in Python using Map function

Pradeep Elance
Updated on 04-Feb-2020 06:41:11

451 Views

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

Usage of Asterisks in Python

Pradeep Elance
Updated on 04-Feb-2020 06:29:16

269 Views

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

Statistical Thinking in Python

Pradeep Elance
Updated on 04-Feb-2020 06:22:13

374 Views

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

Advertisements