Loading...
More Python Posts
from collections import defaultdictdef collect_dictionary(obj):inv_obj = defaultdict(list)for key, value in obj.items():inv_obj[value].append(key)return dict(inv_obj)ages = {'Peter': 10,'Isabel': 10,'Anna': 9,}collect_dictionary(ages) # { 10: ['Peter', 'Isabel'], 9: ['Anna'] }
from math import pidef rads_to_degrees(rad):return (rad * 180.0) / pirads_to_degrees(pi / 2) # 90.0
# function which return reverse of a stringdef isPalindrome(s):return s == s[::-1]# Driver codes = "malayalam"ans = isPalindrome(s)if ans:print("Yes")else:print("No")
import calendar# Prompt user for year and monthyear = int(input("Enter the year: "))month = int(input("Enter the month: "))# Create a calendar objectcal = calendar.monthcalendar(year, month)# Display the calendarprint(calendar.month_name[month], year)print("Mon Tue Wed Thu Fri Sat Sun")for week in cal:for day in week:if day == 0:print(" ", end="")else:print(str(day).rjust(2), " ", end="")print()
def max_n(lst, n = 1):return sorted(lst, reverse = True)[:n]max_n([1, 2, 3]) # [3]max_n([1, 2, 3], 2) # [3, 2]
def when(predicate, when_true):return lambda x: when_true(x) if predicate(x) else xdouble_even_numbers = when(lambda x: x % 2 == 0, lambda x : x * 2)print(double_even_numbers(2)) # 4print(double_even_numbers(1)) # 1