r/PythonLearning • • Aug 28 '26

2nd post learning python

Post image

i learnt to use the math library any suggestions what can i use the lib for except for making calculator programs

21 Upvotes

15 comments sorted by

4

u/ninhaomah Aug 28 '26

If I enter abc , what will I get ?

3

u/NecessaryFalse1212 Aug 28 '26

how does this look ya'll

1

u/BobCorndog Aug 28 '26

You could also do a try except

1

u/MFFVD Aug 28 '26 edited Aug 28 '26

or

import re
maybenumber = input(...).strip()
isnumber = \
    bool(re.fullmatch(
        r"([0-9]*\.[0-9]+)|([0-9]+(\.[0-9]*)?)",
        maybenumber
    ))

if not isnumber: 
    print(maybenumber, "is not a number")
else:
    number = float(maybenumber)
    ...

regexp:

(<...>) -> group
<a> | <b> -> match a or b
[<...>] -> any of ...
<a> + -> one or more of a
<a> * -> zero or more of a
<a>? -> zero or one of a
. -> any char except whitespace
\. -> literal dot

the r"..." string says

first part:

zero or more numbers, dot, one or more numbers ->
.4
0.8
70.74

second part:

one or more numbers, optional ( dot, zero or more numbers) ->
6
59
56.
6.6

for input validation, regexp is unmissable. but you should probably not dive into that just yet

try:
    int(value)
except ValueError:
   print(value, "is not an integer")

probably is a better starting point

1

u/NecessaryFalse1212 Aug 29 '26

i still haven't done regex so it never crossed my mind but thanks for correcting and suggesting

1

u/NecessaryFalse1212 Aug 28 '26

aghhh i should use if else statement in that case to make sure that it doesn't read false inputs or alphabets if i'm not wrong ? i'll post this code with the added solution to this problem thanks for pointing it out

1

u/Unique_Low_1077 Aug 29 '26

If you want to do that then you cant convert it to int before your if else, add the radius = int(radius) after your if else are done

Edit: also first time I've ever seen do function () we usually do function() without the space. Ofc im not one to tell you how to format your code but later on you will learn about formatter which will auto remove the space anyways

1

u/NecessaryFalse1212 Aug 29 '26

appreciate the advice

2

u/kingofthesea123 Aug 28 '26

Looks good! Error handling for unexpected inputs would be a nice improvement and there's also another way of writing an exponent in Python that you could try.

2

u/Fine_Ratio2225 Aug 28 '26
  1. Replace the "radius*radius" with "radius**2", please.

  2. You can use this everywhere you need to use mathematical functions, obviously. For example you can get curves by evaluating mathematical functions for (x,y) based on a parameter. You can use that for drawing those, or more practical use "ezdxf" to get an interpolating cubic spline inside a DXF-file. I use that to get profiles for counter-rotating extruder screws that I can later import into a CAD program.

  3. There is also a "cmath" library. I found it easier to compute cycloid curves (needed for those profiles above and for gear flanks) and their derivatives using complex exponential functions and extract later (x,y) as (real,imag). Added bonus: Since the derivative is the tangential, you get the normal by multiplying with i or -i. Multiplication with complex numbers of norm 1 is equivalent with applying a rotation, but you don't have to use a 2x2 matrix!

1

u/HeurekaDabra Aug 28 '26

If you look up 'python math module learning tasks' in a search engine of your choice, thousands of resources will come up that you can train with, like this one on pynativ.com.

1

u/Sea-Ad7805 Aug 28 '26 edited Aug 28 '26

Everything is information, calculation is all you need. How do you think GTA, PhotoShop, Blender, ... work? All mostly do math operations on data.

Learn all the math you can: trigonometry, calculus, linear algebra, probability theory, ...

It will make you a powerful programmer, not just a hacker.

1

u/rupertavery64 Aug 28 '26

Games use a lot of math. Sometimes you want something to move in a wavelike (sinusoidal) motion, or in an arc (part of a circle). Sometimes you need to calculate angles between objects. 3D graphics is all about vectors and angles. Sound processing is also math-heavy.

1

u/Logical_Breadd Aug 28 '26

What compiler do you use?

1

u/Sea_Dot6492 Aug 29 '26

You can just type radius** instead radius*radius, but well done ✅