0% found this document useful (0 votes)
29 views13 pages

Num 2

The document contains multiple code snippets that demonstrate various Python programming concepts like: 1) Taking user input, comparing numbers, and finding the largest number. 2) Calculating the sum of digits in a number. 3) Generating Fibonacci sequences up to a given number of terms. 4) Multiplying a number by integers from 1 to 10. 5) Summing even and odd elements in a list. 6) Searching for an element in a list. 7) Doubling values between 0-9 in a list. 8) Sorting a list in ascending order using bubble sort. 9) Counting lines starting with uppercase letters in a text file. 10)

Uploaded by

stech9099
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)
29 views13 pages

Num 2

The document contains multiple code snippets that demonstrate various Python programming concepts like: 1) Taking user input, comparing numbers, and finding the largest number. 2) Calculating the sum of digits in a number. 3) Generating Fibonacci sequences up to a given number of terms. 4) Multiplying a number by integers from 1 to 10. 5) Summing even and odd elements in a list. 6) Searching for an element in a list. 7) Doubling values between 0-9 in a list. 8) Sorting a list in ascending order using bubble sort. 9) Counting lines starting with uppercase letters in a text file. 10)

Uploaded by

stech9099
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/ 13

num1 = float(input("Enter the first number: "))

num2 = float(input("Enter the second number: "))


num3 = float(input("Enter the third number: "))
if num1 >= num2 and num1 >= num3:
largest = num1
elif num2 >= num1 and num2 >= num3:
largest = num2
else:
largest = num3
print("The largest number is:", largest)
def sum_of_digits(n):
sum = 0
while n > 0:
digit = n % 10

sum += digit

n = n// 10
return sum

n = int(input("Enter a number: "))

add = sum_of_digits(n)
print("The sum of digits in {n} is”,add)
def generate_fibonacci(n):
fibonacci_sequence = [0, 1]

while len(fibonacci_sequence) < n:


next_number = fibonacci_sequence[-1] + fibonacci_sequence[-2]
fibonacci_sequence.append(next_number)

return fibonacci_sequence

num_terms = int(input("Enter the number of terms in the Fibonacci


sequence: "))

if num_terms <= 0:
print("Please enter a positive integer.")
else:
fibonacci_sequence = generate_fibonacci(num_terms)
print("Fibonacci Sequence:")
for number in fibonacci_sequence:
print(number)
num = int(input("Enter a number: "))

for i in range(1, 11):


product = num * i
print(f"{num} x {i} = {product}")
numbers = eval(input(“enter numbers in a list”))

even_sum = 0
odd_sum = 0

for num in numbers:


if num % 2 == 0:
even_sum += num
else:
odd_sum += num

print("Sum of even elements:", even_sum)


print("Sum of odd elements:", odd_sum)
numbers = eval(input(“enter numbers in a list”))

element_to_search = int(input("Enter the element you want to search for: "))


found=false

for number in numbers:


if number == element_to_search:
found = True
break

if found:
print(f"{element_to_search} was found in the list.")
else:
print(f"{element_to_search} was not found in the list.")
numbers = eval(input(“enter numbers in a list”))

for i in range(len(numbers)):
if 0 <= numbers[i] <= 9:
numbers[i] *= 2

print("Modified list:", numbers)


def bubble_sort(a):
n = len(a)
for i in range(n):
for j in range(0, n - i - 1):
if a[j] > a[j + 1]:

a[j], a[j + 1] = a[j + 1], a[j]

my_list = [64, 34, 25, 12, 22, 11, 90]


bubble_sort(my_list)
print("Sorted list in ascending order:", my_list)
with open('STUD.DAT', 'r') as file:

count = 0
for line in file:
if line.strip() and line[0].isupper():
count += 1
print(“Number of lines starting with an uppercase letter: {count}')
with open('STUD.DAT', 'r') as file:

text = file.read()
word_list = text.split()
count_hello = word_list.count("hello")

print(f'The word "hello" appears {count_hello} times in the text file.')


file_name = "STUD.DAT"
uppercase_count = 0
lowercase_count = 0
with open(file_name, 'r') as file:

content = file.read()
for char in content:
if char.isupper():
uppercase_count += 1
elif char.islower():
lowercase_count += 1
print("Uppercase letters count:", uppercase_count)
print("Lowercase letters count:", lowercase_count)
def count_uppercase_vowels_in_file(STUD.DAT):
uppercase_vowels = "AEIOU"
vowel_count = 0

try:
with open(file_path, 'r') as file:
text = file.read()
for char in text:
if char in uppercase_vowels:
vowel_count += 1
except FileNotFoundError:
print("File not found.")

return vowel_count

file_path = "STUD.DAT"

uppercase_vowel_count = count_uppercase_vowels_in_file(file_path)
print("Number of uppercase vowels in the file: {uppercase_vowel_count}")
import csv

csv_file = 'STUD.csv'

with open(csv_file, 'w', newline='') as file:


writer = csv.writer(file)
writer.writerows(data)

# Display the contents of the CSV file


with open(csv_file, 'r') as file:
reader = csv.reader(file)
for row in reader:
print(', '.join(row))

You might also like