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

Python Code Collection

The document contains a Python script with multiple functionalities including a basic calculator, a custom star pattern generator, LCM calculation, vowel counting in a string, a while-else loop demonstration, file content counting, and Fibonacci series generation. Each section is implemented as a separate function or code block. The script prompts user input for various operations and handles errors such as division by zero and file not found.

Uploaded by

dhanananjay88
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 views2 pages

Python Code Collection

The document contains a Python script with multiple functionalities including a basic calculator, a custom star pattern generator, LCM calculation, vowel counting in a string, a while-else loop demonstration, file content counting, and Fibonacci series generation. Each section is implemented as a separate function or code block. The script prompts user input for various operations and handles errors such as division by zero and file not found.

Uploaded by

dhanananjay88
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/ 2

# 1.

Basic Calculator
def add(x, y):
return x + y

def subtract(x, y):


return x - y

def multiply(x, y):


return x * y

def divide(x, y):


if y == 0:
return "Error! Division by zero."
return x / y

print("Select operation:")
print("1. Add")
print("2. Subtract")
print("3. Multiply")
print("4. Divide")

choice = input("Enter choice (1/2/3/4): ")

if choice in ('1', '2', '3', '4'):


num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))

if choice == '1':
print("Result:", add(num1, num2))
elif choice == '2':
print("Result:", subtract(num1, num2))
elif choice == '3':
print("Result:", multiply(num1, num2))
elif choice == '4':
print("Result:", divide(num1, num2))
else:
print("Invalid input")

# 2. Custom Star Pattern


pattern = [1, 2, 5, 12]
for count in pattern:
print('*' * count)

# 3. LCM of two numbers


a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
max_num = max(a, b)
while True:
if max_num % a == 0 and max_num % b == 0:
print("LCM is", max_num)
break
max_num += 1

# 4. Count vowels in string


def count1(s):
vowels = "AEIOUaeiou"
count = 0
for c in s:
if c in vowels:
count += 1
return count
print(count1('I love India'))

# 5. While-else loop
i = 0
while i < 3:
print(i)
i += 1
else:
print(0)

# 6. Count lines, words, characters in file


def count_file_content(filename):
try:
with open(filename, 'r') as file:
lines = file.readlines()
line_count = len(lines)
word_count = 0
char_count = 0
for line in lines:
word_count += len(line.split())
char_count += len(line)
print("Total Lines:", line_count)
print("Total Words:", word_count)
print("Total Characters:", char_count)
except FileNotFoundError:
print("File not found. Please check the filename.")

count_file_content("ABC.text")

# 7. Fibonacci series
n = int(input("Enter number of terms: "))
a, b = 0, 1
for _ in range(n):
print(a, end=' ')
a, b = b, a + b

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy