Types and declarations, Python Programming

 

Java programs are  what  is known as statically and  strongly defined.  Thus,  the  kinds  of all the variables must  be known at the time that the program is written. That seems  that variables have to be declared to have  a particular type  before  they  are given.   It also seems  that  the  variables cannot  be used  in a way that is inconsistent with their kind.  So, for that point, you would define  x to be an integer by saying

 

int x;

x  = 6 * 7;

But you would get into trouble if you left out the definition, or did

int x;

x  = "thing";

 

because  a type checker  is execute  on your  program to make  sure that you don't try to use a variable in a way that is inconsistent with its declaration.

In Python, however, things  are a lot more  reliable.   There  are no variable definition, and  the similar variable may be used  at different positions  in your  program to hold  data  objects of different kinds.  So, the following is ok, in Python:

 

if x == 1:

x = 89.3

else:

x = "thing"

 

The advantage of having type  declarations and  compile-time type  checking, in Java, is that  a compiler can create an executable version of your  program that  runs  very  rapidly,  because  it may be certain about  what  kind of data is stored in each and every variable, and it does not have to calculate it at execution time. An additional advantage is that several programming mistakes can be caught at compile time, rather than  waiting until  the program is being execute.  Java could complain even before your program started to run that it could not evaluate

 

3 + "hi"

 

Python could not complain until it was executing the program and got to that point.

The advantage of the Python approach is that programs are shorter and cleaner seeing,  and possibly simpler to write.  The flexibility is often useful:  In Python, it is simple to create a list or array with objects of different types  saved in it. In Java, it may be done,  but it is trickier.  The disadvantage of the Python function is that  programs tend  to be lighter.  Also, the rigor  of compile time checking  may replace bugs, especially in big programs.

 

Posted Date: 8/9/2012 6:58:58 AM | Location : United States







Related Discussions:- Types and declarations, Assignment Help, Ask Question on Types and declarations, Get Answer, Expert's Help, Types and declarations Discussions

Write discussion on Types and declarations
Your posts are moderated
Related Questions
Interacting with the environment Computer systems have  to communicate with  the world around them,  getting information about  the external world, and  taking  actions  to cha

Assume you have a truck which has to travel across a desert from the base camp at position 0 (left) to the target camp at position 4 (right). The intermediate positions 1,2, and 3

how to calculate angle between 2 moving line in 2d space

protocol rdt2.2 considers there is a bit errors between sender to receiver and also from receiver to sender. So now we have to consider checking bit errors introduced in reply from

Every programming language has to have  some method for indicating grouping of operations. Here is how you execute an if-then-else structure in Java:   if (s == 1){ s = s

Write a program that requests a password after the author/program information is displayed. Make the password "hello". The program should then ask the user for their name: if the n

Task (Anagrams) Write a function that checks whether two words are anagrams. Two words are anagrams if they contain the same letters. Fore example, silent and listen are anagrams.

Basics of python-introduction Python is designed for easy interaction between a user and the system. It goes with  an inter­ active function  known as  a shell or listener.  T


List Comprehensions   Python has a very nice built-in  facility for doing  many  iterative methods, known as list comprehensions. The basic template is       [