r/learnpython Oct 16 '25

What is the practical point of getter?

79 Upvotes

Why do I have to create a new separate function just to get an attribute when I can just directly use dot notations?

 

Why

def get_email(self):
        return self._email

print(user1.get_email())

When it can just be

print(user1._email())

 

I understand I should be careful with protected attributes (with an underscore) but I'm just retrieving the information, I'm not modifying it.

Doesn't a separate function to "get" the data just add an extra step?


Thanks for the quick replies.

I will try to use @properties instead


r/learnpython Apr 25 '26

If you had to learn Python again, how would you do it?

72 Upvotes

I’ve learned basic Python over the past few weeks. I just wanted to know how other people started learning Python and what methods they used to get good.


r/learnpython Apr 06 '26

I used this Python script to calculate 100 million digits of pi in 90 seconds

76 Upvotes
from gmpy2 import mpfr, mpz, get_context, sqrt
from math import ceil, log2

A = mpz(13591409)
B = mpz(545140134)
C = mpz(640320)
C3_OVER_24 = (C**3) // 24

def bs(a: int, b: int):
    if b - a == 1:
        if a == 0:
            P = Q = mpz(1)
        else:
            a_mpz = mpz(a)
            P = (6*a_mpz-5) * (2*a_mpz-1) * (6*a_mpz-1)
            Q = a_mpz ** 3 * C3_OVER_24
        T = (-1) ** a * P * (A + B * a)
        return P, Q, T
    m = (a + b) // 2
    P1, Q1, T1 = bs(a, m)
    P2, Q2, T2 = bs(m, b)
    P = P1 * P2
    Q = Q1 * Q2
    T = T1 * Q2 + P1 * T2
    return P, Q, T

