Because I first learned Python and then I wanted to learn C, I found it very difficult to even have a basis for comparison between the 2. So here it is, a side by side syntax comparison between Python and C !
A few mentions before we start: in C, you first include libraries, but then everything is written in the function “main”: for example : int main(){your code here}. Indentation is fundamental in Python, while in C it only matters as a coding Style ( you can read it easier). C has extra syntax because it has Pointers and Memory allocation and de-allocation, whilst Python resolves these problems automatically. C has extra variable types as well ( char, int, float, long, double, etc)
Python | C | |
---|---|---|
Descriptions | ## This is a description | // This is a description |
print “Hello” | print('Hello, World!') | // Everywhere where we have a print function , we have to // include this library: #include <stdio.h> |
For loop |
|
|
While loop |
|
|
If else statements |
|
|
List declaration | ## Lists can hold any type of data, ## even other lists or dictionaries aList = [4, 73, 'aWord', 'txt'] | // A list can contain either integers, either chars, // but not combined! int aList[] = {400, 2, 3, 7, 50}; |
List access | ## Gets the item at index 1 and stores it in ## firstNum firstNum = aList[1] | // Same SyntaxfirstNum = aList[1]; |
Strings | ## Pyhton knows that greeting is a stringgreeting = 'Hello' | // String is actually an array of type ‘char’ // Can be also: char hello[6] = {‘H’, ‘e’, ‘l’, ‘l’, ‘o’, ‘\0’}; char greeting[] = "Hello"; |
String Concatenation | txt1 = 'Hello ' | char txt1[] = "Hello "; |
String Length | len(greeting) | strlen(greeting) |
String Compare |
|
|
Functions |
|
|