Answers for "all in python"

5

all in python

'''
Return Value from all()
all() method returns:

True - If all elements in an iterable are true
False - If any element in an iterable is false
'''

# all values true
l = [1, 3, 4, 5]
print(all(l))            # Result : True

# all values false
l = [0, False]
print(all(l))			 # Result : False

# one false value ( As 0 is considered as False)
l = [1, 3, 4, 0]
print(all(l))			 # Result : False

# one true value
l = [0, False, 5]
print(all(l))			 # Result : False

# empty iterable
l = []
print(all(l))
Posted by: Guest on September-24-2020
0

all in python

from dataclasses import dataclass
from enum import Enum
from typing import List


class ParkingSpotStatus(str, Enum):
    FREE = "FREE"
    OCCUPIED = "OCCUPIED"


@dataclass
class ParkingSpot:
    number: int
    status: ParkingSpotStatus


# With for and if
@dataclass
class Garage:
    parking_spots: List[ParkingSpot]

    def is_full(self):
        full = True
        for spot in self.parking_spots:
            if spot.status == ParkingSpotStatus.FREE:
                full = False
                break
        return full


garage = Garage(parking_spots=[ParkingSpot(number=1, status=ParkingSpotStatus.OCCUPIED)])
print(garage.is_full())


# With all
@dataclass
class Garage:
    parking_spots: List[ParkingSpot]

    def is_full(self):
        return all(spot.status == ParkingSpotStatus.OCCUPIED for spot in self.parking_spots)


garage = Garage(parking_spots=[ParkingSpot(number=1, status=ParkingSpotStatus.OCCUPIED)])
print(garage.is_full())
Posted by: Guest on September-13-2021
0

any and all in python3

# Here all the iterables are True so all 
# will return True and the same will be printed 
print (all([True, True, True, True])) 
  
# Here the method will short-circuit at the  
# first item (False) and will return False. 
print (all([False, True, True, False])) 
  
# This statement will return False, as no 
# True is found in the iterables 
print (all([False, False, False]))
Posted by: Guest on July-27-2020

Python Answers by Framework

Browse Popular Code Answers by Language