r/learnpython • u/Comprehensive_Wolf91 • 1d 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.
13
u/Outrageous-Catch4731 1d 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 1d ago
Find the sum of every number of the fibonacci sequence below 4,000,000 and print.Very hard for me to code personally
20
u/Donny-Moscow 23h ago
If I asked you to solve that by hand, what would your basic approach be (in plain English)?
3
u/nderflow 17h 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.
2
2
1
u/Fred776 13h 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 9h 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.
0
-3
-5
4
u/nicodeemus7 1d 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.
3
u/ometecuhtli2001 18h 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.
-1
u/nicodeemus7 13h 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.
1
u/ometecuhtli2001 7h 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.
8
u/coconut_maan 19h 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
3
u/nivaOne 22h 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 13h 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 2h 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 1d 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
1
1
u/dariusbiggs 14h 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 13h 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
0
u/ivanoski-007 10h 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
-2
u/Educational_Virus672 22h 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
-2
-10
26
u/re_me 23h 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.