Python Basics, for Javascript Developes: Part 2
Tags: python, beginners, javascript
Published: 2020-01-20 05:26:38

In the last article we learned about how to "declare" variables and learned what lists are in Python. Today we were learn about how to write functions and loops.

One thing to keep in mind going forward is that Python does not use curly braces or semicolons for organization or to signify code blocks.  Instead Python uses spaces.  In Javascript, it's always best practice to be mindful of spacing/indenting, but it won't break your code if you don't indent correctly. In Python, if you don't indent correctly it will cause errors.  Indentation should be either 2 or 4 spaces. 

Let's take a look at functions in Python.  To bein, a function is declared with the word "def". After 'def' write the word 'function', in paranthesis put what you want to name your function, then a colon.  The next line should be indented.  That will be the body of the function.  Side note, Python has what is called docStrings which allows a developer to write comments about what the function does.  Let's take a look at an example: 

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

```
name = 'The knights who say Niii!'

def beware(name):
"""This function says beware of
the persons passed in as parameter"""
        print('Beware of the' + name)
```

Returning in Python is the same as returning in Javascript.  All you have to do is write the word 'return'.  Also, calling a function in Python is the same as calling a function in Javascript.

```
name = 'The knights who say Niii!'

def beware(name):
"""This function says beware of
the persons passed in as parameter"""
    return 'Beware of the' + name

beware(name)
```

<h3>Time to move on to Loops!</h3>

![Alt Text](https://media.giphy.com/media/nOARCtVgzZgrK/source.gif)

Python has two types of loops, the for loop and while loop. In Javascript, if we wish to exit a loop, we can add a return.  In Python, for both the for loop or the while loop, use the key word 'break'.

<h3> The For Loop </h3>

The for loop, iterates over a sequence, such as a list. As a javascript developer,the structure of a for loop in Python reminds me of the for in loop that we use on objects. 

```
roman_names = ['Brian', 'Pilate', 'Biggus Dickus', 'Incontinentia Buttocks', 'Sillius Soddus']

for name in roman_names:
    print(name)

//'Brian'
//'Pilate'
//'Biggus Dickus'
//'Incontinentia Buttocks'
//Sillius Soddus'
```

One cool thing about for loops is they have a few extra things you can do with them, using specific key words.  One key word that can be used is 'range'.  Range lets you iterate over a set of numbers.

```
for x in range(5):
    print(x)
//prints 0, 1, 2, 3, 4

for x in range(3, 6):
    print(x)
//prints 3, 4, 5
```
<h3> The While Loop </h3>

While loops in Python are similar to while loops in Javascript.  They run as long as a defined boolean is met.  In the example below, we are telling the loop to iterate as long as the number is less than 5.  In this example, in order for it to work, we have to increment the number by 1 every iteration(if we don't, we get the dreaded infinate loop NOOOOO!).

```
count = 0
while count < 5:
    print(count)
    count += 1
//prints 0, 1, 2, 3, 4
```

That was some exciting stuff! We're slowly getting the hang of Python!  I encourge anyone who is interested to take a further dive into learning Python.  It is an easy language to learn and much sought after by employers!  It can really help beef up your reseme and make your coding projects more diverse!  I hope to continue writing more about Python because there is so much more to talk about!  Stay tuned!