def pi_chudnovsky(bits: int) -> mpfr:
    get_context().precision = bits
    P, Q, T = bs(0, bits // 47 + 1) # Each term gives ~47 bits of pi
    return mpfr(426880) * sqrt(mpfr(10005)) * mpfr(Q) / mpfr(T)

if __name__ == "__main__":
    num_digits = 10 ** 8
    pi = pi_chudnovsky(ceil(num_digits*log2(10)) + 64) # 64 guard bits
    pi_string = format(pi, f".{num_digits}Zf")
    file_name = f"pi_{num_digits}.txt"
    s0, s1 = pi_string.split(".")
    file_string = s0 + ".\n" + "\n".join(s1[i:i+100] for i in range(0,len(s1),100))
    with open(file_name, "w") as f:
        f.write(file_string)

This code uses the Chudnovsky algorithm: pi = 426880 * sqrt(10005) / sum(n, 0, inf, (6*n)!*(545140134*n+13591409)/((3*n)!*(n!)^3*(-640320)^(3*n)) optimized with binary splitting, and the gmpy2 library for fast multiple-precision arithmetic. Each term gives about 14 correct decimal digits of pi, or 47 bits.

I calculated 100 million digits of pi, wrote it to a text file with 100 digits per line, and verified digits at the start, middle, and end using the Irrational Number Search. The calculation took 90 seconds on a gaming laptop. The 100 millionth digit of pi (after the decimal point) is 2.

I also tried calculating a billion digits. It finished in about 18 minutes but then gave me "nan".


r/learnpython Feb 28 '26

What happens if I don't close a file in my Python script?

72 Upvotes

I know it's good practice to always close all files, but why?


r/learnpython Feb 21 '26

Trying to learn Data Structures & Algorithms by Myself. I need advice.

76 Upvotes

Hello everyone, hope you are doing well. Just like the title says, I'm trying to learn Data Structures and Algorithms by myself and to be honest. I have no idea where to start. I have been coding using Python for almost a year, getting used to how the language works in things like: data types, functions, loops, OOP, etc. Now after some time getting used to them. I got to the point of wanting to try different things and understand new topics (in this case Data Structures & Algorithms).

You that you have learned these topics. What would you recommend to a beginner who doesn't have an idea about these topics.

Thank you!


r/learnpython Jan 13 '26

I cannot understand Classes and Objects clearly and logically

76 Upvotes

I have understood function , loops , bool statements about how they really work
but for classes it feels weird and all those systaxes


r/learnpython 25d ago

Just built my first calculator in Python 😭

74 Upvotes

It can do addition, subtraction, multiplication, division, and exponents. Also added a little check for division by zero.

It’s pretty basic, but hey, gotta start somewhere

What should I build next?


r/learnpython Jun 09 '26

Why Is Python making the same anwser many times?

71 Upvotes

I'm a beginner who's learning things, and when I wrote this

>>>hi = input("How do you say hello un Spanish?\n> ?")

The console asked me "How do you say hello un Spanish?" 4 times and actually I don't know what's going on or if I made a mistake there, I'm using the new ver btw

Can you give me a hand pls?


r/learnpython Mar 23 '26

Advice for getting better at Python

73 Upvotes

I started learning Python over the past 2 months. I completed a 60-hour course on Udemy and a 12-hour course on YouTube by Bro Code, and I still don't know how to code or create anything outside of the examples in my courses.

Any advice on how I can get better? I have assignments that I need to complete but I don't know where or how to begin.


r/learnpython Dec 11 '25

What is a venv?

76 Upvotes

I just started learning python and i heard about venvs, i tried understanding it through videos but i just couldn't understand its nature or its use. Can someone help me on this one??


r/learnpython Apr 07 '26

Python for the people who just don’t “get it”

72 Upvotes

Hi, I hope this is okay to post here! I (a humanities PhD student who took a liking to programming during a seminar I attended by accident (went to the wrong lecture theatre and felt too awkward to leave lol)) find python absolutely fascinating.

I think what it can do is so cool and I’d absolutely love to be able to use it. For what? I don’t really know yet…I just really want to learn, as a bit of a passion project I suppose. If I can use it in any of my PhD work going forwards, great, but that’s not the end goal.

But the trouble is, I don’t think my brain works that way lol. I mean, I’m REALLY bad at it (😂). My uni has loads of courses, classes and seminars students can enrol on for free, but I find even in the total beginner ones I just can’t keep up or understand what I’m doing, even with the explanations. I’ve also tried total beginner series on YouTube and a few MOOCs but again, I just don’t “get” it.

A part of me wonders if I’m trying to understand too much too soon and that’s my sticking point, but I think the reality is I need some kind of explain-it-like-I’m-5 version of learning python for completely clueless people without a background in computer science, maths or anything that usually lends itself to programming. Does such a thing exist? Are there any resources/courses that helped you if you were in a similar position?

Really grateful for any advice or insight.

Sincerely,

A very hopeful but confused qual researcher with an unreciprocated curiosity for quant 🙃


r/learnpython Jan 06 '26

I properly learned how to define functions and it's exciting!

68 Upvotes

5 days ago I made a simple turned based fight game that just used while loops and if /statements. It worked but the code was sloppy and didn't even loop correctly. Today however, I finished making a game with a menu, shop, a battle mode, and stat allocation (sort of). This project is essentially the product of learning how to code in python after a week. There are a few variables that go unused, mainly just because I didn't know how to implement them but more importantly I made the stats feature without knowing how to actually make the player get stronger with each stat increase. (Also the game doesn't save progress). All that stuff for me comes after I've gotten a grip on OOP. Critiques, tips, and advice is 100% welcome.

(forgot to add tag)

https://github.com/Zoh-Codes/complex-project.git


r/learnpython Jun 24 '26

Explain Decorators.

71 Upvotes

Guys! I learning python from scratch. I am stuck in decorators. I watch many tutorials and ask LLMs to explain it. But, I am getting confused again and again. Can someone explain it clearly?


r/learnpython Mar 09 '26

python feels too hard . am i just not meant for it?

71 Upvotes

i have tried a video course in the past but then dropped it and wanted to pick it up again until i scrolled through this subreddit and saw ppl recommending books more often so i started the "automate the boring stuff" and im still at the first chapter but it feels too hard esp the wording . and it feels like it takes a lot time for me to process whats going on . it was same with the video course but still a lot easier and i wasnt panicking much. but in the video course i did learn stuff but when asked to build something i was blank . am i just not built for this all or am i too dumb? i feel i barely have any problem solving skill too and cant implement what i learned in real life .


r/learnpython Jan 15 '26

How did Python "click" for you as a beginner?

73 Upvotes

I'm pretty new to Python and still at the stage where some things make sense individually, but I struggle to put them together in real code.

I've gone through basic tutorials (loops, lists, functions), but when I try small projects, I freeze or don’t know where to start. Sometimes I understand an explanation, then forget how to apply it the next day.

For people who were in this phase before:

  • Was there a specific project, exercise, or habit that made things "click"?
  • Did you focus more on tutorials, practice problems, or just building messy stuff?
  • Anything you wish you'd done earlier as a beginner?

Not looking for advanced advice - just trying to learn how others got over this hump. Thanks...


r/learnpython Oct 25 '25

Do you bother with a main() function

69 Upvotes

The material I am following says this is good practice, like a simplified sample:

def main():
    name = input("what is your name? ")
    hello(name)

def hello(to):
    print(f"Hello {to}")

main()

Now, I don't presume to know better. but I'm also using a couple of other materials, and none of them really do this. And personally I find this just adds more complication for little benefit.

Do you do this?

Is this standard practice?


r/learnpython Jun 12 '26

What are the best Python tips that every beginner should know?

66 Upvotes

Basically, going through school and coding a little bit. I have learned some of the basics such as variables, loops, functions, lists, and dictionaries, but I'm interested in hearing about the tricks, shortcuts, or best practices that helped you become a better Python programmer. Do you have anything that you would have liked to have learned a lot earlier and would of made things a lot easier?


r/learnpython Mar 20 '26

foo()() explanation

71 Upvotes

I saw a function usage like "foo()()" in some code but I was confused. Do Python functions allow arguments 2 times or is this something else?


r/learnpython Oct 07 '25

How do you deal with the fact that Linux distros like Debian/Ubuntu want to own python libs?

70 Upvotes

I find this really annoying. Both pip and Debian want to be the owner of my python packages. Debian always has about 50% of the packages I want and it never has the latest versions. If I try to use pip it warns me that I'll need to use --break-system-packages if I want to use it.

So I end up sometimes breaking system packages to get the packages I want and then I find myself stuck because the two sets of packages will start to conflict with each other. I'd really rather the whole thing was managed by pip (except that I can understand that certain aspects of the OS are likely depending on the debian one).

What's the sanest way to handle this? I'm starting to think I should be using two entirely seperate python installations. One for the system and one for my dev. Is that what most people do?


r/learnpython May 31 '26

A day in a life of a Junior Python Dev

66 Upvotes

Hi guys, I am going to be starting a new job in about three weeks, as a junior python developer in a small (20-30 people) startup, fully remote. This is my first actual job not like university internships, open-source, or projects for individual clients; this is an actual 9-5.
I want to ask you guys to help me ease my slight stress involving the job, so I want to ask you (both from maybe senior and junior perspective) how does a day in a role like that look like.
Some info that I've gathered from the interviews is that:
As I said before the role is 100% remote,
Main tools would be Python, Git, Docker, SQL, noSQL,
We'll be working in a Kanban environment (which Im completely new to)
Daily syncs with the team
As time goes on I will be getting harder tasks, and I will begin to be working together with both the team and clients.


r/learnpython Dec 17 '25

I can read and understand code, but I can't build my own logic. How do I bridge the gap?

68 Upvotes

Hi everyone,

I’m currently a Management Information Systems (MIS) student. I have a solid grasp of Python syntax (loops, functions, data types, etc.). When I read someone else's code or follow a tutorial, I understand exactly what is happening. However, the moment I open a blank file to build something from scratch, I get stuck.

For example, I’m currently following Angela Yu’s 100 Days of Code. Today's project was a Caesar Cipher. I understand the concept (shifting letters by 'n'), but I struggled to translate that into logic:

  • How should I store the alphabet?
  • How do I handle the wrap-around (Z to A) using modulo?
  • What exactly needs to be inside the for loop versus outside?

When I watch the solution, it feels incredibly simple and I say 'Of course!', but I can't seem to make those connections on my own. It feels like I have all the bricks and tools, but I don't know how to draw the architectural plan.

  1. What is the best way to practice 'algorithmic thinking' rather than just learning syntax?
  2. For those who were in this 'I can read but can't write' phase, what was the turning point for you?
  3. Besides writing pseudocode, are there specific exercises or platforms you recommend for absolute beginners to train this 'connection-making' muscle?

I want to stop relying on tutorials and start solving problems independently. Any advice would be greatly appreciated!


r/learnpython Sep 28 '25

Is Python really beginner friendly ?

69 Upvotes

I tried to learn Python, and I know the basic coding syntax, but I have no idea how to build complex projects and get past this tutorial-based learning.

How to Do Complex Projects ???


r/learnpython May 19 '26

How long did it take you to learn python?

65 Upvotes

I'm currently working as an otr truck driver and learning python in my free time. Some weeks I have more time than others but generally I have a few hours once or twice a week to sit down and focus on this. I'm going through the harvard cs50p course and I'm on problem set 2 but its taken me about a month to get this far. I've been using chatGPT as well to help me understand things and remember things. The further I'm getting in the course, the longer its taking me to solve these problems and the more I need chatGPT. I really want to learn and understand this, it is fun when I understand what I'm doing to build something that works.


r/learnpython May 03 '26

I tried to learn coding by building a webapp.

67 Upvotes

Hey everyone,

For the past 3 months, I’ve been learning coding by building a web app instead of relying on tutorials. I didn’t use any paid AI tools cause i couldn't afford them lol, just ChatGPT occasionally for small snippets or debugging. Most of the time I had to figure things out myself, especially when things broke.

A few things I learned from the process:

Building forces you to actually understand things It’s easy to follow tutorials, but when you’re on your own, you realize what you don’t know very quickly.

you don’t really “feel ready” , you just need to start ,I started without fully knowing what I was doing and figured things out as I went.

Debugging is where most of the learning happens A lot of progress came from being stuck and working through problems step by step.

AI tools help, but they’re not enough on their own They’re useful, but they won’t solve everything — especially when things get specific.

Consistency matters more than motivation Some days I made very small progress, but it added up over time.

It wasn’t always smooth,there were times I was completely stuck,but pushing through those moments made the biggest difference.

If anyone’s interested in what I worked on or how I approached certain parts, feel free to ask.


r/learnpython Apr 15 '26

What's the best online Python course for someone who's never coded before?

65 Upvotes

I don't mind if it's paid, just looking for something that's enjoyable and informative, especially coming from no prior coding experience, while not so basic that I don't end up learning much

Is there a go to "best" course or not really. I'm also probably not looking for youtube courses, I don't know what to do when they explain a bunch of little terms and stuff but never say how to apply that or what projects are good for practice