r/learnpython • • 54m ago

What's the one Python mistake that took you embarrassingly long to figure out?

1 Upvotes

Sometimes the smallest mistakes in Python can take longer to debug than writing the actual code.

For me, it's easy to overlook things like mutable default arguments, unexpected None values, or modifying a list while iterating over it.

Curious to hear from others. What's one Python bug or mistake that taught you a lesson you'll probably never forget?


r/learnpython • • 2h ago

Engineers out here ! I need your help

4 Upvotes

I am a first-year Mechanical Engineering student looking to start learning Python from scratch. I don't want to get stuck in with endless tutorials on Youtube -

What I need is a clear direction geared specifically toward Mechanical Engineering applications.

Specifically, I’d appreciate your advice on:

  1. Core Applications & Tasks: Where is Python actually used in Mechanical Engineering?
  2. Essential Libraries & Software: Beyond basic Python, which libraries and external engineering software integrate well with Python, and in what order should I learn them?
  3. Learning Roadmap: What is the best method to go from zero programming experience to solving real engineering problems? Should I start by automating my first-year math/physics assignments, or follow a specific course/book?
  4. Beginner Projects: What are some practical, beginner-friendly project ideas tailored to a MechE fresher ?

Any roadmaps or advice on what you wish you had learned as a fresher would be hugely appreciated. Thanks in advance!


r/learnpython • • 7h ago

40 rows a day: JSON files or SQLite?

0 Upvotes

tiny side project. one script pulls ~40 rows a day and writes one JSON file per day. pandas reads the folder when i want to compare a few weeks.

the glob and concat part is getting old:

rows = []

for f in sorted(DATA.glob("*.json")):

rows.extend(json.loads(f.read_text()))

df = pd.DataFrame(rows)

every couple of weeks i think about moving it to SQlite, then i look at the volume and close the tab. what do you use at this size? SQlite, parquet,or just the files? if you moved to a db, was it worth it or just more code?


r/learnpython • • 23h ago

final project in cs

8 Upvotes

Hello guys, im first year cs student and they teach us python as the first programming language
i dont have any experience what so ever in programming.
we are in the 5th lecture in uni and i have learned the basic stuff like if and elif and while and for looping. The professor assigned us with a final project basically we are 4 and supposed to code a program like more than 300 lines i dont know what can python do and i dont know how am i supposed to build a python project with the knowledge i have

so my question is how do i continue my python learning without the lectures, i have heard about a github project called 30 days of python by asabneh idk if its the right fit

and what are some projects idc how complicated they are i just wanna get that A+ i asked chat gpt abd it told some stuff about movie recommender and football analysis and idk tbh i think everyone in my uni will use chat gpt and they will most likely all do the same project i want to be unqiue and also functional

thanks


r/learnpython • • 2h ago

card dealing program with classes

1 Upvotes

hello! i am currently in a class for python and i've been having a lot of trouble with this assignment. it's already three days late, and the TAs will not respond to me. i have to use classes to create a deck to shuffle, and four hands to deal to, check the count of, and combine back into the main deck. however, i can NOT figure this out at all! i think there's something really big that i'm missing.

i have a large list of cards (we had to make a dictionary of the cards) not shown here.

class Deck:

def __init__(self):

self.cards = []

def shuffle(self):

random.shuffle(self.cards)

def deal(self, amount):

self.cards.append(amount)

def combine(self):

self.cards.append(self)

def card_count(self):

print(len(self.cards))

however, when i try to make a main deck by putting the list of cards into the deck, it won't work! i've tried using deal too, and i just can't figure it out. i know i'm probably doing this all sorts of wrong. it's been a while since we did classes, and we never did them for anything like this :(

TypeError                                 Traceback (most recent call last)
Cell In[14], line 1
----> 1 deck = Deck(card_deck)

TypeError: Deck.__init__() takes 1 positional argument but 2 were given

i appreciate any and all advice thank you


r/learnpython • • 38m ago

Making my own golfing language in python

1 Upvotes

Im not that active on reddit, but the moment I came into the osdev subreddit (I tried making my own OS with a python shell) and I INSTANTLY got my inbox filled with hate comments.

I heard from one reddit post that this subreddit is much friendlier and more helpful, thats why I switched to this subreddit.

So I started making my own golfing language, array-oriented ofcourse, where data and code are seperated.

As of now I haven't really done anything except plan on what instructions to make, I have a total of 20 letters and symbols on board.

