Variables, Data Types, and Basic Operators

In this lesson, we’ll explore how Python handles variables, data types, and basic operators, comparing these concepts to their Java counterparts. This knowledge will help you transition smoothly from Java’s static typing to Python’s dynamic typing system.

Python’s Basic Data Types

Python has several built-in data types that are similar to Java’s, but with some key differences:

  1. Integers (int): Whole numbers, positive or negative, without a decimal point.

    # Python
    x = 5
    y = -10
    
  2. Floating-point numbers (float): Numbers with a decimal point.

    # Python
    pi = 3.14
    e = 2.71828
    
  3. Strings (str): Sequences of characters, enclosed in single or double quotes.

    # Python
    name = "Alice"
    greeting = 'Hello, World!'
    
  4. Booleans (bool): True or False values.

    # Python
    is_python_fun = True
    is_java_better = False
    

Unlike Java, Python doesn’t have separate types for characters or different sizes of integers (like byte, short, int, long).

Type Inference and Type Hints

While Python is dynamically typed, it supports type hints for better code readability and tool support:

# Python
def greet(name: str) -> str:
    return f"Hello, {name}!"

age: int = 30

These type hints don’t affect runtime behavior but can be used by tools like mypy for static type checking.

Arithmetic Operators

Arithmetic operators in Python are similar to Java:

# Python
a = 10
b = 3

print(a + b)  # Addition: 13
print(a - b)  # Subtraction: 7
print(a * b)  # Multiplication: 30
print(a / b)  # Division: 3.3333... (always returns a float)
print(a // b) # Integer division: 3
print(a % b)  # Modulus: 1
print(a ** b) # Exponentiation: 1000 (not available in Java)

Note that Python’s division operator (/) always returns a float, unlike Java’s integer division.

Logical Operators

Python uses words instead of symbols for logical operators:

# Python
x = True
y = False

print(x and y)  # Logical AND: False
print(x or y)   # Logical OR: True
print(not x)    # Logical NOT: False

This differs from Java’s &&, ||, and ! operators.

Comparison Operators

Comparison operators in Python are similar to Java:

# Python
a = 5
b = 10

print(a == b)  # Equal to: False
print(a != b)  # Not equal to: True
print(a < b)   # Less than: True
print(a > b)   # Greater than: False
print(a <= b)  # Less than or equal to: True
print(a >= b)  # Greater than or equal to: False

The None Type vs Java’s null

Python uses None to represent the absence of a value, similar to Java’s null:

# Python
x = None
print(x is None)  # True

In Python, you typically use is to check for None, rather than ==.

Conclusion

In this lesson, we’ve covered the basics of variables, data types, and operators in Python, highlighting the key differences from Java. Python’s dynamic typing and simplified syntax offer flexibility, but it’s important to be mindful of potential type-related issues that static typing would catch at compile-time in Java.

In the next lesson, we’ll explore control structures and functions in Python, building on the foundation we’ve established here. You’ll see how Python’s approach to these fundamental programming constructs differs from Java’s, further enhancing your ability to write Pythonic code.