No history yet

Python Syntax Basics

Variables Without the Ceremony

Coming from Java, the first thing you'll notice about Python is its relaxed approach to variables. In Java, you must declare a variable's type before you can use it, like String name = "Alice";. This is called static typing.

Python, on the other hand, is dynamically typed. You don't declare types at all. The interpreter figures out the type when you assign a value. This makes the code cleaner and often faster to write.

# No type declarations needed!
name = "Alice"  # This is a string
age = 30       # This is an integer
height = 5.5   # This is a float
is_student = True # This is a boolean

# You can even change the type later
age = "thirty" # Now 'age' is a string

While you don't declare types, Python still has them. The main ones you'll use are integers (int), floating-point numbers (float), strings (str), and booleans (bool). The flexibility of dynamic typing is powerful, but it means you need to be mindful of what type of data your variable holds at any given time.

Operators and Expressions

Most operators in Python will feel familiar. Arithmetic operators like +, -, * work just as they do in Java. However, there's a key difference in division.

In Python, / always performs float division, while // performs integer (floor) division. This is different from Java, where / on two integers results in an integer.

print(10 / 4)   # Output: 2.5 (Float division)
print(10 // 4)  # Output: 2 (Integer division)

# Modulo and exponentiation
print(10 % 3)   # Output: 1 (Remainder)
print(2 ** 3)   # Output: 8 (2 to the power of 3)

Comparison operators (==, !=, <, >) are the same. Logical operators, however, use words instead of symbols. You'll use and, or, and not instead of Java's &&, ||, and !.

Controlling the Flow

The single most important syntactic difference between Java and Python is how they define code blocks. Java uses curly braces {}. Python uses indentation.

Python uses indentation (not curly braces) to define code blocks.

This isn't just a style suggestion; it's a rule. Every line inside a code block must be indented, usually with four spaces. When the indentation stops, the block ends. Let's look at an if statement.

temperature = 75

if temperature > 80:
    print("It's a hot day!")
    print("Drink plenty of water.")
elif temperature < 60: # Note 'elif', not 'else if'
    print("It's a bit chilly.")
else:
    print("The weather is nice.")

print("This line is outside the if/elif/else block.")

Loops also follow this indentation rule. Python's for loop is a bit different from Java's. It works more like a "for-each" loop, iterating over a sequence of items.

# To loop a specific number of times, use range()
for i in range(5):  # Loops from 0 to 4
    print(i)

# To loop over a list of items
colors = ["red", "green", "blue"]
for color in colors:
    print(color)

while loops, thankfully, are almost identical to their Java counterparts, just without the braces and parentheses.

count = 0
while count < 3:
    print(f"Count is {count}")
    count = count + 1

Let's check your understanding of these core concepts.

Quiz Questions 1/5

A Java developer writes String name = "Alice"; which works perfectly. When they write the same line in Python, it fails. Why?

Quiz Questions 2/5

In Java, you might write if (isLoggedIn && isAdmin). What is the equivalent keyword for the logical AND operator in Python?

Getting comfortable with indentation and Python's loop structures is the key to a smooth transition from Java. These basic building blocks are the foundation for everything else you'll do in the language.