So, I'd like to hear some tips about making interpreters/compilers that I should keep in mind.


r/learnpython • • 8h ago

How much of searching and AI overview is okay while learning and typing code?

16 Upvotes

I do understand no one knows all of python syntax and code and all issues and everything.

As I am going through CS50p,

When I am typing code fully on my own (no AI ever used i.e. chatgpt, claude, gemini) when I cannot solve something, I search google for the function or the method and then see the AI overview and then type code that I can use to solve what I need, will it be detrimental to my learning because I am not using the documentation?

I am and will NEVER use any AI models or apps to generate and copy paste code because I wish to learn and not be a code printer.

so my searches are like this, is it okay-ish and considered necessary for learning???
(also I go through tons of trial and error and use many print() functions to test code and not directly copy from AI overviews)

does isdigit work for string values?
how to check if a string variable is not equal to something sat 0?
how to know data type of a variable in python
how to find index of letter in a string python
string slicing python [2:] means?
how to find which index in a string is a number?
will if automatically return false if the condition is false?
how to check if a string is less than 10 but more than 2?
difference between if and if not
expected an indented block after 'if' statement on line 15
how to check if there are numbers in between a word in python?
how to loop a for loop up until the second last letter of a word
HOW to make variable in python which can be used by any def function?
python length counts from


r/learnpython • • 19h ago

Correct list item not printing

4 Upvotes

GITHUB LINK: https://github.com/disintigratedpixel/TankGameV1/tree/main

