A Head Start to Python
Tags: python, beginners, programming, machinelearning
Published: 2022-11-20 16:33:50

## Introduction:
Python is a commonly known programming language now. It supports many programming paradigms. Recently, it is hyped because of data science. lets start our journey.

## Print Function and Formation:
In python we use `print("Your text you want to display")` to display something. 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/r64biiboqnqwyuxq5pa0.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wge2hjcsal0dnd2g52p7.png)

Sometimes, we need to declare something in between text or we want to display some value of the variables between a text which we want to print.
For this we need `curly braces {}` to show the result. Here it is showing how to properly use it.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a6k65bmf9iq6yi72jjxc.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yhumdyy0b916epm13plk.png)

## Variables:
There are four types of variables: **Integer**, **long integer**, **float** and **string**.
In python we do not need to follow any strict rule like other programming languages to declare variables. 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4me8ysqt9x630sye648o.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/f0lwsgn8ry1frm60fev8.png)

To know the type of the variable we use `type()` to know the variable type.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xfym5yozjqgrhzbe3t7y.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yyve6eck4xyiiq48lbac.png)

Lastly, python variables are case sensitive. Which means if you declare a variable named "a" and another variable named "A" both will indicate two different variable.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6wnt9h1uaklbf3x1gpmn.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6v5ol7ai7gwzf0at84fp.png)

## Strings:
In python strings are like arrays. If a string variable is "STRING" we can iterate through all of the elements of it. 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qmk9fdrbmbbq8jbdw4hd.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/55njucaj0kuf9h7eb739.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tq27mimpryb04shhrmbd.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0yxl8yc2lblpub4h3cx1.png)

We can also check the length of string using `len()`.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ju21pr8py4ffefloz3eh.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/98efhco3u76fi4nvguuy.png)

If we want to print something in a range. We need to use slicing. To use slice in we need to put a range. The blueprint for slicing is - `variable[start:stop:step]`
here,
start = Including the position of start
stop = Excluding(until) the position of stop
step = Jump

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uizll1htgc6lrua8ywsh.png)

## Lists:
There are two kinds of list.
(1) Normal List

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/v3jk98frpfpd87dl1fom.png)


(2) Nested List

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9opw1ccyvjanu64xhhop.png)

**List Indexing**:
To access the elements of the list we need list indexing. It is like string  indexing for normal list.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5x1nysjnriepea1bynp4.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/amppp1205gzn4zji8uku.png)

For nested list we need to first highlight which element and then the highlighted element's element.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uinsnds1n03q16wonuvx.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1jsh11ls3h3ev37jppw2.png)

**Length of a list**
Using 'len()' we can print the length of the list.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u9qn5hgnbmkubillbacx.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fw9rxgl3lee0r4knmp5w.png)

**Iteration**
To access all the elements of a list we need iteration.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jwm8icz9wkcxh6m4blgi.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qynq0mx7ouv09s7aomsj.png)

**Slicing**
List slicing is like string slicing. 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ryamr31zy94vxmzztmzh.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4y4y4lffj07nb24a0f8w.png)

**Adding Item in a list**
To add a new item in a list we have to use 'append()'.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/09pzg3lb739rf799jt5h.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kt03wohxf0evvsl0vcfg.png)

**List Comprehension**
To make the code shorter list comprehension is a very good technique. Here is a example of a list using list comprehension and without using list comprehension.

_without using list comprehension_

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/up9g8az3usx2x1upx0gt.png)

_using list comprehension_

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2y1n8gra26h9mifjv3sk.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8zpkdjf9wiyl9j6ez3ht.png)