r/learnpython • u/ToXic73 • Jun 24 '12
r/learnpython • u/[deleted] • Aug 27 '22
I need to print this pattern in python by using a single loop
1 23 456 78910
r/learnpython • u/21bce • Jul 31 '22
Loops in python are weird
I recently started learning python programming. I also had some exposure to programming in my high school.
Today I studied loops but I found them a little bit weird...they do not have any initialisation, condition and increment like other languages like javascript.
It is very confusing to me PLEASE HELP!!
r/learnpython • u/bahhhtam88 • Aug 05 '22
BEGINNER CODER
Hey Peeps! I'm new to python and working and working on code academy. I'm tackling a project called “Len’s Slice” where I have to .insert() [2.5, “peppers”] into an index for a sublist. Please help! I'm stumped!
r/learnpython • u/FunCryptographer4795 • Dec 12 '22
I have some questions about trading bots which are made with python
Hello everyone Is there anyone who knows how to create a bot with python which can work on bybit or metatrader?
r/learnpython • u/[deleted] • May 27 '23
Help me understand the difference between the return keyword and the print function
Here is what I am working with:
def addition():
print(num1 + num2)
addition(4, 5)
why do I need to return a value when I could just print it directly when calling the function?
Also(while I'm here ;) ), could someone explain the need for having a main() function as well, I am not a seasoned programmer. I can't seem to wrap my head around it. Thank you.
r/learnpython • u/AU8640 • Jun 27 '23
Please help me with error
Question:
Income Tax Calculator (hypothetical): Assume the income tax is calculated using the following rules:
Salary/Pension income: taxed at the flat rate of 15% of annual net salary.
Capital gain income: long term capital gain is taxed at 10% while short-term capital gain is taxed at 33%.
All other income: 15% tax rate for the first $100,000 and 26% for any income above $100,000
Senior Citizens: Senior citizens get a 20% exemption (i.e. their taxes are reduced by 20%).
Write a Python GUI application (using tkinter) that prompts the user to enter income for each of the source category listed above and displays their income tax. Use a radio button for the Senior Citizenship status.
-----------------------------------------------------------------------
My answer:
from tkinter import*
class incomeTax():
def __init__(self):
window=Tk()
window.title=("Income Tax Calculator")
frame1=Frame(window)
frame1.pack()
Label(frame1, text="salary/pension income:").grid(row=1, column=1, sticky=W)
Label(frame1, text="capital gain income:").grid(row=2, column=1, sticky=W)
self.capital=StringVar()
rbcapital1=Radiobutton(frame1, text="long term gain",variable=self.capital, value=1, command=self.processradiobutton)
rbcapital2=Radiobutton(frame1, text="short term gain",variable=self.capital, value=2, command=self.processradiobutton)
rbcapital1.grid(row=2, column=2)
rbcapital2.grid(row=3, column=2)
self.senior_citizens=StringVar()
btseniorcitizens=Button(frame1, text="senior citizens",variable=self.senior_citizens,command=self.processcheckbutton)
btseniorcitizens.grid(row=5, column=1, sticky=W)
Label(frame1, text="all other income:").grid(row=4, column=1, sticky=W)
Label(frame1, text="total income tax:").grid(row=6, column=1, sticky=W)
frame2=Frame(window)
frame2.pack()
salaryVar=StringVar()
entrysalary=Entry(frame2,variable=salaryVar).grid(row=1, column=3, sticky=E)
capitalVar=StringVar()
entrycapital=Entry(frame2,variable=capitalVar).grid(row=3, column=3, sticky=E)
otherVar=StringVar()
entryother=Entry(frame2,variable=otherVar).grid(row=4, column=3, sticky=E, command=self.otherincometax)
totalVar=StringVar()
entrtytotal=Entry(frame2,variable=totalVar, command=self.total).grid(row=6, column=3, sticky=E)
btcalculation=Button(window, text="calculate income tax", command=self.processbutton)
window.mainloop()
def processradiobutton(self):
print("long term gain" if self.capital.get()==1 else "short term gain", "is selected")
def processcheckbutton(self):
print("check button is",)+("checked" if self.senior_citizens.get()==1 else "unchecked")
def otherincometax(self):
if otherVar.get()>100000:
self.otherincometax=(float(otherVar.get())-100000)*0.26+15000
else:
self.otherincometax=float(otherVar.get())*0.15
return self.otherincometax
def processbutton(self):
if self.capital.get()==1 and self.senior_citizens.get()==1:
self.processbutton=0.8*(float(salaryVar.get())*0.15+ float(capitalVar.get())*0.1+float(otherincometax).get())
elif self.capital.get()==2 and self.senior_citizens.get()==1:
self.processbutton=0.8*(float(salaryVar.get())*0.15+float(capitalVar.get())*0.33+float(otherincometax).get())
elif self.capital.get()==1 and self.senior_citizens.get()==2:
self.processbutton=float(salaryVar.get())*0.15+ float(capitalVar.get())*0.1+float(otherincometax.get())
else:
self.processbutton=float(salaryVar.get())*0.15+float(capitalVar.get())*0.33+float(otherincometax.get())
return self.processbutton
def total(self):
print(self.processbutton.get())
incomeTax()
--------------------------------------------------------
please help me with error of the above program:
Traceback (most recent call last):
File "C:/Users/13559/OneDrive/桌面/module3.py", line 66, in <module>
incomeTax()
File "C:/Users/13559/OneDrive/桌面/module3.py", line 18, in __init__
btseniorcitizens=Button(frame1, text="senior citizens",variable=self.senior_citizens,command=self.processcheckbutton)
File "C:\Users\13559\AppData\Local\Programs\Python\Python311\Lib\tkinter__init__.py", line 2706, in __init__
Widget.__init__(self, master, 'button', cnf, kw)
File "C:\Users\13559\AppData\Local\Programs\Python\Python311\Lib\tkinter__init__.py", line 2628, in __init__
self.tk.call(
_tkinter.TclError: unknown option "-variable"
r/learnpython • u/hwmsudb • Oct 21 '25
Python's `arg=arg` Syntax
I'm a grad student and my PI just told me that someone using the following syntax should be fired:
# This is just an example. The function is actually defined in a library or another file.
def f(a, b):
return a + b
a = 4
b = 5
c = f(
a=a,
b=b,
)All of my code uses this syntax as I thought it was just generally accepted, especially in functions or classes with a large number of parameters. I looked online and couldn't find anything explicitly saying if this is good or bad.
Does anyone know a source I can point to if I get called out for using it?
Edit: I'm talking about using the same variable name as the keyword name when calling a function with keyword arguments. Also for context, I'm using this in functions with optional parameters.
Edit 2: Code comment
Edit 3: `f` is actually the init function for this exact class in my code: https://huggingface.co/docs/transformers/v4.57.1/en/main_classes/trainer#transformers.TrainingArguments
r/learnpython • u/Fabulous_Bell6806 • Feb 18 '26
Python modules
Can someone please tell me how they learned python modules and how to know which one to use ?
r/learnpython • u/cloudiimofo • Jun 12 '24
Python works, "py" doesn't
I'm running a windows 10 machine with Python 3.12 installed. For some reason the 'python' command works fine, but 'py' gets me "'py' is not recognized as an internal or external command, operable program or batch file."
I have python installed here: C:\Users[username]\AppData\Local\Programs\Python\Python312
And the python launcher installed here: C:\Windows\py.exe
I've added both locations to my PATH, restarted the machine, but still doesn't work.
Any help is appreciated.
r/learnpython • u/Mars0da • Mar 06 '26
Why is the output 1?
I'm trying to write a program that will eventually read the following text file's lines and print the average number of "items" (the numbers) in each "basket" (each line represents a basket). Currently I'm trying to remove duplicate items in each basket, but the output gives me 1? Heres the code + the file's contents:
test = open("basketsfortesting.txt")
for line in test:
purchase_amounts = set(line.split(","))
print(len(purchase_amounts))
I believe set is whats removing duplicates but I have no idea what could be making the 1 output?
r/learnpython • u/No-Professional5609 • Jun 05 '26
Help me to learn python!
I’ve been learning Python through FreeCodeCamp and am currently following their "Introduction to Python Course with Certificate." However, as a beginner, I find it difficult to complete the projects on my own. Honestly, it feels a bit discouraging after spending time learning the material and passing the theory exams.
Sometimes I feel like I’m too dumb to learn coding, even though I know that might not be true.
So, should I give up at this point? I’ve searched on Google and asked AI tools, and they all say that this is normal for beginners. Still, I can’t seem to find the motivation to keep going. I’m also worried that I might be wasting my time—time that I could spend learning something else, like Excel in greater depth.
Maybe these thoughts sound meaningless, but I’m genuinely looking for advice from people who have been through this. Any help would be appreciated.
r/learnpython • u/Valuable_Corner4797 • 4d ago
Easy Calculator (Python) :)
I didn't like the Windows Calculator, so I decided to develop my own CMD-based calculator. It's open-source, so anyone is welcome to use it!
r/learnpython • u/ki4jgt • Sep 10 '26
What's the best way to integrate this database into Flask?
I'm creating an online bulletin board, and I'm feeling hacky (I hate SQL in all its forms and fashions).
What is the best way to integrate this database into a production Flask server: https://github.com/ki4jgt/PPD
Created it in 2020, so it's a bit dated.
But I'm worried about concurrency. I'm not beyond writing a database server, but I'd like to save that as a last resort.
r/learnpython • u/Artistic-Command-376 • Mar 26 '26
Learning Python
I want to start learning python to use in my work and at the same time, I find coding really fun and I think this will be really good for me.
The thing is I get bored quite easily, I need a fun way to learn it.
Any advice from python veterans or people that have went through a fun learning experience and now quite good at coding, your advice would be highly appreciated.
r/learnpython • u/G3N1U8 • Mar 12 '26
Learn two languages as a beginner
Hi guys i am very new to programming, and i have to learn cpp and python for uni and i am struggling hard. I sit in the lectures and i dont understand shit. What would you guys recommend to learn python at home because, the lectures are just a timewaste for me. The exams are in 4-5 months. I have to start as soon as possible.
r/learnpython • u/[deleted] • Jan 05 '24
Why are interpreters required for programming ?
i'm trying to learn python ....i'm hearing a lot of stuff about interpreters....just wanna know what are they for..why are they required...can i learn python without interpreter...without vs code or py charm
Cuz i wrote some codes in python and it worked without connecting python to interpreter ....why is an interpreter necessary
Plz enlighten me
r/learnpython • u/atomicbomb2150 • May 31 '25
Have AI tools like ChatGPT made learning to code so much easier than in the past?
As a university student practicing and learning how to code, I have consistently used AI tools like ChatGPT to support my learning, especially when working with programming languages such as Python or Java. I'm now wondering: has ChatGPT made it significantly easier for beginners or anyone interested in learning to code compared to the past? Of course, it depends on how the tools are used. When used ethically, meaning people use it to support learning rather than copy-pasting without understanding and learning anything, then AI tools can be incredibly useful. In the past, before ChatGPT or similar AI tools existed, beginners had to rely heavily on books, online searches, tutors, or platforms like StackOverflow to find answers and understand code. Now, with ChatGPT, even beginners can learn the fundamentals and basics of almost any programming language in under a month if they use the tool correctly. With consistent practice and responsible usage, it's even possible to grasp more advanced topics within a year, just by using AI tools alone, whereas back then it was often much more difficult due to limited support. So does anyone here agree with me that AI tools like ChatGPT made learning to code easier today than it was in the past?
r/learnpython • u/almightyaad • May 10 '25
QUESTION! I want to be an AI ML Engineer and i heard Python is the most widely used language. Soo.....
What is THE BEST website/course/playlist to learn Python from the very basics?
Also after i learn Python , what should i do so that i enter the field of Artificial Intelligence and Machine Learning?
I know there are things like PyTorch but i just dont know where to find courses for it.
I've read about freecodecamp , cs50 , helsinki program but i wanna know your opinion.
TLDR : What is the best place to learn python and lateron learn the ML part of it?
Thank you!
r/learnpython • u/Cute-Ad7076 • Mar 11 '26
anti pattern education
python educational materials seem to be allergic to communicating actual information. i bought joel grues 'data science from scratch'....guess what, not from scratch and not about data science. its about pythonic list comprehensions and being maximally insufferable with type hints.
all python ds and ml materials seem to be incapable of just writing a flipping algorithm. they all have to call 37 libraries and beat you over the head with being pYtHonIc while never actually spelling out the information thats in the title. i am going to lose my mind. I have looked through every book in my local public library and one, ONE actually implements a meaningful ds algorithm without sklearn.mouth_breathing.
I clicked on a 'Learn linear algebra with python!!!!' medium article and the first thing was just
**how to solve a linear equation**
```python
np.solve(x)
```
yipee you did it
ummm no
its like the entire ecosystem of things written in python is anti-understanding and pro superficial pointless api
literally i have spent hours trying to find K-means clustering without someone just calling sklearn.cluster.learn_nothing
i am losing my mind
have no educators stopped to think... "hmmmm maybe we shoudl include the information on the topic in the title??"
r/learnpython • u/nine-ine • 13d ago
Should I learn python?
Hii there😀
I wanted the community's opinion as many of you all know how it works and stuff
I recently thought of learning a language and got to know that python is a great starting place to do so
As it's really close to the language English and isn't that complex or complicated
Indeed it will take time for me to learn and apply it but is it worth
What is python mostly used for ?
Waiting for comments 📝
r/learnpython • u/Longjumping_Peak_840 • Jan 02 '25
How can I understand loops I'm frustrated
I know basics of python and can code well but sucks loops and developing logic about it idk what but it's doesn't make sense to me practiced some questions. My pratical examination went bad because of loops i have 2 days for my semester exam how can I understand fully it because it's next loops are very much c++
r/learnpython • u/TheHumanAnimal- • Oct 02 '24
I spend about 4-5 hours a day learning python. 1-1.5 chapters a day. Should I be doing more in less time?
Hello, I’m currently involved in the study of python but I am unsure if I am studying at a slow place.
The book I am currently using to progress my studies is Eric Matthes: Python Crash Course Edition 2.
I’m 84 pages in and it’s only been day 3 of my studies.
I guess my question is: Am I going too slow?
I seem to understand what’s being taught and find myself trying to test the system beyond my current knowledge before I discover if it’s possible from the book.
Also to keep my mind from wandering off, I listen to podcasts on current impressions/discussions regarding Artificial Intelligence since that will be where I inevitably find myself studying.
Any tips to increase the speed of learning python is appreciated.
r/learnpython • u/EveryStoryEnds • Sep 01 '25
Is AI really going to take over Python jobs as they say?
I'm trying to learn Python and in the last period I did manage to learn pretty a lot, but I have just a question. Are people that say to stop learning it since AI will take my job telling rhe truth or is it due to uncertainty?
I want to relocate in Asia for a Python job in the next 2 years and if any of you could help me tell more, I'd be very glad
And I apologise for to saying too much, maybe the subject was brought many times before me, but I'm kind of concerned since I'll have to look for anything else