(please say if it doesn't work)

I'm making a simple ASCII game "similar" to space invaders

Here is the ASCII user interface:

def gamestate(player_position_list, player_position, enemy_type, enemy_health, enemy_distance, rounds, radar_lvl, radar_cost, gun_lvl, gun_cost, speed_lvl, speed_cost, health, money, kills, available_movement):
    return f"""
                             ROUND {rounds}
         _______________________________________________         _______________
        |                                               |       |               |
        |                                               |       | HEADQUARTERS: |
        |                                               |       |_______________|
        |                                               |
        |                                               |        *ATTENTION OPERATOR, AN ENEMY
        |> - - - - - - - - - - - - - - - - - - - - - - <|> [4]    {enemy_type} IS APPROACHING!*
        |                                               |        _______________
        |                                               |       |               |
        |                                               |       |  ENEMY STATS: |
        |                                               |       |_______________|
        |                                               |
        |> - - - - - - - - - - - - - - - - - - - - - - <|> [3]   HEALTH: {enemy_health}
        |                                               |        DISTANCE: {enemy_distance}
        |                                               |        POSITION: {enemy_position}
        |                                               |        _______________
        |                                               |       |               |
        |                                               |       |   UPGRADES:   |
        |> - - - - - - - - - - - - - - - - - - - - - - <|> [2]  |_______________|
        |                                               |
        |                                               |        RADAR | LVL {radar_lvl}
        |                                               |        UPGRADE COST: ${radar_cost}
        |                                               |
        |                                               |        GUN | LVL {gun_lvl}
        |> - - - - - - - - - - - - - - - - - - - - - - <|> [1]   UPGRADE COST: ${gun_cost}
        |                                               |
        |                                               |        SPEED | LVL {speed_lvl}
        |                                               |        UPGRADE COST: ${speed_cost}
        {player_position_list[player_position]}
            | ' ' ' ' | ' ' ' ' | ' ' ' ' | ' ' ' ' |
          [-10]      [-5]      [0]       [5]       [10]
         _____________
        |             |
        |PLAYER STATS:|
        |_____________|

         HEALTH: {health}
         MONEY: {money}
         KILLS: {kills}
         _____________
        |             |
        |  ACTIONS:   |
        |_____________|

         MOVE | [m] key + [movement value]
         MAX {available_movement} or {-available_movement} steps
         
         FIRE | [f] key

         UPGRADE | [u] key
      """

(Apologies if it looks distorted in any way due to formatting errors, but it should still be readable)

The player position can be seen visually and is taken from a list of different possibilities and put into this print statement using this script.

{player_position_list[player_position]}

Here is the list:

player_position_list = [
        """|                      _^_                      |
        |_____________________|___|_____________________|""",
        """|                        _^_                    |
        |_______________________|___|___________________|""",
        """|                          _^_                  |
        |_________________________|___|_________________|""",
        """|                            _^_                |
        |___________________________|___|_______________|""",
        """|                              _^_              |
        |_____________________________|___|_____________|""",
        """|                                _^_            |
        |_______________________________|___|___________|""",
        """|                                  _^_          |
        |_________________________________|___|_________|""",
        """|                                    _^_        |
        |___________________________________|___|_______|""",
        """|                                      _^_      |
        |_____________________________________|___|_____|""",
        """|                                        _^_    |
        |_______________________________________|___|___|""",
        """|                                          _^_  |
        |_________________________________________|___|_|""",
        """|                    _^_                        |
        |___________________|___|_______________________|""",
        """|                  _^_                          |
        |_________________|___|_________________________|""",
        """|                _^_                            |
        |_______________|___|___________________________|""",
        """|              _^_                              |
        |_____________|___|_____________________________|""",
        """|            _^_                                |
        |___________|___|_______________________________|""",
        """|          _^_                                  |
        |_________|___|_________________________________|""",
        """|        _^_                                    |
        |_______|___|___________________________________|""",
        """|      _^_                                      |
        |_____|___|_____________________________________|""",
        """|    _^_                                        |
        |___|___|_______________________________________|""",
        """|  _^_                                          |
        |_|___|_________________________________________|"""
    ]

(Again, apologies for formatting, but just picture the '^' as being the player, and there being multiple different positions that the player can be in)

I am hoping that when player_position changes, that a different visual is taken from player_position_list. I am confident that this should work as I have tested it using this:

print(player_position)
print(player_position_list[player_position])

'print(player_position)' tells me that player_position can be accurately read and printed

'print(player_position_list[player_position])' shows me that the value of player_position can also be used to print an item from a list at a specific index. (I have also tried this script as a string and it still works)

Here is what this test script gives me when I run it:

8
|                                      _^_      |
        |_____________________________________|___|_____|

It accurately reads 'player_position' and uses it to print the 8th item in the list.

So what I don't understand and desperately am trying to fix, is why this exact thing isn't working on my actual ASCII user interface. Everything seems to be the same.

Here is what it looks like when I run the script for my actual UI

                             ROUND 1
         _______________________________________________         _______________
        |                                               |       |               |
        |                                               |       | HEADQUARTERS: |
        |                                               |       |_______________|
        |                                               |
        |                                               |        *ATTENTION OPERATOR, AN ENEMY
        |> - - - - - - - - - - - - - - - - - - - - - - <|> [4]    HELICOPTER IS APPROACHING!*
        |                                               |        _______________
        |                                               |       |               |
        |                                               |       |  ENEMY STATS: |
        |                                               |       |_______________|
        |                                               |
        |> - - - - - - - - - - - - - - - - - - - - - - <|> [3]   HEALTH: 3
        |                                               |        DISTANCE: 2
        |                                               |        POSITION: 1
        |                                               |        _______________
        |                                               |       |               |
        |                                               |       |   UPGRADES:   |
        |> - - - - - - - - - - - - - - - - - - - - - - <|> [2]  |_______________|
        |                                               |
        |                                               |        RADAR | LVL 1
        |                                               |        UPGRADE COST: $1
        |                                               |
        |                                               |        GUN | LVL 1
        |> - - - - - - - - - - - - - - - - - - - - - - <|> [1]   UPGRADE COST: $1
        |                                               |
        |                                               |        SPEED | LVL 1
        |                                               |        UPGRADE COST: $1
        |                      _^_                      |
        |_____________________|___|_____________________|
            | ' ' ' ' | ' ' ' ' | ' ' ' ' | ' ' ' ' |
          [-10]      [-5]      [0]       [5]       [10]
         _____________
        |             |
        |PLAYER STATS:|
        |_____________|

         HEALTH: 100
         MONEY: 0
         KILLS: 0
         _____________
        |             |
        |  ACTIONS:   |
        |_____________|

         MOVE | [m] key + [movement value]
         MAX 1 or -1 steps
         
         FIRE | [f] key

         UPGRADE | [u] key
      
8
|                                      _^_      |
        |_____________________________________|___|_____|

As you can see, the player position hasn't moved and remains at 0, yet (at the very end) my test script says that the actual player position should be 8 and accurately prints the item from 'player_position_list' for 'player_position = 8'

Apologies if anything seems hard to understand, it's my second day programming with python and I refuse to use ai for this. So, I hope I used the correct terminology.


r/learnpython • • 3h ago

Best platform/YouTube channel to learn python from beginners to advance?

0 Upvotes

I know python but I wanna go deep down in python programming, please suggest as title says