Sunday 29 July 2018

Python | Why Python?

If you want to automate some task, example, you may wish to perform a search-and-replace over a large number of text files or rename and rearrange a bunch of photo files in a complicated way. Perhaps you’d like to write a small custom database, or a specialized GUI application, or a simple game.

If you’re a professional software developer, you may have to work with several C/C++/Java libraries but find the usual write/compile/test/re-compile cycle is too slow. Perhaps you’re writing a test suite for such a library and find writing the testing code a tedious task. Or maybe you’ve written a program that could use an extension language, and you don’t want to design and implement a whole new language for your application. Python is just the language for you.

Unix shell script or Windows batch files are also options for some of these tasks, but shell scripts are best at moving around files and changing text data, not well-suited for GUI applications or games and writing C/C++/Java programs can take a lot of development time to get even a first-draft program. Python is simpler to use, available on Windows, Mac OS X, and Unix operating systems, and will help you get the job done more quickly.

Python is simple to use, but it is a real programming language, offering much more structure and support for large programs than shell scripts or batch files can offer. On the other hand, Python also offers much more error checking than C, and, being a very-high-level language, it has high-level data types built in, such as flexible arrays and dictionaries. Because of its more general data types, Python is applicable to a much larger problem domain than Awk or even Perl, yet many things are at least as easy in Python as in those languages.

Python allows us to split the program into modules that can be reused in other Python programs. It comes with a large collection of standard modules which can be used as the basis of programs. Some of these modules provide things like file I/O, system calls, sockets, and even interfaces to graphical user interface toolkits.

Python is an interpreted language, which can save you considerable time during program development because no compilation and linking are necessary. The interpreter can be used interactively, which makes it easy to experiment with features of the language, to write throw-away programs, or to test functions during bottom-up program development. It is also a handy desk calculator.

Why Programs written in Python are typically much shorter than equivalent C, C++, or Java programs?
1. the high-level data types allow you to express complex operations in a single statement;
2. statement grouping is done by indentation instead of beginning and ending brackets;
3. no variable or argument declarations are necessary.

Python is extensible
If you know how to program in C it is easy to add a new built-in function or module to the interpreter, either to perform critical operations at maximum speed or to link Python programs to libraries that may only be available in binary form (such as a vendor-specific graphics library). Once you are really hooked, you can link the Python interpreter into an application written in C and use it as an extension or command language for that application.

Python | Introduction

Python is easy to learn, powerful programming language. It has efficient high-level data structures and a simple but effective approach to object-oriented programming. Python’s elegant syntax and dynamic typing, together with its interpreted nature, make it an ideal language for scripting and rapid application development in many areas on most platforms.

The Python interpreter is easily extended with new functions and data types implemented in C or C++ (or other languages callable from C). Python is also suitable as an extension language for customizable applications.

Python supports multiple programming patterns, including object-oriented, imperative and functional or procedural programming styles. Python is not intended to work on a special area such as web programming. That is why it is known as multipurpose because it can be used with the web, enterprise, 3D CAD etc.

Python makes the development and debugging fast because there is no compilation step included in python development and edit-test-debug cycle is very fast.



Programming Concepts | What is static and dynamic typing?

In this article, we are going to discuss the static and dynamic typing. I will make references of the programming language like Perl, PHP and Python, however, you do not have to know any particular programming language to understand these concepts. Who don’t know the particular programming language, you should think of the code as pseudocode.

"typing" in terms of static and dynamic typing refers to "type" as in data type — not the process of pressing keys on a keyboard. And the languages that have static typing or dynamic typing are said to be "static typed" or "dynamic typed".

Static Typing
Static typed programming languages are those in which variables need not be defined before they’re used i.e. explicit declaration (or initialization) of variables required before they’re used. Java, C, and C++ are the examples of a static typed language.

Static typing does not imply that you have to declare all the variables first, before you use them; variables maybe be initialized anywhere, but developers have to do so before they use those variables anywhere. Consider the following example:

/* Java code */
int num1, num2, sum; // explicit declaration
num1 = 15; // now use the variables
mum2 = 10;
sum = num1 + num2;

The above code is an example of how variable declaration in static typed languages generally appears.

Note that, int has been used to initialize the num1, num2 and sum to zero.

Dynamic Typing
Dynamic typed programming languages are those languages in which variables must necessarily be defined before they are used. This implies that we do not require the explicit declaration of the variables before using them. Python is an example of a dynamic typed programming language, and so is PHP.

/* Python code */
num = 20 // directly using the variable

In the above code, we have directly assigned the variable num the value 10 before initializing it. This is characteristic to dynamic typed programming languages.

Another Analogy
A lot of people define static typing and dynamic typing with respect to the point at which the variable types are checked. Using this analogy, static typed languages are those in which type checking is done at compile-time, whereas dynamic typed languages are those in which type checking is done at run-time.

