Types and declarations, Python Programming

Assignment Help:

 

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.

 


Related Discussions:- Types and declarations

Procedure calls, Procedure calls When you compute an expression of the...

Procedure calls When you compute an expression of the form ( ,  ..., )   the Python interpreter treats  this as a procedure call. It will be simpler to talk about

Question, Data array A has data series from 1,000,000 to 1 with step size 1...

Data array A has data series from 1,000,000 to 1 with step size 1, which is in perfect decreasing order. Data array B has data series from 1 to 1,000,000, which is in random order.

Variable, from urllib2 import urlopen var= ('' response">http://placekitt...

from urllib2 import urlopen var= ('' response">http://placekitten.com'') response = kittens.read(body) body = response[559:1000] print ''Kittens:'', response

Program to solve word search puzzles, This assignment involves writing a pr...

This assignment involves writing a program to solve word search puzzles. For example in the following word grid it is possible to find the words active, stock, ethernet and java. N

Program in c to generate random input - python script, For this assignment,...

For this assignment, you are to: 1.    Modify the output format of your Python script from Assignment 1 to match the input format of your C program from Assignment 2. 2.    W

Euler method, python program of motion of a particle in viscus medium

python program of motion of a particle in viscus medium

Packages and Libraries, For this assignment, you need to create a program t...

For this assignment, you need to create a program that allows the user to do basic trigonometry functions. First, ask the user if he or she would like to do sine, cosine, or tangen

Common Vulnerabilities, 1 Low Level Exploits 1.1 Savegames Jimmy is becomi...

1 Low Level Exploits 1.1 Savegames Jimmy is becoming increasingly frustrated at the computer game hes playing. He has a save right before the levels boss but he needs either more

Non-local references in procedures, Non-local references in procedures ...

Non-local references in procedures There  is an important subtlety in the  way  names  are handled in the  environment created by a function call. When  a value  that  is not

CIS, Define a function that draws a pentagon of a size you provide when you...

Define a function that draws a pentagon of a size you provide when you call the function. Include a ''''''docstring'''''' as the first line of the function''s body — it tells the p

Write Your Message!

Captcha
Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd