31/03/2025, 15:30                                                                     about:blank
Module 1 Cheat Sheet: Python Basics
   Package/Method   Description                                                                  Code Example
                                                                                                      # This is a comment
                    Comments are lines of text that are ignored by the Python interpreter when
   Comments
                    executing the code<./td>
                                                                                                 Syntax:
                                                                                                      concatenated_string = string1 + string2
   Concatenation    Combines (concatenates) strings.
                                                                                                 Example:
                                                                                                      result = "Hello" + " John"</td>
                                                                                                 Example:
                                                                                                      x=7
                                                                                                      # Integer Value
                                                                                                      y=12.4
                                                                                                      # Float Value
                                                                                                      is_valid = True
                                                                                                      # Boolean Value
                                                                                                      is_valid = False
                                                                                                      # Boolean Value
   Data Types       - Integer - Float - Boolean - String                                              F_Name = "John"
                                                                                                      # String Value
                                                                                                 Example:
                                                                                                      my_string="Hello"
                                                                                                      char = my_string[0]
   Indexing         Accesses character at a specific index.
   len()            Returns the length of a string.                                              Syntax:
                                                                                                      len(string_name)
                                                                                                 Example:
                                                                                                      my_string="Hello"
                                                                                                      length = len(my_string)
about:blank                                                                                                                                     1/3
31/03/2025, 15:30                                                                          about:blank
                                                                                                     Example:
                                                                                                          my_string="Hello"
                                                                                                          uppercase_text = my_string.lower()
   lower()            Converts string to lowercase.
                                                                                                     Example:
                                                                                                          print("Hello, world")
                                                                                                          print(a+b)
   print()            Prints the message or variable inside `()`.
                                                                                                     Example:
                                                                                                          x = 9 y = 4
                                                                                                          result_add= x + y   #   Addition
                                                                                                          result_sub= x - y   #   Subtraction
                      - Addition (+): Adds two values together.                                           result_mul= x * y   #   Multiplication
                                                                                                          result_div= x / y   #   Division
                      - Subtraction (-): Subtracts one value from another.                                result_fdiv= x //   y   # Floor Division
                      - Multiplication (*): Multiplies two values.                                        result_mod= x % y   #   Modulo</td>
   Python Operators   - Division (/): Divides one value by another, returns a float.
                      - Floor Division (//): Divides one value by another, returns the quotient as
                      an integer.
                      - Modulo (%): Returns the remainder after division.
                                                                                                     Example:
                                                                                                          my_string="Hello"
                                                                                                          new_text = my_string.replace("Hello", "Hi")
   replace()          Replaces substrings.
                                                                                                     Syntax:
                                                                                                          substring = string_name[start:end]
   Slicing            Extracts a portion of the string.
                                                                                                     Example:
                                                                                                          my_string="Hello" substring = my_string[0:5]
about:blank                                                                                                                                              2/3
31/03/2025, 15:30                                                     about:blank
                                                                                Example:
                                                                                     my_string="Hello"
                                                                                     split_text = my_string.split(",")
   split()          Splits string into a list based on a delimiter.
                                                                                Example:
                                                                                     my_string="Hello"
                                                                                     trimmed = my_string.strip()
   strip()          Removes leading/trailing whitespace.
                                                                                Example:
                                                                                     my_string="Hello"
                                                                                     uppercase_text = my_string.upper()
   upper()          Converts string to uppercase.
                                                                                Syntax:
                                                                                     variable_name = value
   Variable
                    Assigns a value to a variable.                              Example:
   Assignment
                                                                                     name="John" # assigning John to variable name
                                                                                     x = 5 # assigning 5 to variable x
  © IBM Corporation. All rights reserved.
about:blank                                                                                                                          3/3