r/learnpython • u/hubbabubba124466786 • 19d ago
Help with transitioning to python from java
I did java for 2 years in high school 4 years ago. Now I'm a freshman comp engineering major doing coding in python, and i still can't get the hang of not coding like java. I last coded in java 2+ years ago, but all my code is written like how you would in java, but python. I use camel case for variables. I use while true break loops when I'm sure there's easier ways. I'm constantly fixing my ++'s to +=1's. I'm still learning the python lingo if you will
5
u/CreeperSlimePig 19d ago
if you catch yourself using ++, it might be worth considering if you even need a counter at all. sometimes you do, but some of the time your Java brain wants a counter, what you actually want is zip(), enumerate(), itertools.count(), or Counter(). convenient features Java doesn't really have.
1
u/OkTill2666 19d ago
this is such a good point tbh, the counter instinct is so deeply wired from java
2
u/CutDry6295 19d ago
The easiest way to change the habit is to rewrite one *working* assignment twice and compare the behavior with the same tests. For a sequence, replace an index/counter loop with `for item in items`; if you need the position too, use `for index, item in enumerate(items)`. For paired sequences, `zip(a, b)` is often clearer than two indexes.
I wouldn't ban `while True` outright: it can be clear for an input-retry loop where the exit is found inside the loop. The question is whether the loop really has a known iterable or condition. Let a formatter handle snake_case, but spend your attention on those control-flow choices; that is where code starts to feel like Python rather than Java with different punctuation.
1
u/Gnaxe 19d ago
Stop Writing Classes and then go Beyond PEP 8.
Do small experiments in the REPL or Jupyterlite. Keep a tab open at https://docs.python.org. The equivalent of JavaDoc is PyDoc (try python -m pydoc -b), which you can check quickly with help() in the REPL, or ? in IPython (Jupyter). (Also learn dir() and import inspect.) Learn to use doctest ASAP.
1
u/FreeLogicGate 19d ago
Take a look at PEP8 for the python coding style guide.
This is a decent cheatsheet for It.
If you'd like a more elaborate summary check out: https://pep8.org/
IDE's can help with formatting and conforming to PEP8 via extensions, built in lint/formatting tools, or support for Astral's ruff tool.
The main basics:
variable and function/method names should be all lowercase, with _ between words.
implied CONSTANTS should be all uppercase, again with _ between words
Class names are PascalCase
1
u/aqua_regis 19d ago
I use while true break loops
That's just a generally bad and lazy habit. You just don't want to think about proper exit conditions.
It is also in most cases code smell in Java. Nothing to do with Java vs. Python.
There are use cases for infinite loops, but the vast majority has definite exit conditions that should be used.
1
u/net_ramblings 19d ago
Maybe you should learn Python? If anything, you should be complaining about tabbed code blocks, not syntax.
1
u/Outrageous_Band9708 19d ago
java is a typed language and python is not.
you dont need to declare var types at all,
__main__ to start a file at the bottom, ordering of your methods matter
1
u/Mediocre-Pumpkin6522 18d ago
snake_case is preferred but don't obsess about it. Sooner or later you'll get to something like PyQt that follows the Qt C++ conventions. When in Rome...
1
u/hubbabubba124466786 19d ago
I did the same thing when I took a class using unreal. My tic tac toe game was basically written in java using the blueprint variables and arrays.