From 53d7ce026b347a86dcf601ef74ea684835aae318 Mon Sep 17 00:00:00 2001 From: mciorast Date: Fri, 12 Sep 2025 18:48:11 +0300 Subject: [PATCH] adding some of the chapter4 exercices --- chapter4-exercices/chapter4_01_ex.py | 9 +++++++ chapter4-exercices/chapter4_02_ex.py | 18 +++++++++++++ chapter4-exercices/chapter4_03_ex.py | 38 ++++++++++++++++++++++++++++ chapter4-exercices/chapter4_04_ex.py | 24 ++++++++++++++++++ chapter4-exercices/chapter4_05_ex.py | 35 +++++++++++++++++++++++++ chapter4-exercices/chapter4_06_ex.py | 12 +++++++++ chapter4-exercices/chapter4_1_ex.py | 4 +++ chapter4-exercices/chapter4_2_ex.py | 2 ++ chapter4-exercices/chapter4_3_ex.py | 7 +++++ chapter4-exercices/chapter4_5_ex.py | 16 ++++++++++++ chapter4-exercices/chapter4_6_ex.py | 25 ++++++++++++++++++ chapter4-exercices/chapter4_7_ex.py | 16 ++++++++++++ chapter4-exercices/chapter4_8_ex.py | 3 +++ 13 files changed, 209 insertions(+) create mode 100755 chapter4-exercices/chapter4_01_ex.py create mode 100755 chapter4-exercices/chapter4_02_ex.py create mode 100755 chapter4-exercices/chapter4_03_ex.py create mode 100755 chapter4-exercices/chapter4_04_ex.py create mode 100755 chapter4-exercices/chapter4_05_ex.py create mode 100755 chapter4-exercices/chapter4_06_ex.py create mode 100644 chapter4-exercices/chapter4_1_ex.py create mode 100644 chapter4-exercices/chapter4_2_ex.py create mode 100644 chapter4-exercices/chapter4_3_ex.py create mode 100755 chapter4-exercices/chapter4_5_ex.py create mode 100755 chapter4-exercices/chapter4_6_ex.py create mode 100755 chapter4-exercices/chapter4_7_ex.py create mode 100644 chapter4-exercices/chapter4_8_ex.py diff --git a/chapter4-exercices/chapter4_01_ex.py b/chapter4-exercices/chapter4_01_ex.py new file mode 100755 index 0000000..9a91581 --- /dev/null +++ b/chapter4-exercices/chapter4_01_ex.py @@ -0,0 +1,9 @@ +""" Calculate the square of number. """ + +number = float(input("Enter a number: ")) +number_expo = float(input("Enter a number as exponent: ")) + +def raising(x, y): + return x ** y + +print(f'{raising(number, number_expo)} is the result from {number} raised by {number_expo}.') \ No newline at end of file diff --git a/chapter4-exercices/chapter4_02_ex.py b/chapter4-exercices/chapter4_02_ex.py new file mode 100755 index 0000000..5bcb8a0 --- /dev/null +++ b/chapter4-exercices/chapter4_02_ex.py @@ -0,0 +1,18 @@ +""" Let’s define a maximum function that determines and returns the + largest of three values—the following session calls the function three times with integers, + floating-point numbers and strings, respectively. """ + +value1 = float(input("Enter the first value: ")) +value2 = float(input("Enter the second value: ")) +value3 = float(input("Enter the third value: ")) + + +def largest_three(value1, value2, value3): + max_value = value1 + if value2 > max_value: + max_value = value2 + if value3 > max_value: + max_value = value3 + return max_value + +print(f'largest value of {value1}, {value2}, and {value3} is {largest_three(value1, value2, value3)}') diff --git a/chapter4-exercices/chapter4_03_ex.py b/chapter4-exercices/chapter4_03_ex.py new file mode 100755 index 0000000..8c7b6a7 --- /dev/null +++ b/chapter4-exercices/chapter4_03_ex.py @@ -0,0 +1,38 @@ +""" Let’s produce 6 mil random integers in the range 1–6 to simulate rolling a six-sided die: """ + +import random + +def roll_normal_dice(): + roll_die_list = [] + frequency_1 = 0 + frequency_2 = 0 + frequency_3 = 0 + frequency_4 = 0 + frequency_5 = 0 + frequency_6 = 0 + for roll in range(60_000_000): + n = random.randrange(1, 7) + roll_die_list.append(n) + if n == 1: + frequency_1 += 1 + elif n == 2: + frequency_2 += 1 + elif n == 3: + frequency_3 += 1 + elif n == 4: + frequency_4 += 1 + elif n == 5: + frequency_5 += 1 + elif n == 6: + frequency_6 += 1 + print(f'Face{"Frequency":>13}') + print(f'{1:>4}{frequency_1:>13}') + print(f'{2:>4}{frequency_2:>13}') + print(f'{3:>4}{frequency_3:>13}') + print(f'{4:>4}{frequency_4:>13}') + print(f'{5:>4}{frequency_5:>13}') + print(f'{6:>4}{frequency_6:>13}') + + return + +roll_normal_dice() \ No newline at end of file diff --git a/chapter4-exercices/chapter4_04_ex.py b/chapter4-exercices/chapter4_04_ex.py new file mode 100755 index 0000000..a66ace5 --- /dev/null +++ b/chapter4-exercices/chapter4_04_ex.py @@ -0,0 +1,24 @@ +""" Requirements statement: + Use a for statement, randrange and a conditional expression (introduced in the preceding chapter) + to simulate 20 coin flips, displaying H for heads and T for tails all on the same line, each separated by a space. """ + +import random + +def heads_tails(): + result = [] + for _ in range(20): + roll = random.randrange(0, 2) + if roll == 0: + result.append("H") + elif roll == 1: + result.append("T") + return print(result) + +def heads_tails_2(): + for _ in range(20): + print('H' if random.randrange(2) == 0 else 'T', end=' ') + return print() + +heads_tails() +heads_tails_2() + diff --git a/chapter4-exercices/chapter4_05_ex.py b/chapter4-exercices/chapter4_05_ex.py new file mode 100755 index 0000000..61ab652 --- /dev/null +++ b/chapter4-exercices/chapter4_05_ex.py @@ -0,0 +1,35 @@ +""" Create a function named calculate_product that receives an arbitrary argument list + and returns the product of all the arguments. Call the function with the + arguments 10, 20 and 30, then with the sequence of integers produced by range(1, 6, 2). """ + +import math + + + +def calculate_product(*prod): + """ calculate the product of all the arguments. """ + return math.prod(prod) + +def calculate_product_2(*args): + """ calculate the product of all the arguments. """ + product_2 = 1 + for arg in args: + product_2 *= arg + return product_2 + +def calculate_product_3(*args): + """ calculate the product of all the arguments. """ + product_3 = 1 + for arg in range(1, 6, 2): + product_3 *= arg + return product_3 + +def calculate_product_4(*args): + """ calculate the product of all the arguments. """ + return math.prod(args) + +print(calculate_product(10, 20, 30)) +print(calculate_product_2(10, 20, 30)) +print(calculate_product_3()) +print(calculate_product_4(*range(1, 6, 2))) + diff --git a/chapter4-exercices/chapter4_06_ex.py b/chapter4-exercices/chapter4_06_ex.py new file mode 100755 index 0000000..c6e3527 --- /dev/null +++ b/chapter4-exercices/chapter4_06_ex.py @@ -0,0 +1,12 @@ +""" Import the decimal module with the shorthand name dec, then create a Decimal object with the value 2.5 and square its value. """ + +import decimal as dec + +Decimal = dec.Decimal(2.5) +print(Decimal ** 2) + + + + + + diff --git a/chapter4-exercices/chapter4_1_ex.py b/chapter4-exercices/chapter4_1_ex.py new file mode 100644 index 0000000..4b2b3e9 --- /dev/null +++ b/chapter4-exercices/chapter4_1_ex.py @@ -0,0 +1,4 @@ +""" 4.1 (Discussion: else Clause) In the script of Fig4.1. , we did not include an else clause in the if…elif statement. + What are the possible consequences of this choice? + Answer: the ELSE was included in all the elif statements + - Use if-else statements when the alternatives are mutually exclusive. This means that if one alternative is true, the other alternatives must be false. """ \ No newline at end of file diff --git a/chapter4-exercices/chapter4_2_ex.py b/chapter4-exercices/chapter4_2_ex.py new file mode 100644 index 0000000..d1808e1 --- /dev/null +++ b/chapter4-exercices/chapter4_2_ex.py @@ -0,0 +1,2 @@ +""" 4.2 (Discussion: Function-Call Stack) What happens if you keep pushing onto a stack, without enough popping? + Answer: Stack overflow; memory consumption, right """ \ No newline at end of file diff --git a/chapter4-exercices/chapter4_3_ex.py b/chapter4-exercices/chapter4_3_ex.py new file mode 100644 index 0000000..a0095c5 --- /dev/null +++ b/chapter4-exercices/chapter4_3_ex.py @@ -0,0 +1,7 @@ +""" (What’s Wrong with This Code?) What is wrong with the following cube function’s definition? + + + def cube(x): + Calculate the cube of x. + x ** 3 + print('The cube of 2 is', cube(2)) """ \ No newline at end of file diff --git a/chapter4-exercices/chapter4_5_ex.py b/chapter4-exercices/chapter4_5_ex.py new file mode 100755 index 0000000..dba2779 --- /dev/null +++ b/chapter4-exercices/chapter4_5_ex.py @@ -0,0 +1,16 @@ +""" Replace the ***s in the seconds_since_midnight function so that it returns the number of seconds since midnight. + The function should receive three integers representing the current time of day. + Assume that the hour is a value from 0 (midnight) through 23 (11 PM) and that the minute and second are values from 0 to 59. + Test your function with actual times. + For example, if you call the function for 1:30:45 PM by passing 13, 30 and 45, the function should return 48645. """ + +def seconds_since_midnight(*args): + hour_in_seconds = args[0] * 3600 + minute_in_seconds = args[1] * 60 + if args[3] == 'PM': + hour_in_seconds = 12 * 3600 + (args[0] * 3600) + else: + hour_in_seconds = args[0] * 3600 + return print(hour_in_seconds + minute_in_seconds + args[2]) + +seconds_since_midnight(1, 30, 45, 'PM') diff --git a/chapter4-exercices/chapter4_6_ex.py b/chapter4-exercices/chapter4_6_ex.py new file mode 100755 index 0000000..3e11a55 --- /dev/null +++ b/chapter4-exercices/chapter4_6_ex.py @@ -0,0 +1,25 @@ +""" (Modified average Function) + The average function we defined in Section 4.11 can receive any number of arguments. + If you call it with no arguments, however, the function causes a ZeroDivisionError. + Reimplement average to receive one required argument and the arbitrary argument list argument *args, and update its calculation accordingly. + Test your function. + The function will always require at least one argument, so you’ll no longer be able to get a ZeroDivisionError. + When you call average with no arguments, Python should issue a TypeError indicating "average() missing 1 required positional argument. """ + +def average(*args): + if len(args) == None: + args = [] + number_of_args = int(input("How many arguments would you like to average? ")) + for value in range(number_of_args): + value = float(input(f"Enter the {value + 1} number for adding to the list of arguments so we can calculate the average value of that list: ")) + args.append(value) + else: + args = [] + number_of_args = int(input("How many arguments would you like to average? ")) + for value in range(number_of_args): + value = float(input(f"Enter the {value + 1} number for adding to the list of arguments so we can calculate the average value of that list: ")) + args.append(value) + return print(f'The average of the following numbers {args} is {sum(args) / len(args)}') + +average() + diff --git a/chapter4-exercices/chapter4_7_ex.py b/chapter4-exercices/chapter4_7_ex.py new file mode 100755 index 0000000..bb15a3c --- /dev/null +++ b/chapter4-exercices/chapter4_7_ex.py @@ -0,0 +1,16 @@ +""" (Date and Time) + Python’s datetime module contains a datetime type with a method today that returns the current date and time as a datetime object. + Write a parameterless date_and_time function containing the following statement, then call that function to display the current date and time: + + print(datetime.datetime.today()) + + On our system, the date and time display in the following format: + + 2018-06-08 13:04:19.214180 """ + +import datetime + +def date_and_time(): + return print(datetime.datetime.today()) + +date_and_time() diff --git a/chapter4-exercices/chapter4_8_ex.py b/chapter4-exercices/chapter4_8_ex.py new file mode 100644 index 0000000..df4e012 --- /dev/null +++ b/chapter4-exercices/chapter4_8_ex.py @@ -0,0 +1,3 @@ +""" (Rounding Numbers) Investigate built-in function round at https://docs.python.org/3/library/functions.html#round + then use it to round the float value 13.56449 to the nearest integer, tenths, hundredths and thousandths positions. """ +