r/learnpython • u/[deleted] • Aug 25 '24
Can you show me a real program written in Python?
I have no experience with programming. I try to learn Python for data analysis. But it is hard to see how the hell can THIS be used for data analysing? What I have seen from this language so far are 3-lines-codes or 1-line-codes. Like
def abc(a, b)
temp=a
a=b
b=temp
return a, b
I understand what this "code" is doing, but can anyone show me a "normal" longer code in Python that actually IS a real program and not only a little trick? I'd like to analyze how these things I have learned about looks like in "real life". (I have learned about lists, tuples, variables, loops, functions and methods, but all examples and exercises were these little somethings like the example I wrote above.)
25
u/Chris_Hemsworth Aug 25 '24
Here is a repo of a project I made a while back. This is 100% written in python, but uses the ffmpeg library.
https://github.com/mysterylektro/AutoVideoEditor/tree/frame-analysis
The project may not be perfect coding, but it gets the job done. If you want to clone the repo and make some improvements, I’d be happy to review a pull request.
4
3
u/supanatral Aug 25 '24
Interesting use of try with importing modules
It makes sense as I read it but never thought of using it in that way
9
3
u/mfar__ Aug 25 '24 edited Aug 25 '24
Visit www.kaggle.com you might find what you want there.
Also, python doesn't need a trick to swap variables.
a, b = b, a5
u/sausix Aug 25 '24
Since it's just a return from a function just return it swapped:
return b, aThe variables a and b are local to the function anyway and won't change anything in the upper namespace.
1
2
u/mriswithe Aug 25 '24
You mention specifically data analyzing. Apache Superset is a Python powered open source project you can look at. https://github.com/apache/superset
There is Javascript on the web presentation side, but 90% of the app is written using Flask (unless they transitioned in the last year or two to something else).
The app's purpose is to query and graph data from a large array of different sources.
2
u/Jello_Penguin_2956 Aug 25 '24
Some embarrassing stuff I did while taking Courera data science cert course
1
u/Clear_Watch104 Aug 25 '24
As someone mentioned you mostly do it in Pandas. You can create a data frame from whatever you want (CSV, Excel, JSON etc). Once you have the df you write the program to do whatever you want to your data by using functions or already existing methods. You can use python to build SQL statements dynamically so you can automatically query stuff given a stable dataset (so you don't have to re-type queries manually if your data changes). Finally you can use python for data science.
https://youtu.be/eMOA1pPVUc4?si=2Z1Bi4gaFcIIpSAZ
This guy has some videos that may give you some perspectives.
Personally the biggest strength is that having a programming language, you can do literally everything you want to do to your data (unlike Excel and macros), you just need to figure out how to write it
1
u/Opposite-Day2878 Aug 25 '24
Depending how your definition of a real program here is some simple examples of 'full programs' below
https://github.com/Ninedeadeyes/15-mini-python-games-tutorial-series
If you want a much more complicated example it will be something like the below game I've written purely using from the standard library.
https://github.com/Ninedeadeyes/Grimlore-Land-of-the-Heretic-Hand
1
u/RiverRoll Aug 25 '24 edited Aug 25 '24
You can find small real world data analysis code examples in Kagle (code section), they will be mostly calls to existing libraries because there's no point in reinventing the wheel if you just want to get things done.
Reinventing the wheel can still be a good learning excercise though. For instance a good deal of data analysis is about statistical formulas and procedures and some of them are not hard to implement, for instance you could try implementing your own functions to calculate the mean and standard deviation from a list of measurements, the things you've learned about are enough to do this.
1
u/NoDadYouShutUp Aug 25 '24
https://github.com/nodadyoushutup/vulcan-utils
This is a package I include in most of my other packages as a dependency. It's got advanced logging output. That's really all it is for. Right now the current PyPi package is busted because it's missing a dependency in the requirements.txt but as for looking at what a package in the real world looks like it is what you want to see. Simple, small, but not a few lines of code.
Here is a more advanced framework you can poke around in. It's very complex and large.
1
u/CounterStrikeRuski Aug 25 '24
I'm not sure what you mean by "real program" as anything written in Python is a real program but here is a link to a game I made entirely in python: https://github.com/IanM07/RogueBlocks
1
u/ericjmorey Aug 26 '24
Take a look at https://www.kaggle.com/learn/pandas
The Tutorials and exercises should show you how Python can be used to gather and organize data to be analyzed
1
u/Slow_Rush1960 Apr 24 '26
import random
import time
# --- Setup ---
print("=== STREET RUNAWAY: THE VOIDS ===")
print("You just moved into a new house...")
print("The TV flickers on by itself...\n")
time.sleep(1)
print("NEWS: WARNING: Entities known as THE VOIDS are appearing in homes.\n")
time.sleep(1)
name = input("What is your name, survivor? ")
print(f"\nWelcome, {name}. Something is already inside your house...\n")
inventory = []
# --- Functions ---
def encounter_void():
print("\nA VOID appears in the hallway...")
choice = input("Do you RUN or HIDE? ").lower()
if choice == "run":
# 50% chance to escape
if random.randint(1, 2) == 1:
print("You escaped into the basement!")
if "key" not in inventory:
print("You found a key on the floor!")
inventory.append("key")
return True
else:
print("The VOID caught you... GAME OVER.")
return False
elif choice == "hide":
# 33% chance to survive
if random.randint(1, 3) == 1:
print("It passed you... but barely.")
return True
else:
print("It found you hiding... GAME OVER.")
return False
else:
print("You froze... the VOID noticed you. GAME OVER.")
return False
def final_room():
print("\nYou reach a locked door in the basement...")
if "key" in inventory:
print("You use the key you found...")
print("The door opens... light floods in... YOU ESCAPED!")
else:
print("You need a key... but it's too late.")
print("The VOID surrounds you... GAME OVER.")
# --- Game Loop ---
survived = True
for i in range(3):
if not encounter_void():
survived = False
break
time.sleep(1)
# If they survived all encounters, they reach the final room
if survived:
final_room()
print("\n=== END OF GAME ===")
1
u/heidensieck Aug 25 '24
Wow, but.. couldn't you have just googled this? .. yes, yes you could have. And should have.
2
u/SquiffyUnicorn Aug 27 '24
It sounds more like a question of ‘where do I look to find code for a finished app’ if any code is ever finished…
Certainly when I was first learning Python I could see the toy code in tutorials but found it difficult to see where I should be trying to go. I suppose it was a bit like seeing lots of tutorials on how to make a brick, but nothing to show how to build a house.
Of course in hindsight that wasn’t what I needed, but I didn’t know that at the time. And when I saw ‘complete’ code in GitHub it was overwhelming and I didn’t know where to begin to understand it.
Not being in a comp sci programme, I didn’t have any frame of reference.
10
u/atom12354 Aug 25 '24
If you want long python codes you can open Github and search for python, you gonna find around 10 million or something projects to go through in varying sizes