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

Python Programs Functions Strings Tuple Dict

The document contains various Python functions demonstrating fundamental programming concepts such as calculating factorials, generating Fibonacci series, checking for prime numbers, finding GCD, and more. It includes examples for checking palindromes, counting vowels, reversing strings, and managing dictionaries. Each function is accompanied by a print statement to display the results of its execution.

Uploaded by

Albert
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)
1 views4 pages

Python Programs Functions Strings Tuple Dict

The document contains various Python functions demonstrating fundamental programming concepts such as calculating factorials, generating Fibonacci series, checking for prime numbers, finding GCD, and more. It includes examples for checking palindromes, counting vowels, reversing strings, and managing dictionaries. Each function is accompanied by a print statement to display the results of its execution.

Uploaded by

Albert
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
You are on page 1/ 4

Factorial of a Number

def factorial(n):

return 1 if n == 0 else n * factorial(n - 1)

print("Factorial:", factorial(5))

Fibonacci Series

def fibonacci(n):

a, b = 0, 1

for _ in range(n):

print(a, end=' ')

a, b = b, a + b

fibonacci(10)

Check Prime Number

def is_prime(n):

if n <= 1:

return False

for i in range(2, int(n**0.5)+1):

if n % i == 0:

return False

return True

print(is_prime(17))

GCD of Two Numbers

def gcd(a, b):

while b:

a, b = b, a % b
return a

print("GCD:", gcd(48, 18))

Find Maximum of Three Numbers

def find_max(a, b, c):

return max(a, b, c)

print("Maximum:", find_max(4, 8, 2))

Palindrome Checker

def is_palindrome(s):

return s == s[::-1]

print(is_palindrome("madam"))

Count Vowels in String

def count_vowels(s):

vowels = "aeiouAEIOU"

return sum(1 for char in s if char in vowels)

print("Vowels:", count_vowels("Hello World"))

Reverse a String

def reverse_string(s):

return s[::-1]

print(reverse_string("Python"))

Check Anagram

def is_anagram(s1, s2):


return sorted(s1) == sorted(s2)

print(is_anagram("listen", "silent"))

Frequency of Each Character

def char_frequency(s):

freq = {}

for char in s:

freq[char] = freq.get(char, 0) + 1

return freq

print(char_frequency("banana"))

Tuple Packing and Unpacking

def pack_unpack():

person = ("Aryan", 20, "India")

name, age, country = person

print(f"Name: {name}, Age: {age}, Country: {country}")

pack_unpack()

Find Maximum in Tuple

def max_in_tuple(t):

return max(t)

print(max_in_tuple((3, 7, 1, 9)))

Phone Book Program

def phone_book():

contacts = {"Alice": "1234", "Bob": "5678"}


contacts["Charlie"] = "9012"

print(contacts["Alice"])

phone_book()

Count Word Frequency in Sentence

def word_count(sentence):

words = sentence.split()

freq = {}

for word in words:

freq[word] = freq.get(word, 0) + 1

return freq

print(word_count("this is a test this is only a test"))

Merge Two Dictionaries

def merge_dicts(d1, d2):

return {**d1, **d2}

print(merge_dicts({'a': 1, 'b': 2}, {'b': 3, 'c': 4}))

You might also like