Python Zero to Hero #Beginners⚡
Tags: beginners, python, datascience, machinelearning
Published: 2020-11-02 13:09:01

As a 3+ years Python Programmer I would like to share some basics of Python, This Tutorial is for absolute beginners and who want to revise python in less time.

This tutorial gives enough understanding on Python programming language. feel free to [contact me](https://www.instagram.com/vivek_codes/) if you have any silly doubt I will be super Happy to help you out.😊😊😊😃

 - **Python is a Beginner's Language** − Python is a great language for the beginner-level programmers and supports the development of a wide range of applications from simple text processing to WWW browsers to games .

# **Pre-requisite:**
# 1. Python in your machine, thats it.
---
(If you Don't have python in your system)
Steps to install Python:
>1. Go to <a href=https://www.python.org/downloads/>https://www.python.org/downloads</a> and Download it.
>2. Install Python in Your system. it is easy as 123!!

# **If you have already installed python in your system**

![haha](https://i.redd.it/c7m5fvjfi2051.jpg)

# **Press Win key and open Command Prompt**

![haha](https://i.imgflip.com/4kopgn.jpg)

 - in command prompt type 'python' and press enter

# **Hello World in Python**
```python
>>> print("Hello World!")
Hello World!
```
# **Comments**
Here is example of single line comment
single line comment start with '#'
```python
>>> print("Hello World!") #this is single line comment
Hello World!
```
Here is example of multiline comments(Docstrings)
multi line comments starts with 
```python
"""This is a
multiline
Comment. It is also called docstrings"""
>>> print("Hello, World!")
 Hello World!
```
>In computer programming, a comment is a programmer-readable explanation or annotation in the source code of a computer program. They are added with the purpose of making the source code easier for humans to understand, and are generally ignored by compilers and interpreters.

# **Data Types in Python**
```python
>>>x = 10 
>>>print(x) 
 10
```
```python
>>>print(type(x))
 <class 'int'>
```
```python
>>>y = 7.0 
>>>print(y)
 7.0
>>>print(type(y))
 <class, 'float'>
```
```python
>>>s = "Hello"
>>>print(s)
 Hello
>>>print(type(s))
 <class, 'str'>
```
# **take input from user and store it in variable**
```python
a = input('Enter Anything') 
print(a)
```
# **List and indexing**

![](https://cdn.programiz.com/sites/tutorial2program/files/python-list-index.png)
```python
>>>X=['p','r','o','b','e']
>>>print(X[1])
 r
```
>here it will print 'r' because index of 'r' is 1.(index always starts with 0)
```python 
>>>X.append('carryminati')
>>>print(X)
 ['p','r','o','b','e','carryminati']
```
```python 
>>>print(X[-1])
 carryminati
```
```python
>>>x = True
>>>print(type(x))
 <class 'bool'>
```
>Boolean values can be True or False 
```python
>>>S={1,1,2,2,2,3,3,3,4,4,4,4,5,6}
>>>print(S)
 {1, 2, 3, 4, 5, 6}
```
>Dictionary in Python - We will learn this later
```python
x = {"Name":"Dev", "Age":18, "Hobbies":["Code", "Music"]}
```
>In programming, variables are names used to hold one or more values. Instead of repeating these values in multiple places in your code, the variable holds the results of a calculation,

### **Mutable and Immutable Data Types** 
![Immutable  vs mutable](https://dev-to-uploads.s3.amazonaws.com/i/w3vfntivfcooyz2wwz37.png)
 - **Immutable Objects** : These are of in-built types like int, float, bool, string, unicode, tuple. In simple words, an mutable object's value can’t be changed after it is created.
```python
# Tuples are immutable in python 

>>>tuple1 = (0, 1, 2, 3)  
>>>tuple1[0] = 4
>>>print(tuple1) 
 TypeError: 'tuple' object does not support item assignment
```
 - **Mutable Objects** : These are of type list, dict, set . Custom classes are generally mutable.In simple words, an immutable object's value can be changed even after it is created.
```python  
# lists are mutable in python
>>>color = ["red", "blue", "green"] 
>>print(color) 
 ["red", "blue", "green"]
>>>color[0] = "pink"
>>>color[-1] = "orange"
>>>print(color) 
 ["red", "blue", "green", "orange" ]
```
# **Python Operators**
```python
x = 10
# Sum of two variables
>>> print(x+2)
 12
# x-2 Subtraction of two variables
>>> print(x-2)
8
# Multiplication of two variables
>>> print(x*2)
 20
# Exponentiation of a variable
>>> print(x**2)
 100
# Remainder of a variable
>>> print(x%2)
 0
# Division of a variable
>>> print(x/float(2))
 5.0
# Floor Division of a variable
>>> print(x//2)
 5
```
# **Python String Operations**
```python
>>> x = "awesome"
>>> print("Python is " + x) # Concatenation
 Python is awesome
```
> **python can't add string and number together**
```python
>>>x=10
>>>print("Hello " + x)
  File "<stdin>", line 1
    print("Hello " + x)x=10
                       ^
SyntaxError: invalid syntax
```
**String multiplication**
```python
>>> my_string = "iLovePython"
>>> print(my_string * 2)
 'iLovePythoniLovePython'
```
**Convert to upper**
```python
>>> print(my_string.upper()) # Convert to upper
 ILOVEPYTHON
```
**more on string methods**
```python
>>> print(my_string.lower()) # Convert to lower
 ilovepython
>>> print(my_string.capitalize()) # Convert to Title Case
ILovePython
>>> 'P' in my_string # Check if a character is in string
True
>>> print(my_string.swapcase()) # Swap case of string's characters
 IlOVEpTHON
>>> my_string.find('i') # Returns index of given character
 0
```
# **Sets in Python**
```python
>>>S = {"apple", "banana", "cherry"}
>>>print("banana" in S)
 True
```
```python
>>>S={1,1,2,2,2,3,3,3,4,4,4,4,5,6}
>>>print(S)
 {1, 2, 3, 4, 5, 6}
```
# **Type Casting in Python**
```python
>>> x = int(1)
>>> print(x)
1
>>> y = int(2.8)
>>> print(y)
2
>>> z = float(3)
>>> print(z)
 3.0
```
# **Subset in Python**
```python
 # Subset
>>>my_list=['my', 'list', 'is', 'nice']
>>> my_list[0]
 'my'
>>> my_list[-3]
'list'
>>> my_list[1:3]
 ['list', 'is']
>>> my_list[1:]
['list', 'is', 'nice']
>>> my_list[:3]
['my', 'list', 'is']
>>> my_list[:]
['my', 'list', 'is', 'nice']
```
```python
>>> my_list + my_list
>>>print(my_list)
['my', 'list', 'is', 'nice', 'my', 'list', 'is', 'nice']
>>> my_list * 2
['my', 'list', 'is', 'nice', 'my', 'list', 'is', 'nice']
>>> my_list2 > 4
True
```
# **List Operations**
```python
>>> my_list.index(a)
>>> my_list.count(a)
>>> my_list.append('!')
>>> my_list.remove('!')
>>> del(my_list[0:1])
>>> my_list.reverse()
>>> my_list.extend('!')
>>> my_list.pop(-1)
>>> my_list.insert(0,'!')
>>> my_list.sort()
```
# String Operations
```python
>>> my_string[3]
>>> my_string[4:9]
>>> my_string.upper()
>>> my_string.lower()
>>> my_string.count('w')
>>> my_string.replace('e', 'i')#will replace 'e' with 'i'
>>> my_string.strip()#remove white space from whole string

```
# **Indentation**

<b>Python indentation is a way of telling the Python interpreter that a series of statements belong to a particular block of code. In languages like C, C++, Java, we use curly braces { } to indicate the start and end of a block of code. In Python, we use space/tab as indentation to indicate the same to the compiler.</b>

![](https://cdn.askpython.com/wp-content/uploads/2019/06/python-indentation.png)

# **Working With Functions**
**Follow Indentation Rules Here(White Space Before return Statement)**

```python
>>> def myfunAdd(x,y):
...     return x+y
>>> myfunAdd(5,100)
105
```
# **For Loop in Python**
```python
>>>fruits = ["Carry", "banana", "Minati"]
>>>for x in fruits:
...    print(x)
carry
banana
Minati
```
# **If statement:**
```python
a = 33
b = 200
if b > a:
  print("b is greater than a")
```
# **If statement, without indentation (will raise an error)**
```python
a = 55
b = 300
if b > a:
print("b is greater than a")
```
# **While Loop in python**
```python
i = 1
while i < 6:
  print(i)
  i += 1
```
# **Exception Handling**

<b> When an error occurs, or exception as we call it, Python will normally stop and generate an error message.These exceptions can be handled using the try statement </b>

```python
try:
  print(x)
except:
  print("An exception occurred")
```
# **Working with modules**

```python
>>>import math
>>>print(math.pi)
 3.141592653589793
```
# **what's now ?**
Well, There is lot more Stuff To Cover But That Is Your Home work😉!
Thank you so much for reading and good luck on your Python journey!!
*As always, I welcome feedback, constructive criticism, and hearing about your projects. I can be reached on [Linkedin](https://www.linkedin.com/in/vivek-patel-0553731a5), and also on [my website](vivolscute.github.io).*