1. Intro
- # : Comments
- Can appear at the start of the line / After Whitespace in line / After code in the same line
- Cannot appear within a string
- + , – , * , / , () :
- Regular arithmetic operations and grouping using ()
- /: returns float
- //: returns floor division or int
- ** : Power of
- 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
- If you use mixed operands of int & float then the result will be float.
- In interactive Python, the last printed value from a numeric operation can be accessed from “_”
- Complex numbers notation – suffix j/J
- 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
- If a quote ” or ‘ is part of a string, precede it with the escape
- 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.
- “”” 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
- * : String when multiplied with a number will repeat the sting occurrence.
- + : Can be used to concatenate strings. It works with the concatenation of a variable and a string too
- Separate strings next to each other in quotes will be concatenated. Only works with strings, not with variables or expressions.
- 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
- 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
- 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]