C++ to Python Syntax Cheat Sheet
1. End of Statement
• C++: Use semicolons ( ; )
• Python: No semicolons
int x = 5;
x = 5
2. Braces vs Indentation
• C++: Uses {} for blocks
• Python: Uses indentation
if (x > 0) {
cout << "Positive";
}
if x > 0:
print("Positive")
3. Variable Declaration
• C++: Requires type
• Python: Dynamic typing
int a = 10;
double b = 3.14;
a = 10
b = 3.14
1
4. Input/Output
cin >> x;
cout << x;
x = input()
print(x)
To convert input to int/float:
x = int(input())
y = float(input())
5. Functions
int add(int a, int b) {
return a + b;
}
def add(a, b):
return a + b
6. Control Statements
If-Else:
if (x == 10) {
//...
} else if (x == 5) {
//...
} else {
//...
}
if x == 10:
# ...
elif x == 5:
2
# ...
else:
# ...
For Loop:
for (int i = 0; i < 5; i++) {
cout << i;
}
for i in range(5):
print(i)
While Loop:
int i = 0;
while (i < 5) {
cout << i;
i++;
}
i = 0
while i < 5:
print(i)
i += 1
7. Arrays / Lists
int arr[3] = {1, 2, 3};
arr = [1, 2, 3]
8. Strings
string s = "hello";
cout << s.length();
3
s = "hello"
print(len(s))
9. Classes
class Person {
public:
string name;
Person(string n) { name = n; }
};
class Person:
def __init__(self, name):
self.name = name
10. Miscellaneous
• No headers like #include
• No main() function required (but you can define one)
• No namespace std
• No pointers in basic Python
11. Comments
// single line
/* multi
line */
# single line
""" multi
line """
This cheat sheet covers the essentials to help you convert your C++ knowledge into Python fluency quickly.