0% found this document useful (0 votes)
2 views22 pages

Python Notes Final Updated

The document outlines various programming concepts including appending and inserting data in lists, creating 2D arrays, file operations such as reading and writing, and data validation techniques. It also covers string manipulation through slicing and the use of object-oriented programming principles like inheritance. Key methods and functions such as strip(), split(), and the use of loops are discussed to enhance coding efficiency.

Uploaded by

fiadmasood777
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views22 pages

Python Notes Final Updated

The document outlines various programming concepts including appending and inserting data in lists, creating 2D arrays, file operations such as reading and writing, and data validation techniques. It also covers string manipulation through slicing and the use of object-oriented programming principles like inheritance. Key methods and functions such as strip(), split(), and the use of loops are discussed to enhance coding efficiency.

Uploaded by

fiadmasood777
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Append VS Inserting using a Pointer:

Append

Inserting using a Pointer

Short Rule

●​ append() → grow list


●​ arr[index] = data → replace/fill existing position
Creating a 2D array:

1st Method: Using nested loops

**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)

Method 2: Initialize with strings (the easier way)

“ “ 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:

Access a specific element in a 2D array:​

The value of row and column can be passed by an user.

Traverse and Output the contents of a 2D array:

File: READ, WRITE, APPEND OPERATION


readline vs read:

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

Using readline() (manual control)

Easier Method to read lines in file with unknown number of lines:

The output will be the same as the above.

Use of Strip() and Split():

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:

Always remember we use ‘+’ to concatenate strings and every element we


concatenate must be a string or else there will be type error between
int and str.

** ‘\n’ is the newline character — it tells Python to move to the next


line.
.upper() and .lower()
Data Validation using while :

The while loop will keep on running until the user inputs a valid input. The while condition
should represent the INVALID condition.

or vs and in while validation loops


importing random module and using it:

importing date module and using it:


Useful Operators
Use of break in loops:

The break statement is used to exit a loop prematurely, regardless of whether


the loop's condition is still true or if there are items left to iterate through.

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

[6:] will extract characters from the 6th index onwards.

this cheat sheet might help:​


If we want to extract values from the middle we will use this:

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

This can be thought of as the MID() function from AS.

*** Slicing only works on sequences like Strings or Lists.


OOP:​

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().

You might also like