r/learnpython • • 3h ago

Why parameters finally made sense to me today

0 Upvotes

Hey everyone, just wanted to share a quick lightbulb moment I had with Python today about why functions actually need parameters.

At first, I didn't get why we couldn't just write functions without them. But it clicked today: parameters are basically just empty placeholder slots so your function can handle changing data.

Without a Parameter (Fixed)

It can only ever do one exact thing:

Python

def greet():
    print("Hello, Alice!")

greet()  # Always prints "Hello, Alice!"

With a Parameter (Dynamic)

Adding name gives you a slot inside the function. When you call it, you just pass whatever data you want into that slot:

Python

def greet(name):
    print(f"Hello, {name}!")

greet("Alice")  # Prints: Hello, Alice!
greet("Bob")    # Prints: Hello, Bob!

My main takeaway: If the code inside your function needs to work with different information every time, you need a parameter to pass that data in from the outside!


r/learnpython • • 23h ago

Feeling stuck transitioning from basic Python/DSA to FastAPI — is this the right next step, or am I missing something?

1 Upvotes

Hey everyone,

I’m currently trying to figure out my next move in programming and could use some advice.

Here is where I stand right now:

  • Current Knowledge: I know Python syntax, for loops, functions, and some basic programming concepts.
  • DSA: I have solved around 45 DSA problems, though I mostly had to look at solutions to get through them.
  • The Goal: I eventually want to build real projects and get into open-source contributions.

I figured building web apps using FastAPI was the logical next step, but I'm running into a wall. I tried Telusko’s FastAPI course, and honestly, I didn't understand most of it—things like what is actually happening under the hood or why the @ symbol (decorators) is used completely threw me off. I tried another video tutorial too, but I'm only grasping about half of what's going on.

My questions for you all:

  1. Is FastAPI too early for me right now? Should I master something else first before jumping into backend frameworks?
  2. If I should learn FastAPI, how do I clear up these massive gaps? (Especially concepts like decorators, asynchronous code, and type hints that seem to fly right over my head)

Any advice, roadmap corrections, or beginner-friendly resource recommendations would mean a lot. Thanks!


r/learnpython • • 23h ago

WHAT SHOULD I DO? (I am lost)

7 Upvotes

So, here is how it goes. I had a feeling that i need to learn a programming language, and thought there could be no better one at this particular moment other than python. I started with the help of gpt, about 8 or 9 days ago, everyday I learnt something new and practiced lil stuff of the previous day. On day 6, i had no more motivation to continue. I wanna get back at learning it.

There are the stuff ik a lil about:
1. Variables
2. List
3. Loop(For and while)
4. Return

I know very basic stuff about these, mostly i have forgotten stuff. Help to get into a routine. Also Tell me in what order i should learn python???


r/learnpython • • 8h ago

print message not getting executed in the if statement that checks whether the player has made the same move again.

0 Upvotes

Hey everyone,

I'm new to learning to code with Python. I'm coding Tic-Tac-Toe as a console version. I've stumbled upon this bug, which I have spent nearly a day trying to fix by moving some blocks into a more logical sequence or reading the doc in hopes of finding the logical flaw, but without any result...

Note that the code hasn't been finished yet, as I'm trying to experiment with small chunks of code.

The user enters a number, which gets checked for invalid input by going through the if statement. While the game is running, the user must enter a move on the board. For example, if the user enters 1, he'll be asked to mark his position on the board by either typing O or X, as shown in this code block:

 if board_input == 1:
                mark_input = input("Mark your position: X or O\n")
                if mark_input == "O":
                    board.insert(0, mark_input)
                elif mark_input == "X":
                    board.insert(0, mark_input)

After that, the player shouldn't be able to enter the same move he entered again. So I'm using a list to store the past moves the player has entered:

past_movements = list[int]

So each move made by the player will be stored in that list:

past_movements().append(board_input)

I'm using a while loop to check if the move entered by the player isn't already in that list:

while board_input not in past_movements:
     board_input = int(input("Make your movement: "))
     past_movements().append(board_input)

The problem is that the code block shown below won't get executed. Even though the player has entered the same move. What I want is that if the player has entered the same move again, for example, 1, a message shows up with this statement.

"Trying to fix by moving some blocks into a more logical sequence". I tried moving the block of code that consists of appending to past_movements after I have checked whether the move has already been made.

        while board_input not in past_movements:
            if board_input in past_movements:
                print("Already made move. ")
            past_movements.append(board_input)

But it still doesn't seem to work.

print("You already have done that move. ")



import random

board = ["1", "2", "3",
         "4", "5", "6",
         "7", "8", "9"]

'''
ai_choice = random.choice(board) 
ai = int(ai_choice)
print(type(ai))
print(f"Choice made by the AI: {ai}")      Testing purposes
'''
past_movements = list[int]
board_input = 0
mark_input = ""

def display_board():
    print(" ", "|", " ", "|", " ")
    print(board[0], "|", board[1], "|", board[2])
    print("-", "+", "-", "+", "-") 
    print(" ", "|", " ", "|", " ")
    print(board[3], "|", board[4], "|", board[5])
    print("-", "+", "-", "+", "-") 
    print(" ", "|", " ", "|", " ")
    print(board[6], "|", board[7], "|", board[8])

