Getting started with Python
Tags: python, beginners, tutorial
Published: 2020-01-05 02:57:43

# Introduction
![undraw new decade n4qd][1]

Since it's the start of the New Year. I thought of writing a tutorial. For anyone interested to learn Python to start their journey to get a job with it or automate your tasks.

# Getting Started
![undraw progress overview 2dik][2]

Let's head on down to [Repl.it][3] to create your new repl with Python by selecting `new repl` button and use the dropdown to select `Python`. 

![create new repl][4]

Do you have something like this?

![main screen][5]

Now let us start with your first code and run it on Repl.it.

Let us enter the codes here in the middle under the file name called `main.py`:

```python
print('Hello World')
```

The `print` statement is used to display words & numbers on your screen. It could be used for debugging purposes as well as you go along in programming in Python.

Great!! Now let us run it by clicking on the `run` button!!! Don't be shy!!! Does it look like this?

![hello world][6]

Awesome!!! Give yourself a pat on the back for a job well done.

# Variable
![undraw solution mindset 34bi][7]

A variable is a container which you could store something on your computer's memory. 

![donttakethisthewrongway-chair-carboard-box dezeen 2364 col 6][8]

Think of it like it is a bottle or a box created by you. It could be used to just store numbers, letters, date and many others. 

Here is how it look like to create a variable:

```python
a = 'Hello World'
```

The `a` is the name of the variable, think of it like it's a label for your box.

For the `Hello World`, it is the content that is stored in `a`. Which is called a `string` data type that stores letters, characters or text in it.

```python
print(a)
```

Now try running it on Repl.it, it should look like just two lines of code in Repl.it.

```python
a = 'Hello World'
print(a)
```

![variable][9]
 
## Data Types
Data types are a list of categories on how data is stored in a variable. Here are some common data types, you will be using for Python or other programming languages:

* Integer (int)- whole numbers


```python
a = 1
b = 11002
c = -11002
```


* Float (float) - floating-point or decimal numbers

```python
a = 1.22
b = 3.221
c = -2918.111
```

* Boolean (bool) - `True` which represents `1` or `False` which represents `0`. This is borrowed from the [binary number system][10].

```python
a = True
b = False
```

* Mapping (dict) - It is called a `dictionary` that uses `keys` as a label to get the content. In the example below, `a` is the `key` that retrieves a `value` called "apple".
 
```python
book = {'a': "apple", 'b': 'banna', 'c': 'cat'}
print(book['a'])
```  

* Sequence ( tuple or list) - Both `tuple` & `list` are different ways to store data. 

It uses an `index` to retrieve a `value` based upon the position of how it is arranged. 

It starts with `0` in the first place on the left with an increment of `1` toward the next position. 


```python
a = (1, 2, 3, 4, 5) # tuple
b = [1, 2, 3, 4, 5] # list

print(a[0])
print(b[2])
```  

# Functions
![undraw annotation 7das][11]

Functions are basically pieces of reusable code that are packed nicely in a package.  This is how it is declared:

```python
def magic():
	print('Hello World')
```

You can even add in `variables` as an `input` for your functions to process it. It is called `parameters` or `arguments`:

```python

def calculate_area(length, breath):
	return length * breath 

```

Now let us call the function, by using the `name` of the function with open and close curly braces like this `()`:

 ```python

def magic():
	print('Hello World')

def calculate_area(length, breath):
	return length * breath 

magic()
print(calculate_area(5,10))

```

Now let us try and run it in Repl.it.

![functions][12]

# If & Else Statements
![undraw project completed w0oq][13]

The `If` and `Else` statement is a checklist you had made that starts from the top to the bottom. 

## If
The `if` statement checks for **certain conditions** that meets the **expectation** and proceeds to **execute** the piece of code that under the **specific** condition

You can think of `if` statement like asking a question and you reply if it meets your expectation. Here is an example of an `if` statement: 

```python

area = 24

if (area == 24):
	print('Area is 24')

if (area == 12):
	print('Area is 12')

```

The code with `==` means `equals` in Python. Therefore there are two `if` statements. 

The first `if` statement checks if  `area` is equal to `24`, whereas another is checking `if` the area is `12`. 

## Else
For the `else` statement, it is sort of like the **backup** plan when your `if` statement fails to get the correct condition:

```python

area = 24

if (area == 10):
	print('The area is 10')
else:
	print('The area is not 10')

```

## Else If
An `elif` statement represents something like having `multiple plans` starting from the first to the last. 

This is different from the previous two `if` statements. Since there is a flow which they depend on the `if` or `elif` to fail. Before proceeding to the next `elif` or `else` statement:

```python

area = 24

if (area == 10):
print('The area is 10')

elif (area == 12):
	print('The area is 12')

elif (area == 20):
    print('The area is 20')

else:
print('Area is more than 12')

``` 

This is a way of programming called `if-else-if` ladder that makes decisions in your program.

# For
![undraw destinations fpv7][14]

The `for` statement or `for loop` is used to repeat a piece of code multiple times. 

Here is an example of how it looks like in declaring and creating a `for` loop statement:

```python

for i in range(11):
	print(i)

```

Now try running this code before proceeding with my explanation. Does it look like this?

![forloop1][15]

Alright, let me explain how this is done. The `i` is a variable. Think of it like it is the **head** of a crawling machine that grabs a **value** and keeps it **temporary**. 

Now `range` is a function which returns a sequence of numbers. This starts with `0` (default) and increments by `1` till the last position.

This essentially repeats a code for `11` times which is the number that you had included in the `range` function for the `for` loop.

