1 Reverse a string
text="Python" print(text[::-1]) #source code --> clcoding.com
Output:
nohtyP2 Swap two vairables without a temp variable
a,b=5,20 a,b=b,a print(a,b) #source code --> clcoding.comOutput:
20 53. check the string is palindrome
word="madam" print(word==word[: :-1]) #source code --> clcoding.comOutput:
True
4. Count Frequency of each element in a list
from collections import Counter print(Counter(['a','b','c','b','a'])) #source code --> clcoding.comOutput:
Counter({'a': 2, 'b': 2, 'c': 1})5. Get all even numbers from a list
nums=[1,2,3,4,5,6,7,8] print([n for n in nums if n%2==0]) #source code --> clcoding.comOutput:
[2, 4, 6, 8]6. Flatten a nested list
nested=[[1,2],[3,4],[5,6]] print([x for sub in nested for x in sub]) #source code --> clcoding.comOutput:
[1, 2, 3, 4, 5, 6]7. Find the factorial of a number
import math print(math.factorial(5)) #source code --> clcoding.comOutput:1208. Find common elements between two list
a=[1,2,4,5,4] b=[3,4,5,1,2] print(list(set(a)& set(b))) #source code --> clcoding.comOutput:
[1, 2, 4, 5]10. one liner FizzBuzz
print(['Fizz'*(i%3==0)+'Buzz'*(i%5==0) or i for i in range(1,16)]) #source code --> clcoding.comOutput:
[1, 2, 'Fizz', 4, 'Buzz', 'Fizz', 7, 8, 'Fizz', 'Buzz', 11, 'Fizz', 13, 14, 'FizzBuzz']
.png)

0 Comments:
Post a Comment