0% found this document useful (0 votes)
3 views4 pages

Python Code Snippets for Common Tasks

The document contains a series of Python code snippets that perform various tasks such as checking for leap years, converting decimal to binary, finding the GCD of two numbers, summing natural numbers, removing duplicates from a list, counting character frequency, printing multiplication tables, checking for perfect numbers, converting strings to title case, and finding the largest element in a list.

Uploaded by

ANKIT GAUTAM
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)
3 views4 pages

Python Code Snippets for Common Tasks

The document contains a series of Python code snippets that perform various tasks such as checking for leap years, converting decimal to binary, finding the GCD of two numbers, summing natural numbers, removing duplicates from a list, counting character frequency, printing multiplication tables, checking for perfect numbers, converting strings to title case, and finding the largest element in a list.

Uploaded by

ANKIT GAUTAM
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

21.

Check Leap Year


year = int(input("Enter a year: "))
if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):
print("Leap Year")
else:
print("Not a Leap Year")

22. Convert Decimal to Binary


num = int(input("Enter a decimal number: "))
print("Binary:", bin(num)[2:])
23. Find GCD of Two Numbers
import math
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
print("GCD is:", [Link](a, b))

24. Sum of N Natural Numbers


n = int(input("Enter a number: "))
total = n * (n + 1) // 2
print("Sum is:", total)

25. Remove Duplicates from a List

lst = [1, 2, 3, 2, 4, 3]
unique = list(set(lst))
print("Unique List:", unique)
26. Count Frequency of Each Character
s = input("Enter a string: ")
freq = {}
for ch in s:
freq[ch] = [Link](ch, 0) + 1
print(freq)

27. Print Multiplication Table


n = int(input("Enter a number: "))
for i in range(1, 11):
print(f"{n} x {i} = {n*i}")
28. Check if Number is Perfect
num = int(input("Enter a number: "))
sum = 0
for i in range(1, num):
if num % i == 0:
sum += i
if sum == num:
print("Perfect Number")
else:
print("Not Perfect")

29. Convert String to Title Case


s = input("Enter a string: ")
print([Link]())

30. Find Largest Element in a List


lst = [10, 30, 5, 7, 99]
print("Largest:", max(lst))

You might also like