DEV Community

Cover image for Introduction to Python
zachmarullo
zachmarullo

Posted on

Introduction to Python

Basic Information about Python

Python is quickly becoming one of the most popular coding languages out today. This is because it is packed with functionality, and is a relatively beginner-friendly language syntactically. In this post, we will explore some of the basic concepts in python, how they relate to JavaScript, and a brief history of how Python came to be as popular as it is today.

History of Python

According to Python Institute, Python was created by Guido van Rossum and originally released on February 20, 1991. Later, in 1999, Guido van Rossum defined his goals for Python as follows:

  • an easy and intuitive language just as powerful as those of the major competitors

  • open source, so anyone can contribute to its development

  • code that is as understandable as plain English

  • suitable for everyday tasks, allowing for short development times.

Another fun fact about the programming language is that even though most think it is in reference to the snake, Python got its name from an old BBC comedy sketch series called Monty Python's Flying Circus. Python did not have as quick of a rise as something like JavaScript, but year after year the number of users of the language continues to increase. According to the Stack Overflow Developer Survey taken in 2022, Python is the fourth most popular language. Python ranks lower than JavaScript, HTML/CSS, and SQL.

Basic Syntax and Operations

One of the notable differences about Python, as it relates to JavaScript, is that there are no "var", "let", or "const" variable declarations. Instead, variables are just named and set to a value with no declarative keyword. As far as actual usage of these variables, they behave very similarly to JavaScript variables. Basic operations like addition, subtraction, division, multiplication, and the modulus operator exist and operate just as they do inside of JavaScript, allowing us to add numbers, concatenate strings, and pretty much any other operation contained within the Python library.

One notable difference between JavaScript and Python is the way comments are written. In Python, a single line comment is written using the hash character(#), and will extend the comment to the end of the current line. For multi-line comments, a triple quotation is used("').

Basic Python Operation Examples

First we will go over simple concatenation as well as printing values in Python. As mentioned earlier, there are no declarative variable keywords, so all the programmer needs to do is name their variable and set it equal to a string or other value.

Variable Declaration and String Concatenation in Python

In the above snippet we have created three variables, each containing a piece of a sentence to be concatenated. Once the strings are stored in the variables, its as easy as creating a fourth variable and setting it equal to the concatenation of the first three. This example will "print" the sentence "This is an example of creating and concatenating in Python!". Pretty simple, right?

Looping In Python

Loops, like in many coding languages, are used to repeat a task several times without having to rewrite the code to do so. Structurally, and in operation, Python's loops are quite similar to that of JavaScript. Let's take a look at a couple examples of these loops.

Loops in Python

In the two examples above, we looped through numbers 1-10, and in the second example, we multiplied a number by two each time and printed it as long as it was less than the value of 100.

Lists in Python

The next concept we are going to cover is lists in Python as well as interacting with them, which are quite similar to JavaScript. Lists can hold different types of data upon initialization, or can be initialized empty and added to at a later time. Just like a JavaScript array, Python's lists are zero-indexed and can be accessed using the index. Below shows a few, but far from all, of the useful methods a programmer can use to interact with values in a list.

List Methods in Python

Dictionaries in Python

In Python, the dictionary is similar to an object in JavaScript, having a key as well as a value. Just like in JavaScript, dictionaries can be more complex than lists, but also have a bit more functionality. Similar to objects in JavaScript, you can't have duplicate key values. Trying to add a value to a key that is already assigned will just overwrite the existing value. Additionally, just like the lists in python, you are able to initialize an empty dictionary in Python and add values at a later time. One of the more recent changes in Python, according to W3 Schools, is that dictionaries in python are now ordered. This means that the items have a defined order and will not change. This means that you can technically get values or keys from a dictionary using index value, however, this is not advised as removing an element will shift the index values of each other element contained in the dictionary. This can make things cloudy and lead to bugs so it is usually best to use a different method to return data from a dictionary.

Examples of Python Dictionaries

Conclusion

All in all, I've only taught myself a little bit of Python in my free time, so this post is barely scratching the surface of the capabilities of Python, but hopefully a little bit of introduction to the language has you curious enough to go and learn a few more things about Python. Included below in my sources are everything used to write this article as well as some generally helpful and interesting information about the language. Thanks for reading!

Sources:
Python Docs
Stack Overflow Developer Survey 2022
Python Institute
W3 Schools
Python Essay with language comparison link

Top comments (0)