Customize Consent Preferences

We use cookies to help you navigate efficiently and perform certain functions. You will find detailed information about all cookies under each consent category below.

The cookies that are categorized as "Necessary" are stored on your browser as they are essential for enabling the basic functionalities of the site. ... 

Always Active

Necessary cookies are required to enable the basic features of this site, such as providing secure log-in or adjusting your consent preferences. These cookies do not store any personally identifiable data.

No cookies to display.

Functional cookies help perform certain functionalities like sharing the content of the website on social media platforms, collecting feedback, and other third-party features.

No cookies to display.

Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics such as the number of visitors, bounce rate, traffic source, etc.

No cookies to display.

Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.

No cookies to display.

Advertisement cookies are used to provide visitors with customized advertisements based on the pages you visited previously and to analyze the effectiveness of the ad campaigns.

No cookies to display.

1.2 Το module math στην Python

Η Python διαθέτει μια σειρά από ενσωματωμένες μαθηματικές συναρτήσεις, συμπεριλαμβανομένου ενός εκτεταμένου math module, το οποίο σας επιτρέπει να εκτελείτε μαθηματικές εργασίες σε αριθμούς.

Ενσωματωμένες μαθηματικές συναρτήσεις:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
x = 5
y = 3
print("Addition:", x + y)
print("Subtraction:", x - y)
print("Multiplication:", x * y)
print("Division:", x / y)
print("Modulus:", x % y)
print("Exponentiation:", x ** y)
print("Floor division:", x // y)
x = 5 y = 3 print("Addition:", x + y) print("Subtraction:", x - y) print("Multiplication:", x * y) print("Division:", x / y) print("Modulus:", x % y) print("Exponentiation:", x ** y) print("Floor division:", x // y)
x = 5
y = 3

print("Addition:", x + y)
print("Subtraction:", x - y)
print("Multiplication:", x * y)
print("Division:", x / y)
print("Modulus:", x % y)
print("Exponentiation:", x ** y)
print("Floor division:", x // y)

Επιπρόσθετα, το math module παρέχει πολλές προχωρημένες μαθηματικές συναρτήσεις και σταθερές:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
import math
print("Square root:", math.sqrt(25))
print("Absolute value:", math.fabs(-7))
print("Rounding up:", math.ceil(4.3))
print("Rounding down:", math.floor(4.7))
print("Hyperbolic sine:", math.sinh(1))
print("Circle circumference ratio:", math.tau)
print("Euler's constant:", math.e)
import math print("Square root:", math.sqrt(25)) print("Absolute value:", math.fabs(-7)) print("Rounding up:", math.ceil(4.3)) print("Rounding down:", math.floor(4.7)) print("Hyperbolic sine:", math.sinh(1)) print("Circle circumference ratio:", math.tau) print("Euler's constant:", math.e)
import math

print("Square root:", math.sqrt(25))
print("Absolute value:", math.fabs(-7))
print("Rounding up:", math.ceil(4.3))
print("Rounding down:", math.floor(4.7))
print("Hyperbolic sine:", math.sinh(1))
print("Circle circumference ratio:", math.tau)
print("Euler's constant:", math.e)

Αυτά είναι μόνο μερικά παραδείγματα των δυνατοτήτων της Python και του math module για την εκτέλεση μαθηματικών εργασιών.
Οι συναρτήσεις min() και max() μπορούν να χρησιμοποιηθούν για να βρουν τη χαμηλότερη ή την υψηλότερη τιμή σε μια επαναληπτική δομή:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
numbers = [12, 4, 9, 2, 17, 25]
print("Minimum value:", min(numbers))
print("Maximum value:", max(numbers))
numbers = [12, 4, 9, 2, 17, 25] print("Minimum value:", min(numbers)) print("Maximum value:", max(numbers))
numbers = [12, 4, 9, 2, 17, 25]

print("Minimum value:", min(numbers))
print("Maximum value:", max(numbers))

[adinserter block=”2″]

Επίσης, οι συναρτήσεις abs() και pow() μπορούν να χρησιμοποιηθούν για να υπολογίσουν την απόλυτη τιμή ενός αριθμού και τον αριθμό υψωμένο σε μια δεδομένη δύναμη, αντίστοιχα:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
x = -7
y = 2
z = 3
print("Absolute value:", abs(x))
print("Power:", pow(y, z))
x = -7 y = 2 z = 3 print("Absolute value:", abs(x)) print("Power:", pow(y, z))
x = -7
y = 2
z = 3

print("Absolute value:", abs(x))
print("Power:", pow(y, z))

Αυτές οι συναρτήσεις είναι μόνο μερικά παραδείγματα των ενσωματωμένων μαθηματικών συναρτήσεων που παρέχει η Python.

Η συνάρτηση abs() επιστρέφει την απόλυτη (θετική) τιμή του καθορισμένου αριθμού:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
number = -42
result = abs(number)
print("The absolute value of the number is:", result)
number = -42 result = abs(number) print("The absolute value of the number is:", result)
number = -42
result = abs(number)

print("The absolute value of the number is:", result)

Επιπλέον, η συνάρτηση round() μπορεί να χρησιμοποιηθεί για να στρογγυλοποιήσει έναν αριθμό στον κοντινότερο ακέραιο. Εάν προσφέρεται ένας δεύτερος παράγοντας, η συνάρτηση θα επιστρέψει τον αριθμό στρογγυλοποιημένο στον κοντινότερο δεκαδικό ψηφίο που καθορίζεται από τον δεύτερο παράγοντα:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
number1 = 3.14159
number2 = 8.52764
print("Rounding to the nearest integer:", round(number1))
print("Rounding to two decimal places:", round(number2, 2))
number1 = 3.14159 number2 = 8.52764 print("Rounding to the nearest integer:", round(number1)) print("Rounding to two decimal places:", round(number2, 2))
number1 = 3.14159
number2 = 8.52764

print("Rounding to the nearest integer:", round(number1))
print("Rounding to two decimal places:", round(number2, 2))

Με αυτές τις ενσωματωμένες συναρτήσεις, μπορείτε να εκτελέσετε βασικές μαθηματικές πράξεις και να προσαρμόσετε τις τιμές των αριθμών για τις ανάγκες του προγράμματός σας.

Η συνάρτηση pow(x, y) επιστρέφει την τιμή του x στη δύναμη του y (x^y).

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
x = 4
y = 3
result = pow(x, y)
print("The value of x to the power of y is:", result)
x = 4 y = 3 result = pow(x, y) print("The value of x to the power of y is:", result)
x = 4
y = 3

result = pow(x, y)
print("The value of x to the power of y is:", result)

Σε αυτό το παράδειγμα, η συνάρτηση pow() υπολογίζει την τιμή του 4 στην τρίτη δύναμη (4^3), που ισούται με 64. Μπορείτε να χρησιμοποιήσετε αυτήν τη συνάρτηση για να υπολογίσετε πολύπλοκες εκφράσεις και να καθορίσετε τη σχέση μεταξύ διαφόρων μεταβλητών στο πρόγραμμά σας.

Το Python διαθέτει επίσης ένα ενσωματωμένο module με το όνομα math, το οποίο επεκτείνει τη λίστα των μαθηματικών συναρτήσεων.

[adinserter block=”3″]

Για να το χρησιμοποιήσετε, πρέπει να εισάγετε το math module:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
import math
import math
import math

Μετά την εισαγωγή του module, μπορείτε να χρησιμοποιήσετε διάφορες μαθηματικές συναρτήσεις που παρέχει, όπως η sqrt() για τον υπολογισμό της τετραγωνικής ρίζας ενός αριθμού, η sin() και η cos() για τον υπολογισμό των τριγωνομετρικών συναρτήσεων, και πολλές άλλες.

Για παράδειγμα:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
import math
number = 9
square_root = math.sqrt(number)
print("The square root of the number is:", square_root)
import math number = 9 square_root = math.sqrt(number) print("The square root of the number is:", square_root)
import math

number = 9
square_root = math.sqrt(number)

print("The square root of the number is:", square_root)

Σε αυτό το παράδειγμα, η συνάρτηση sqrt() του math module χρησιμοποιείται για να υπολογίσει την τετραγωνική ρίζα του 9, που είναι 3.

Η μέθοδος math.ceil() στρογγυλοποιεί έναν αριθμό προς τα πάνω στον κοντινότερο ακέραιο, ενώ η μέθοδος math.floor() στρογγυλοποιεί έναν αριθμό προς τα κάτω στον κοντινότερο ακέραιο και επιστρέφει το αποτέλεσμα:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
import math
number = 3.7
ceil_result = math.ceil(number)
floor_result = math.floor(number)
print("Rounded up to the nearest integer:", ceil_result)
print("Rounded down to the nearest integer:", floor_result)
import math number = 3.7 ceil_result = math.ceil(number) floor_result = math.floor(number) print("Rounded up to the nearest integer:", ceil_result) print("Rounded down to the nearest integer:", floor_result)
import math

number = 3.7

ceil_result = math.ceil(number)
floor_result = math.floor(number)

print("Rounded up to the nearest integer:", ceil_result)
print("Rounded down to the nearest integer:", floor_result)

Σε αυτό το παράδειγμα, η συνάρτηση math.ceil() στρογγυλοποιεί τον αριθμό 3.7 προς τα πάνω στον κοντινότερο ακέραιο, που είναι 4. Η συνάρτηση math.floor() στρογγυλοποιεί τον ίδιο αριθμό προς τα κάτω στον κοντινότερο ακέραιο, που είναι 3.

Δωρεα μεσω Paypal

Για την κάλυψη αναγκών φιλοξενίας και δημιουργίας περιεχομένου.

κατηγοριες μαθηματων

Ιστορικο ενοτητων

top
error: Content is protected !!
Μετάβαση σε γραμμή εργαλείων