0% found this document useful (0 votes)
24 views7 pages

Project File Part 1

Uploaded by

Sanju Kumari
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)
24 views7 pages

Project File Part 1

Uploaded by

Sanju Kumari
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/12/25, 9:47 PM ChatGPT

Page 23: Count Occurrences of an Element in a Tuple


python Copy code

# Program to count occurrences of an element in a tuple def count_occurrences(tup,


element): return [Link](element) # Example usage: tup = (1, 2, 3, 4, 2, 5, 2)
element = 2 print(f"The element {element} occurs {count_occurrences(tup, element)}
times.")

Output:

bash Copy code

The element 2 occurs 3 times.

Page 24: Create a Dictionary from User Input

python Copy code

# Program to create a dictionary from user input and display it def create_dict(): n =
int(input("Enter number of items: ")) # Input: 3 d = {} for _ in range(n): key =
input("Enter key: ") # Input: "name" value = input("Enter value: ") # Input: "John"
d[key] = value return d # Example usage: my_dict = create_dict() print("Created
Dictionary:", my_dict)

Output:

yaml Copy code

Enter number of items: 3 Enter key: name Enter value: John Enter key: age Enter value:
30 Enter key: city Enter value: New York Created Dictionary: {'name': 'John', 'age':
'30', 'city': 'New York'}

Page 25: Remove an Element from a List by Value

python Copy code

[Link] 1/7
1/12/25, 9:47 PM ChatGPT

# Program to remove an element from a list by value def remove_element(lst, value): if


value in lst: [Link](value) print(f"Updated List: {lst}") else: print(f"{value}
not found in list.") # Example usage: lst = [1, 2, 3, 4, 5] remove_element(lst, 3) #
Output: [1, 2, 4, 5]

Output:

less Copy code

Updated List: [1, 2, 4, 5]

Page 26: Merge Two Dictionaries

python Copy code

# Program to merge two dictionaries def merge_dicts(dict1, dict2): [Link](dict2)


return dict1 # Example usage: dict1 = {'a': 1, 'b': 2} dict2 = {'c': 3, 'd': 4}
print("Merged Dictionary:", merge_dicts(dict1, dict2))

Output:

css Copy code

Merged Dictionary: {'a': 1, 'b': 2, 'c': 3, 'd': 4}

Page 27: Check if a String is a Palindrome

python Copy code

# Function to check if a string is a palindrome def is_palindrome(s): return s ==


s[::-1] # Example usage: string = input("Enter a string: ") # Input: "madam" if
is_palindrome(string): print(f"'{string}' is a palindrome.") else: print(f"'{string}'
is not a palindrome.")

Output:

csharp Copy code

[Link] 2/7
1/12/25, 9:47 PM ChatGPT

Enter a string: madam 'madam' is a palindrome.

Page 28: Calculate the Power of a Number


python Copy code

# Function to calculate the power of a number def power(base, exponent): return base
** exponent # Example usage: base = int(input("Enter base: ")) # Input: 2 exponent =
int(input("Enter exponent: ")) # Input: 3 print(f"{base} raised to the power of
{exponent} is {power(base, exponent)}")

Output:

csharp Copy code

Enter base: 2 Enter exponent: 3 2 raised to the power of 3 is 8

Page 29: Convert Celsius to Fahrenheit

python Copy code

# Function to convert Celsius to Fahrenheit def celsius_to_fahrenheit(celsius): return


(celsius * 9/5) + 32 # Example usage: celsius = float(input("Enter temperature in
Celsius: ")) # Input: 25 print(f"{celsius}°C is equal to
{celsius_to_fahrenheit(celsius)}°F")

Output:

mathematica Copy code

Enter temperature in Celsius: 25 25.0°C is equal to 77.0°F

Page 30: Calculate the Sum of Digits of a Number

[Link] 3/7
1/12/25, 9:47 PM ChatGPT
python
Copy code

# Function to calculate the sum of digits of a number def sum_of_digits(num): return


sum(int(digit) for digit in str(num)) # Example usage: num = int(input("Enter a
number: ")) # Input: 1234 print(f"The sum of digits of {num} is {sum_of_digits(num)}")

Output:

yaml Copy code

Enter a number: 1234 The sum of digits of 1234 is 10

Page 31: Find the Least Common Multiple (LCM) of Two Numbers
python Copy code

# Function to find the Least Common Multiple (LCM) of two numbers def lcm(a, b):
greater = max(a, b) while True: if greater % a == 0 and greater % b == 0: return
greater greater += 1 # Example usage: num1 = int(input("Enter first number: ")) #
Input: 4 num2 = int(input("Enter second number: ")) # Input: 5 print(f"The LCM of
{num1} and {num2} is {lcm(num1, num2)}")

Output:

mathematica Copy code

Enter first number: 4 Enter second number: 5 The LCM of 4 and 5 is 20

Page 32: Implement a Stack with Push and Pop Operations

python Copy code

# Function to implement stack with push and pop operations stack = [] def push(stack,
item): [Link](item) def pop(stack): if stack: return [Link]() else: return
"Stack is empty" # Example usage: push(stack, 10) push(stack, 20) print(f"Popped item:
{pop(stack)}") print(f"Stack after pop: {stack}")

Output:

yaml Copy code

[Link] 4/7
1/12/25, 9:47 PM ChatGPT

Popped item: 20 Stack after pop: [10]

Page 33: Check if the Stack is Empty

python Copy code

# Function to check if the stack is empty def is_empty(stack): return len(stack) == 0


# Example usage: stack = [10, 20] print(f"Is stack empty? {is_empty(stack)}")

Output:

sql Copy code

Is stack empty? False

Page 34: Peek at the Top Element of the Stack

python Copy code

# Function to peek at the top element of the stack def peek(stack): if stack: return
stack[-1] else: return "Stack is empty" # Example usage: stack = [10, 20, 30]
print(f"Top element: {peek(stack)}")

Output:

css Copy code

Top element: 30

Page 35: Get the Size of the Stack

python Copy code

[Link] 5/7
1/12/25, 9:47 PM ChatGPT

# Function to get the size of the stack def size(stack): return len(stack) # Example
usage: stack = [10, 20, 30] print(f"Size of the stack: {size(stack)}")

Output:

arduino Copy code

Size of the stack: 3

Page 36: Display All Elements in the Stack


python Copy code

# Function to display all elements in the stack def display(stack): return stack #
Example usage: stack = [10, 20, 30] print(f"Elements in the stack: {display(stack)}")

Output:

arduino Copy code

Elements in the stack: [10, 20, 30]

Page 37: Count Lines Starting with 'W' or 'H' in a File


python Copy code

# Function to count the number of lines in a file that start with 'W' or 'H' def
count_lines(): with open('[Link]', 'r') as file: lines = [Link]() count =
0 for line in lines: if [Link]().startswith(('W', 'H')): count += 1 return count #
Example usage: print(f"Number of lines starting with 'W' or 'H': {count_lines()}")

Page 38: Count Total Number of Words in a File


python Copy code

[Link] 6/7
1/12/25, 9:47 PM ChatGPT

# Function to count the total number of words in a file def countwords(): with
open('[Link]', 'r') as file: content = [Link]() words = [Link]() return
len(words) # Example usage: print(f"Total number of words: {countwords()}")

Page 39: Count Occurrences of "AND" in a File

python Copy code

# Function to count the occurrences of "AND" (including different cases) in a file def
count_and(): with open('[Link]', 'r') as file: content = [Link]() and_count =
sum(1 for word in [Link]() if [Link]() == 'and') return and_count # Example
usage: print(f"Occurrences of 'AND' in the file: {count_and()}")

Page 40: Display Words Starting with 'T' or 't' in a File

python Copy code

# Function to display words starting with 'T' or 't' in a file def displaywords():
with open('[Link]', 'r') as file: content = [Link]() words = [Link]()
t_words = [word for word in words if [Link]().startswith('t')] return len(t_words)
# Example usage: print(f"Words starting with 'T' or 't': {displaywords()}")

[Link] 7/7

You might also like