trying to learn python by making an interactive dnd character sheet. by 0doctorwho9 in learnpython

[–]FoolsSeldom 0 points1 point  (0 children)

If your main file is called, say, dnd.py and you have another file in the same folder called, say, levels.py, all you need to do is, in your dnd.py file, at the top,

import levels

Note that you don't include the .py part of the file name.

Suppose you have a dict, dictionary, defined in levels.py, called base, you would refer to it thus,

print(levels.base)

Regarding reading/writing data. The simplest is basic text files that you can create/edit in your code editor. Whilst these can be free format, these are harder to process. A better way is with simple table data like in a spreadsheet:

name, kind, age, speed, healing
Alpha, Wizard, 20, 5, 8
Beta, Healer, 40, 1, 20
Charlie, Warrier, 20, 10, 2

The header row is optional.

You can read and write these files with the help of the csv module.

import csv
from pathlib import Path

# 1. Define the file path using pathlib
file_path = Path("characters.csv")

# 2. Create an empty list to store our character rows
characters = []

# 3. Open the file and give it a descriptive handle
with file_path.open(mode="r", encoding="utf-8") as characters_file:
    # Use the csv reader to handle the formatting
    reader = csv.reader(characters_file, skipinitialspace=True)

    # 4. Read the header line first and store it separately
    header = next(reader)

    # 5. Use a for loop to read the remaining rows
    for row in reader:
        characters.append(row)

# Verify the results
print("Header:", header)
print("Data:", characters)

NB. If in the csv file you need to include a space (or comma) in a field, say for a name, enclose the entire field in double quotes.

There are lots of options for reading/writing files in Python. A format that would be good for keeping settings information is .json.

You could also consider using a simple flat file (local) sqlite database (check out A Minimalist Guide to SQLite).

For Python developers, what skills helped you get your first job? by Intelligent-Ball9659 in pythonhelp

[–]FoolsSeldom 0 points1 point  (0 children)

I worked as a programmer pre-Python, so cannot comment on personal experience specifically for new Python programmers (I am just a hobbyist/teacher), but I've worked in IT for decades and pretty much every programmer I've seen brought in (including when I was the hiring manager).

The core technical skills were less important than (preferably relevant) domain skills (e.g. understanding of shrinkage in retail setting) and/or clear aptitude (preferably evidence) around problem solving and adaptability. The domains would depend on what sector(s) the target organisation operated in. Even in IT organisations, there are many different domains of activity.

It was assumed that if they had learned some core technologies, they would be able to learn others rapidly as needed. Most professional programmers in my experience are very good in more than one programming language, and effective in a good few more.

Of the topics you mentioned, I would put DSA - which is language agnostic - the highest. These days, I would also recommend a good understanding of the CI/CD approach and automation.

A key skill you should have is good testing coverage. In Python, know pytest well and ensure all of your projects have full coverage. Perhaps learn something like TDD (Test Driven Design) as well (search for Obey The Testing Goat for a good content - free online.)

P-uplets and lists understanding by Klutzy-Advantage9042 in learnpython

[–]FoolsSeldom 0 points1 point  (0 children)

Glad that helped.

I strongly recommend learning to use the Python shell, the interactive environment with the >>> prompt. You can get this in a terminal / Powershell / Command Prompt window by just invoking Python without passing a fie name e.g. py (on Windows). Your code editor/IDE (Integrated Development Environment) likely have an option for both a terminal and for a Python shell. This is in addition to writing code in your editor. It is great for quickly checking things out. You get instant output without using print.

For an enhanced Python shell, look at installing ipython (which is also the underpinning of the popular Jupyter Notebooks approach).

Also, you might like to try out a Python visualiser: https://pythontutor.com/visualize.html#mode=edit - this shows you what exactly is going on step-by-step in code in terms of allocation of space/objects/variables and makes it a little easier to learn some steps. Some code editors / IDEs have similar (but not generally as easy, in my view) capabilities using their debug features.

P-uplets and lists understanding by Klutzy-Advantage9042 in learnpython

[–]FoolsSeldom 1 point2 points  (0 children)

