Python- Day8- Numbers in Python
Numbers in Python
Used frequently in programming for various tasks like scoring in games, data representation, and information storage.
Integers:
- Basic operations: add (+), subtract (-), multiply (*), divide (/).
>>> 2 + 3
5
>>> 3 - 2
1
>>> 2 * 3
6
>>> 3 / 2
1.5
- Exponents: use (**) for power calculations.
>>> 3 ** 2
9
>>> 3 ** 3
27
>>> 10 ** 6
1000000
- Order of operations: follows standard math rules; parentheses can alter the order.
>>> 2 + 3*4
14
>>> (2 + 3) * 4
20
Floats:
- Numbers with a decimal point.
>>> 0.1 + 0.1
0.2
>>> 0.2 + 0.2
0.4
>>> 2 * 0.1
0.2
>>> 2 * 0.2
0.4
- Occasionally, calculations may produce extra decimal places due to internal representation.
>>> 0.2 + 0.1
0.30000000000000004
>>> 3 * 0.1
0.30000000000000004
Mixing Integers and Floats:
- Division always results in a float.
>>> 4/2
2.0
- Mixing integers and floats in other operations also results in a float.
>>> 1 + 2.0
3.0
>>> 2 * 3.0
6.0
>>> 3.0 ** 2
9.0
Underscores in Numbers:
- Used to group digits for readability (e.g., 1_000_000).
>>> universe_age = 14_000_000_000
- Python ignores underscores when storing and processing these values.
>>> print(universe_age)
14000000000
Multiple Assignment:
- Assign values to multiple variables in a single line using commas.
x, y, z = 0, 0, 0
- The number of variables must match the number of values.
Constants:
- Variables intended to remain unchanged throughout a program.
- Indicated by using all capital letters (e.g.,
MAX_CONNECTIONS = 5000
). - Python does not have built-in constants, but this convention is commonly used.
Comments :
Useful for adding notes and explanations within your code.
Helps describe the overall approach to solving problems, making code easier to understand.
Writing Comments:
- Use the hash mark (
#
) to start a comment. - Everything after
#
is ignored by the Python interpreter.
# Say hello to everyone.
print("Hello Python people!")
Python ignores the first line and executes the second line.
Hello Python people!
Aim for simplicity in your code whenever possible; avoid unnecessary complexity.
Write code that works; perfection can come later, or you can move on to new projects.
If you found this guide helpful then do click on 👏 the button.
Follow for more Learning like this 😊
If there’s a specific topic you’re curious about, feel free to drop a personal note or comment. I’m here to help you explore whatever interests you!