Python Is Weird
If you had programmed a little bit in one or two other programming languages, and started to learn Python, you will surely notice that their something weird about its syntax.
Every programming language has its quirks, and Python is no exception. If you're coming from languages like Java, C, or JavaScript, you might find some things about Python... weird.
But don’t worry! These quirks make Python simpler and more readable once you get used to them.
Let's explore some these weird, yet interesting, aspects of the Python language. I would like you to hold this chapter deerly and come back to it from time to time.
1. No Curly Brackets {}
- In languages like C, Java, and JavaScript, curly brackets
{}
define blocks of code. - Python does NOT use curly brackets. Instead, it relies on indentation to define code blocks.
JavaScript example:
if (x > 0) {
console.log("Positive number");
}
Python equivalent:
if x > 0:
print("Positive number")
2. Indentation Matters!
- In most languages, indentation is just for readability.
- In Python, indentation is REQUIRED! It defines the structure of your code.
Wrong Python code (no indentation):
if x > 0:
print("Positive number") # IndentationError: expected an indented block
Correct Python code:
if x > 0:
print("Positive number") # Indented properly
3. No Semicolons ;
Required
- Most languages require semicolons (
;
) to end statements. - Python doesn’t need them!
JavaScript example:
console.log("Hello, World!");
Python equivalent:
print("Hello, World!")
4. def
Instead of function
- In JavaScript, you define functions using
function
. - In Python, you use
def
instead.
JavaScript function:
function greet(name) {
return "Hello, " + name;
}
Python equivalent:
def greet(name):
return "Hello, " + name
5. No var
, let
, or const
– Just Assign Variables Directly!
- Python doesn’t need
var
,let
, orconst
to declare variables. - Just assign a value to a variable, and Python figures out the type automatically.
JavaScript:
let age = 25;
const name = "Alice";
Python:
age = 25
name = "Alice"
6. elif
Instead of else if
- In most languages, multiple conditions use
else if
. - In Python, it's
elif
(shorter and cleaner).
JavaScript example:
if (x > 0) {
console.log("Positive");
} else if (x < 0) {
console.log("Negative");
} else {
console.log("Zero");
}
Python equivalent:
if x > 0:
print("Positive")
elif x < 0:
print("Negative")
else:
print("Zero")
7. True
and False
Are Capitalized
- In Python, Boolean values must be written as
True
andFalse
, nottrue
orfalse
.
JavaScript:
let isHappy = true; // Lowercase
Python:
is_happy = True # Uppercase
8. None
Instead of null
- In many languages, a variable with no value is
null
. - In Python, it’s called
None
(capitalized).
JavaScript:
let value = null;
Python:
value = None
9. String Formatting with f"..."
- Python has a special way to insert variables into strings using f-strings (
f"..."
). - This is simpler than using
+
or.format()
.
JavaScript:
let name = "Alice";
console.log("Hello, " + name + "!");
Python equivalent:
name = "Alice"
print(f"Hello, {name}!")
10. Lists Instead of Arrays
- Python doesn’t have traditional arrays like JavaScript or Java.
- Instead, it has lists, which can store different types of values.
Python list example:
numbers = [1, 2, 3, 4, 5]
mixed = [1, "hello", 3.5, True]
11. is
vs ==
(Be Careful!)
==
checks if values are the same.is
checks if two variables refer to the same object in memory.
Example:
x = [1, 2, 3]
y = [1, 2, 3]
print(x == y) # ? True (values are the same)
print(x is y) # ? False (different objects in memory)
Key Takeaways
- No curly brackets
{}
, indentation matters. - No semicolons
;
needed at the end of lines. - Use
def
instead offunction
for defining functions. - Use
elif
instead ofelse if
for multiple conditions. - Booleans are
True
andFalse
, nottrue
andfalse
. - Use
None
instead ofnull
for empty values. - Use f-strings (
f"..."
) for inserting variables into strings. - Lists (
[]
) replace traditional arrays. - Be careful with
is
vs==
!.
Mini Challenge: Spot the Errors!
Below is a buggy Python script. Can you spot at least 3 mistakes before running it?
if (x > 0) {
print "Positive"
} else if x < 0:
print("Negative);
else:
print("Zero")
Give it a try! Would you like an answer key for this challenge?