Created
July 25, 2023 09:22
-
-
Save i2x/7e2f4beb478d247ac10174a73fb93df5 to your computer and use it in GitHub Desktop.
week4
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def calculate_area(length, width): | |
| return length * width | |
| length_input = float(input("Enter the length of the rectangle: ")) | |
| width_input = float(input("\nEnter the width of the rectangle: ")) | |
| area = calculate_area(length_input, width_input) | |
| print("\nThe area of the rectangle is:", area) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def calculator(num1, operator, num2): | |
| if operator == '+': | |
| return num1 + num2 | |
| elif operator == '-': | |
| return num1 - num2 | |
| elif operator == '*': | |
| return num1 * num2 | |
| elif operator == '/': | |
| return num1 / num2 | |
| else: | |
| return "\nInvalid operator" | |
| num1_input = float(input("Enter the first number: ")) | |
| operator_input = input("\nEnter the operator (+, -, *, /): ") | |
| num2_input = float(input("\nEnter the second number: ")) | |
| result = calculator(num1_input, operator_input, num2_input) | |
| print("\nResult:", result) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def find_minimum(num1, num2, num3): | |
| min_number = min(num1, num2, num3) | |
| return min_number | |
| # Receive input from the user | |
| num1 = float(input("Enter the first number:\n")) | |
| num2 = float(input("Enter the second number:\n")) | |
| num3 = float(input("Enter the third number:\n")) | |
| # Call the function to find the minimum of three numbers | |
| result = find_minimum(num1, num2, num3) | |
| print("Minimum:", result) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def print_word(word): | |
| print(word) | |
| text = str(input("Enter a word:\n")) | |
| print_word(text) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def show_employee(name, salary=2000): | |
| print("Name:", name, "salary:", salary) | |
| show_employee("Ryu", 15000) | |
| show_employee("KJ") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def calculation(a, b): | |
| addition = a + b | |
| subtraction = a - b | |
| return addition, subtraction | |
| num1 = float(input("Enter the first number:\n")) | |
| num2 = float(input("Enter the second number:\n")) | |
| result = calculation(num1, num2) | |
| print(result) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def is_leap_year(year): | |
| if year % 4 == 0 and (year % 100 != 0 or year % 400 == 0): | |
| return "\nit's a leap year." | |
| else: | |
| return "\nit's not a leap year." | |
| user_year = int(input("Enter a year: ")) | |
| result = is_leap_year(user_year) | |
| print(result) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def add_numbers(*args): | |
| total_sum = sum(args) | |
| return total_sum | |
| result = add_numbers(10, 15, 20) | |
| print("Sum of numbers:", result) | |
| result2 = add_numbers(5, 7, 12, 3, -20, 1.345, 0, 1, 9) | |
| print("Sum of numbers:", result2) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def print_date(*date): | |
| if len(date) <= 2: | |
| print(f"Year: {date[0]}") | |
| print(f"Month: {date[1]}\n") | |
| else: | |
| print(f"Year: {date[0]}") | |
| print(f"Month: {date[1]}") | |
| print(f"Date: {date[2]}\n") | |
| print_date(2023, 7) | |
| print_date(2023, 7, 24) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| global_variable = 10 | |
| def modify_global_variable(): | |
| global global_variable | |
| global_variable = 20 | |
| print("Before function call: global_variable =", global_variable) | |
| modify_global_variable() | |
| print("After function call: global_variable =", global_variable) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def first_function(): | |
| print("This is the first function.") | |
| second_function() | |
| def second_function(): | |
| print("This is the second function.") | |
| first_function() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| area_rectangle = lambda x, y: x * y | |
| x = float(input("Enter the length of the rectangle:\n")) | |
| y = float(input("Enter the width of the rectangle:\n")) | |
| print(f"The area of the rectangle is: {area_rectangle(x,y)}") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def calculate_sum(numbers): | |
| total_sum = 0 | |
| for num in numbers: | |
| total_sum += num | |
| return total_sum | |
| numbers_list = [1, 2, 3, 4, 5] | |
| sum_of_numbers = calculate_sum(numbers_list) | |
| print("Sum of numbers:", sum_of_numbers) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def find_max(numbers): | |
| max_number = float('-inf') # Initialize max_number to negative infinity | |
| for num in numbers: | |
| if num > max_number: | |
| max_number = num | |
| return max_number | |
| numbers_list = [23, 56, 10, 35, 42, 78] | |
| max_number = find_max(numbers_list) | |
| print("Maximum number:", max_number) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def is_prime_simple(num): | |
| if num <= 1: | |
| return "\nit's not the number prime." | |
| for i in range(2, num): | |
| if num % i == 0: | |
| return "\nit's not the number prime." | |
| return "\nit's the number prime." | |
| user_num = int(input("Enter an integer: ")) | |
| result = is_prime_simple(user_num) | |
| print(result) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment