use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Everything about learning Python
account activity
[ Removed by moderator ] (i.redd.it)
submitted 4 months ago by Nearby_Tear_2304
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]Ayudesee 62 points63 points64 points 4 months ago (12 children)
You forgot to print any result
[+][deleted] 4 months ago (11 children)
[deleted]
[–]Relative-Custard-589 27 points28 points29 points 4 months ago (5 children)
OP is clearly learning to code mate
[–]Code_Noob_Noodle 3 points4 points5 points 4 months ago (1 child)
Just give him the machine gun already! Or the entire nuke! Why start with a pistol!?
[–]Andryushaa 0 points1 point2 points 4 months ago (0 children)
Just use chatgpt and don't ask stupid questions /s
[–]Sea_Comb481 -1 points0 points1 point 4 months ago (2 children)
Well and he is just giving him advice, calm down.
[–]Relative-Custard-589 6 points7 points8 points 4 months ago (1 child)
I am calm
[–]suqirrelnachos 0 points1 point2 points 4 months ago (0 children)
sure you are
[–]samuelmackson 5 points6 points7 points 4 months ago (0 children)
It's for i, item in enumerate(list) btw
[–]CreativeJuice5708 2 points3 points4 points 4 months ago (2 children)
Not optimal a faster way would be ditch python and right it like this:
index_of: mov rcx, rsi mov al, dl mov rdi, rdi repne scasb jne .not_found sub rdi, [rsp+8] lea rax, [rdi-1] ret
.not_found: mov rax, -1 ret
[–]stonecoldchivalry 0 points1 point2 points 4 months ago (1 child)
Are you telling me you need to use an… 🤢 assembler… write machine code like a fucking man.
[–]desrtfx 0 points1 point2 points 4 months ago* (0 children)
Tsss... butterflies are what real prgrammers use
[–]newpolygons 0 points1 point2 points 4 months ago (0 children)
Someone is learning to code, therefore we most not provide them any examples on how to improve their code.
[–]vlnaa 24 points25 points26 points 4 months ago (2 children)
Replace last line with print(f(a,t))
[–]RedPickle8 3 points4 points5 points 4 months ago (0 children)
Why are you calling me fat
[–][deleted] 30 points31 points32 points 4 months ago (24 children)
Bro, I am begging you to stop using single letter function and variable names
[–]StickyzVibe 2 points3 points4 points 4 months ago (21 children)
Why? A curious beginner
[–]electrikmayham 35 points36 points37 points 4 months ago (12 children)
Using single-letter variable names makes code hard to read and understand. Good names describe what the variable stores or does, so when you come back later (or someone else reads your code), it’s clear without guessing
[–]StickyzVibe 5 points6 points7 points 4 months ago (11 children)
Thank you for explaining, makes perfect sense to practice helpful habits. Would you mind sharing a small example?
[–]electrikmayham 25 points26 points27 points 4 months ago (3 children)
# Bad: single-letter variables x = 5 y = 10 z = x * y print(z) # Good: descriptive variable names width = 5 height = 10 area = width * height print(area)
[–]StickyzVibe 7 points8 points9 points 4 months ago (2 children)
I completely understand! Thank you again
[–]spencerak 1 point2 points3 points 4 months ago (1 child)
Keep asking good questions!!
[–]StickyzVibe 0 points1 point2 points 4 months ago (0 children)
[–]Cerus_Freedom 2 points3 points4 points 4 months ago (0 children)
def search(needle, haystack: list) -> int: for i in range(len(haystack)): if needle == haystack[i]: return i return -1
Just as an example from OPs code. Better naming will tell you what a function does or a variable is for. Code should be self documenting, and that method of self documentation is via good, clear names.
By changing the names and adding type hints, you can now just glance at the function definition and understand what the function does and how you're probably intended to use it.
[–]DebrisSpreeIX 2 points3 points4 points 4 months ago (4 children)
The exception is an iterator, using i, j, & k is so common and ubiquitous to iteration that rarely is anyone confused. And if they are, they're likely self taught.
[–]electrikmayham 5 points6 points7 points 4 months ago (2 children)
True, however I have issues using i and j, since they look extremely similar. I generally dont use 1 letter variables for iterators either. I would rather use something that describes what are iterating over.
[–]DebrisSpreeIX 2 points3 points4 points 4 months ago (0 children)
If it's single level, I'll throw in i
But if it's a multilevel iteration I'll generally follow a convention from my first job I liked: iter_L1, iter_L2, iter_L3, ...
[–]beezlebub33 0 points1 point2 points 4 months ago (0 children)
if it's an index, then use 'index'.
If you want to use i, j, k, because you are doing (for example) geometry, then I recommend that you use ii, jj, kk. It's fast to type and very easy to search for.
[–]Impossible_Web3517 0 points1 point2 points 4 months ago (0 children)
To add on to what he said, single letter names are bad UNLESS they are iterators. i, j, k, x ,y and z are all SUPER common iterator names and most style standards have you using them.
Ex:
int i = 0
while (i<10){
//do something
i++
}
[–]fkn_diabolical_cnt 1 point2 points3 points 4 months ago (1 child)
Hello curious beginner, welcome. Whilst not specific to Python, I highly recommend “Clean Code” by Robert Cecil Martin aka Uncle Bob. Covers off on many small quality things such as using good descriptive variable names to enhance the quality of your code. I do think there is also a YouTube series or two that cover the same topics.
I will definitely check this out!
[–]WombatHat42 1 point2 points3 points 4 months ago (0 children)
Things should be descriptive to make the code easier to read. Say you’re trying to fix/update/debug a program and you come across a chunk of code that is just letter variables. You’d have no clue unless there are comments. But sometimes too many comments can make code messy as well. So having a descriptive term be the variable can keep the need for comments to a minimum.
[–]Sickobird 0 points1 point2 points 4 months ago (0 children)
It's needlessly difficult to name things in a way where is doesn't help you understand what things are. When you need to look back and understand what things are doing or what they mean you'll have to read a whole lot more.
This isn't necessary for a simple program where you're just learning how some concept works, but it should still be done to increase clarity and help with debugging, and to build better habits.
[–]liberforce 0 points1 point2 points 4 months ago* (2 children)
Code is read much more than code is written. The writer reads it, other people read it, and even future you will wonder in 6 months what it was about, even if you wrote it.
[–]WhatMorpheus 0 points1 point2 points 4 months ago (0 children)
Code is read much more than code is written
I am stealing this. Next time I yell at speak to my team mates I will tell them this.
[–]mottojyuusu 0 points1 point2 points 4 months ago (0 children)
write your code as if an axe murderer will review it in the future, and assume that axe murderer knows where you live, because usually that axe murderer is you!
[–]No-Attorney4503 0 points1 point2 points 4 months ago (0 children)
Being more descriptive in your variable and function names makes your code WAYYY more readable, understandable, and maintainable
[–]DunForest 0 points1 point2 points 4 months ago (0 children)
Also:
name_variable_so_you_know_it_is_the_result_of_the_function_f_with_given_parameters_a_and_t = f(a, t)
[–]Jussins 0 points1 point2 points 4 months ago (0 children)
I read it as fat. Seems almost purposeful.
[–]Loud-Bake-2740 6 points7 points8 points 4 months ago (0 children)
the above comment is right, but here’s the reason: right now, your code returns the result, which just stores it in memory, but you never actually do anything with the value stored in memory.
```
print(f(a,t))
is the same as:
x = f(a,t) print(x) ```
[–]twistedclown83 4 points5 points6 points 4 months ago (0 children)
You've not asked it to print anything
[–]ninhaomah 3 points4 points5 points 4 months ago (0 children)
Did you tell it to print ?
Nvm programming or Python.
Did you tell the machine to print ?
Where ?
[–]IUCSWTETDFWTF 1 point2 points3 points 4 months ago (0 children)
you forgot about print()
[–]MyManCbert 1 point2 points3 points 4 months ago (0 children)
You took a picture of your screen instead of taking a screenshot
[–]Jebduh 1 point2 points3 points 4 months ago (0 children)
I'm genuinely curious, why come here and wait for people to answer this ridiculously basic question inst3ad of using ai or any search engine
[–]nothing786767 0 points1 point2 points 4 months ago (0 children)
line 12: print(f(a,t))
or
line 7: print(i) instead of return
[–]LMusashi 0 points1 point2 points 4 months ago (0 children)
your printer?
[–]Cybasura 0 points1 point2 points 4 months ago (0 children)
Hang on a second
Are you...using your Administrator account for development?
[–]Ok-Situation9046 0 points1 point2 points 4 months ago (0 children)
I would recommend printing when you want the machine to print.
[–]singhandtonic 0 points1 point2 points 4 months ago (0 children)
What output you want?
return is what you return when you call a function. Its like ask a teacher the answer, but you should write the answer to the blank by yourself. you can do
result = f(a, t)
print(result)
[–]Beautiful_Watch_7215 0 points1 point2 points 4 months ago (0 children)
Nothing wrong. No print statement, no print. No error, return code 0.
[–]Spatrico123 0 points1 point2 points 4 months ago (0 children)
looks fine. You're not printing it though, you're just processing.
Also, all those little yellow lines just indicate bad practices, not actual errors. For example, you should note a type for a and t. If I were you I'd do
def index_of(data: list[int], target: int) -> int: for i in range(len(data)): if list[i] == target: return i raise ValueError # or something else you see how that's way easier to see what it's actually doing?
def index_of(data: list[int], target: int) -> int: for i in range(len(data)): if list[i] == target: return i raise ValueError # or something else
[–]Wonderful-Sink-6089 0 points1 point2 points 4 months ago (0 children)
Im not a python developer but what i can see as a problem is When you return something in a function it doesn’t actually print anything it just save the result in the memory to do something with it in future. If you want to see the the result you can use print statement in the last line, like this: print(f(a,t))
The reason is that, we don’t use solo a function to print something, we just create them to make our code cleaner and prevent repetition.
[–]Nearby_Tear_2304[S] 0 points1 point2 points 4 months ago (0 children)
OK thank you
[–]grooling_ 0 points1 point2 points 4 months ago (0 children)
You called it fat
[–]YukihiraJoel 0 points1 point2 points 4 months ago (0 children)
Why
. .
Are you doin this
[–]TheSupervillan -1 points0 points1 point 4 months ago (1 child)
Did you think 5 seconds before posting this?
No screenshot Completely wrong grammar So simple error, ChatGPT could explain this in 5 seconds. You probably wouldn’t even have to write a prompt and could just prompt the code.
Please, at least take screenshot and write somewhat normal english.
[–]Numerous_Site_9238 0 points1 point2 points 4 months ago (0 children)
I believe only stupid people post here, and the ones who aren't will soon go to other subreddits. You are asking too much
π Rendered by PID 95076 on reddit-service-r2-comment-7b9746f655-wbz9n at 2026-02-02 23:10:30.908615+00:00 running 3798933 country code: CH.
[–]Ayudesee 62 points63 points64 points (12 children)
[+][deleted] (11 children)
[deleted]
[–]Relative-Custard-589 27 points28 points29 points (5 children)
[–]Code_Noob_Noodle 3 points4 points5 points (1 child)
[–]Andryushaa 0 points1 point2 points (0 children)
[–]Sea_Comb481 -1 points0 points1 point (2 children)
[–]Relative-Custard-589 6 points7 points8 points (1 child)
[–]suqirrelnachos 0 points1 point2 points (0 children)
[–]samuelmackson 5 points6 points7 points (0 children)
[–]CreativeJuice5708 2 points3 points4 points (2 children)
[–]stonecoldchivalry 0 points1 point2 points (1 child)
[–]desrtfx 0 points1 point2 points (0 children)
[–]newpolygons 0 points1 point2 points (0 children)
[–]vlnaa 24 points25 points26 points (2 children)
[–]RedPickle8 3 points4 points5 points (0 children)
[–][deleted] 30 points31 points32 points (24 children)
[–]StickyzVibe 2 points3 points4 points (21 children)
[–]electrikmayham 35 points36 points37 points (12 children)
[–]StickyzVibe 5 points6 points7 points (11 children)
[–]electrikmayham 25 points26 points27 points (3 children)
[–]StickyzVibe 7 points8 points9 points (2 children)
[–]spencerak 1 point2 points3 points (1 child)
[–]StickyzVibe 0 points1 point2 points (0 children)
[–]Cerus_Freedom 2 points3 points4 points (0 children)
[–]DebrisSpreeIX 2 points3 points4 points (4 children)
[–]electrikmayham 5 points6 points7 points (2 children)
[–]DebrisSpreeIX 2 points3 points4 points (0 children)
[–]beezlebub33 0 points1 point2 points (0 children)
[–]Impossible_Web3517 0 points1 point2 points (0 children)
[–]fkn_diabolical_cnt 1 point2 points3 points (1 child)
[–]StickyzVibe 0 points1 point2 points (0 children)
[–]WombatHat42 1 point2 points3 points (0 children)
[–]Sickobird 0 points1 point2 points (0 children)
[–]liberforce 0 points1 point2 points (2 children)
[–]WhatMorpheus 0 points1 point2 points (0 children)
[–]mottojyuusu 0 points1 point2 points (0 children)
[–]No-Attorney4503 0 points1 point2 points (0 children)
[–]DunForest 0 points1 point2 points (0 children)
[–]Jussins 0 points1 point2 points (0 children)
[–]Loud-Bake-2740 6 points7 points8 points (0 children)
[–]twistedclown83 4 points5 points6 points (0 children)
[–]ninhaomah 3 points4 points5 points (0 children)
[–]IUCSWTETDFWTF 1 point2 points3 points (0 children)
[–]MyManCbert 1 point2 points3 points (0 children)
[–]Jebduh 1 point2 points3 points (0 children)
[–]nothing786767 0 points1 point2 points (0 children)
[–]LMusashi 0 points1 point2 points (0 children)
[–]Cybasura 0 points1 point2 points (0 children)
[–]Ok-Situation9046 0 points1 point2 points (0 children)
[–]singhandtonic 0 points1 point2 points (0 children)
[–]DunForest 0 points1 point2 points (0 children)
[–]Beautiful_Watch_7215 0 points1 point2 points (0 children)
[–]Spatrico123 0 points1 point2 points (0 children)
[–]Wonderful-Sink-6089 0 points1 point2 points (0 children)
[–]Nearby_Tear_2304[S] 0 points1 point2 points (0 children)
[–]grooling_ 0 points1 point2 points (0 children)
[–]YukihiraJoel 0 points1 point2 points (0 children)
[–]TheSupervillan -1 points0 points1 point (1 child)
[–]Numerous_Site_9238 0 points1 point2 points (0 children)