[python] int, float, bool, str

brandon·2025년 10월 27일

python

목록 보기
2/3

int

Arbitrary Precision: Unlike Java's primitive int and long (which have fixed sizes based on the architecture, typically 32-bit and 64-bit), Python's int has arbitrary precision. It can handle integers of any size limited only by the available memory.

  • Interview Relevance: This means you don't need to worry about integer overflow for extremely large numbers, a common concern in Java.

Everything is an Object: In Python, int is a class, and variables holding integers are objects, not primitives.

float

bool

  • Truthiness (Falsiness): This is a critical concept in Python and a major difference from Java. Many built-in types have a boolean equivalent:

    • Falsy values (evaluate to False in a boolean context like an if statement):

      • False

        • None

        • 0 (integer zero)

        • 0.0 (float zero)

        • Empty sequences (e.g., "", [], ())

        • Empty mappings (e.g., {})

    • Truthy values (evaluate to True): Everything else.

  • Interview Relevance: You can write concise code like if my_list: instead of if len(my_list) > 0:.

str

  • Immutable: Like in Java, Python strings are immutable. Operations that appear to modify a string actually create a new one.

  • Unicode: Python strings are Unicode by default (like Java's String).

  • Slicing: This is a key Python feature. You can access substrings using the slicing syntax: my_str[start:end:step].

    • s[:5] (first 5 characters)

    • s[2:] (from index 2 to the end)

    • s[::-1] (a common and concise way to reverse a string)

  • Interview Relevance: Be prepared to use slicing for efficient string manipulation.

  • String Formatting: Be familiar with f-strings (formatted string literals) as the modern and preferred way to format strings (e.g., f"The answer is {result}").

profile
everything happens for a reason

0개의 댓글