2. Kurs Python 3 - operatory matematyczne, konwersja typów
Table of Contents
Introduction
This tutorial provides a comprehensive guide on mathematical operators and type conversion in Python 3. Understanding these concepts is crucial for effective programming as they allow you to perform calculations and manipulate data types efficiently.
Step 1: Understanding Mathematical Operators
In Python, mathematical operators are used to perform arithmetic operations. Here are the basic operators you should know:
- Addition (
+
): Adds two numbers. - Subtraction (
-
): Subtracts one number from another. - Multiplication (
*
): Multiplies two numbers. - Division (
/
): Divides one number by another, yielding a float. - Floor Division (
//
): Divides and returns the largest integer value. - Modulus (
%
): Returns the remainder of a division operation. - Exponentiation (
**
): Raises a number to the power of another.
Practical Example
To see these operators in action, you can run the following code:
a = 10
b = 3
print("Addition:", a + b) # 13
print("Subtraction:", a - b) # 7
print("Multiplication:", a * b) # 30
print("Division:", a / b) # 3.3333
print("Floor Division:", a // b) # 3
print("Modulus:", a % b) # 1
print("Exponentiation:", a ** b) # 1000
Step 2: Type Conversion in Python
Type conversion is crucial when you need to ensure that operations are performed on compatible data types. In Python, you can convert between different types using built-in functions:
- int(): Converts a value to an integer.
- float(): Converts a value to a floating-point number.
- str(): Converts a value to a string.
Practical Example
Here's how you can use these functions:
# Converting string to integer
num_str = "5"
num_int = int(num_str)
print("String to Integer:", num_int) # 5
# Converting integer to float
num_float = float(num_int)
print("Integer to Float:", num_float) # 5.0
# Converting float to string
num_float_str = str(num_float)
print("Float to String:", num_float_str) # "5.0"
Step 3: Combining Operators and Type Conversion
Often, you'll need to combine mathematical operations with type conversion. For instance, if you take input from a user, it will usually be in string format and you'll need to convert it before performing calculations.
Practical Example
Consider the following code snippet:
# User input
user_input = input("Enter a number: ") # Assume user enters "10.5"
# Convert input to float and perform an operation
result = float(user_input) + 5
print("Result after addition:", result) # 15.5
Conclusion
In this tutorial, we covered the essential mathematical operators and type conversion in Python 3. Understanding these fundamental concepts will enhance your ability to work with numerical data and perform calculations effectively.
Next Steps
- Experiment with different mathematical operations and type conversions in your own code.
- Explore more advanced topics such as error handling during type conversion.
- Consider practicing with user inputs to solidify your understanding of these concepts.