0% found this document useful (0 votes)
33 views5 pages

Assi 2

The document contains 16 code snippets performing various tasks like: 1) Counting even/odd numbers in a list and separating them into even and odd lists 2) Calculating total, count, and average of user-entered numbers 3) Finding maximum and minimum numbers in a user-entered list 4) Cubing numbers in a user-entered list 5) Filtering numbers in a user-entered list that are divisible by 5 6) Printing squares in a pyramid pattern based on user input 7) Checking if a number is prime 8) Comparing first and last elements of two lists 9) Adding "ing" or "ly" to a word based on its ending 10)

Uploaded by

Aziz عزيز
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views5 pages

Assi 2

The document contains 16 code snippets performing various tasks like: 1) Counting even/odd numbers in a list and separating them into even and odd lists 2) Calculating total, count, and average of user-entered numbers 3) Finding maximum and minimum numbers in a user-entered list 4) Cubing numbers in a user-entered list 5) Filtering numbers in a user-entered list that are divisible by 5 6) Printing squares in a pyramid pattern based on user input 7) Checking if a number is prime 8) Comparing first and last elements of two lists 9) Adding "ing" or "ly" to a word based on its ending 10)

Uploaded by

Aziz عزيز
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

1)

numbers = [2, 5, 6, 8, 10, 9, 11, 12]


even_count = 0
odd_count = 0
even_list = []
odd_list = []
for num in numbers:
if num % 2 == 0:
even_count += 1
even_list.append(num)
else:
odd_count += 1
odd_list.append(num)
print("Count of even numbers:", even_count)
print("Count of odd numbers:", odd_count)
print("Even numbers:", even_list)
print("Odd numbers:", odd_list)

2)

count = 0
total = 0
average = 0
while True:
num_input = input("Enter a number (or 'done' to finish): ")
if num_input == "done":
break
try:
num = float(num_input)
count += 1
total += num
except ValueError:
print("Invalid input, please enter a number or 'done'.")
if count > 0:
average = total / count
print("Total:", total)
print("Count:", count)
print("Average:", average)

3)

numbers = []
while True:
num_input = input("Enter a number (or 'done' to finish): ")
if num_input == "done":
break
try:
num = float(num_input)
numbers.append(num)
except ValueError:
print("Invalid input, please enter a number or 'done'.")
if len(numbers) > 0:
max_num = max(numbers)
min_num = min(numbers)
print("Maximum number:", max_num)
print("Minimum number:", min_num)
else:
print("No valid inputs.")

4)

numbers = []
while True:
num_input = input("Enter a number (or 'done' to finish): ")
if num_input == "done":
break
try:
num = float(num_input)
numbers.append(num)
except ValueError:
print("Invalid input, please enter a number or 'done'.")
cubes = [num ** 3 for num in numbers]
print("Original list:", numbers)
print("Cube values:", cubes)

5)

numbers = []
while True:
num_input = input("Enter a number (or 'done' to finish): ")
if num_input == "done":
break
try:
num = float(num_input)
numbers.append(num)
except ValueError:
print("Invalid input, please enter a number or 'done'.")
divisible_by_5 = [num for num in numbers if num % 5 == 0]
print("Original list:", numbers)
print("Divisible by 5:", divisible_by_5)

6)

num_rows = int(input("Enter the number of rows: "))

for i in range(num_rows):
for j in range(i+1):
print((i+1)**2, end=" ")
print()

7)

num = int(input("Enter an integer: "))


if num > 1:
for i in range(2, int(num/2)+1):
if num % i == 0:
print(num, "is not a prime number")
break
else:
print(num, "is a prime number")
else:
print(num, "is not a prime number")

8)

list1 = [2, 3, 4, 5, 6]
list2 = [10, 12, 4]

if list1[0] == list2[-1] or list2[0] == list1[-1]:


print(True)
else:
print(False)
9)word = input("Enter a word: ")

if len(word) >= 3:
if word[-3:] == "ing":
word += "ly"
else:
word += "ing"

print("Output:", word)

10)

input_string = input("Enter a string: ")


string_without_spaces = input_string.replace(" ", "")
num_characters = len(string_without_spaces)

print("Number of characters in the string (excluding spaces):", num_characters)

11)

str1 = "CSCI29@#8496"

# Initialize variables for sum and count


sum_digits = 0
count_digits = 0

# Iterate over each character in the string


for c in str1:
# Check if the character is a digit
if c.isdigit():
# If it's a digit, add it to the sum and increment the count
sum_digits += int(c)
count_digits += 1

# Check if any digits were found in the string


if count_digits > 0:
# Calculate the average
avg_digits = sum_digits / count_digits
print("Sum is:", sum_digits, "Average is", avg_digits)
else:
print("No digits found in the string.")

12)

str1 = "Ali*is*a*data*scientist*student"

# Split the string on *


substrings = str1.split("*")

# Display each substring


for substring in substrings:
print(substring)

13)

words = ['Kemy', 'Jon', ' ', 'Kelly', None, 'Eric', ' ', 'Jena', '']

# Iterate over each word and replace empty strings with 'empty'
for i in range(len(words)):
if not words[i]:
words[i] = 'empty'

# Remove any remaining empty strings


words = list(filter(lambda x: x != 'empty', words))

# Convert all words to uppercase


words = [word.upper() for word in words]

print(words)

14)

# Take input string from user


input_str = input("Enter a string: ")

# Initialize counter
count = 0

# Iterate over each character in the string


for char in input_str:
# Increment counter for each character
count += 1

# Display the length of the string


print("Length of string:", count)

15)

my_list = [5, 20, 15, 20, 25, 50, 20]


# Get user input for item to remove
item = int(input("Enter item to remove: "))

# Remove all occurrences of the item


while item in my_list:
my_list.remove(item)

# Print the updated list


print(my_list)

16)

def same_letter(s):
words = s.split()
return words[0][0].lower() == words[1][0].lower()

17)

def adjacent_number(lst, target):


for i in range(len(lst)-1):
if lst[i] == target and lst[i+1] == target:
return True
return False

You might also like