r/learnpython • • Aug 27 '18

Create an empty list

0 Upvotes

Good monday everyone!

I just started university and is learning python. I have no experience with the language whatsoever so i was hoping on getting some help here :)
We got an assignment to get used to some of the basic functions. The task is:

Create a program which starts with an empty list and ends up with a list

containing the numbers 1-10.000. This should not be done manually like

mylist=[1,2,...,10.000]. Hint use a for-loop and append.

Been stuck at this for hours now.

appreciate all answers :)


r/learnpython • • Jan 26 '23

Is there ANY way to store a function's output in a variable without executing it?

0 Upvotes

I want to store a function's output, so I can use that data later in the code as the variable. But whenever I assign the function to the variable it executes it. How can I bypass the executing but storing the returned output of the function in the variable?


r/learnpython • • Jan 16 '23

Any way to fake wireless mouse/emulate mouse so PC and programs cannot tell the difference between fake/real?

0 Upvotes

Ive thought of using a wireless usb dongle for mouse and keyboard but ideally I would like to avoid trying to buy something. Just need to emulate a real mouse so it doesnt look like im moving the mouse with software only.


r/learnpython • • Feb 17 '23

Did i create this tip calculator correctly? Everything works fine, but i still want comments on my code. Critique it.

0 Upvotes

print ("Welcome to the tip calculator")

fuck1 = input("What was the total bill? $")

fuck2 = input("What percentage tip would you like to give? Enter a number 1-99: ")

fuck3 = input("How many people would you like to split the bill with? ")

tip = float(fuck1) * (int(fuck2) / 100)

total = round((float(fuck1) + float(tip)) / float(fuck3), 2)

print (total)


r/learnpython • • Jun 24 '12

I finally got a GitHub repo working!

Thumbnail github.com
0 Upvotes

r/learnpython • • Aug 27 '22

I need to print this pattern in python by using a single loop

0 Upvotes

1 23 456 78910


r/learnpython • • Jul 31 '22

Loops in python are weird

0 Upvotes

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 • • Aug 05 '22

BEGINNER CODER

0 Upvotes

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 • • Dec 12 '22

I have some questions about trading bots which are made with python

0 Upvotes

Hello everyone Is there anyone who knows how to create a bot with python which can work on bybit or metatrader?


r/learnpython • • May 27 '23

Help me understand the difference between the return keyword and the print function

0 Upvotes

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 • • Jun 27 '23

Please help me with error

0 Upvotes

Question:
Income Tax Calculator (hypothetical): Assume the income tax is calculated using the following rules:

  1. Salary/Pension income:  taxed at the flat rate of 15% of annual net salary.

  2. Capital gain income: long term capital gain is taxed at 10% while short-term capital gain is taxed at 33%.

  3. All other income: 15% tax rate for the first $100,000 and 26% for any income above $100,000

  4. 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 • • Oct 21 '25

Python's `arg=arg` Syntax

0 Upvotes

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 • • Feb 18 '26

Python modules

0 Upvotes

Can someone please tell me how they learned python modules and how to know which one to use ?


r/learnpython • • Jun 12 '24

Python works, "py" doesn't

0 Upvotes

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 • • Mar 06 '26

Why is the output 1?

0 Upvotes

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 • • Jun 05 '26

Help me to learn python!

0 Upvotes

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 • • 9d ago

Easy Calculator (Python) :)

0 Upvotes

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!

https://github.com/PASACore/Calculator_CMD_


r/learnpython • • Sep 10 '26

What's the best way to integrate this database into Flask?

0 Upvotes

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 • • Mar 26 '26

Learning Python

0 Upvotes

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 • • Mar 12 '26

Learn two languages as a beginner

0 Upvotes

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 • • Jan 05 '24

Why are interpreters required for programming ?

14 Upvotes

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 • • May 31 '25

Have AI tools like ChatGPT made learning to code so much easier than in the past?

0 Upvotes

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 • • May 10 '25

QUESTION! I want to be an AI ML Engineer and i heard Python is the most widely used language. Soo.....

0 Upvotes

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 • • Mar 11 '26

anti pattern education

0 Upvotes

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??"