Python Notes

1. Intro

  1. # : Comments
    • Can appear at the start of the line / After Whitespace in line / After code in the same line
    • Cannot appear within a string
  2. + , – , * , / , () :
    • Regular arithmetic operations and grouping using ()
    • /: returns float
    • //: returns floor division or int
  3. ** : Power of
  4. Variable – The variable needs to be assigned some value (defined). Calling an unassigned variable will cause an error
    • Variables don’t store the value – they point to the values – like pointers
  5. If you use mixed operands of int & float then the result will be float.
  6. In interactive Python, the last printed value from a numeric operation can be accessed from “_”
  7. Complex numbers notation – suffix j/J
  8. Strings – type str – can use both “” or ”
    • If a quote ” or ‘ is part of a string, precede it with the escape
      character “\” or use the alternate quote. i.e if ” needs to be
      displayed, enclose it with ‘ and vice versa
  9. print() – considers anything with \ as special character
    • use “r” inside the function before quotes to print raw string – i.e without considering \ as a special character
    • use “r” and “\\” to display “\” character.
  10. “”” or ”’ : Triple quote for multiple-line string
    • New Line / End of line is already included – no need to add. To avoid a new line use “\” at the end of the line
  11. * : String when multiplied with a number will repeat the sting occurrence.
  12. + : Can be used to concatenate strings. It works with the concatenation of a variable and a string too
  13. Separate strings next to each other in quotes will be concatenated. Only works with strings, not with variables or expressions.
  14. Indexing : String can be accessed with indexing. First character is 0 – <variablename>[0]
    • [-1] Last character
    • [-2] Second last character
    • Accessing an index with a large number will give an error if it goes beyond the index count of the string
    • String once defined in a variable cannot be changed – a new variable can be created for a new string
  15. Slicing: Access a substring
    • <variablename>[0:3] – substring of characters from position 0 to position 2
    • <variablename>[:4] – substring of characters from position 0 to position 3
    • <variablename>[5:] – substring of characters from position 5 to end
    • <variablename>[-2:] – substring of characters from second last to end
    • words[:index] + words[index:] is always equal to words
    • len() – returns the length of the string
  16. Lists – comma-separated values between square brackets – squares = [1, 4, 9, 16, 25]
    • usually, items are of the same type but can contain different types
    • Can be indexed and sliced
    • supports concatenation with variables and other lists
    • String variables are immutable i.e. cannot be changed. List Variables are mutable i.e. they can be changed once defined.
    • list.append() – adds new items at the end of the list
>>>cubes = [1, 8, 27, 64, 125]
>>>cubes.append(216) # add the cube of 6
>>>cubes.append(7 ** 3) # and the cube of 7
>>>cubes
[1, 8, 27, 64, 125, 216, 343]

Leave a Comment

Your email address will not be published. Required fields are marked *