0% found this document useful (0 votes)
2 views2 pages

Python Function Solutions

The document contains a list of Python function definitions that perform various tasks, such as mathematical operations, string manipulations, and list processing. Each function is defined concisely with a single line of code, demonstrating different programming concepts. The functions include operations like calculating factorials, checking for prime numbers, and manipulating strings and lists.

Uploaded by

Pule Mothobi
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views2 pages

Python Function Solutions

The document contains a list of Python function definitions that perform various tasks, such as mathematical operations, string manipulations, and list processing. Each function is defined concisely with a single line of code, demonstrating different programming concepts. The functions include operations like calculating factorials, checking for prime numbers, and manipulating strings and lists.

Uploaded by

Pule Mothobi
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

1.

def square(n): return n*n

2. def is_even(n): return n % 2 == 0

3. def largest(a,b,c): return max(a,b,c)

4. def count_vowels(s): return sum(ch in 'aeiouAEIOU' for ch in s)

5. def reverse_str(s): return s[::-1]

6. def sum_list(lst): return sum(lst)

7. def is_palindrome(s): return s == s[::-1]

8. def factorial(n): return 1 if n==0 else n*factorial(n-1)

9. def c_to_f(c): return (c*9/5)+32

10. def find_min(lst): return min(lst)

11. def word_count(s): return len([Link]())

12. def merge_lists(a,b): return a+b

13. def remove_duplicates(lst): return list(set(lst))

14. def fibonacci(n): seq=[0,1];


[[Link](seq[-1]+seq[-2]) for _ in range(n-2)]; return seq[:n]

15. def is_prime(n): return n>1 and all(n%i for i in range(2,int(n**0.5)+1))

16. def sum_digits(n): return sum(int(d) for d in str(n))

17. def count_occ(lst,x): return [Link](x)

18. def average(lst): return sum(lst)/len(lst)

19. def are_anagrams(a,b): return sorted(a)==sorted(b)

20. def second_largest(lst): return sorted(set(lst))[-2]

21. def upper_all(lst): return [[Link]() for s in lst]

22. def remove_spaces(s): return [Link](' ','')

23. def last_element(lst): return lst[-1]

24. def common(a,b): return list(set(a)&set;(b))

25. def is_consonant(c): return [Link]() and [Link]() not in 'aeiou'

26. def table(n): return [n*i for i in range(1,13)]

27. def flatten(lst): import itertools; return list([Link](*lst))

28. def longest_word(s): return max([Link](), key=len)


29. def char_freq(s): return {c:[Link](c) for c in set(s)}

30. def all_unique(lst): return len(lst)==len(set(lst))

You might also like