r/learnpython • • 11h ago

Little fun code I put together:)

0 Upvotes

Edit: I fixed the last line in the program, just had an issue when pasting it and didn't think when I filled it back in. Thank you guys for letting me know!

Hi! So I've been learning code for a little while now and lately, I've been getting more ideas for little programs to put together. They're not really useful in practical sense, but they're still fun exercises and ways to challenge myself while I'm still learning and understanding the basics.

I just thought this one would be fun to share because it took me a second to figure out the best way to have both lists be printed at the same time. Honestly though, I don't know why I didn't think of just doing a function right away though HSDH

The basic idea of the program is to generate a full breakfast by randomly choosing an entree and a side to go with it!

import random

entree_list = ["eggs", "cereal", "waffles", "oatmeal"]

side_dish = ["hashbrowns", "toast", "orange", "banana", "yogurt"]

def breakfast_run():
    print("Get someeeeeee")
    print(random.sample(entree_list, k=1))
    print("and")
    print(random.sample(side_dish, k=1))

breakfast_run()

r/learnpython • • 23h ago

Basically, if main

9 Upvotes

In Python, when you run your program in another folder, we add this, but I don't understand why. What's the point of doing this? If someone could explain it in a simplified way, I'd appreciate it.


r/learnpython • • 42m ago

How to not clobber a class when inheriting

0 Upvotes

I wrote a visitor to extract some datas via the ast

class VariableFinder(ast.NodeTransformer):
    """ Visitor pattern impl."""
    def __init__(self, find_object:str, find_method:str):
        """ 
        filter for only method calls that match find_object.find_method 
        store the line number and store the value or args passed to the call
        """
        self.find_method = find_method
        self.find_object = find_object
        self.found = []
...

And it struck me this is not C++, so how do we stop ourselves accidentally blatting/clobbering over ast.NoteTransformer attributes? I'm missing something and my searches are all indicating that the visitor pattern is frowned upon. Is there an alternative way to do these safely?


r/learnpython • • 20h ago

On preventing the propagation of floating point error

0 Upvotes

TL:DR: how large is the floating point error on an mpmath float compared to the last exact significant digit?

I'm working on a program to calculate the value of various iterated maps.

The problem I was running into was, let's take, for the sake of simplicity a 2x mod 1 map with starting value at 0.4, after just a few iterations the values would deviate from the 0.4, 0.8, 0.6, 0.2, 0.4... cycle due to the spread of floating point error.

Now, I'm saving my data in a hdf5 file which uses numpy float64, which is more than acceptable for my use case as it gives me precision up to the 15th significant figure, so long as the overall behavior of the map is within that margin.

My solution to this was, working with mpmath, to take the input for the starting value as a string, calculate the value for the next iteration after converting in to an mpf, and then to run that into this function:

import mpmath as mp

def floaterrorfix (f):
    return mp.mpf (mp.nstr (f, int (mp.floor (mp.mp.prec * mp.log (2, 10)))))

Using an user submitted number of bits for the precision, also given that the number of digits of precision afforded by a float with b bites of mantissa is d = b*log_10(2), it basically removes the propagated error from the previous iteration and leaves me with the regular error for whatever the value/the number of bits used for storing was.

Now, I haven't noticed any error propagation while running maps for which I know the values (mainly cyclic ones) even millions of iterations in, but I think it should still be possible to get some, especially with more complex maps that involve more calculations per iteration (eg a standard map), all that is needed is for the error pre-iteration to be large enough to exceed into my supposedly "exact" figures after I apply the calculations and, given that I'd be running off of randomised start values and parameters I'd have no way to check if that was the case.

My solution to this would be getting the number of significant digits from the user input (or the 15 for standard float64 in case it has less than that) and set the precision so that, even the largest possible float error, will not exceed into those digits after I apply all calculations; this would also save me that pesky user submitted number of bits.

Before I do all that tho I want to make sure it's actually possible for the error to still propagate or if there are already checks in place within python/mpmath that would prevent this for any reasonable calculation and what the limit on that would be, also if I do need to apply my solution, I was unable to find a way to easily count the number of significant figures on a number stored in a string.


r/learnpython • • 12h ago

Help Help!!!

1 Upvotes

Title: How should I get started with AI, and which direction should I focus on?

I am currently a PhD student in environmental science, but I want to transition into AI. My CS background is still weak, and I am currently learning Python and basic math.

There are so many directions—LLMs, agents, computer vision, robotics, AI for Science, etc.—that I am not sure where to focus.

If you were starting from my position today, what would you learn first, and which AI direction would you recommend focusing on for the next 6–12 months?

My goal is to eventually become capable of doing real AI research or engineering work, not just using AI tools.


r/learnpython • • 21h ago

Stuck at Helsinki mooc.fi exercise

1 Upvotes

I have been doing one of the exercises in the mooc fi course : https://programming-26.mooc.fi

Specifically part 3 (working with strings - 15, the last one in that section), here's the question : https://programming-26.mooc.fi/part-3/2-working-with-strings#programming-exercise-the-second-occurrence

but sadly been days and couldn't get what's wrong with my solution, here it's as well : https://pastebin.com/kmHf6jjm

I tried to check for their discord server at :

https://programming-26.mooc.fi/support-and-assistance

But it keeps redirecting me to student of helsinki's login portal asking for a uni account, is the discord server public or restricted to those who attend the Helsinki University....or maybe I am missing something out

would appreciate the help whether for solving it or for how to access the discord server (or shedding some light on if it's accessible or not)

thanks in advance


r/learnpython • • 23h ago

Learning Python at 37 and I can read code fine but can't write 10 lines without the lesson open

152 Upvotes

37, learning Python in the evenings for about 3 months. I can read other people's code and follow every lesson, but the moment I close the lesson and try to write something small on my own, like a script to rename my photos, I'm stuck after a few lines. I did a free intro course before this and hit the same wall, so this time I'm paying for something structured. What made writing code on your own start to click?


r/learnpython • • 16h ago

Trying to import a file into Thonny

2 Upvotes

Trying to import a rom file to convert to bytes and then mod, but the game title has "of" in it, so it returns as a syntax error. I guess the IDE thinks I'm trying to write an if statement? Anyway, google wasn't helpful, so i came here.
Code below
import Breath of Fire II (USA) - Copy.sfc


r/learnpython • • 9h ago

Python Help

0 Upvotes

I need tutoring for Python. I am in masters program and can't understand the homework. Need someone to explain and teach. Please reach out if you can help.


r/learnpython • • 40m ago

At what point did Python finally click for you as a beginner?

2 Upvotes

I've recently started learning Python and have covered variables, loops, functions, lists and dictionaries. I also started building a simple banking system to put what I've learned into practice.

I can understand most of the code when reading it, but writing everything from scratch is a different challenge.

For those who started with absolutely no programming background, at what point did you become confident enough to build something independently?

Did you finish a course first or just keep building small projects until things started clicking?

Would love to hear about your experiences and anything you wish you had done differently as a beginner.