To customise your for loop, you can make changes to the `range` to provide you with a different result:

```python

for i in range(1, 11):
	print(i)

```

Now try running again what do u see? Is it something like this 1?

![forloop2][16]

Now in the `range`, you had included 2 parameters. The first one  `1` as the number for `i` to `start` and `11` as the **ending** which is why it returns `10` times. 

You can control the increment as well instead of just the default `1` for the `range` function you can add a 3rd parameter that represents the increment in the **right** side.

```python

for i in range(1, 11, 2):
    print(i)

```

![forloop3][17]

# Conclusion
![undraw partying p92d][18]

Phew.....that was a lot of information to digest, congrats in making it through the end of the tutorial. I hope you had gained useful insights into it.

Lastly, I suggest that you jump over to [Clever programmer's 11 hrs tutorial on Python][19] for the absolute beginner.  He offers tons of insights to get a job or getting your feet wet as a Python freelancer.

If you like this article **sign up** for my [Adventurer's Newsletter](https://maxongzb.activehosted.com/f/1) for a weekly update in the area of **Python**, **Startup** and **Web Development**.

You can also **follow** me to get the **latest** update of my article on **Dev**

The original post was on [Getting started with Python - Reading Time: 6 Mins](https://www.maxongzb.com/getting-started-with-python-reading-time-6-mins/) and cover image by [Photo by Element5 Digital on Unsplash](https://unsplash.com/photos/OyCl7Y4y0Bk)



# Reference
* [Repl.it][3]
* [Python W3Schools][20]
* [Python Tutorial for Beginners - Full Course in 11 Hours [2020]](https://youtu.be/4F2m91eKmts)
* [11 Begineer Tips for Learning Python][21]
* [100 Days of Code in Python][22]
* [binary number system][10]
* [Undraw][23]


[1]: //images.ctfassets.net/ly2f59p4unnn/70rCRTXQSJYxGBdwEcJCp6/8d498eb510891ad0fdf1ffd7380e0727/undraw_new_decade_n4qd.png
[2]: //images.ctfassets.net/ly2f59p4unnn/6ch8UmJSrIDybMC4mnr8Ff/4f82987f2c1702e030b1a13cca9c0f46/undraw_progress_overview_2dik.png
[3]: https://repl.it/
[4]: //images.ctfassets.net/ly2f59p4unnn/13XyLFRWOd8mnZyfPNYYlE/3c2250b7ec2745beccbe7ee9dbd805aa/create_new_repl.png
[5]: //images.ctfassets.net/ly2f59p4unnn/2xpBZJKeh5wy1D6z7BMlWc/93650417ee1e3a1cc39baa22f0d36a6e/main_screen.png
[6]: //images.ctfassets.net/ly2f59p4unnn/1U37yl8BWmsI7GfsZiH7yb/97a3def67154462cacea57b45c9ad481/hello_world.png
[7]: //images.ctfassets.net/ly2f59p4unnn/5j7cP7IoR7Q66EnCunmS0l/11b72cf77e139de05a0b7174b4b25991/undraw_solution_mindset_34bi.png
[8]: //images.ctfassets.net/ly2f59p4unnn/1eD0XPwfxpSpUjfaGMsktz/a8746cf45c794f5df6507601f4e641eb/donttakethisthewrongway-chair-carboard-box_dezeen_2364_col_6.jpg
[9]: //images.ctfassets.net/ly2f59p4unnn/1iDqwzrX9zW9sUioIlpHIL/1160cc20e990c8667ccdb2a71e7a5896/variable.png
[10]: https://www.computerhope.com/jargon/b/binary.htm
[11]: //images.ctfassets.net/ly2f59p4unnn/133AA7v0jLTCHqBETJ7xDY/101764b9f495ffcfb9e754fe1c3b1ecb/undraw_annotation_7das.png
[12]: //images.ctfassets.net/ly2f59p4unnn/4mvlEy9IrZN42O5He6t1BE/20b91ee3d6c6996de63a6ff9c26cc8bb/functions.png
[13]: //images.ctfassets.net/ly2f59p4unnn/1KpQyvRRxrZEm7sov3aO7Y/6e997df4a82f456c916ffaf50f637b8a/undraw_project_completed_w0oq.png
[14]: //images.ctfassets.net/ly2f59p4unnn/5eREAqIcB7P9iYabFEfwsE/a464fd126ba831a777c0853086a8cd66/undraw_destinations_fpv7.png
[15]: //images.ctfassets.net/ly2f59p4unnn/5gkRCroN4qwit16xRFLafe/7c18f94bd3f6996184e50ef7f248272d/forloop1.png
[16]: //images.ctfassets.net/ly2f59p4unnn/6K4JNegm3OGkj1sMw06BRJ/7f72ade88968932d4f0a65e9f2c5d0cf/forloop2.png
[17]: //images.ctfassets.net/ly2f59p4unnn/2qLuH0mMYbVFrtNRn2Fe3i/dcd575a02d8420f073f04fedfcf3d992/forloop3.png
[18]: //images.ctfassets.net/ly2f59p4unnn/1hAWQKRb9HfNRDDSjGEl6a/7fe7c557b705ea3b339d6eaa3c572861/undraw_partying_p92d.png
[19]: https://youtu.be/4F2m91eKmts
[20]: https://www.w3schools.com/python/default.asp
[21]: https://realpython.com/python-beginner-tips/
[22]: https://training.talkpython.fm/courses/explore_100days_in_python/100-days-of-code-in-python
[23]: https://undraw.co/