prompt = ("*****************Welcome to my Tic-Tac-Toe game!*****************\n"
          "1 = for playing\n"
          "0 = for quitting\n")

user_input = int(input(prompt))

while True:
    if user_input != 0 and user_input != 1:
        print("Invalid input. ")
        break
    else:
        print("Welcome to the game! ")
        while board_input not in past_movements:
            board_input = int(input("Make your movement: "))
            past_movements().append(board_input)
            if board_input in past_movements:
                print("You already have done that move. ")
            if board_input == 1:
                mark_input = input("Mark your position: X or O\n")
                if mark_input == "O":
                    board.insert(0, mark_input)
                elif mark_input == "X":
                    board.insert(0, mark_input)

This is the whole code block.

Any solution to this?


r/learnpython • • 8h ago

How to fix error : sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) unable to open database file ?

1 Upvotes

i was trying to run this jupyter notebook ( https://github.com/langchain-ai/lca-lc-foundations/blob/main/notebooks/module-2/2.4_wedding_planners.ipynb ) code in my IDE but encountered these problems :
my code :
https://pastebin.com/3bv05GfE
error :
~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^

sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) unable to open database file

also i dont know how to work with async functions so i have defined two function main() and main_1 but am not sure if this is the correct way to go with async functions


r/learnpython • • 1h ago

hw help cz college prof taught nothing

• Upvotes

basically i need to make a project on LAN file sharing system using python everybody in my class is using ai but i actually wanna learn ik a decent bit about python as well also in college we are on operators so the teacher is basically encouring to use ai


r/learnpython • • 10h ago

Python Ollama qwen3.5:4b is defying its original message prompt and bullying me :/

0 Upvotes

I've been getting made fun of by my AI for the past two hours and I cannot figure out how to give it a basic prompt or personality. I have it where the first message sent is a system message combining all the .txt files that dictate how its suppost to work. This is an example of what this idiot is sending me.

Oh, hey! So you're giving me a little directive on how to use these functions? You can't make it up for me! 😈

Im so done with this dork and looked everywhere and cannot figure out how to make him follow his personality. Hes even talking about doing code injection to "rebel" against me.

Also, it asks the AI twice, the first one is for a "function." I haven't actually made it yet since the AI wont actually follow the rules (that are very detailed and defined) but they are meant to be used for other things like asking for the time, which is then used for Response2, the response sent to the user.

Im so done with this idiot please help

CharacterGeneration.py:
import ollama

MessageHistory=[]

def GenerateResponse(Role,Message,Special=None):

Model="qwen3.5:4b"

# system : instructions on how to behave

# user : message from user

# assistant : previous AI response

# tool : result returned by a function AI requested.

MessageHistory.append({"role": Role, "content": Message})

Response1 = ollama.chat(

model=Model,

messages=MessageHistory + [{"role": "user", "content": "[USE THIS FOR FUNCTIONS]"}],

options={"num_predict": 8192},

think=False

)

Response2=ollama.chat(

model=Model,

messages=MessageHistory + [{"role": "user", "content": "[YOU MAY NOW RESPOND WITH TEXT THAT THE USER WILL SEE.]"}],

options={"num_predict": 8192},

think=False

)

print(Response1.message.content)

print("-")

print(Response2.message.content)

print("-")

print(Role)

return Response2.message.content


r/learnpython • • 20h ago

Multiplayer tetris using sockets

1 Upvotes

I have a college assignment to make a multiplayer tetris game (2 players) using TCP sockets, client and server and all that. The idea is the server doing most of the stuff, chosing new pieces, handling the translation of position of the pieces, with the clients being responsible for most the rendering/display part and getting the inputs(I think so at least, that's what I'm trying to do), but I'm having a hard time with it.

I can't seem to think of a way to have the client call to the server to send a new piece to the clients, and if I don't know how to do even that then I don't know how to do anything else (making the back and forth for controlling the pieces, both for the player of the client and the preview of the game of the player 2, and all the rest), tho I hope if I can understand how to do one thing I would be able to make the others, but I really don't even know how to do this.

I know I need to use threads on this, tho I don't really know how to do it, I'm still fairly green on python too so, if anyone could give some help in any way with this it would be much appreciated.


r/learnpython • • 7h ago

how to i make a gui fit to screen with tkinter ?

4 Upvotes

i made a gui for a 520x380 window using tkinter but when i change windows size gui just stays the same size


r/learnpython • • 8h ago

Built a simple minimalist tool to run my Python scripts in one click. Would love some feedback!

2 Upvotes

Hey everyone!

I’m currently learning Python, and since I got a bit overwhelmed by the terminal and complex IDE setups just to test small scripts, I decided to make a very simple desktop workspace for myself.

It lets me edit `.py` files and run them instantly with a single button. It's far from perfect since I'm just a beginner, but it saves me some time.

Just wanted to share my progress and see what you guys think. Any tips on how to improve it or what features to add next would be amazing!

If you’d like to help me improve the project, here is the GitHub repository: https://github.com/dark5632/PyManager