DEV Community

Cover image for C++ Vs Python Syntax Difference
Natnael Getenew
Natnael Getenew

Posted on

C++ Vs Python Syntax Difference

In my humble opinion learning, by comparison, is the best way of learning, To stay in the loop while the industry is moving extremely rapidly, learning complex concepts by comparing to already familiar stuff is our best bet.

In this blog post, I will show you some common programs implemented in python and C++, So if you are trying to learn one of those languages or both at the same time! this post will serve you to learn in a comparable manner, let's dive in!

Hello World Program

  1. Hello World Program in python
print("Hello, World!")
Enter fullscreen mode Exit fullscreen mode
  1. Hello World Program in C++

#include <iostream>

int main() {
    std::cout << "Hello World!";
    return 0;
}
Enter fullscreen mode Exit fullscreen mode

Display Fibonacci Series

  1. Display Fibonacci Series in python
nterms = int(input("How many terms you want? "))  
# first two terms  
n1 = 0  
n2 = 1  
count = 2  
# check if the number of terms is valid  
if nterms <= 0:  
   print("Please enter a positive integer")  
elif nterms == 1:  
   print("Fibonacci sequence:")  
   print(n1)  
else:  
   print("Fibonacci sequence:")  
   print(n1,",",n2,end=', ')  
   while count < nterms:  
       nth = n1 + n2  
       print(nth,end=' , ')  
       # update values  
       n1 = n2  
       n2 = nth  
       count += 1  
}
Enter fullscreen mode Exit fullscreen mode
  1. Display Fibonacci Series in C++

#include <iostream>  
using namespace std;  
int main() {  
  int n1=0,n2=1,n3,i,number;    
 cout<<"Enter the number of elements: ";    
 cin>>number;    
 cout<<n1<<" "<<n2<<" "; //printing 0 and 1    
 for(i=2;i<number;++i) //loop starts from 2 because 0 and 1 are already printed    
 {    
  n3=n1+n2;    
  cout<<n3<<" ";    
  n1=n2;    
  n2=n3;    
 }    
   return 0;  
   } 

Enter fullscreen mode Exit fullscreen mode

weather If you already know cpp and trying to learn python or vice-versa I hope you get the general gist of the Syntax difference between the two languages,
If you like this and this method of learning things, I have a repository dedicated to comparing programming languages and concepts, you can check it out on github

Top comments (1)

Collapse
 
miloszstx profile image
Miłosz Gałganek

That's informative, I would like to also recommend this resource: pythonvs.com/cplusplus