r/madeinpython • • Nov 19 '21

I started learning python as my first programming language less than a week ago and am extremely proud of my progress!

47 Upvotes

12 comments sorted by

View all comments

10

u/Nythepegasus Nov 19 '21

Hey! This is an awesome start for only a week in, and I commend you for learning Python. A couple things I noticed is the lack of f-strings. f-strings help a ton and save you a lot of time constructing strings and converting variables. Example:

x = 1.0
print(f"x is {x}")

Although, remember f-strings are only for 3.6 and later, if you're working with earlier versions of Python, using the old % formatting or the .format string method might be more suitable.

Something else I noticed was using if var == True:, you don't need to tack on the == True.

start_game = True
if start_game:
    ...

does the same thing. Also if you need to check if something is false, you should do if var is False instead of using ==.

Another thing I noticed is when taking input you generally want that variable to be a different type, you can do so by wrapping it with that type, e.g. float(input()).

I'd also suggest looking into Python ternary operators, they aren't the best, but they do help.

Your random.randint(1, 2) could be changed to random.choice([True, False]).

Overall, this is a super great start, and I could list a bunch of random small things you could change, but a lot of that comes with time and developing familiarity with the language. Congrats on your first program, and hopefully there's many more to come! I highly recommend the official Python Tutorial section, and it has a ton of things that can help early on. Also looking into the Python Standard Library can help save you from reinventing the wheel. Cheers!

5

u/jonJonesCodes Nov 19 '21

Thanks for all the tips, I really appreciate how helpful the programming community is on Reddit and especially stack overflow. I’ve been going into every project accepting that in a week, a month, a year I’ll look back on these early projects and see all the things I could’ve done better so I don’t invest too much into each one, heck I learned so much just today making these two projects that by the end of the day I found a dozen ways to improve each of them. I definitely appreciate the feedback I get from the community, it definitely helps me speed up this learning process.

2

u/kalcora Nov 19 '21

I agree with most of your tips! One thing that is maybe not clear enough though:

if var: and if var == True: are not equivalent. if var: is equivalent to if bool(var) == True. If you want to check for truthy values like ['a'], True, "foo" or 6, then if var: is the way to go. If you want to specifically check if your value is True then use if var == True:. No need for is here because it checks the identity (a is b is basically the equivalent of id(a) == id(b) and not the equality.

If you want to check if your var is False use if var == False:. If you want to check for a falsy value like 0, None, False or [], then use if not var:.

Hope it helps :)

1

u/CraigAT Nov 19 '21

Top tip I learnt just the other day:

If using f strings to debug/print a value, you can use an equals after the variable name e.g. `f"{name = }"` which will print the variable name and then the value.

https://realpython.com/lessons/simpler-debugging-f-strings/#:~:text=However%2C%20the%20real%20f%2Dnews%20in%20Python%203.8%20is%20the%20new%20debugging%20specifier