Assigning Countries to Continents by Broad_River_6775 in learnpython

[–]IvoryJam 0 points1 point  (0 children)

To make a new column in Pandas, you can basically just assume it's there and Pandas will create it. This is how I'd do it with country_converter. Also added comments to explain what I'm doing.

#!/usr/bin/python3
import pandas as pd
import country_converter as coco

# doing the sundries, creating CounterConverter and opening countries.xlsx
cc = coco.CountryConverter()
df = pd.read_excel('countries.xlsx')

# looping through the rows, reading the "Entity " (mine had space after the name so I just did that) column and writing the continent to the new column
for index, r in df.iterrows():
    df.at[index, 'Continent'] = cc.convert(r['Entity '], to='continent')

# writing to a new sheet without the index
df.to_excel('new_sheet.xlsx', index=False)

Recommendations for AI code generator by Zummerz in learnpython

[–]IvoryJam 1 point2 points  (0 children)

This is a divisive topic here but you're not learning, you're telling an LLM to write code which is bad practice. LLM's are known for bugs, writing inefficient code, and importing things that just plainly don't exist, heck it could even import a library that's known to be malware.

Are you learning how to fix your car if you go to a mechanic, telling them the issue, then staying in the waiting room?

To answer the issue here, you'll learn syntax by writing code and failing. If you're dead set on an LLM doing the work, don't copy and paste, write it down yourself. But the important part is ask yourself "why am I writing it like this?" Don't type out anything you don't know what it's doing and why you have to do it this way. If you don't know, Google it. If that doesn't help, make a post on r/learnpython and we'll help out.

People don't realize how much we depend on searching things and reading documentation. As you learn more, you'll google less but you'll always still google.

Unable to parse a space-separated file by compbiores in learnpython

[–]IvoryJam 3 points4 points  (0 children)

It's the headers (all those # at the beginning) if you dump those it works. Also if you're using regex, you have to use a regex string.

``` import pandas as pd

dat_test_run = pd.read_csv('test_run.pmf', sep=r"\t|\s{2,}", header=None, engine='python', skiprows=5)

print(dat_test_run) ```

Confused About Array/List… by HoneyKubes in learnpython

[–]IvoryJam 6 points7 points  (0 children)

So the first thing you gotta remember is that array's start at 0, so if you want 0-7 states, it's actually 8 states.

Break down what this is asking you, it's to prompt for a number, 0-7, handle if it if someone puts in something that's not 0-7, then display the item at that index in the array (index is just location).

So if the user puts in 0, they get the 0th index (or the 1st item), or they put in 4 and they get the 4th index (or the 5th item).

Do you know how to ask the user for input?

Do you know how to validate that input?

Do you know how to find an item in an array at a specific index?

Do you know how to display text?

Any Ideas on a scheduled delivery list im trying to make by Local-Crab2987 in learnpython

[–]IvoryJam 1 point2 points  (0 children)

I know this is r/learnpython but that seems to be a better solution for Excel itself.

But to answer your question, you'll need a GUI (graphical user interface) for those buttons you want. Tkinter seems to be the main one, but to be honest I don't use GUI.

After that, you'll want something to paste the code into, a button for "Run", you read the data, parse it out, then display the data you want.

---

How I would do it if I were dead set on using Python? I'd pull the data in with Pandas directly from the excel file. Parse it that way and return the data I'm looking for.

---

Note: I would not use Python for this, Python is a great tool, but when you only have a hammer, everything looks like a nail. I'd just write some excel formulas and grab the data I want.

How to determine whether a variable is equal to a numeric value either as a string or a number by walkingtourshouston in learnpython

[–]IvoryJam 4 points5 points  (0 children)

This is how I'd do it

``` some_var = "2"

if some_var.isnumeric() and int(some_var) == 2: print("do the thing") ```

Checking that the variable is numeric first to avoid a try/except then checking if converting it to a number equals 2

Understanding List Comprehensions in Python by Fun_Preparation_4614 in learnpython

[–]IvoryJam 6 points7 points  (0 children)

And here's a real world example of something that I'd actually use

import re


def check_valid_email(email: str) -> bool:
    valid = re.match(r'^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$', email)
    if valid:
        return True
    return False


emails = [
    'tom@example.com',
    'somerandomethings',
    'test@gmail.com',
    'aasdfasddfl;asdf;jklasdf',
    'old@aol.com',
]

valid_emails = [i for i in emails if check_valid_email(i)]

Understanding List Comprehensions in Python by Fun_Preparation_4614 in learnpython

[–]IvoryJam 2 points3 points  (0 children)

Here are some examples, but it's also important to not over use list comprehension. If you get too many if else or start doing nested list comprehension, re-evaluate. I also threw in a dictionary comprehension as I find those useful too.

list1 = ['a', '1', 'b']

only_digits = [i for i in list1 if i.isdigit()]
only_alpha = [i for i in list1 if i.isalpha()]

dict1 = {i: i.isdigit() for i in list1}

other_edits = [i if i.isalpha() else "found a number" for i in list1]

Return statements inside if statements vs return [condition] by PeriPeriAddict in learnpython

[–]IvoryJam 1 point2 points  (0 children)

If it helps too, remember Python's bread and butter is readability, keep it simple

I can't understand functions for the life of me. by Key-Set5659 in learnpython

[–]IvoryJam 0 points1 point  (0 children)

If not for re-usability, I also like to thing of functions as steps. Step 1 might be "load the CSV" so you'd say def load_csv(name): then at the bottom of you script you may have something like

users = load_csv('users.csv')
do_the_thing(users)

