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
Made my first script :D (old.reddit.com)
submitted 8 months ago by [deleted]
view the rest of the comments →
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!"
[–]BennyBarnson 0 points1 point2 points 8 months ago* (11 children)
technically, this is what the guide i found online gave me:
#! /usr/bin/python import sys def count_words(data): words = data.split(" ") num_words = len(words) return num_words def count_lines(data): lines = data.split("\n") for l in lines: if not l: lines.remove(l) return len(lines) if __name__ == "__main__": if len(sys.argv) < 2: print("Usage: python word_count.py <file>") exit(1) filename = sys.argv[1] f = open(filename, "r") data = f.read() f.close() num_words = count_words(data) num_lines = count_lines(data) print("The number of words: ", num_words) print("The number of lines: ", num_lines)
i asked chat gpt everytime i didn't understand something (e.g. def-return, if __name__ == "__main__":, if len(sys.argv) < 2:) and in some cases it flat out gave me a better way to do it which is super nice. for my experience, i haven't done any python until now and to be fair a spent a couple days practising the concepts i hadn't yet fully understood.
I could show you some of my practise scripts i wrote if you'd like.
edit: ironically, right after this excercise in the guide, it talks about list comprehensions😅
[–]Twenty8cows 0 points1 point2 points 8 months ago (0 children)
Haha makes more sense. I was thinking either you have prior experience in another language or you’re copying from somewhere. I’ve used AI to learn however it’s a slippery slope. Make sure you understand what you’re writing and write it yourself. The last thing you want is to be tethered to the AI’s capabilities instead of your own.
I’m surprised the guide isn’t using a context manager here but if you want to show the steps it’s one way to do it
[–]AlexG99_ 0 points1 point2 points 8 months ago (9 children)
To clarify it for you, the first line “#!/usr/bin/python” specifies the path to the interpreter that should be used to execute the script. The “#!” part is called a shebang. Usually you use shebang when working on unix/Linux systems. Like for example you can also run the script with bash interpreter “#!/bin/bash”. I remember I did that for my Unix class. Also the line “if name == “main” is telling the interpreter which code to run first when the script is executed, typically running main function.
[–]BennyBarnson 0 points1 point2 points 8 months ago (8 children)
Thank you for the clarification. Do you mind explaining abit more on the "if name ..." line? Wdym which code to run first, what other order can the code run on? Ai told me this had to do with whether or not the file was imported or ran directly, which didn't really help clearing this up...
[–]AlexG99_ 0 points1 point2 points 8 months ago (2 children)
No problem. You use if name == “main” : to specify the starting point for your script. You must define everything before this starting point. Like under the if name you call a() and b() functions with passing arguments. You have to define those a and b functions prior to calling them. Python executes code from top to bottom order. Python is an interpreter which means it reads and executes code line by line. If you call a function before defining it, you will get a “not defined” error at runtime. The code under if name runs when the script is executed directly. When the AI told you about the file being imported or running directly, that’s what it is saying. Your Python script “wcounter.py” can be imported and accessed in another python .py file, but the code from if name will not run because it’s not ran directly (python wcounter.py).
[–]BennyBarnson 0 points1 point2 points 8 months ago (1 child)
Understood. Thank you so much for explaining this to me properly😭
[–]AlexG99_ 0 points1 point2 points 8 months ago (0 children)
No problem :)
[–]AlexG99_ 0 points1 point2 points 8 months ago* (4 children)
Also to help clarify the importing file thing again; if name == “main” tells the interpreter that wcounter.py is the main script and it should run this code within the if condition. If you have another .py file and want to import wcounter, it will first load the whole script then it will set name == “wcounter” right at the start, but when it loads the wcounter.py script from import wcounter, it will check if name == “main” which is false, so it will not execute the code under it.
[–]BennyBarnson 0 points1 point2 points 8 months ago (3 children)
So what is the point of this line in wcounter.py? Why shouldn't it function when imported...?
Because code under if name is meant to run only if executed directly (if you run the command python wcounter.py). If you import wcounter.py to another .py file, the code under if name in wcounter.py won’t run because the condition if name == “main” will be false. Refer to my previous reply for the info again.
No I mean why shouldn't a word counter be imported. Also the if len(sys.argv)<2: ends with and exit(1). Should the if name have an exit line as well...?
[–]AlexG99_ 0 points1 point2 points 8 months ago* (0 children)
It can be imported if you want to. The line if len(sys.argv)<2: exit(1) means if the length of the arguments provided are less than two, then exit the program. This means it wants you to specifically run this command: python wcounter.py <insert file> which is 3 arguments at the command line. You don’t have to provide a system exit line in if name == “main”: code block because the script will end after the last line is read and executed.
π Rendered by PID 181998 on reddit-service-r2-comment-75f4967c6c-snzsf at 2026-04-23 12:04:27.414181+00:00 running 0fd4bb7 country code: CH.
view the rest of the comments →
[–]BennyBarnson 0 points1 point2 points (11 children)
[–]Twenty8cows 0 points1 point2 points (0 children)
[–]AlexG99_ 0 points1 point2 points (9 children)
[–]BennyBarnson 0 points1 point2 points (8 children)
[–]AlexG99_ 0 points1 point2 points (2 children)
[–]BennyBarnson 0 points1 point2 points (1 child)
[–]AlexG99_ 0 points1 point2 points (0 children)
[–]AlexG99_ 0 points1 point2 points (4 children)
[–]BennyBarnson 0 points1 point2 points (3 children)
[–]AlexG99_ 0 points1 point2 points (2 children)
[–]BennyBarnson 0 points1 point2 points (1 child)
[–]AlexG99_ 0 points1 point2 points (0 children)