• Home
  • LLMs
  • Docker
  • Kubernetes
  • Java
  • Ubuntu
  • Maven
  • Archived
  • About
Python | Variables
  1. Variables
  2. Comment
  3. Numbers
  4. Strings
  5. Dates and Times

  1. Variables
    Variables names:
    • Can contain only letters ([a-zA-Z]), numbers ([0-9]), and/or underscores (_).
    • Must start with a letter or underscore.
    • Can't start with a number.
    • Are case-sensitive.
    • Can't use Python reserver keywords.

    As best practices, you should use descriptive and lowercase (e.g., value) for variable names.
    You should use undrscores for multi-word variable names (e.g., max_value) and use all uppercase for constants (MAX_VALUE = 10).

    Define a variable:

    Multiple assignment: Assign multiple variable in one instruction:

    Multiple assignment: Assign same value to multiple variables:

    Swapping variables:
  2. Comments
    To comment a line or part of a line, use the hash mark character (#).

    For multi-line comments, use triple quotes (''', """):
  3. Numbers
    Integers: -1, 0, 1, 2, 1_000_000, ...
    Floats, 1.0, 2.0, 1.2e-3, ...
    Operators: Addition (+), Subtraction (-), Multiplication (*), Division (/), Modulo (%), Exponentiation (**)

    Example - Modulo operator (Remainder operator):

    Using underscores for readability:

    Complex numbers:
  4. Strings
    • Valid Strings:

      Multi-line Strings:

    • Changing string case:

      The title() function changes each word of a string in title case (begins with a capital letter):

      The upper() function changes a string to all uppercase:

      The lower() function changes a string to all lowercase:

    • Formatted Strings (f-strings):
      To use a variable, expression, or a function inside a string, place the letter f before opening quotation mark.
      Inside the string put any variable or function you want to use and surround it with braces.
      You can also use the format() method:

    • Adding whitespace to strings with tabs (\t) or newlines (\n):
      Output:

    • Removing whitespace:


      Use strip() function to remove whitespace from the beginning (left) and the end (right) of a string:

      Use lstrip() function to remove whitespace from the beginning (left) of a string:

      Use rstrip() function to remove whitespace from the end (right) of a string:

    • Removing prefixes and suffixes:
    • String splitting:

      • Split by whitespace:

        Output:

      • Split by specific delimeter:

        Output:

      • Split lines:

        Output:

    • Other methods:

    • Type conversion:
  5. Dates and Times
    Parsing a date string using a specific format:

    Date and time format codes:
    • %Y: Year, in four-digit format (2023, ...)
    • %y: Year, in two-digit format (23, ...)
    • %m: Month number (01 - 12)
    • %B: Month name (March)
    • %b: Month name abbreviated (Mar)
    • %d: Day of the month (01 - 31)
    • %A: Weekday name (Friday)
    • %a: Weekday name abbreviated (Fri)
    • %H: Hour, in 24-hour format (00 - 23)
    • %I: Hour, in 12-hour format (00 - 12)
    • %M: Minutes (00 - 59)
    • %S: Seconds (00 - 59)
    • %f: Microseconds (090579)
    • %p: AM|PM
    • %z: UTC offset (+0000)
    • %Z: Timezone name (EST)
© 2025  mtitek