DEV Community

Cover image for Introduction To Modern Python
FRANCIS ODERO
FRANCIS ODERO

Posted on

Introduction To Modern Python

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, visual code editor and jupyter notebook or anaconda 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

  • str (strings)

They represent text.
Image description

  • float (floating point numbers)

Numbers with decimal points.
Image description

  • bool(booleans)

True or False values.
Image description

Compound data structures

  • List

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

Image description

  • Tuple

A collection of ordered items but they are unchangeable.

Image description

  • Dictionary

A collection that is unordered, changeable and indexed.

Image description

  • Set

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

Image description

Conditional,loops and functions

  • The if statement
if BOOLEAN EXPRESSION:
     STATEMENTS
Enter fullscreen mode Exit fullscreen mode

an example

amount = 400

if amount == 400:
  print("The tax price is " (amount*(16/100))
Enter fullscreen mode Exit fullscreen mode
  • 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!")
Enter fullscreen mode Exit fullscreen mode
  • 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!")
Enter fullscreen mode Exit fullscreen mode
  • 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
Enter fullscreen mode Exit fullscreen mode

Practical example

if 0 < x:            # assume x is an int here
    if x < 10:
        print("x is a positive single digit.")
Enter fullscreen mode Exit fullscreen mode
  • 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
Enter fullscreen mode Exit fullscreen mode

practical example

for x in range(0, 3):
    print("We're on time %d" % (x))
Enter fullscreen mode Exit fullscreen mode
x = 1
while True:
    print("To infinity and beyond! We're getting close, on %d now!" % (x))
    x += 1
Enter fullscreen mode Exit fullscreen mode
  • 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

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)
Enter fullscreen mode Exit fullscreen mode

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))
Enter fullscreen mode Exit fullscreen mode

output is

<class 'module'>
Enter fullscreen mode Exit fullscreen mode

using matplotlib

Image description

That's all for today Happy coding!!

Top comments (0)