Introduction to Python

 

Introduction to Programming

I categorize programming as an Art like any other art. You need to work on it if you want to be the master in it.It is also a good platform on which you can express your thoughts process. As being an engineer my self I think if you want to be a good programmer you need to think about the certain way. Let’s see what some thinkers have to say about programming.

“Everybody in this country should learn to program a computer because it teaches you how to think” by Steve Jobs

Introduction to Python

The Python programming language was created in 1991 by Guido van Rossum. Let’s start by this quote by nonother then the creator of the Python language.

“Now, it’s my belief that Python is a lot easier than to teach to students programming and teach them C or C++ or Java at the same time because all the details of the languages are so much harder. Other scripting languages really don’t work very well there either.” by Guido van Rossum

I chose python because of its simplicity and broad usage across all the fields I have seen till now. Python is a very elegant and handy when you want to start your journey as a programmer as it gives you a bunch of modules which are very helpful.Python is high level, Object-Oriented Programming Language.Unlike other classical Programming, Languages Python is interpreted (Scripting) Language. Now rather going into the detail of why python is so popular among the Computer Science Community lest dive into it let’s have Python itself show you why it is so. when you open your terminal and write your first import that will tell you the detail regarding the language. so write below in the terminal after typing ‘python’ (‘command line’ in the case of windows operating system). Here, I am using Python 3.5.* version with Jupyter Notebook as developing environment.

python
import this

Output:


	Beautiful is better than ugly.
	Explicit is better than implicit.
	Simple is better than complex.
	Complex is better than complicated.
	Flat is better than nested.
	Sparse is better than dense.
	Readability counts.
	Special cases aren't special enough to break the rules.
	Although practicality beats purity.
	Errors should never pass silently.
	Unless explicitly silenced.
	In the face of ambiguity, refuse the temptation to guess.
	There should be one-- and preferably only one --obvious way to do it.
	Although that way may not be obvious at first unless you're Dutch.
	Now is better than never.
	Although never is often better than *right* now.
	If the implementation is hard to explain, it's a bad idea.
	If the implementation is easy to explain, it may be a good idea.
	Namespaces are one honking great idea -- let's do more of those!

Fetures Of Python

  1. Simple to Learn and code
  2. Interpreted
  3. Object-Oriented in Nature
  4. Dynamic typing
  5. Robust
  6. Exception Handling
  7. High Performance with respect to other Languages

1. Simple to Learn and code

def do_add(a,b):
add = a + b
return add

print(do_add(2,8))
print(do_add(2.5,1.25))
print(do_add("Hello","World"))

Output:


10
3.75
HelloWorld

As we have seen what Python is all about let’s find out more about it.

Python as a Calculator

Python gives us the facility that we can do normal numerical calculations in it without doing nothing. Let’s look at it as below:

2+3
5

4*5
20

4**2
16

9/2
4.5

9//2
4

So as we have seen above thet python works perfectly when using it for the numeric calculations. as we can see that operator ‘//’ is rounding of the answer. Other new operator is ‘**’ is basically doing the sqare of the number on the left side of the operator.

3. Object-Oriented in Nature


class Bag:
    def __init__(self):
        self.data = []

    def add(self, x):
        self.data.append(x)

    def addtwice(self, x):
        self.add(x)
        self.add(x)
    
    def print(self):
        for i in self.data:
            print(i)

num = Bag()
num.add(10)
num.addtwice(20)
num.print()

shape = Bag()
shape.add('square')
shape.add('triangle')
shape.add('circle')
shape.print()

Output:

    10
    20
    20
    square
    triangle
    circle

Here i made class which takes values from user and store it into the list. i will cover more advance topic related to this in fuature.

4. Dynamic typing


num = [input().strip().split()]
print(num)

Output:

4 5 2 6
    
[['4', '5', '2', '6']]

Python is Dynamic typing in the sense that we don’t need to specify datatype for the variable or the data structure like List.

5. Robust

Python provides run-time environment that is very robust. Errors are raised as exceptions, that allows the programs to catch and handle a variety of conditions. All Python language mistakes lead to simple, easy-to-interpret error messages from exceptions.

6. Exception Handling

Python has exceptions, and a sophisticated “try” statement that handles exceptions. Below example demonstrate the Pythonic way to handle the error.

while True:
    try:
        x = int(input("Please enter a number: "))
        break
    except ValueError:
        print("Oops!  That was no valid number.  Try again...")

Output:

    Please enter a number: k
    Oops!  That was no valid number.  Try again...
    Please enter a number: 4

7. High Performance with respect to other Languages

The Python interpreter is quite fast. We don’t need to compile the code. However, where necessary, a class or module that is slow we can implement it in C or C++, that will reduce the time and improves performance.

Basic Functionality print(), input()

Now let’s start the python by printing “Hello World!” on standerd output as we usally do in any other programing Language. It is very easy in the python. Print() in python is a built in function. Let’s see some examples.

type(print)

builtin_function_or_method

print("Hello World!")
Hello World!

print("I like Computer Science","I am a programmer","Good Bye...",end='')

Output:

I like Computer Science I am a programmer Good Bye

In above example we have seen some functionality of the python’s print built in Function. what “sep” argument does is it adds string between the two or more string values where default is a space. What “end” does is it appended string after the last value where default is a newline.

Now we will see how python takes the values from standered input or how it take the input from the user.

input()
4

Output:

'4'

This the most simplest functionality of the input built in Function. We will discuss other functionality in the future. Finally, i dedicate my first article to all known and unknown python genious over intenet who helped me directly or undirectly.

Summary:

In the above post we have seen some fetures of Python Language and some basics about the Language. We have also seen the two important necessary Python built in functions namely Print() and input(). In the next Post i will append more to the Python World

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s