This analogy leads to the analogy we used above to define static and dynamic typing. I believe it is simpler to understand static and dynamic typing in terms of the need for the explicit declaration of variables, rather than as compile-time and run-time type checking.

Static Typing and Dynamic Typing versus Strong Typing and Weak Typing
Static and dynamic typing, and strong and weak typing, are two totally different concepts, which, unfortunately, are very often confused. It is erroneous to say that a language that is static or dynamic typed cannot be strong or weak typed. Static and dynamic typing, and strong and weak typing, are different forms of classification of programming languages, and one of each class necessarily characterizes a given language. It is thus imperative to discuss strong and weak typing vis-a-vis static and dynamic typing.

Programming languages that exhibit "strong typing" are "strong typed," and those that exhibit "weak typing" are "weak typed".

Strong Typing
Programming languages in which variables have specific data types are strong typed i.e. variables are necessarily bound to a particular data type. Python is strong typed, and so is Java. Consider the following example:

/* Python code */
foo = "x"
foo = foo + 2
Traceback (most recent call last):
  File "/Users/rajesh.dixit/Desktop/code_drive/Python/Workspace/com.algorithmforum.python/HelloWorld.py", line 2, in <module>
    foo = foo + 2
TypeError: can only concatenate str (not "int") to str


In the above example, foo is of str type. In the second line, we’re attempting to add 2 (an int) to a variable of str type. As we can see, a TypeError is returned, indicating that a str object cannot be concatenated with an int object. This is what characterizes strong typed languages: variables are bound to a particular data type.

Weak Typing
As opposed to strong typed languages, weak typed languages are those in which variables are not of a specific data type i.e. variables are not "bound" to a specific data type. PHP and C are examples of weak typed languages. Consider the following:

/* PHP code */
<?php
$foo = "x";
$foo = $foo + 2; // not an error
echo $foo;
?>

In this example, foo is initially a string type. In the second line, we add this string variable to 2, an integer. This is permitted in PHP, and is characteristic of all weak typed languages.

Now that we know about the two concepts, we can augment both of them to characterize any given language. Thus, Python is dynamic typed and strong typed; Java is static typed and strong typed; PHP is dynamic typed and weak typed; C is static typed and weak typed (owing to its casting ability).

Why Dynamic Typing?
In a dynamic typed language, you don’t have to initialize variables, as programmers can use a variable at will when required (without having to initialize it) which is a big bonus for many developers. Dynamic typing is characteristic of many of the scripting languages: Perl, PHP, Python, etc. Dynamic typing, in fact, does save you from writing a few "extra" lines of code, which, in turn, means less time spent writing code.

Problem with the Dynamic typing?
The very characteristic of dynamic typed languages that appeal to many developers is also a pitfall and a major one at that.

/* Python code */
variable = 10
while variable > 0:
        i = foo(variable)
        if i < 100:
            variable++
        else
            varaible = (variable + i) / 10 //spelling error intentional

As we can observe in the above code, varaible is a spelling mistake and it leads to a problem here, since Python is dynamically typed, it will not return an error, but instead will create a new variable called variable and now we have two variables: variable and varaible. This obviously is a serious problem; some would suggest that forced variable declaration is an important requirement in any programming language.

Static Typed Behavior in Dynamic Typed Languages
Perl is also a dynamic typed programming language. However, it provides a means to "simulate" static typing by means of a pragma called strict.

Example:
/* Perl code */
$sum = 10;
print $sum;

The above code will run without any problem and will print 10 to the console. Note that here, we have not initialized the variable sum; this exemplifies the dynamic typing characteristic of Perl.

To enforce variable declaration, we make use of the strict pragma as follows:

/* Perl code */
use strict;
$sum = 10;
print $sum;

The above code will return the following error when you try to run it:

Global symbol "$num" requires explicit package name at perl.pl line 2.
Execution of perl.pl aborted due to compilation errors. To rectify the above error, we are forced to declare the variable num as follows:

/* Perl code */
use strict;
my $num; // forced declaration
$sum = 10;
print $sum;

The above codes are specific to Perl; not all programming languages have a way to enforce variable declaration: Python, for example, doesn’t have a way to enforce variable declaration. However, there is a tool, called "pychecker", that can be used to "detect" stray variables; this is, of course, far from a desirable solution.

Static Typed or Dynamic Typed?
This is highly debating topic as there are advocates of both forms of typing. Asserting that one is better than the other would only lead to a debate of no consequence.

There are those who advocate dynamic typing for the simplicity and saving in terms of time that it offers; they believe that type checking need not be an integral part of the programming language design per se, but instead, that third-party solutions (like pychecker) could be used to server that purpose.

On the other hand, there are advocates of static typing, who believe that static typing (leading to forced variable declaration) is an important requirement of programming language design.

Related Posts Plugin for WordPress, Blogger...