Introduction To Modern Python
Tags: beginners, python, programming
Published: 2022-02-12 11:52:49

What comes to your mind when a word like python is mentioned ? Snakes right...

For now let's talk about programming leave alone about the snakes.
Did you know that Python language is the most growing programming language in the world of programming.Be it web development, gaming, software development, machine learning, data analysis, data science ,Artificial Intelligence and more trending and upcoming crazy stuff in the globe? 

**Let's dive in.**

What is Python?
Python is Python is a high-level, general-purpose programming language that is interpreted.
It is considered as "battery included" language because of its comprehensive standard library.This helps in supporting many programming paradigms like structural,object-oriented and functional programming.

Guido van Rossum started working on Python in the late 1980s as a replacement for the ABC programming language, and Python 0.9.0 was released in 1991.

**Advantages of Python.**

1. Easy to read, learn and write.
2. Highly productive due to its simplicity to help developers focus on solving world problems.
3. An interpreted language which means that Python directly executes the code line by line.
4. Has a vast libraries support of over 200,000 packages.

**Disadvantages of Python**

1. Slow in speed since python execute line by line of code.
2. Python programming language uses a large amount of memory.
3. Weak in mobile computing.

Before we dive deep into the essentials, kindly make sure that you have installed and set up your [Python](https://www.python.org), [visual code editor](https://code.visualstudio.com) and [jupyter notebook or anaconda](https://www.anaconda.com/products/individual) are standby and ready.


**Python Fundamentals.**

1. Data types
2. Compound data structures
3. Conditional,loops and functions
4. Object-oriented programming and external libraries


**Data types.**

- `int` (Integer)

Just numbers without a decimal.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mfjhbbq7h4vp3llk7e2y.png)
- `str` (strings)

They represent text.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kpzew1eakmraokxini1m.png)
- `float` (floating point numbers)

Numbers with decimal points.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u9j6txqdaq28dpbzg5og.png)
- `bool`(booleans)

`True` or `False` values.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/51paaydv9x64wh7apdfl.png)

 **Compound data structures**

- List

This is a collection which is ordered, they are changeable, can be duplicated.

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

- Tuple

A collection of ordered items but they are unchangeable. 

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

A collection that is unordered, changeable and indexed.

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

- Set

This a collection of unordered, unindexed items but they are changeable and doesn't take duplicate values.

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

**Conditional,loops and functions**

- The if statement

```
if BOOLEAN EXPRESSION:
     STATEMENTS
```
an example

```
amount = 400

if amount == 400:
  print("The tax price is " (amount*(16/100))
```

- The if else statement

It is frequently the case that you want one thing to happen when a condition it true, and something else to happen when it is false. For that we have the if else statement.

```
food = 'spam'

if food == 'spam':
    print("Ummmm, my favourite!")
else:
    print("No, I won't have it. I want spam!")
```

- Chained conditionals

This is when there are more than two possibilities, if choice one is not true or false select choice two if choice two is not select three.

```
var1 = 100

if var1:
   print ("1 - Got a true expression value")
   print (var1)
    
else:
   print( "1 - Got a false expression value")
   print (var1)

var2 = 0

if var2:
   print ("2 - Got a true expression value")
   print( var2)
    
else:
   print ("2 - Got a false expression value")
   print (var2)
        
print ("Good bye!")
```

- Nested conditionals

A nested if statement is an if statement that is nested (meaning, inside) another if statement or if/else statement. Those statements test true/false conditions and then take an appropriate action).

```
if x < y:
    STATEMENTS_A
else:
    if x > y:
        STATEMENTS_B
    else:
        STATEMENTS_C
```

Practical example


```
if 0 < x:            # assume x is an int here
    if x < 10:
        print("x is a positive single digit.")
```

- The for loop

The for loop processes each item in a sequence, so it is used with Python’s sequence data types - strings, lists, and tuples.
Each item in turn is (re-)assigned to the loop variable, and the body of the loop is executed.

```
for LOOP_VARIABLE in SEQUENCE:
    STATEMENTS
```
practical example

```
for x in range(0, 3):
    print("We're on time %d" % (x))
```

```
x = 1
while True:
    print("To infinity and beyond! We're getting close, on %d now!" % (x))
    x += 1
```

- Functions

A function is a block of code that performs some operations on input data and gives you the desired output. 

Functions are used in working on big projects to make maintainance of the code to be easier and real task. If a code performs similar tasks many times, a convenient way to manage your code is by using functions.


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

 **Object-oriented programming and external libraries**

Objects are instance of a class and are defined as an encapsulation of variables (data) and functions into a single entity. They have access to the variables (attributes) and methods (functions) from classes.

Here is how you define a class and an object of it:

```
class Rectangle:
    
    def __init__(self, height, width):
        self.height = height
        self.width = width
    
    def area(self):
        area = self.height * self.width
        return area

rect1 = Rectangle(12, 10)
```
The attributes and methods can be accessed using dot(.) operator.

`rectl.height`

`rectl.width`

`rectl.area()`

External libraries/modules

Python has vast modules that are defined classes,attributes and methods that we can use to accomplish multiple tasks. For example, the `math` library has many mathematical functions that can be used to perform various calculations. `matplotlib` used for plotting. 

```
import math as m
print(type(m))
```

output is 

```
<class 'module'>
```
using matplotlib


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

**That's all for today Happy coding!!**