Python


Python is a great programming language for beginners to start with. It is an interpreted language. This means on runtime the program must first convert to machine code before running the program. One of Python’s great features is that Its syntax is easy to understand and it is formatted in an elegant way. Python is often used as a non object oriented programming language, but it does support objects and classes. To keep things simple any Python code blocks you see should run standalone. 

print("Hello World")

While in Java the same program would be longer. We’ll talk more about Java in a future post.

public class Hello {
    public static void main(String[] args) {
        System.out.println("Hello, world");
    }1
}

While the Java code isn’t terribly long, the Python code is shorter and simpler. That isn’t to say that Java isn’t better in some use cases. I just think that Python is more user friendly for a beginner. Python’s simple and consistent code style means that you can worry less about the code style and more about the code’s functionality.

Python is a dynamically typed language. The types of variables are determined when the program is running. This means that you can either declare a variable without knowing the exact type that it is going to be or explicitly define the type. Some argue this can be a bad thing, but I think, especially for beginners, dynamic typing alleviates the hassle of worrying about the declaring types. Here is an example of declaring a string in Python.

hello = "Hello World"

Relatively recently in version 3.5, Python added type hints. Type hints let the programmer state the type of the variable, such as String or and integer. Python does not enforce the types declared in the hints. They are there to spell out the intention of the person who wrote the code. Type hints are also intended for tools, like IDEs and linters to provide hints, errors, and warning for developers as they write code.

Type hints are useful for function parameters.

# Without type hints 
def greet(name):
    print(f"Hello {name}")

# Same function with type hints
def greet(name: str) -> None:
    print(f"Hello {name}")

One of the nice things about dynamic typing is that it allows a variable type to switch from a String to an integer without having to define another variable.

number = input("Enter a number: ")
print(number)  # Prints String
number = int(number)
print(number)  # Prints an int 

Some people complain that Python is slow. This is true, as Python is much slower than a compiled language. However, most people won’t notice how it is slower for their tasks. Modern computers are fast so even slow languages can be fast. Python will be fast enough for many applications. The standard Python most people use is CPython, but that isn’t the only version of Python that exists. For instance PyPy is Python written in Python. While it doesn’t beat out CPython in every way it is certainly faster in specific areas. For more info check here https://www.pypy.org/

Pros and Cons

  • Pros
    • Beginner friendly
    • Shorter code
    • Garbage collected
    • Dynamic types
  • Cons
    • Not compiled which can mean slower runtime
    • Not as good for object oriented programming
    • Dynamic types (Sometimes a con)

If you want to learn more about Python there are lots of great resources. Zed Shaw has a excellent book and video series called “Learn Python the Hard Way.” His book was how I was first introduced to Python. There is also “https://realpython.com/

Up next: Java!


Leave a Reply

Your email address will not be published. Required fields are marked *