DEV Community

Cover image for A Quick Peek At Python
Tres Bien
Tres Bien

Posted on

A Quick Peek At Python

Peeking Python Image Credit: Dmitrij Rodionov on Wikimedia Commons

I have begun my coding education with JavaScript, but I'm very interested in Python. Why? Mostly curiosity, as I've heard from several acquaintances that it's the easiest of the languages to learn & use.
So I want to compare a few concepts to decide if I agree.

First of all, what is 'Python'?

"Python is a high level general-purpose programming language."(MDN). It's an interpreted language, meaning that the code can be executed without first being converted into machine language. It is done through a program called an 'Interpreter' (Wikipedia).
It is also an Object-Oriented language, so it "relies on the concept of classes and objects" (FreeCodeCamp). It involves grouping specific data values and code blocks that perform certain actions together inside of the same 'container', but that can be manipulated to create unique versions of themselves. Like a template.
Python also uses a simple syntax so that programs can be written with fewer lines than some other languages (W3Schools).

Variable Creation

Variables are custom names you assign to a single data value, a collection of data values, or even a large collection of code that performs multiple actions.

In JavaScript, you create a variable by prefacing your custom word with 1 of 3 keywords: Var, Let, & Const. You choose based on the Scope you need the variable to be accessed in, and whether you want its value to be mutable or not.
This is followed by the custom name you decide, which should describe the data you want associated with it. Variable names should be written in camelCase: justLikeThisExample.
Then connect the word to the value using '=', an Assignment Operator.
Ex:

const myWord = {'Associated', 'Value'}
Enter fullscreen mode Exit fullscreen mode

In Python, variables are created without prefacing your custom name with any keyword. Scope and mutability are determined by the data type assigned to your keyword. Names, according to this style guide: "should be lowercase, with words separated by underscores as necessary to improve readability." (Python.org), also called 'snake case'. The equal sign is still used to connect your variable name to your data.
Ex:

my_word = {'Associated', 'Value'}
Enter fullscreen mode Exit fullscreen mode

Using Operators

Operators are symbols used to assign data to variable names, comparing data, performing calculations, converting data types, and more. There are multiple categories of Operators, but here I will look at the Arithmetic ones.
For the most part, Operators that perform math equations are identical in JavaScript & Python.
However, Python lacks JavaScripts Increment & Decrement operators, which involves using '++' after a value to increase it by 1 & '--' after a value to decrease it by one.
Ex:

let count = 1;
count++; 
// The value of count is now 2
count--;
// The value of count is now 1 again
Enter fullscreen mode Exit fullscreen mode

While both languages use '*' to multiply one value by another, JavaScript lacks Pythons Matrix Multiplication operator, '@'. This symbol is used specifically for taking in 2 matrices and "multiplying rows of the first matrix to the column of the second matrix."(Geeks For Geeks).

firstMatrice = [[1, 2], [3, 4]]
secondMatrice = [[5, 6], [7, 8]]
multipliedMatrice = firstMatrice @ secondMatrice
// multipliedMatrice will display as:
[[19, 22], [43, 50]]
Enter fullscreen mode Exit fullscreen mode

Python also has an extra function involving the division operator, '/'. When used doubled up, '//' this will return a whole number instead of a decimal.

10 / 3 = 3.33333333
10 // 3 = 3
Enter fullscreen mode Exit fullscreen mode

Function Declaration & Invoking

Functions are code that takes some input data, manipulates it in some way, and produces some form of output. They are called, or invoked, the same way in both languages. But their syntax for creation differs a bit.

In JavaScript, functions are created a few ways, but one of the basic formats is:

function combineParameters (paramter1, parameter2) {
return parameter1 + parameter2;
}
Enter fullscreen mode Exit fullscreen mode

You start with the keyword 'function' followed by your custom name that should describe what the function will do. Next you encase parameters for any inputs you need between parenthesis. Then lest you encase code for what you need to accomplish between curly brackets.

Python uses keyword 'def' for creating functions, or defining them. Def is also followed by a custom name to describe the functions purpose, along with parenthesis to envelop parameters. But after that, instead of curly brackets there is a colon. Then "the statements that form the body of the function start at the next line, and must be indented." (Python.org). So the JavaScript example shown earlier would be rewritten as:

def combineParameters (paramter1, parameter2):
    return parameter1 + parameter2;
Enter fullscreen mode Exit fullscreen mode

Image description

Image from FreeCodeCamp

Overall thoughts

Regarding access to learning, there are tons of resources that eagerly explain using both. Whether dedicated to one language like Python.org, or connoisseurs of many such as Free Code Camp. So I feel like it would be fair to rate them on equal footing concerning educational materials.

Snake Case is more easily readable, in my opinion. I also like the idea of not needing a specific keyword to declare variables (since I have a bad habit of forgetting them, uh oh~)

Operators being mostly the same for both leaves little room for comparison, but Pythons additional '@' operator is very convenient when dealing with matrices. So is only needing '//' to get a whole number.

Personally, I feel that Pythons function defining method is less clear. I like using 'function' as a keyword to clearly mark what I'm creating. And not encasing the code in curly brackets will make it harder (for me) to quickly know the boundaries of a function.

Based on the few concepts I've looked into so far, despite Python having quite a few "quality of life" improvements over JavaScript, I feel that starting with JavaScript was the right choice for me. While I definitely understand the usefulness of Pythons simpler syntax, I struggle with recognizing the connections & boundaries between code, so JavaScript using descriptive keywords & encasing related code in brackets will help me on my coding journey.
Though I greatly look forward to the day I have enough education & experience that removing them will give me no trouble.

Information Sources:

Top comments (0)