Python Articles

Page 206 of 852

How to compare calendar.timegm() vs. time.mktime() in Python?

SaiKrishna Tavva
SaiKrishna Tavva
Updated on 19-May-2025 1K+ Views

In Python, the mktime() function (from the time module) assumes that the passed tuple is in local time, while the calendar.timegm() (from the calendar module) assumes it's in GMT/UTC. Depending on the interpretation, the tuple represents a different time, so both functions return different values (seconds since the epoch are UTC-based). The difference between the values should be equal to the time zone offset of your local time zone. Understanding time.mktime() in Local Time Context The Python time.mktime() method converts the object form of local time into seconds since the epoch (January 1, 1970, 00:00:00 UTC). This method is the inverse function of localtime() and ...

Read More

How to print Narcissistic(Armstrong) Numbers with Python?

Sumana Challa
Sumana Challa
Updated on 16-May-2025 577 Views

A narcissistic number (also known as an Armstrong number) is a number that equals the sum of its digits, each raised to the power of the number of digits. For example, 370 - 33+73+03 = 370. The algorithm to check for an Armstrong number is as follows - Determine the number of digits for the mentioned number. Extract each digit and calculate the power of that digit with the exponent equal to the number of digits. Calculate the sum of the power. Compare ...

Read More

How to declare a variable in Python without assigning a value to it?

Nikitasha Shrivastava
Nikitasha Shrivastava
Updated on 16-May-2025 11K+ Views

Python variable is a name that you give to something in your program. It basically helps you keep track of objects, which are things like numbers, words or any other data. When you assign an object to a variable, you can use that name to refer to that object later. The data, on the other hand, is still contained within the object. For example, a is assigned the value 100. Here 'a' is a variable. a = 100 This assignment creates an integer object with the value 100 and assigns the variable a to point to that object. In ...

Read More

What is the difference between working of append and + operator in a list in Python?

Nikitasha Shrivastava
Nikitasha Shrivastava
Updated on 16-May-2025 4K+ Views

In this article, we will see the differences between two common ways of adding elements to lists in Python: the append method and the "+" operator.   The append() method is used to add elements to the list by utilizing a mutator() method. The '+' operator is used to create a new list with the capacity for one more element. Behaviour of "+" with Python Lists Python accesses each element of the first list by using the '+' operator. When the '+' symbol is used, a new list with the capacity for one more element is produced. The old ...

Read More

What is a sequence data type in Python?

Nikitasha Shrivastava
Nikitasha Shrivastava
Updated on 16-May-2025 18K+ Views

Sequence Data Types are used to store data in containers in the Python programming language. The different types of containers used to store the data are List, Tuple, and String. Lists are mutable and can hold data of any type, whereas Strings are immutable and can only store data of the str type. Tuples are immutable data types that can store any sort of value. So let's discuss these data types one by one in the coming section - List The sequential data-type class includes the list data type. The list is the only mutable data type in the sequential ...

Read More

How to index and slice lists in Python?

Nikitasha Shrivastava
Nikitasha Shrivastava
Updated on 16-May-2025 5K+ Views

Lists are one of the four most commonly used data structures provided by Python. A list is a data structure in Python that is mutable and has an ordered sequence of elements. In Python, a list is like a box in which you can keep many things like numbers, names, or your favorite fruits. Let us say you have a list like this: fruits = ["apple", "banana", "mango"] Here, each fruit has a position in the list. But in Python, we start counting from 0, not 1. So here is the indexing of apple, banana, and mango - ...

Read More

How does in operator work on list in Python?

Nikitasha Shrivastava
Nikitasha Shrivastava
Updated on 16-May-2025 19K+ Views

The in operator in Python In Python, the in operator determines whether a given value is a constituent element of a sequence such as a string, array, list, or tuple. When used in a condition, the statement returns a Boolean result of True or False. The statement returns True if the specified value is found within the sequence. When it is not found, we get a False. In Python, a list is an ordered sequence that can hold several object types such as integer, character, or float. In other programming languages, a list is equivalent to an array. Square brackets ...

Read More

How does * operator work on list in Python?

Nikitasha Shrivastava
Nikitasha Shrivastava
Updated on 16-May-2025 18K+ Views

In Python, a list is an ordered sequence that can hold several object types such as integer, character, or float. In other programming languages, a list is equivalent to an array. In this article, we will show you how the * operator works on a list in python. So we will show you different examples to understand how * works on a python list in the below section − Repetition Operator(*) A repetition operator is supported by sequence datatypes (both mutable and immutable). The repetition operator * creates several copies of that object and joins them together. When used with ...

Read More

How to get formatted date and time in Python?

Nikitasha Shrivastava
Nikitasha Shrivastava
Updated on 16-May-2025 113K+ Views

This article will discuss how to format date and time in Python. Python makes it easy to work with date and time with the help of the datetime module. You can format dates and times in different formats as per your needs. Datetime Module of Python The datetime module in Python offers methods for working with date and time values. To use this module, we must first import it using the following import keyword- import datetime Strftime() Function The strftime() function returns a formatted date and time. It accepts a format string that you can use to get the ...

Read More

How to catch multiple exceptions in one line (except block) in Python?

Sarika Singh
Sarika Singh
Updated on 16-May-2025 534 Views

In Python, instead of writing separate except blocks for each exception, you can handle multiple exceptions together in a single except block by specifying them as a tuple. In this example, we are catching both ValueError and TypeError using a single except block - try: x = int("abc") # Raises ValueError y = x + "5" # Would raise TypeError if above line did not error except (ValueError, TypeError) as e: print("Caught an exception:", e) The above program will generate the following error ...

Read More
Showing 2051–2060 of 8,519 articles
« Prev 1 204 205 206 207 208 852 Next »
Advertisements