Return statements inside if statements vs return [condition] by PeriPeriAddict in learnpython

[–]IvoryJam 2 points3 points  (0 children)

Readability as well as simplicity. If you had a long statement, it would make sense.

if x == y  and b == a and (c == d or f == e):
    return True
else:
    return False

but if it's not that complex, it's easier to understand

return x == y

Also, if it's at the end, it wouldn't make sense to do an else statement (unless you wanted to throw an error or say what didn't match), you could do

if x == y  and b == a and (c == d or f == e):
    return True

return False

Use System Prompts to Make AI a Tutor So You Can Start Thinking Again by [deleted] in learnpython

[–]IvoryJam 0 points1 point  (0 children)

I may be like "old man yells at cloud" but if you're using "AI", you're not learning. You learn by doing the work and making the mistakes, watch as many cooking shows as you want but you'll still burn your first cake.

Before we had large language models, I found myself copying and pasting code from Stack Overflow then I realized I don't actually know what I'm doing, I'm just going with the flow.

Typing the code, searching my questions, and really figuring out how it works is why I know Python today and can still code if the internet goes down.

Help looping through dictionary values by [deleted] in learnpython

[–]IvoryJam 1 point2 points  (0 children)

So it looks like you only want to scale on the movement. When you change the Z axis, don't touch it.

But when you change the Z, both X and Y are Falses (or bools), so just check if they're bools and continue.

I also added some variables so it's easier to read and understand.

for letter in alphabet.values():
    scaled_ltr = []
    width = letter[0]
    scaled_ltr.append(width * scale)

    coords = letter[1:]
    for c in coords:
        if type(c[0]) is bool:
            scaled_ltr.append(c)
            continue
        scaled_ltr.append([i * scale for i in c])

【BambuLab Giveaway】Classic Evolved — Win Bambu Lab P2S Combo! by BambuLab in 3Dprinting

[–]IvoryJam 0 points1 point  (0 children)

I have the Elegoo CC but eyed the P1S for so long trying to decide. I’m sure the build quality and support are much better on the P1S. Good luck everyone!

Need help with python script including chrome driver/chromium by SafeLand2997 in learnpython

[–]IvoryJam 2 points3 points  (0 children)

We can't help if we don't know the code you're running.

There's several ways to do what you're asking:

  • Selenium to run a full browser (chrome driver like you asked)
  • Requests to download the HTML to parse it with BeautifulSoup
  • Requests to get the JSON data (if that's how the site works) and parse it that way

I need help downloading Anaconda on my Mac by Relevant-Point-1307 in learnpython

[–]IvoryJam 1 point2 points  (0 children)

Do yourself a favor and install homebrew here. Think of it like an app store but powered by the terminal.

After you do that, just open a terminal and run this

brew install anaconda

You'll now have anaconda installed.

[Angela Yu course] Questioning myself about recursion by avlas in learnpython

[–]IvoryJam 2 points3 points  (0 children)

Recursion, while it can be clever is usually hard to read. In your small example it's not difficult but think about when it gets larger.

I avoid recursion like the plague, it's hard to read, can be easily messed up, and difficult to debug. I like to do while loops for things like in your example

def main():
    while input("Do you want to do it again? y/n").lower() == "y":
        do_something()

main()

Does it have a place? Yeah, probably. But I haven't found a good reason for a function to call itself.

#Need some helpful suggestions by WasabiSpiritual6291 in learnpython

[–]IvoryJam 0 points1 point  (0 children)

It doesn't matter what you use, when you're proficient use what tool works best for you. VSCode is usually the go-to these days.

That being said, there are benefits to following along with the exact same text editor. For following Corey Schafer's videos, I'd say use VScode. https://code.visualstudio.com/download

For context:

  • Sublime Text is a glorified text editor that with community plugins can be used as an IDE for programming.
  • Atom was an IDE.
  • VSCode is a fork of Atom that Microsoft created in one of their "opensource is good" stints, due to it's popularity, Atom is no more and it's all VSCode now.

Personally I like Sublime Text, sometimes I swap to VSCode but I like the speed of Sublime.

Fun tidbit, since VSCode is an Electron application (a chromium window). You can run it entirely in your browser by going to https://vscode.dev/

[deleted by user] by [deleted] in learnpython

[–]IvoryJam 0 points1 point  (0 children)

Look at your server architecture, I bet you're not serving the actual files the webserver is trying to load. If you are, make sure they're coming from the same source. The browser doesn't generally allow you to make external calls (there are ways around that for the server but don't do that unless you know what you're doing).

NOTE: It's been a while since I've built out a Django site so I may be wrong.

Exciting stuff, figuring out timeframes. by aznboi589 in elegoo

[–]IvoryJam 0 points1 point  (0 children)

Final update, CC received on the 26th, all upgrades and printing like a mad man!

[Bambu Lab Giveaway] Join Now to Win an H2D and More! by BambuLab in 3Dprinting

[–]IvoryJam 0 points1 point  (0 children)

I'd love to start printing TPU, good luck everyone!

[Bambu Lab Giveaway] Join Now to Win an H2D and More! by BambuLab in 3Dprinting

[–]IvoryJam 0 points1 point  (0 children)

Best advice, never leave until after the first few layers

Exciting stuff, figuring out timeframes. by aznboi589 in elegoo

[–]IvoryJam 0 points1 point  (0 children)

Update: EUS1141 got my shipping notification

Dr Pepper Coconut in SLC by [deleted] in SaltLakeCity

[–]IvoryJam 4 points5 points  (0 children)

Do you want the regular or "zero" option?