Found 10784 Articles for Python

Calendar Functions in Python - ( calendar(), month(), isleap()?)

Naveen Singh
Updated on 02-Jan-2020 06:53:15

425 Views

We can perform the calendar operations using the calendar module in Python. Here, we are going to learn about the different methods of calendar class instance.calendar.calendar(year)The calendar class instance returns the calendar of the year. Let's see one example.Example Live Demo# importing the calendar module import calendar # initializing year year = 2019 # printing the calendar print(calendar.calendar(year))OutputIf you run the above code, you will get the following results.calendar.firstweekday()The method calendar.firstweekday() returns the first weekday in the week i.e.., MONDAY.Example Live Demo# importing the calendar import calendar # getting firstweekday of the year print(calendar.firstweekday())OutputIf you run the above program, you will get ... Read More

Calendar Functions in Python -(monthrange(), prcal(), weekday()?)

Naveen Singh
Updated on 02-Jan-2020 06:47:01

488 Views

We are going to explore different methods of calendar module in this tutorial. Let's see one by one.calendar.monthrange(year, month)The method calendar.monthrange(year, month) returns starting weekday number and number of days of the given month. It returns two values in a tuple. Let's see one example.Example Live Demo# importing the calendar module import calendar # initializing year and month year = 2019 month = 1 # getting the tuple of weekday and no. of days weekday, no_of_days = calendar.monthrange(year, month) print(f'Weekday number: {weekday}') print(f'No. of days: {no_of_days}')OutputIf you run the above code, you will get the following results.Weekday number: 1 No. of ... Read More

casefold() string in Python Program

Naveen Singh
Updated on 02-Jan-2020 06:31:14

75 Views

In this tutorial, we are going to discuss the string method str.casefold(). It doesn't take any arguments. The return value of the method is a string that is suitable for the caseless comparisons.What are caseless comparisons? For example, the german lower case letter ß is equivalent to ss. The str.casefold() method returns the ß as ss. It converts all the letters to lower case.Example Live Demo# initialising the string string = "TUTORIALSPOINT" # printing the casefold() version of the string print(string.casefold())OutputIf run the above program, you will get the following result.tutorialspointLet's see the example where caseless comparison works. If you directly compare ... Read More

Change data type of given numpy array in Python

Naveen Singh
Updated on 02-Jan-2020 06:23:52

11K+ Views

We have a method called astype(data_type) to change the data type of a numpy array. If we have a numpy array of type float64, then we can change it to int32 by giving the data type to the astype() method of numpy array.We can check the type of numpy array using the dtype class. Let's check the data type of sample numpy array.Example# importing numpy library import numpy as np # creating numpy array array = np.array([1, 2, 3, 4, 5]) # printing the data type of the numpy array print(array.dtype)OutputIf you run the above code, you will get the ... Read More

Number of operations required to make all array elements Equal in Python

Naveen Singh
Updated on 02-Jan-2020 06:11:25

383 Views

We have given an array of elements, and we have to make them all equal by incrementing the elements by 1. We are allowed to increment n - 1 element at each step. Our goal is to calculate the total number of operations required to make all the array elements equal.For example, if you take the list [1, 2, 3], it took three operations to make all the elements equal. One solution to the problem is. Find the most significant number at each step and increment the rest of the elements by 1. Let's write the code.Example Live Demodef main():   ... Read More

Python Program to print the diamond shape

Naveen Singh
Updated on 30-Dec-2019 11:53:13

306 Views

The looping features in python can be used to create many nicely formatted diagrams using various characters from the keyboard. One such shape is diamond shape which will involve multiple loops. This is because we have to print the character both vertically and horizontally. Also we have to take care of the shape gradually growing from top till middle and then gradually shrinking from middle till the bottom. For this reason, we will use two for loops each containing one more for loop inside it.Below is the code for creating the diamond shape.Exampledef Shape_of_Diamond(shape): a = 0 for m in ... Read More

Python - fmod() function

Naveen Singh
Updated on 30-Dec-2019 10:39:05

558 Views

the fmod()in python implements the math modulo operation. The remainder obtained after the division operation on two operands is known as modulo operation. It is a part of standard library under the math module. In the below examples we will see how the modulo operation gives different out puts under various scenarios.Positive numbersFor positive numbers, the result is the remainder of the operation after the first integer is divided by the second. Interesting the result always comes as a float as we can see from the type of the result.Example Live Demofrom math import fmod print(fmod(6, 7)) print(type(fmod(6, 7))) print(fmod(0, 7)) ... Read More

Find minimum of each index in list of lists in Python

Naveen Singh
Updated on 30-Dec-2019 10:36:09

2K+ Views

In some problems we need to identify the minimum of each element in a list. But in solving the matrix operations, we need to find the minimum of each column in the matrix. That needs us to find the minimum value from list of lists. Because each column of a matrix is a list of lists.Using min() and zip()In the below example we use the min() and zip(). Here the zip() function organizes the elements at the same index from multiple lists into a single list. Then we apply the min() function to the result of zip function using a ... Read More

Nested list comprehension in python

Naveen Singh
Updated on 30-Dec-2019 10:32:32

2K+ Views

A nested list is a list within a list. Python provides features to handle nested list gracefully and apply common functions to manipulate the nested lists. In this article we will see how to use list comprehension to create and use nested lists in python.Creating a MatrixCreating a matrix involves creating series of rows and columns. We can use for loop for creating the matrix rows and columns by putting one python list with for loop inside another python list with for loop.Example Live Demomatrix = [[m for m in range(4)] for n in range(3)] print(matrix)Running the above code gives us ... Read More

Multi-Line printing in Python

Naveen Singh
Updated on 30-Dec-2019 10:26:04

696 Views

We have usually seen the print command in python printing one line of output. But if we have multiple lines to print, then in this approach multiple print commands need to be written. This can be avoided by using another technique involving the three single quotes as seen below.Example Live Demoprint(''' Motivational Quote : Sometimes later becomes never, Do it now. Great things never come from comfort zones. The harder you work for something, the greater you'll feel when you achieve it. ''') Running the above code gives us the following result:Motivational Quote : Sometimes later becomes never, Do ... Read More

Advertisements