Comparison of Python with C and Java
A Simple Comparison between “C” and “Python”
Example : Write a source code in C to print 1 to 10 numbers.
“C “Code:
#include<stdio.h>
int main( ) { for (i = 1; i <= 10; i++) { |
Output
0 1 2 3 4 5 6 7 8 9 10 |
The below is the Python code for the same example
>>> for i in range(11):
print(i) |
Output
0
1 2 3 4 5 6 7 8 9 10 |
Comparison with Java
1.Intuitive Data Structures
Lists, tuples,Sets, Dictionaries
Powerful, yet simple and intuitive to use
Flexible (mixed data types)
2.English-like commands
Java: String name=”Bob”;
System.out.println(name);
Python: name=’Bob’
print(name)
3.One-Liners
Elegant 1-line solutions to what takes a whole block of code in other languages.
One example: swap x and y
Java: int temp=x;
x=y;
y = temp;
Python: x,y=y,x
4.Simple syntax
Some programming languages will kill you with parenthesis, brackets, braces, commas and colons. With Python you spend less time debugging syntax and more time programming.
5.Dynamically typed
No type when declaring a variable
Skip headaches of java type casting
Java: int x=1;
x=(int)x/2; x now equals 0
x can never equal 0.5
Python:
x=1
x=x/2 x now equals 0.5
We summarized the differences in the below table
Java |
Python |
Java programs are slower |
Python programs run faster |
Java uses traditional braces to start and end. | Python uses indentation to start and end blocks. |
Java employs static typing | Python uses dynamic typing |
Java is a strongly typed language | Python is not a strongly typed language |
Java does not compile to native byte code. | Python compile to native byte code |
Java is complex | Python is simpler and more compact |
Java does not allow the data type of variable to be changed | Python allows the data type of variable to be changed. |
Java code is lengthier | Python code is shorter. |
Java applications can work across various platforms. | Python applications do not support various platforms. |
Java code is difficult to understand. |
Python code is easy to understand |
Similarities: Both are programming languages. |