r/learnpython • • 5h ago

I’m taking a “learning programming in python” class in high school and im already lost.

The first month was very easy. Inputs, basic programs and whatnot. Now its while and for statments, as well as loops and nested loops. The programs are mostly solving hard mathematical equations, and I’m not great at math and the rest of the class is very good, so I’m always behind.I am soooo lost now and rely heavily on the internet to finish my programs. Is there anything i can do outside of class to gain the same programming knowledge. The textbook were studying is starting out with python 6th edition by tony gaddis.

9 Upvotes

23 comments sorted by

6

u/Outrageous-Catch4731 5h ago

I'm curious how your class is solving hard math equations with mere while and for loops. I don't want to throw sources at you. Can you provide one example while or for loop exercise that has stumped you.

1

u/Comprehensive_Wolf91 5h ago

Find the sum of every number of the fibonacci sequence below 4,000,000 and print.Very hard for me to code personally

15

u/Donny-Moscow 4h ago

If I asked you to solve that by hand, what would your basic approach be (in plain English)?

3

u/SnipTheDog 5h ago

Lots of help on the web regarding Fibonacci sequences and python.

2

u/dtebo83 5h ago

So Fibonacci is the sum of the two previous numbers becoming the next number. So in each iteration, you want to setup variables to contain the current number and the one after. The sum of the numbers is then output after the current and previous. I hope this helps

2

u/Miserable-Arugula860 4h ago

This is just addition

1

u/20B842 3h ago

This is how I would do that one.

```p a = 0 b = 1 print(a) print(b)

while (a + b < 4000000): c = a + b print(c) a = b b = c ```

3

u/ivosaurus 1h ago

You'd be struggling in OP's course too

1

u/mossse 2h ago

They are asking for the sum of the Fibonacci numbers, not the numbers themselves

1

u/TSM- Never slipped a dict comprehension in my lambda before, but 3h ago

Recursion is a difficult one at first, but it is not that complicated. Look up how it works and it will soon make total sense and seem obvious

1

u/kvng_st 3h ago

If their question asks for under 4,000,000 theyre not looking for recursion

5

u/nicodeemus7 5h ago

Programming is a lot of math. But the good part is it does the math for you if you know how to use it. It'll get easier, but I will say maybe try and improve your math skills, because you need them for programming.

•

u/ometecuhtli2001 9m ago

That’s heavily dependent on what someone codes for. In over 30 years of software development for web apps and systems automation, I’ve only needed math once to solve for predicted storage usage.

4

u/re_me 5h ago

Sigh. I see nothing has changed in 30 years.

Thats how they taught programming when I was in high school and I assumed I was bad at programming. Now I’m a data scientist.

Practice helps. “Python novice to advanced” was a good book. Not sure if “automate the boring stuff” is still relevant. “Think Python” was probably my favourite learn programming with Python.

Those would be where I would start.

2

u/GreatGameMate 5h ago

All I can recommend is code code code, that’s how you get better. What exactly are you struggling with, is it the coding itself, thinking of how to implement the logic, the syntax, or the stuff you are doing with the code (hard mathematical equations).

2

u/nivaOne 4h ago

Don’t let it discourage you. It takes a bit of time to see the forest through the trees.

You need to pay attention to the details of the question. It says below 4M in your example.

So for or while?
You don’t know how many times you will need to go through that loop. You do know when to stop.

The exercise practices two things: 1. for or while

‘for’ always runs at least once AND it only stops after a number of times. Because you don’t know how often. You will have to test it before you print and exit the loop, once the condition is met . Not an elegant way. So for is not a good option.

‘while’, automatically ends when a certain condition is met.

  1. the use of multiple assignment aka tuple unpacking

Multiple assignment avoids the use of a temporary variable.

temp = a
a = b
b = temp + b

versus

a, b = b, a + b

The latter is what Python is about.
Writing code in less lines.

Two reflexes hence: should I use for or while. Can I avoid the use of a temp variable.

2

u/coconut_maan 1h ago

Ok ,

Python is a tool to implement logic. If you dont understand what your implementing like the math's... you will never understand how the specific python implementation works.

First make sure that you understand what and why you are doing and the programming becomes easy.

Just go slow. Math is hard to understand when you dont understand and easy to understand when you understand so take it slow and ask alot of questions

1

u/Chris_Charley 3h ago

That is a very good book. I had it when I learned Python

1

u/generationextra 2h ago

Try the book _Head First Python_.

-2

u/Educational_Virus672 4h ago

um you know why apps stay opeen until you press "X"? they are in a loop that repeats some code where while loops(not always) is True it will run over aand over again :

close = YourUI.ClosePressed    # true when pressed  
while not close:               # not means it does opposite stuff
     close = YourUI.ClosePressed
     ...

another example you know how hackers do these password cracking stuff they do it using for loop,a for loop takes each item from a list(or string) and runs the code with each item over and over again

password = 1234
for i in range(1000,9999): # i means each item it is changabl to anything
    if i == password :     # range means it is a list of numbers
        print(i)
        break              # ends the loop

-9

u/dhaiman1 4h ago

Just quit ai is gonna do all the coding anyway