Found 10476 Articles for Python

Minimum Path Sum in Python

Arnab Chakraborty
Updated on 04-May-2020 06:07:35

767 Views

Suppose we have a m x n matrix filled with non-negative integers, find a path from top left corner to bottom right corner which minimizes the sum of all numbers along its path. Movements can only be either down or right at any point in time. So for example, if the matrix is like below131151421The output will be 7, the path will be 1, 3, 1, 1, 1, this will minimize the sumLet us see the steps −a := number of rows, b := number of columnsi := a – 1, j := b – 1while j >= 0matrix[a, j] ... Read More

Calculating Wind Chill Factor(WCF) or Wind Chill Index(WCI) in Python Program

Hafeezul Kareem
Updated on 24-Apr-2020 12:40:14

497 Views

In this tutorial, we are going to learn how to calculate Wind Chill Index in Python. We have the formula to calculate the WCI and it's straightforward. We are going to use the following formula to calculate the WCI.Twc(WCI) = 13.12 + 0.6215Ta – 11.37v+0.16 + 0.3965Tav+0.16whereTwc = Wind Chill Index (Based on Celsius temperature scale)Ta = Air Temperature (in degree Celsius)v = Wind Speed (in miles per hour)We are going to use the math module function wherever we need them. Using the math module function decreases the execution time of a program.Follow the below steps to complete the program.Import the math moduleInitialize the ... Read More

Different Methods to find Prime Number in Python Program

Hafeezul Kareem
Updated on 24-Apr-2020 12:39:11

1K+ Views

In this tutorial, we are going to explore different methods to find whether a given number is valid or not. Let's start without further due.Method-1It's a general method to find prime numbers.If the number is less than or equal to one, return False.If the number is divisible by any number, then the function will return False.After the loop, return True.Example Live Demo# checking for prime def is_prime(n):    if n

Lambda expression in Python Program to rearrange positive and negative numbers

Hafeezul Kareem
Updated on 24-Apr-2020 12:34:21

774 Views

In this tutorial, we are going to write an anonymous function using lambda to rearrange positive and negative number in a list. We need the pick the negative numbers and then positive numbers from a list to create a new one.AlgorithmLet's see how to solve the problem step by step.1. Initialize a list with negative and positive numbers. 2. Write a lambda expression the takes a list as an argument.    2.1. Iterate over the list and get negative numbers    2.2. Same for positive numbers    2.3. Combine both using concatination operator. 3. Return the resultant list.Note - Use ... Read More

Log functions in Python Program

Hafeezul Kareem
Updated on 24-Apr-2020 12:32:25

475 Views

In this tutorial, we are going to learn about the logarithmic functions from math module. We have four variants of logarithmic functions. Pythons' provides all of them in the math module. Let's learn about them one by one.math.log(number, [Base])The math.log(number, [Base]) method is used to compute the logarithm of any Base. If we didn't specify any base value, then it will take e as default base.Note − You will get ValueError if you pass a negative number to the method.ExampleLet's see some examples. Live Demo# importing math module import math # logarithm with base 3 print(math.log(15, 7))OutputIf you run the above ... Read More

Private Variables in Python Program

Hafeezul Kareem
Updated on 24-Apr-2020 12:30:05

4K+ Views

In this tutorial, we are going to learn about the private variables in Python classes.Python doesn't have the concept called private variables. But, most of the Python developers follow a naming convention to tell that a variable is not public and it's private.We have to start a variable name with a double underscore to represent it as a private variable (not really). Example:- one, two, etc.., .As we already said the variables whose names start with a double underscore are not private. We can still access. Let's see how to create private type variables and then we will see how ... Read More

Python Dictionary Comprehension

Hafeezul Kareem
Updated on 24-Apr-2020 12:26:13

404 Views

In this tutorial, we are going to learn how to use dictionary comprehensions in Python. If you are already familiar with list comprehension, then it won't take much time to learn dictionary comprehensions.We need the key: value pairs to create a dictionary. How to get these key-value pairs using dictionary comprehension? See the general statement of dictionary comprehension.{key: value for ___ in iterable}We need to fill in the above statement to complete a dictionary comprehension. There are many ways to fill it. Let's see some of the most common ways.Let's see how to generate numbers as keys and their squares ... Read More

Program to print its script name as output in Python

Hafeezul Kareem
Updated on 24-Apr-2020 12:20:22

1K+ Views

In this tutorial, we are going to write a program that prints the name of the Python script file. We can find the script name using the sys module.The sys module will store all the command line arguments of python command in the sys.argv list. The first element in the list is the script name. We can extract it from that list. Python makes it easy.Let's see the steps involved in the program.Import the sys module.Now, print the first element of the sys.argv list.That's it. You got the script name.ExampleLet's see it practically. Live Demo# importing the sys module import sys ... Read More

Difference between Python and Ruby

Kiran Kumar Panigrahi
Updated on 25-Aug-2022 13:09:18

378 Views

Python is a high-level, general-purpose programming language. It is used in website development, machine learning, and creative software technology. Guido Van Rossam created Python in the Netherlands in 1989. Python became public in 1991. New programmers are typically advised to learn Python. Ruby is an interpreted, open-source, object-oriented language. It was developed by Yukihiro Matsumoto in the year 1995. Ruby is an object-oriented language, hence everything is an object. OOP gives developer projects a modular structure. Read through this article to find out more about Python and Ruby and how they are different from each other. What is ... Read More

try and except in Python Program

Hafeezul Kareem
Updated on 24-Apr-2020 12:17:13

364 Views

In this tutorial, we are going to learn about the try and except of Python. Python has a concept called error and exception handling.The keywords try and except are used in the error and exception handling.Basically, we will find two types of errors in Python. They are −Syntax errors - Python gives these types of error when it doesn't understand a line of code in the program.Exception errors - Errors which are detected during the runtime of the program. Ex:- ZeroDivisionError, ValueError, etc.., We can't stop syntax errors. But, we can intimate if the program runs into an exception error ... Read More

Advertisements