Python for Experienced JavaScript Developers
Python Syntax Basics
From JavaScript to Python
If you're coming from JavaScript, Python's syntax will feel both familiar and refreshingly simple. The biggest change to get used to is that Python uses indentation, not curly braces {}, to group code. This might seem strange at first, but it enforces a clean, readable style across all Python projects.
In Python, whitespace is meaningful. It's not just for looks; it's part of the syntax itself.
Variables and Data Types
Like JavaScript, Python is dynamically typed, meaning you don't have to declare a variable's type. You can forget let, const, and var. In Python, you just name your variable and assign it a value.
# No keywords needed, just a name and a value
user_name = "Alex"
user_age = 32
is_active = True
# You can change the type later
user_age = "thirty-two" # Now it's a string
Python's basic data types have direct parallels in JavaScript. You have strings (using single or double quotes), numbers (integers like 10 and floats like 10.5), and booleans (True and False, with a capital letter).
Controlling the Flow
Conditional logic and loops work similarly, but the syntax is cleaner. Every control statement ends with a colon :, and the code that belongs to it is indented one level deeper.
The colon
:signals the start of an indented block of code.
For conditionals, Python uses if, elif (short for 'else if'), and else.
score = 85
if score >= 90:
print("You got an A!")
elif score >= 80:
print("You got a B.")
else:
print("Keep studying!")
# Output: You got a B.
Python's for loop is simpler than JavaScript's traditional for loop. It's designed to iterate directly over a sequence, like a list (Python's version of an array). This is similar to a for...of loop in JavaScript.
# Loop over a list of strings
colors = ["red", "green", "blue"]
for color in colors:
print(color)
If you need to loop a specific number of times, like in a classic for (let i = 0; ...) loop, you can use the range() function.
# Loop 5 times, from 0 to 4
for i in range(5):
print(f"Loop number {i}")
Defining Functions
To create a function, you use the def keyword, followed by the function name, parentheses for parameters, and a colon. Just like with control structures, the function's body is indented.
# A function that takes two arguments and returns their sum
def add_numbers(num1, num2):
result = num1 + num2
return result
# Call the function
sum_value = add_numbers(5, 10)
print(sum_value) # Output: 15
Here’s a direct comparison of the syntax for a simple function in both languages.
| JavaScript | Python |
|---|---|
| ```js | |
| function greet(name) { | |
return Hello, ${name}!; | |
| } | |
| ``` | ```python |
| def greet(name): | |
| return f"Hello, {name}!" |
The logic is the same, but Python's version is more compact, replacing curly braces and the function keyword with def and indentation.
Time to check your understanding of these core concepts.
What does Python use to define the scope of a code block, such as the body of a loop or function?
In Python, every control statement (like if, for, def) must end with which character?
That covers the essential syntax. By focusing on variables, control flow, and functions, you've already learned the building blocks for writing simple Python programs.