Found 10784 Articles for Python

Contingency Table in Python

Naveen Singh
Updated on 30-Dec-2019 10:22:38

1K+ Views

A contingency table is a table showing the distribution of one variable in rows and another variable in columns. It is used to study the correlation between the two variables. It is a multiway table which describes a dataset in which each observation belongs to one category for each of several variables. Also It is basically a tally of counts between two or more categorical variables. Contingency tables are also called crosstabs or two-way tables, used in statistics to summarize the relationship between several categorical variables.The contingency coefficient is a coefficient of association which tells whether two variables or datasets ... Read More

Adding two Python lists elements

Naveen Singh
Updated on 30-Dec-2019 10:14:57

1K+ Views

Lists can be added in python resulting in creation of a new list containing elements from both the lists. There are various approaches to add two lists and they are described below. But in all these cases the lists must be of same length.Using Append()Using append() we can add the elements of one list to another.Example Live DemoList1 = [7, 5.7, 21, 18, 8/3] List2 = [9, 15, 6.2, 1/3, 11] # printing original lists print ("list1 : " + str(List1)) print ("list2 : " + str(List2)) newList = [] for n in range(0, len(List1)):    newList.append(List1[n] + List2[n]) print(newList) ... Read More

Python-program-to-convert-pos-to-sop

Naveen Singh
Updated on 24-Dec-2019 05:55:35

170 Views

In this article, we will learn about the solution to the problem statement given below.Problem statement − We are given pos form we need to convert it into its equivalent sop formThe conversion can be done by first counting the number of alphabets in the pos form and then calculating all the max and the minterms.Now let’s observe the concept in the implementation below−Example# Python code to convert standard POS form # to standard SOP form # to calculate number of variables def count_no_alphabets(POS):    i = 0    no_var = 0    # total no. of alphabets before will be ... Read More

Python program to print all Prime numbers in an Interval

Naveen Singh
Updated on 24-Dec-2019 05:46:13

360 Views

In this article, we will learn about the solution to the problem statement given below.Problem statement − We are given an interval we need to compute all the prime numbers in a given rangeHere we will be discussing a brute-force approach to get the solution i.e. the basic definition of a prime number. Prime numbers are the number which has 1 and itself as a factor and rests all the numbers are not its factors.Each time the condition of a prime number is evaluated to be true computation is performed.Now let’s observe the concept in the implementation below−Example Live Demostart = 1 ... Read More

Python program to interchange first and last elements in a list

Naveen Singh
Updated on 11-Jul-2020 12:01:53

475 Views

In this article, we will learn about the solution to the problem statement given below.Problem statement − We are given a list, we need to swap the last element with the first element.There are 4 approaches to solve the problem as discussed below−Approach 1 − The brute-force approachExample Live Demodef swapLast(List):    size = len(List)    # Swap operation    temp = List[0]    List[0] = List[size - 1]    List[size - 1] = temp    return List # Driver code List = ['t', 'u', 't', 'o', 'r', 'i', 'a', 'l'] print(swapLast(List))Output['t', 'u', 't', 'o', 'r', 'i', 'a', 'l']Approach 2 − The ... Read More

Python program to insert an element into sorted list

Naveen Singh
Updated on 24-Dec-2019 05:30:25

973 Views

In this article, we will learn about the solution to the problem statement given below.Problem statement − We are given a list, we need to insert an element in a list without changing sorted orderThere are two approaches as discussed below−Approach 1: The brute-force methodExample Live Demodef insert(list_, n):    # search    for i in range(len(list_)):       if list_[i] > n:          index = i          break    # Insertion    list_ = list_[:i] + [n] + list_[i:]    return list_ # Driver function list_ = ['t', 'u', 't', 'o', 'r'] n = ... Read More

Python program to get all subsets of a given size of a set

Naveen Singh
Updated on 24-Dec-2019 05:26:57

642 Views

In this article, we will learn about the solution to the problem statement given below.Problem statement − We are given a set, we need to list all the subsets of size nWe have three approaches to solve the problem −Using itertools.combinations() methodExample Live Demo# itertools module import itertools def findsubsets(s, n):    return list(itertools.combinations(s, n)) #main s = {1, 2, 3, 4, 5} n = 4 print(findsubsets(s, n))Output[(1, 2, 3, 4), (1, 2, 3, 5), (1, 2, 4, 5), (1, 3, 4, 5), (2, 3, 4, 5)]Using map() and combination() methodExample# itertools module from itertools import combinations def findsubsets(s, n):    return ... Read More

Python Program to find whether a no is power of two

Naveen Singh
Updated on 24-Dec-2019 05:18:26

2K+ Views

In this article, we will learn about the solution to the problem statement given below.Problem statement − We are given a number, we need to check that the number is a power of two or not.We can solve this using two approaches as discussed below.Approach 1: Taking the log of the given number on base 2 to get the powerExample Live Demo# power of 2 def find(n):    if (n == 0):       return False    while (n != 1):       if (n % 2 != 0):          return False       n = ... Read More

Python program to find the sum of all items in a dictionary

Naveen Singh
Updated on 11-Jul-2020 11:56:02

583 Views

In this article, we will learn about the solution to the problem statement given below.Problem statement − We are given a dictionary, and we need to print the 3 highest value in a dictionary.Three approaches to the problem statement are given below:Approach 1 − Calculating sum from the dictionary iterableExample Live Demo# sum function def Sum(myDict):    sum_ = 0    for i in myDict:       sum_ = sum_ + myDict[i]    return sum_ # Driver Function dict = {'T': 1, 'U':2, 'T':3, 'O':4, 'R':5} print("Sum of dictionary values :", Sum(dict))OutputSum of dictionary values : 14Approach 2 − Calculating the ... Read More

Event scheduler in Python

Naveen Singh
Updated on 23-Dec-2019 10:35:38

2K+ Views

Python gives us a generic scheduler to run tasks at specific times. We will use a module called schedule. In this module we use the every function to get the desired schedules. Below is the features available with the every function..SynatxSchedule.every(n).[timeframe] Here n is the time interval. Timeframe can be – seconds, hours, days or even name of the Weekdays like – Sunday , Monday etc.ExampleIn the below example we will see fetch the price of bitcon in every few seconds by using the schedule module. We also use the api provided by coindesk. For that purpose we will use ... Read More

Advertisements