Jayashree has Published 62 Articles

How do I find the largest integer less than x in Python?

Jayashree

Jayashree

Updated on 27-Oct-2022 12:23:22

In this article, we will show you how to find the largest integer less than x in python. The Greatest Integer Function [X] denotes an integral part of the real number x that is the closest and smallest integer to x. It's also called the X-floor. [x]=the largest integer less ... Read More

How to Check Leap Year using Python?

Jayashree

Jayashree

Updated on 22-Dec-2020 07:33:54

Leap year comes after every four years. For normal year, if it is divisible by four, it is called leap year, whereas for century year, it should be divisible by 400. The following Python program shows whether the year is leap or notExampleyr=int(input('enter year')) if yr%100==0: #century year if yr%400==0: ... Read More

What is random.uniform method in Python?

Jayashree

Jayashree

Updated on 24-Jun-2020 07:00:09

The uniform() function is defined in the random module of the standard Python library. It returns a random floating-point number between a given range of numbers>>> import random >>> random.uniform(10,100) 20.118467024396452 >>> random.uniform(10,100) 23.739576765885502

How to randomize the items of a list in Python?

Jayashree

Jayashree

Updated on 24-Jun-2020 06:59:23

The random module in the Python standard library provides a shuffle() function that returns a sequence with its elements randomly placed.>>> import random >>> l1=['aa',22,'ff',15,90,5.55] >>> random.shuffle(l1) >>> l1 [22, 15, 90, 5.55, 'ff', 'aa'] >>> random.shuffle(l1) >>> l1 ['aa', 'ff', 90, 22, 5.55, 15]

Why do we use random.seed() in Python?

Jayashree

Jayashree

Updated on 24-Jun-2020 06:56:39

The seed() method of random module initializes the random number generator.random.seed(a, b)If a is omitted or None, the current system time is used. If randomness sources are provided by the operating system, they are used instead of the system timeIf a is an int, it is used directly.With version 2 (the default), ... Read More

How to extract numbers from a string using Python?

Jayashree

Jayashree

Updated on 23-Jun-2020 06:13:54

To extract each digit from a string −>>> str1='a34e 345 bcd 5he 78 xyz' >>> for s in str1: if s.isdigit():print (s) 3 4 3 4 5 5 7 8To extract only integers from a string in which words are separated by space character −>>> str1='h3110 23 cat 444.4 ... Read More

How to print out the first character of each list in Python?

Jayashree

Jayashree

Updated on 18-Jun-2020 13:35:47

Assuming that the list is collection of strings, the first character of each string is obtained as follows −>>> L1=['aaa', 'bbb', 'ccc'] >>> for string in L1: print (string[0]) a b cIf list is a collection of list objects. First element of each list is obtained as follows −>>> ... Read More

What are multiple statement groups as suites in python?

Jayashree

Jayashree

Updated on 18-Jun-2020 06:31:45

A group of individual statements, which make a single code block are called suites in Python. Compound or complex statements, such as if, while, def, and class require a header line and a suite.Header lines begin the statement (with the keyword) and terminate with a colon (: ) and are ... Read More

Python: Cannot understand why the error - cannot concatenate 'str' and 'int' object ?

Jayashree

Jayashree

Updated on 13-Mar-2020 05:15:39

This can be corrected by putting n+1 in brackets i.e. (n+1)for num in range(5):     print ("%d" % (num+1))Using %d casts the object following % to string. Since a string object can't be concatnated with a number (1 in this case) interpreter displays typeerror.

How to create Ordered dictionaries in Python?

Jayashree

Jayashree

Updated on 02-Mar-2020 10:22:19

An OrderedDict is a dictionary subclass that remembers the order in which its contents are added, It is defined in collections module of Python library. OrderDict remembers the order of addition of key-value pairs in a dictionary>>> from collections import OrderedDict >>> od=OrderedDict(d.items()) >>> od OrderedDict([('banana', 3), ('apple', 4), ('pear', ... Read More

1 2 3 4 5 ... 7 Next
Advertisements