Python Notes Final Updated
Python Notes Final Updated
Append
Short Rule
**Note: There's an inner third bracket and when we close that one it represents ONE ROW and
it is followed up by a comma afterwards to represent another row( we use open and close third
brackets again). The elements in the inner bracket represent the number of columns present in
the 2D array. At the end of the array(list) in python we close the third bracket indicating the end
of list)
“ “ initialises every element in the column for the first row. After it is done iterating for 3 times, it
moves onto the next row and this continues for 3 times.
Method 3: append using third brackets directly:
Read:
Python takes the entire file and converts it into one single string variable under the
variable Content.
readline:
Using readline when there's multiple lines
strip() is used to remove unwanted spaces or newline characters from the beginning and
end of a string.
split({parameter}) splits at EXACT parameter given and removes the
separator and returns a list.
Example:
Txt file contents
Code to see the contents without the , and on a single line (the Data
variable lists all the elements separated by the comma and gets reset
every time when it moves on the next line in the file. The Name variable
stores the 0th index of the array(the name) and Mark stores the 1st
index of the array(the score itself)).
Example of Writing contents in a file:
The while loop will keep on running until the user inputs a valid input. The while condition
should represent the INVALID condition.
while loop:
You can use a flag though if you intend on using a while loop.
for loop:
In this example, even though the for loop was supposed to run for 5 times, it
exited the loop after the 2nd attempt because of the break statement.
Slicing (String Operation):
[:4] extracts the first four characters of the string from the left. This acts as the
LEFT() prebuilt function which we used in AS Cs.
1st Method of extracting the last 4 letters:
[-4:] extracts the last four digits from the string. This acts as the RIGHT() inbuilt
function as we used in AS Cs.
Or
To get ‘02’ we will use [4:6], the 6th index is exclusive so we don't extract the
letter at the 6th index. We stop at the 5th index. We will extract all the values until
the value of the index which is on the right hand side of the ‘:’ .
Another example:
To get the number ‘3443’ we will start from the 3rd index and extract values until
the 7th index(again 7th index is exclusive).
General Rule
Parent Class:
Inherited class:
Always remember to include the name of the parent class in the parenthesis of
the child class. super().__init__() will have parameters of the parent class.
As SetPay() was a setter for the parent class we will use super().