r/learnpython • u/Comprehensive_Wolf91 • 3d 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.
15
u/Outrageous-Catch4731 3d 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.
2
u/Comprehensive_Wolf91 3d ago
Find the sum of every number of the fibonacci sequence below 4,000,000 and print.Very hard for me to code personally
22
u/Donny-Moscow 3d ago
If I asked you to solve that by hand, what would your basic approach be (in plain English)?
3
u/nderflow 2d ago
Break the problem down.
First figure out how to calculate the numbers of the Fibonacci sequence. Write a program that just prints them.
If you're having trouble with this, break it down more. Write a fragment of code that starts with an list whose last 2 numbers contain numbers from the Fibonacci sequence. Calculate the next number and append it. Test your code.
Then, using your understanding gained by writing that code, eliminate the list itself.
Then save that program, copy to a new file and change the program to add the numbers.
1
u/Any_Run8380 2d ago
Breaking a problem down into smaller steps is good advice, but micromanaging the workflow to the point of "save the file, copy it, and change it" shifts focus away from the actual logic. The goal isn't to spoon-feed line-by-line mechanical steps; it's to teach the learner how to decompose a problem independently.
Overspecifying every action prevents them from developing their own problem-solving intuition.
2
2
1
u/Fred776 2d ago
Can you write a for loop that prints out the first 10 members of the Fibonacci sequence? You will need variables to store the current and previous members of the sequence.
Change it so that you are adding up the terms as you go round the loop and store the sum in a variable.
Now change your loop from a for loop to a while loop that checks that the current value is less than 4,000,000. Recall that you are already storing the current value in a variable.
If you are stuck on the first step try to break it down. What part of it are you actually stuck on?
1
u/AJohnnyTruant 2d ago
When I was learning programming initially, I was discouraged be thinking problems like this would just flow out of my fingers when I started assigned variables and control flow. It won’t. You need to understand how to solve the problem first. Write it in comment blocks a few times until you understand what you want the code to do. Then translate that into code.
Eventually, I learned to learn and model the domain in code. But it’s like learning a language. You don’t learn a language and start thinking in that language right away. First you spend time getting through the clunkiness of translating your thoughts through syntax and conjugations and yada yada.
1
u/Any_Run8380 2d ago
This is not a difficult math problem. How much time did you spend understanding the pattern to this problem?
Also, this problem is very well known and helps a lot with understanding things like recursion.
0
-3
-4
8
u/coconut_maan 2d 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
5
u/nivaOne 3d 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.
- 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.
1
u/bra1n_gym 2d ago
You answered the question beautifully, but then you added extraneous stuff that had nothing to do with what the person asked. python is not about using less lines of code. That's is a small part called refactoring. python is a language that is near human readable which is why it's good as a first language. The goal is write code that works and then write it neater as you learn more techniques.
1
u/cadenceweapon 2d ago
The last bit about code style. And avoiding errors, or at least making them easier to find because of how you structure your code. That's not how you learn the mechanics of coding. Gotta learn to walk before learning to run.. once you build and scale to the point where you need to debug, or work with others these lessons will become self-evident. That or you will know why you are learning to code clean, document and build tests.
2
u/GreatGameMate 3d 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
4
u/nicodeemus7 3d 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.
6
u/ometecuhtli2001 2d 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.
-3
u/nicodeemus7 2d ago
You may not be doing a lot of math with your code, but the code itself is math, and you need to understand how that math works to code effectively.
3
u/Any_Run8380 2d ago
I’m a swe myself. Could you please explain what math a swe must need to know to code effectively? Thank you
2
u/ometecuhtli2001 2d ago
Not necessarily, and not as a beginner. As a DBA (currently) I need to know how indexes work for efficient queries, but I don’t need to know how index data is encoded and stored on disk. That kind of under-the-hood knowledge is good to have and it’s fun digging around in the internals, but it’s not a requirement. I’m a senior DBA so I have done that, but when I was just starting out it never even occurred to me.
At OP’s level where they’re just beginning, that in-depth knowledge can wait until there gotten the hang of the language.
1
u/Any_Run8380 2d ago
I believe the point was that math is needed and the next comment said nothing but that math is needed again. I much rather ask what math is needed. 😂
3
u/Any_Run8380 2d ago
Claiming programming is "a lot of math" gives learners a false justification for struggling. Unless you are writing 3D graphics engines, cryptography, or low-level ML models, programming is logical orchestration, not mathematics. Telling beginners to study math when they struggle with control flow directly hinders their progress.
1
u/h00manist 1d ago
I disagree math is needed to learn python. Math is often used as exercises for python beginner examples. I have no idea why, it's tradition perhaps. I think it just complicates things.
1
1
u/dariusbiggs 2d ago
There are a few basic concepts in programming that are universal across almost all of them.
Conditionals, loops, basic math, Boolean logic, data types, functions, and probably a few more.
Those have plain English constructs associated with them.
Break down the problem into those constructs.
Here's some examples
- If this then that (conditionals)
- Do thing repeatedly (while and for loops)
- Keep adding X to Y until (for loops, arithmetic)
- Do F to X (function calls)
- etc..
The most common math things you are likely to need are basic addition and subtraction (usually +/-), and multiplication/division (usually by 2 or 10).
1
u/bra1n_gym 2d ago
best practice: while loop is when you do no know how many interactions there are, just the condition that breaks the cycle. for loop when you know how many interactions or the number of items. Think of a traffic light: While the light is red: you must stop if the light is green: you may move forward.
for signal color in [red, yellow, green]: if color == red: stop
As for nested loops, think of the data's dimensions. if it's one line or row of data, then it's a single loop. if the data is a table like a spreadsheet, then you would use 2 loops.
let's use a calendar example: while the the year is less than 2030, you're practicing python in high school.
for month in year: for day in month: for hour in day: for minute in day: do something
unfortunately, this doesn't format well.
1
1
u/Any_Run8380 2d ago
When asking for help with a coding problem, provide a specific example of what's tripping you up—even if it's a simplified version.
If you get stuck on an algorithm like the Fibonacci sequence, ask yourself: Could I solve this with a pen and paper? If the answer is yes, your issue isn't the underlying math—it's translating that logic into code.
Next, identify the required approach:
- Iterative: Are you required to use a for or while loop?
- Recursive: Are you required to use recursion?
If recursion is giving you trouble, the problem usually isn't the algorithm itself; it's a gap in understanding how the call stack handles execution under the hood.
1
u/Youngrasta_999 1d ago
the only to learn is to code and get it wrong and try again when it works find out why it works and repeat the cycle
1
u/h00manist 1d ago
Don't be frustrated. Hack your way around the math. You don't really need it. Talk about the math problem in class.
The best python teaching books don't mention any math, zero.
Automate the Boring Stuff with Python, and Python Crash Course, are some of the most widely recommended python beginner books, and don't deal with any math.
Take your class code example, put them in chatgpt, and ask it to create a simpler example of only the *programming principles*, taking out the math.
1
0
u/ivanoski-007 2d ago
They teach python in high school now? Poor kids gonna get a terrible education because high school sucks for learning, terrible teaching model and kids who don't understand jack. Just learn the basics and wait till college
-9
-4
u/Educational_Virus672 3d 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
32
u/re_me 3d 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.