Python Features That May Be Unfamiliar to C/Java Programmers
(Quick reference)
Python was designed with simplicity and readability in mind. As a result, it differs from traditional programming languages such as C, C++, Java, and JavaScript in several ways. This section highlights some of the most notable differences.
1. No Traditional switch Statement
Older versions of Python do not have a switch statement. Instead, they use if-elif-else.
day = 3
if day == 1:
print("Monday")
elif day == 2:
print("Tuesday")
elif day == 3:
print("Wednesday")
else:
print("Unknown")
Starting with Python 3.10, Python introduced match-case, which provides functionality similar to a switch statement while supporting more advanced pattern matching.
day = 3
match day:
case 1:
print("Monday")
case 2:
print("Tuesday")
case 3:
print("Wednesday")
case _:
print("Unknown")
2. No Curly Braces
Most programming languages use curly braces ({}) to define code blocks. Python uses indentation instead.
C / Java
Python
Consistent indentation is mandatory in Python.
3. No Semicolons
Statements do not require semicolons.
Although semicolons are allowed, they are rarely used.
4. Dynamic Typing
Variables do not require explicit type declarations.
C
Python
Python automatically determines the data type at runtime.
5. Everything Is an Object
In Python, nearly everything is an object, including integers, strings, functions, and classes.
Output:
6. No ++ or -- Operators
Python does not support increment or decrement operators.
C
Python
7. No do...while Loop
Python does not include a do...while loop.
C
Python Equivalent
8. Different for Loop
Traditional languages typically use a counter-controlled loop.
C
Python
The expression range(5) generates:
9. Lists Instead of Arrays
Python's built-in collection is the list, which is dynamic and flexible.
Lists can also contain different data types.
10. Multiple Assignment
Python allows multiple variables to be assigned in one statement.
Variables can also be swapped without using a temporary variable.
11. Easy Iteration
Instead of looping through indexes, Python usually iterates directly over elements.
Instead of:
Use:
If both the index and value are needed:
12. List Comprehensions
Python provides concise syntax for creating lists.
Traditional approach:
Using a list comprehension:
13. Functions Can Return Multiple Values
A function can return more than one value.
14. No Mandatory main() Function
Python programs execute from top to bottom.
For larger programs, the following convention is commonly used:
This is optional but considered good practice.
15. Rich Built-in Data Structures
Python includes several powerful built-in data structures.
# List
numbers = [1, 2, 3]
# Tuple (immutable)
point = (2, 5)
# Set
unique = {1, 2, 3}
# Dictionary
student = {
"name": "Alice",
"age": 20
}
16. Functions Are First-Class Objects
Functions can be assigned to variables and passed as arguments.
This enables powerful programming techniques such as callbacks and higher-order functions.