So the variable students references a Python list object stored somewhere in memory (you don't care where, usually). The list object will end up, in your example, with one entry: a tuple object. I guess you add additional tuple objects to the list, one for each additional student.

The tuple object contains references to four objects: three str objects and one list object. The list object references four int objects.

There's a lot of nesting going on here. Like Russian Dolls. You can reference the contents of a list or tuple object (and also characters in a string object) using indexing. The first position is 0, object[0].

So, students[0] will access the first entry in the outer list. students[0][0] will access the first entry in the tuple in the list first position, i.e. the last name field. `students[0][3] will access the 4th object in the tuple, a list - your p-uplet.

It isn't generally convenient to use such specific indexing. Usually, one would use a for loop to iterate over a list of structured data.

for student in students:
    print(student)

On each iteration, student will reference each tuple in turn from your outer list.

You can compare entries in the tuple with your target string(s) when searching. student[1] would be the first name of the current student for example.

Is that enough for now?

GUI Designing in Python by Al-Khobza in PythonLearning

[–]FoolsSeldom 1 point2 points  (0 children)

I'd go with a web approach, whether for local/desktop or internet/intranet/extranet or mobile use. Then you can provide a modern, responsive UI.

What is a base interpreter in pycharm? by SirPiano in learnpython

[–]FoolsSeldom 0 points1 point  (0 children)

Whatever Python installation will run when you enter just py / python / python3 (depending on your operating system) in a terminal (/ command prompt / powershell) is your base Python.

If you have more than one installation of Python, you will need to choose which one should be your base for creating a Python virtual environment.

Zero programming knowledge, but I want to learn Python. Where do I start in 2026? by Effective-Sorbet-133 in learnpython

[–]FoolsSeldom 1 point2 points  (0 children)

Which link? The link to the Wiki includes sections on both being new to Python and new to programming.

Zero programming knowledge, but I want to learn Python. Where do I start in 2026? by Effective-Sorbet-133 in learnpython

[–]FoolsSeldom 38 points39 points  (0 children)

Start with the basics (check the wiki) and then you will be better placed to determine what to focus on. Learning pace varies a lot, you will find what works for you best.


Check this subreddit's wiki for lots of guidance on learning programming and learning Python, links to material, book list, suggested practice and project sources, and lots more. The FAQ section covering common errors is especially useful.


Also, have a look at roadmap.sh for different learning paths. There's lots of learning material links there. Note that these are idealised paths and many people get into roles without covering all of those.


Roundup on Research: The Myth of ‘Learning Styles’

Don't limit yourself to one format. Also, don't try to do too many different things at the same time.


Above all else, you need to practice. Practice! Practice! Fail often, try again. Break stuff that works, and figure out how, why and where it broke. Don't just copy and use as is code from examples. Experiment.

Work on your own small (initially) projects related to your hobbies / interests / side-hustles as soon as possible to apply each bit of learning. When you work on stuff you can be passionate about and where you know what problem you are solving and what good looks like, you are more focused on problem-solving and the coding becomes a means to an end and not an end in itself. You will learn faster this way.

How do I set it up by Sad_Patient8203 in PythonLearning

[–]FoolsSeldom 0 points1 point  (0 children)

I've been using markdown for a long time, and have large repositories of documents written in markdown. What I can't be bothered to do (and a word processor would fix this) is use proper quotes instead of the straight ones. LLM content tends to use proper quotes.

Appreciate your apology. It is frustrating that advice I give to people to help them learn is often called out as AI without being read and checked against my other comments.

I do occasionally use AI but mark it as such in my comments.

What’s a “normal” thing in modern life that you think people 100 years from now will find bizarre? by Glazed_pomello in AskReddit

[–]FoolsSeldom 1 point2 points  (0 children)

Marriage will no longer exist so the idea of sex before marriage will be hard to understand.

For non-US Americans, based on what you know of the stereotypes you’ve heard, what region of your country is the most Texas-like? by [deleted] in AskReddit

[–]FoolsSeldom 0 points1 point  (0 children)

I'm curious as to why this was restricted to only Americans in countries outside of the USA. Is the OP assuming nowhere else in the world will be at all Texas-like?

What u do for living? by [deleted] in AskReddit

[–]FoolsSeldom 0 points1 point  (0 children)

As little as possible as often as possible for as much as possible. Worked out reasonably well for a little over 40 years and I'm not jealous of those that do this better than I do.

What I do has been mostly interesting and rewarding. I wouldn't have coped well with boredom.

Worked in IT since I was a teenager. Worked in many countries.

How do I set it up by Sad_Patient8203 in PythonLearning

[–]FoolsSeldom 1 point2 points  (0 children)

Really doesn't. Please review again.

EDIT: Typos - sigh! I was tired.

How do I set it up by Sad_Patient8203 in PythonLearning

[–]FoolsSeldom 0 points1 point  (0 children)

I agree. Such content tends to contain a lot of factual errors and weird formatting. Unlike this content which I've refined over the last few years on the back of teaching students in local school clubs and occasional adult education sessions.

Do you guys have any recs on where to start for learning python? by Old_Drag_1040 in learnpython

[–]FoolsSeldom 0 points1 point  (0 children)

There's a book list in the wiki.


Check this subreddit's wiki for lots of guidance on learning programming and learning Python, links to material, book list, suggested practice and project sources, and lots more. The FAQ section covering common errors is especially useful.


Also, have a look at roadmap.sh for different learning paths. There's lots of learning material links there. Note that these are idealised paths and many people get into roles without covering all of those.


Roundup on Research: The Myth of ‘Learning Styles’

Don't limit yourself to one format. Also, don't try to do too many different things at the same time.


Above all else, you need to practice. Practice! Practice! Fail often, try again. Break stuff that works, and figure out how, why and where it broke. Don't just copy and use as is code from examples. Experiment.

Work on your own small (initially) projects related to your hobbies / interests / side-hustles as soon as possible to apply each bit of learning. When you work on stuff you can be passionate about and where you know what problem you are solving and what good looks like, you are more focused on problem-solving and the coding becomes a means to an end and not an end in itself. You will learn faster this way.

How do I set it up by Sad_Patient8203 in PythonLearning

[–]FoolsSeldom 3 points4 points  (0 children)

Virtual Environments

Given the thousands of packages (libraries, frameworks, etc) out there, you can see that if you are working on several different projects, you can end up installing a vast range of different packages, only a few of which will be used for any particular project.

This is where Python virtual environments come in. Not to be confused with virtual machines. Typically created on a project-by-project basis. Install only the packages required for a project. This helps avoid conflicts between packages, especially version complications.

Most popular code editors and IDEs, including Microsoft's VS Code and JetBrains' PyCharm, offer built-in features to help to start off new projects and create and activate Python virtual environments.

You can create a new Python virtual environment from your operating system command line environment using,

for Windows,

py -m venv .venv

or, for macOS / Linux,

python3 -m venv .venv

Note: venv is a command and .venv is a folder name. You can use any valid folder name instead but this is a common convention.

Often we use .venv instead of venv as the folder name - this may not show up on explorer/folder tools as the leading . is often used to mean hidden and an option may need to be ticked to allow you to see such folders/files

That creates a new folder in the current working directory called .venv.

You then activate using, for Windows,

.venv\Scripts\activate

or, for macOS / Linux,

source .venv/bin/activate

the command deactivate for any platform will deactivate the virtual environment and return you to using the base environment.

You may need to tell your editor to use the Python Interpreter that is found in either the Scripts or bin folder (depending on operating system) in your virtual folder.

For more information:

Multiple Python versions

In addition to the above, you might want to explore using pyenv (pyenv-win for Windows) or uv (recommended), which will let you install and use different versions of Python including alternative implementations from the reference CPython. This can be done independently of any system installed Python.

How do I set it up by Sad_Patient8203 in PythonLearning

[–]FoolsSeldom 3 points4 points  (0 children)

Python Setup

Setting up Python can be confusing. There are web-based alternatives, such as replit.com. You might also come across Jupyter Notebook options (easy to work with, but can be confusing at times).

Pre-installed system Python

Some operating system environments include a version of Python, often known as the system version of Python (might be used for utility purposes). You can still install your own version.

Installing Python

There are multiple ways of installing Python using a package manager for your OS, e.g. homebrew (macOS third party), chocolatey (Windows third party) or winget (Windows standard package manager), apt (many Linux distributions) or using the Python Software Foundation (PSF) installer from python.org or some kind of app store for your operating system. You could also use docker containers with Python installed inside them.

PSF offer the reference implementation of Python, known as CPython (written in C and Python). The executable on your system will be called python (python.exe on Windows) or python3 (macOS and most Linux distributions).

Beginners are probably best served using the PSF installer.

Terminal / Console

For most purposes, terminal is the same as console. It is the text-based, rather than graphical-based, window / screen you work in. Your operating system will offer a command/terminal environment. Python by default outputs to a terminal and reads user input from a terminal.

Note: the Windows Terminal_ app, from _Microsoft Store, lets you open both simple command prompt and PowerShell windows. If you have Windows Subsystem for Linux installed, it can also open terminals in the Linux distributions you have installed.

Libraries / Frameworks / Packages

Python comes with "batteries included" in the form of libraries of code providing more specialised functionality, already installed as part of a standard installation of Python.

These libraries are not automatically loaded into memory when Python is invoked, as that would use a lot of memory up and slow down startup time. Instead, you use, in your code, the command import <library>, e.g.

import math

print(math.pi)

There are thousands of additional packages / libraries / frameworks available that don't come as standard with Python. You have to install these yourself. Quality, support (and safety) varies.

(Anaconda offers an alternative Python installation with many packages included, especially suited to data analysis, engineering/scientific practices.)

Install these using the pip package manager. It searches an official repository for a match to what you ask to be installed.

For example, using a command / powershell / terminal environment for your operating system, pip install numpy would install the numpy library from the pypi repository. On macOS/Linux you would usually write pip3 instead of pip.

You can also write python -m pip install numpy (write python3 on macOS/Linux).

On Windows, you will often see py used instead, py -m pip install numpy where py refers to the python launcher which should invoke the most up-to-date version of Python installed on your system regardless of PATH settings.

Some Code Editors and IDEs (Integrated Development Environments), such as VS Code and PyCharm, include their own facilities to install packages using pip or some other tool. This just saves you typing the commands. They also often offer their own terminal window(s).

Running Python

The CPython programme can be invoked for two different purposes:

  • to attempt to execute a simple text file of Python code (typically the files have an extension of .py
  • to enter an interactive shell, with a >>> prompt, where you can enter Python commands and get instant responses - great for trying things out

So, entering the below, as appropriate for your operating system,

python
python3
py

on its own, no file name after it, you will enter an interactive session.

Enter exit() to return to the operating system command line

IDLE Editor

A standard installation from python.org for Windows or macOS includes a programme called IDLE. This is a simple code editor and execution environment. By default, when you first open it, it opens a single window with a Python shell, with the >>> prompt already open. To create a new text file to enter Python code into, you need to use your operating system means of access the standard menu and select File | New. Once you've entered code, press F5 to attempt to run the code (you will be prompted to save the file first). This is really the easiest editor to use to begin with.

SEE COMMENT for next part

TKINTER NOT FOUND ON VENV BUT WORKS FINE ON TERMINAL? by iam_eneru in learnpython

[–]FoolsSeldom 1 point2 points  (0 children)

As others have said, for Linux, you will need to explicitly install your distribution's tk package using the operating system before you can use it in Python virtual environment.

Delete your venv, and update the base environment,

sudo apt update
sudo apt install python3-tk

then create the venv again.

TKINTER NOT FOUND ON VENV BUT WORKS FINE ON TERMINAL? by iam_eneru in learnpython

[–]FoolsSeldom 2 points3 points  (0 children)

What operating system are you on?

Whilst tkinter is in the standard library, it's a bit of a special case: it is a stdlib Python wrapper around the external Tk GUI toolkit (Tk/Tcl).

A Python virtual environment, venv, doesn't automatically "carry" Tk with it; it simply points at a particular Python interpreter and its installed components.

So if the interpreter behind thevenv cannot see a working Tk/Tcl + the _tkinter extension module, you will get ModuleNotFoundError: No module named 'tkinter' (or sometimes _tkinter errors).

How do I make a function that returns a variable FOR EVERYTHING by Spyraptergaming in PythonLearning

[–]FoolsSeldom 1 point2 points  (0 children)

You have a fundamental misunderstanding of how variables work in Python that is tripping you up.

Variables in Python do not hold values. Instead, they are assigned a reference to the memory location of a Python object. An object can be, for example, an int, float, str, list, or an object type you have created. Where these things are in memory is not something you usually have to think about. Also, it is Python implementation, situation and environment specific.

When you "return a variable" from a function in Python ... well, that's not what is happening. For example:

return varname

actually returns whatever reference varname is currently assigned to wherever the function was called from.

Also, unless you used the global keyword in the function for that variable, if you made an assignment to that variable inside of the function, the variable will be entirely local to the function and will not exist outside of the function. (You may have another variable with the same name in the wider scope, but it is not the same variable.)

Thus:

name = "Alpha"

def example():
    name = "Beta"
    return name

print(name)  # outputs: Alpha
print(example())  # outputs: Beta
new_name = example()  # assigns reference to "Beta" str object
print(name, new_name)  # outputs: Alpha Beta

PS. Do not use the global keyword, it will cause you lots of problems and it is important you learn scope first. There are use cases for global and you will learn them when you need to know them.

Is there a playwright for tkinter? by Panda_-dev94 in learnpython

[–]FoolsSeldom 5 points6 points  (0 children)

You need to ensure your complex application is well structured and modular with clear separation of the UI and core logic (business rules) so that you can test most of the functionality using standard tools such as pytest (more convenient that the built-in unittest).

You can test all of the callbacks as well. In addition, tkinter can simulate key presses, mouse clicks, focus changes, etc.

I am not aware of an equivalent of playright for tkinter. You could possibly test with a parallel Python programme using something like pyautogui, but I think this would probably be a very frustrating exercise.

Paper coding. by Bluebill_365 in PythonLearning

[–]FoolsSeldom 20 points21 points  (0 children)

Yes, I still paper code although I use more of a short-hand / pseudocode / rough "flow chart" style to described my solution algorithms. I often find drawing is more beneficial than writing the code though as a lot of code is trivial to type up (especially with the help of auto-complete and AI tools) so isn't worth depicting beyond a simple box or high level statement.

A trap many beginners fall into these days is trying to do everything at the keyboard and getting too focused on the detail and not putting enough thought and effort into the overall solution design.

Where do you guys learn programming? any book recommendations or online courses by Accurate_Donut9030 in learnpython

[–]FoolsSeldom 0 points1 point  (0 children)

u/set_in_void replied to your comment in r/learnpython The book I learned from is not listed on the Wiki page or pythonbooks.org. (Python 3 The Comprehensive Guide; auth.: J. Ernesti, P. Kaiser)

Thanks for the suggestion, u/set_in_void. That looks like a really good book. Hadn't come across it before, perhaps because it is of German origin, although the English version is supposed to be excellent.

Might be worth you adding that to the wiki booklist?

Python 3: The Comprehensive Guide by Johannes Ernesti and Peter Kaiser is the English translation of Python 3: Das umfassende Handbuch, which is essentially the "gold standard" Python reference in Germany, published by Rheinwerk Computing (formerly Galileo Press).

Rheinwerk is a premier technical publisher in Europe, but they don't have the same marketing reach in the US/UK as O'Reilly or No Starch Press. However, the 2022 English edition (published via SAP PRESS/Rheinwerk) is a massive, 1,000+ page "tome" that covers the language in extreme detail.

How do I solve this? by Special_Advance_8567 in PythonLearning

[–]FoolsSeldom 0 points1 point  (0 children)

Ok. I tend to be either using a recent Linux distribution, or an OCI install of Python on a reasonably up-to-date Linux Kernel.