DEV Community

Austin Harlow
Austin Harlow

Posted on

Learning a New Language

As a recent graduate of the Flatiron School's Web Development program, I have spent the past 4 months working with Ruby and JavaScript. I have been exposed to other languages in some capacity before but I had not tried to learn a new language and framework on my own.

Why Python?

I was on the fence about a new language and decided (for better or worse) to go with a dynamically typed language rather than something like Java which is statically typed. My main interest was to learn a new language that was strongly typed as a change from JavaScript which I have been focusing on heavily for the past couple months.

Brief aside on typed languages

For the purposes of understanding the differences between static and dynamically typed languages all you need to understand is that static languages are more strict and do not allow types to be mutated. Furthermore, strongly typed in this scenario is referring to type errors being prevented at runtime. This basically means that instead of returning undefined like in JavaScript, I would receive an error if I was using incorrect types in Python.

Python Initial Response

Once I got started with Python, I noticed a few things that I liked immediately. List, Dictionary, Set, and Generator comprehensions are things that I very much enjoy using. Although in a general sense it is basically just a way to iterate over these structures, their syntax is very enticing to me. Let's just use a brief example here.

new_list = [1, 2, 3, 4, 5]
new_list = [num for num in new_list if num % 2 == 0]
print(new_list)
#=> [2, 4]

All I did here was iterate over new_list and return only the even numbers. This is similar to a filter function in JavaScript which we can call on an array.

newArray = [1, 2, 3, 4, 5]
newArray = newArray.filter(num => num%2 === 0)
console.log(newArray)
//=> [2, 4]

However, the list comprehension in python is more powerful than just a filter, it can also perform the same action as a map function in JavaScript.

new_list = [1, 2, 3, 4, 5]
new_list = [num*2 for num in new_list]
print(new_list
#=> [2, 4, 6, 8, 10]

This is just the surface of what comprehensions in Python can do but I am very much a fan of their syntax.

Continuing to learn

For me, one of the best ways for me to learn is to simply use something. In Python, that means writing code and making applications. I have tried to make a few different simple CLI applications to further my understanding of Python. One similarity that made it easier for me to grasp is function definition syntax in both Python and JavaScript

# Python

def foo():
    print('Hello World!')
foo()
// JavaScript

function foo() {
    console.log('Hello World!')
}
foo()

Even though these two functions are a bit different, the way that they are invoked is the same which makes it easier for me to grasp how to actually print out 'Hello World!'.

Next Steps

I am planning to be able to continue to practice Python using the Django web framework. I have started with Django but will need more time to be able to implement it the same way I could with a Rails application right now. I will also likely be writing about Python more as I continue to try to learn it so look out for a few more Python related posts here.

References

Top comments (0)