Python basics, for Javascript developers: Part One
Tags: python, beginners
Published: 2020-01-13 03:48:42



Python is a great coding language to learn. There are a lot of reasons to learn Python. Aside from the fact that it's name came from the great Monty Python, it is relativly simple to learn, popular among various jobs and allows coders to code quickly. Like Javascript, Python is an object oriented programming language.   

If you are interested in coding in Python, you will need to install it on your computer.  You can download Python by going to https://www.python.org/downloads/.  After it is installed, you will need to see if the coding editor you use requires an extension.  When creating a new file, the file end with .py (as apposed to .js for javascript). 

Now that our computers are set up for Python, we can learn the basics!

Let's talk about variables!
In Python, all variables are objects. Unlike Javascript, in Python, variables do not need to be declared before using them.  There is nothing like 'var', 'const' or 'let'. Not having to type 'var', 'const' or 'let' can help us code faster. In javascript we sometimes declare a variable and set it to an empty string, array or other data type, as a place holder.  In python, a variable can be any data type and does not need to be defined ahead of time and therefor we do not need to set variables as place holders.  A simple way of looking at it is, you are simply naming a value. 

Example: 
```witch = 1```
```name = 'Sir Lancelot'```
```my_list = ['african swallow', 'coconuts', 'the oppressed']```


Variables can also be re-assigned different types
```black_knight = 'worthy adversary'```
```black_knight = ['Tis but a scratch', 'I've had worse', 'Oh, come on, you pansy!']```

![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/28hunjjt8dmx6j6izn9e.jpeg)

Another handy thing about variables, and something else that is attributed to Python being an easy, fast language to use, is that you can define more than one variable at a time.  
```obj1, obj2 = 'holy grail', 'shrubbery' ```


Now, let's take a look at arrays...or in Python, they are called lists! For the most part, lists in Python are very similar to arrays, but there are a few differences.  One interesting thing about lists is, unlike arrays in Javascript, where it's best practice to have to same data type within one array, in Python you can mix the data types that are stored inside a list. 
```monty_python_list = ['witch', 'false', 1, ['knights of the round table', 'Castle Anthrax']]```

Another interesting thing about lists in Python is that they can have negative indexes (which we don't usually see in Javascript).
```roman_names = ['Brian', 'Pilate', 'Biggus Dickus', 'Incontinentia Buttocks', 'Sillius Soddus']```
```print(-3) // 'Biggus Dickus'```

![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/osar2bhwc923fvpxtbeh.jpg)

Let's learn how to add to a list! In Javascript there are several ways to add to an array.  The most common way is to use .push. The equivalent in Python is .append.

```political_groups = ['Judean People's Front', 'People's Front of Judea', 'Judean People's Front']```

```political_groups.append('People's Front')
 // political_groups = ['Judean People's Front', 'People's Front of Judea', 'Judean People's Front', 'People's Front']```


![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/3ei4bxh73udh5onuhsui.jpeg)

To add multiple items to a list you can use .extend

```monty_python_cast = ['Graham Chapman', 'Michael Palin', 'Terry Jones']
monty_python_cast.extend('Eric Idle', 'John Cleese')
//monty_python_cast = ['Graham Chapman', 'Michael Palin', 'Terry Jones', 'Eric Idle', 'John Cleese']```
 
Today we've covered how to set up Python, basic variables and lists. Next week we will delve a little more into python! We will talk more about methods we can use on lists, functions and more! Stay tuned!