Monday, December 13, 2021

Real Python: Java vs Python: Basic Python for Java Developers

Python is a general-purpose programming language. You can understand its growth in the last couple of years by considering its approachability for learning and its high suitability for data analysis, machine learning, and web development. But what kind of programming language is it? What are some differences when you compare Java vs Python? What can you do with it? And is it really as “easy to learn” as some people claim?

You’ll explore Python from a Java perspective in this tutorial. After you’ve read it, you’ll be able to decide whether Python is a viable option to solve your use cases and to appreciate when you can use Python in combination with Java for certain types of problems.

In this tutorial, you’ll learn about:

  • The general Python programming language syntax
  • The most relevant standard data types
  • The differences and similarities in Java vs Python
  • Resources for high-quality Python documentation and tutorials
  • Some of the Python community’s favorite frameworks and libraries
  • Approaches for getting started with Python programming from scratch

This tutorial is for software developers who are familiar with Java’s inner workings, concepts, terminology, classes, types, collections framework, and so on.

You don’t need to have any Python experience at all.

Free Bonus: Click here to get a Python Cheat Sheet and learn the basics of Python 3, like working with data types, dictionaries, lists, and Python functions.

Where Does Python Come From?

Python is a programming language that was developed by Guido van Rossum. He was looking for a hobby programming project that would keep him occupied during the Christmas holiday in 1989, so that’s when he started developing the Python interpreter.

Python has its origins in a number of languages: ABC, C, and Modula-3. It’s basically an object-oriented, imperative programming language.

Depending on your preference and desired functionality, it can be applied in a full object-oriented style or in a procedural programming style with functions. The object-oriented capabilities are addressed later in this tutorial.

Note: Just for clarity, from a Java perspective, Python functions are like static methods, and you don’t necessarily need to define them within a class. Later on, you’ll see an example of a Python function definition.

Additionally, a more functional programming style is also perfectly possible. To learn more, you’ll want to explore Python’s functional programming capabilities.

In early 2021, TIOBE announced Python as the programming language of the year for the fourth time. As of the 2021 Octoverse report, Python ranks as the second most popular language on GitHub by repository contributors.

What Is the Philosophy of Python?

Soon, you’ll get hands-on with Python in the sections following this one. First, however, you’ll explore why it’s worth getting to know Python better by going through some features that you can trace back to Python’s philosophy.

Some ideas behind Java and Python are similar, but every programming language has its own unique characteristics. The philosophy of Python is captured as a collection of nineteen guiding principles, the Zen of Python. Python hides a few Easter eggs, and one of them is the Zen of Python. Consider what happens when you issue the following command in the Python read–eval–print loop (REPL):

>>>
>>> import this
The Zen of Python, by Tim Peters

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!

While you shouldn’t take the statements above too literally, a few of these relate directly to the characteristics that you’ll walk through next.

Note: The Python read–eval–print loop is explained later on in this tutorial.

By considering the guiding principles of the Zen of Python, you’ll get a good idea about how you can approach working with the language.

Python Code Is Readable

If you come from a Java background and you look at a typical fragment of Python code, you might think that you’re looking at pseudo-code. There are a few factors that contribute to this:

  • Indentation is used for statement grouping. This makes code blocks shorter and promotes a uniform coding style. You’ll find more on this topic later on.
  • A few built-in high-level data structures, combined with a modest set of operator symbols, make Python very expressive.
  • The choice of using exceptions as the primary way to deal with errors keeps the code clean.
  • Python programmers prefer a coding style inspired by the concept that it’s Easier to Ask for Forgiveness than Permission (EAFP) instead of the Look Before You Leap (LBYL) concept. This style puts emphasis on the normal, happy path of your program, and you’ll figure out how any anomalies are handled afterward.

You can find a few examples of how this appears in practice during this tutorial, as well as in other linked resources.

Read the full article at https://realpython.com/java-vs-python/ »


[ Improve Your Python With 🐍 Python Tricks 💌 – Get a short & sweet Python Trick delivered to your inbox every couple of days. >> Click here to learn more and see examples ]



from Planet Python
via read more

No comments:

Post a Comment

TestDriven.io: Working with Static and Media Files in Django

This article looks at how to work with static and media files in a Django project, locally and in production. from Planet Python via read...