Waheeb Algabri¶

Question 1¶

Write a function to calculate the area and perimeter of a rectangle.¶
In [6]:
def rectangle_area(length, width):
    area = length * width
    perimeter = 2 * (length + width)
    return area, perimeter
In [12]:
# Call the function with specific values for length and width
length = 5
width = 3
area, perimeter = rectangle_area(length, width)

# Print the results
print("Area:", area)
print("Perimeter:", perimeter)
Area: 15
Perimeter: 16

Question 2¶

Write a function to check if a number is even or not. The function should indicate to the user even or odd.¶
In [13]:
def is_even(number):
    if number % 2 == 0:
        return "Even"
    else:
        return "Odd"
In [14]:
# Call the function with a specific number
number = 4
result = is_even(number)

# Print the result
print(f"The number {number} is {result}.")
The number 4 is Even.

Question 3¶

Write a Python function that accepts a string and calculate the number of upper case letters and lower case letters.¶

Sample string: “CUNY sps”

# of upper case characters: 4

# of lower case characters: 3

In [15]:
def count_case_letters(input_string):
    uppercase_count = 0
    lowercase_count = 0
    for char in input_string:
        if char.isupper():
            uppercase_count += 1
        elif char.islower():
            lowercase_count += 1
    return uppercase_count, lowercase_count
In [19]:
# Sample string
sample_string = "CUNY sps"

# Call the function with the sample string
upper_count, lower_count = count_case_letters(sample_string)

# Print the results
print("# of upper case characters:", upper_count)
print("# of lower case characters:", lower_count)
# of upper case characters: 4
# of lower case characters: 3

Quuestion 4¶

Write a Python function to sum all the numbers in a list¶
In [20]:
def sum_list_numbers(numbers):
    total = sum(numbers)
    return total
In [21]:
# Sample list of numbers
numbers_list = [1, 2, 3, 4, 5]

# Call the function with the sample list
result = sum_list_numbers(numbers_list)

# Print the result
print("Sum of numbers:", result)
Sum of numbers: 15

Question 5¶

Create a function that shows an example of global vs local variables.¶
In [22]:
global_variable = 10

def example_function():
    local_variable = 5
    print("Global variable:", global_variable)
    print("Local variable:", local_variable)

example_function()
print("Outside the function, global variable:", global_variable)
# The local_variable is not accessible outside the function.
Global variable: 10
Local variable: 5
Outside the function, global variable: 10

Question 6¶

Write a Python program to create a function that takes one argument, and that argument will be multiplied with an unknown given number.¶
In [23]:
def multiply_with_unknown(value):
    unknown_number = 7  # You can replace this with any desired number
    result = value * unknown_number
    return result
In [24]:
# Call the function with a specific value
value = 5
result = multiply_with_unknown(value)

# Print the result
print(f"{value} multiplied by the unknown number is {result}.")
5 multiplied by the unknown number is 35.