Monday, 28 May 2012

Python and c++

Python is also a powerful language having features of c, c++ and java. The following table shows how python is different from c++.



Sr. No
Python
C++
1.
Main function and header files are not mandatory part of a program.
Header files and main functions are essential part of every program.
2.
Line termination semi-colon(;) is not required at the end of every line to indicate the end of a line when each line of code starts in new line. But if we want to write two or more lines of code in one line then semi-colon requires.
Semi-colon is necessary to terminate the line.
3.
Python uses indentation(4 spaces or one tab) to indicate code blocks.
It uses curly braces to indicate code blocks.
4.
It takes advantage of modules(the python script with variable, functions can be imported in another script).
C++ takes advantage of header files and libraries to make code sharable and reusable.
5.
We can start it from shell prompt by typing python and able to type python expression directly without making files. But we can also write the python script into files.
Statements are saved into files then that are executed.
6.
While assigning values to variables data types are not written at that time. Default user inputs are taken as strings and converted to other types by python itself.
While declaring and assigning values to variables programmer needs to write data types as essential part.
7.
No increment and decrement operators in python.
Increment and decrement operators are available.
8.
Allow to import packages for example math package.
Built-in functions are imported through header files.
9.
It uses Dictionaries type which acts as structures in c++ and lists same as array in c++
It uses structure type which is collection of elements of different types and array collection of same type of data elements.
10.
Python accepts single('), double(“) and triple(''' or '''''') quotes to denote string literal and string should start and end with same type.
C++ supports string literal encoded in double(“) quotes.
11.
Every line should be proceeded with hash(#) symbol to make that comment.
We can use single line and multi-line comments.
12.
We can use OS (operating system) module to perform directories operations.
Standard c++ don't have a library for directries.
13.
It provides smtplib module to send email to any other Internet machine.
It does not provides direct methods to send mails to other Internet machines.





1.
To print hello.
print 'hello'


#include<iostream>
using namespace std;
main()
{
cout<<"hello";
}
2.
To add two numbers and to display a string.
a=raw_input("Enter value of A")
b=raw_input("Enter value of B")
c=int(a)+int(b)
print c
name="satvir"
print name
#include<iostream>
using namespace std;
int main()
{
using std::cin;
using std::cout;
int a,b,c;
cout<<"enter value of a";
cin>>a;
cout<<"enter value of b";
cin>>b;
c=a+b;
cout<<"Addition of two numbers :\t"<<c<<"\n";
string str="Satvir kaur";
cout<<str<<"\n";
return(0);
}
3.
To import package like math.
from math import *
print pi , e
print sqrt(64)
#include<iostream>
#include<math.h>
using namespace std;
int main()
{
using std::cout;
using std::cin;
cout<<"square roort of 64 is: "<<sqrt(64);
return(0);
}

No comments:

Post a Comment