0% found this document useful (0 votes)
3 views10 pages

Coding

The document provides a comprehensive overview of data types, specifically converting between strings, integers, and floats, as well as dictionary operations including adding, deleting, and modifying items. It also covers file handling techniques, advanced list handling methods such as enumerate and zip, and introduces concepts like lambda functions, recursion, and conditional expressions. Additionally, it includes examples of mathematical operations and basic loop functions.

Uploaded by

0923ryanng
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)
3 views10 pages

Coding

The document provides a comprehensive overview of data types, specifically converting between strings, integers, and floats, as well as dictionary operations including adding, deleting, and modifying items. It also covers file handling techniques, advanced list handling methods such as enumerate and zip, and introduces concepts like lambda functions, recursion, and conditional expressions. Additionally, it includes examples of mathematical operations and basic loop functions.

Uploaded by

0923ryanng
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

Data type

Turning data type > For string to integer


For example: “21.5” > int(float(age)) is better (int(age) might not work)

Turning float to string >> Will still contain the .0 >> better remove it
Code: str(age) or str(int(age))

Dictionary
animals = { "ant": "a small insect with 6 legs", "dog": "an animal that goes woof", "cat": "an animal that goes miaow"}
> Print(animals["ant"]) Output: a small insect with 6 legs

Or
heads = {"David": (589, 106, 48, 63), "Gibson": (474, 102, 44, 58), "Paul": (522, 162, 55, 68)}
>> david_data = heads["David"] Output: (589, 106, 48, 63)
Retrieving just one item>> width = david_data[2] > print(width) >> Output: 48
Adding new items>> heads["Sean"] = (628, 146, 46, 58) >> add at the back
Del: del heads["Paul"] >> Gone (whole delete)
Change: heads["David"] = (588, 104, 48, 57) >> Direct change it
GO through the dictionary

For Key in heads: For value in heads

Print >> the left hand side Print >> right hand side

No heads = { [474, 102, 44, 58]: "Gibson"}

Yes heads = { (474, 102, 44, 58): "Gibson"}

If for key, value in [Link]():


print(key, value) >> Printing both

>> cann’t use a list as key but can use a tuple as key

>>> Dictionary > Use slicing [2:]


>> Also output same line > “\n”

Past paper > swapping the key & value Baby shark case
>> For LG7 only have TT veggie left cuz key only can have one >> cant be LG7, >> Rep 1: Value[0] or Lyrics[key][0]
LG7, LG7.. > the lastest one will replace the top ones
More example:
capitals = { "Belgium": "Brussels", "France": "Paris", "Germany": "Berlin", "Netherlands": "Amsterdam"}

for key in [Link](): for key, value in [Link]():


print("The capital of", key, "is", capitals[key]) print("The capital of", key, "is", value)

File handling

\t >>> TAB rstrip() > remove anything you can’t see on the right After rstrip > Use .split
side

>> split () vs split(“”)


\n >>> New line
Cal
Code:
result1 = 0
for segment in [Link]('+'):
result2 = 1
for number in [Link]('*'):
number = int(number)
result2 *= number
result1 += result2

Result:
1+2+3+4+5 is 15
>> Altho no * > Also will loop once
(1+ 1*2 +1*3+1*4+1*5)
Result of 1*2*3*4*5 is 120
>> Altho no + > Also loop once
Result of 1+2*3+4*5 is 27
Result of 1+2*3*4+5 is 30

> Strip & compare


[Janny,100,90,80] vs [Chan,50,90,60] etc
>> Keeping the highest name due to for loop

Advanced List Handling

Enumerate: obtain an Zip: pair multiple lists together


indexing of a list with the items in a loop

Mochi is a Bulldog!
Sunny is a Corgi!
New: Cocoa is a Poodle!

List comprehension

>>> 1. Bulldog 2. Corgi 3. Poodle


[0, 1, 2, 3, 4]
Printing

[4.4, 7.2, 6.8, 10.0, 6.4] >> better than a for loop

>> Filtering with comprehension

Combining

>> you can


tags = [f"{f}\n${p * 0.8:.2f}" for f, p in zip(flowers, prices)]
Or new_prices = [p * 0.8 for f, p in zip(flowers, prices)]
Or
print(tags[0]) > Rose $5.50 print(tags[3] > Orchid $12.5

[Link]

Colour

Add text

Legend

plt.tight_layout()
Conditional expression

Match statement

Just consider one > Cuz it is MATCH >> Replace multiple if elif statement &

‘_’ to match any pattern, to act like else in if statements: Assign value
‘|’ to separate & can have two conditions
Sequence Condition

Fruit store case example

Lambda Functions>> power function

To print function > enclose it with a list

For example:
Complex example using Map

_ Using split
Filter

Mixing both Using for & if (alternative)

Class
>> reminder: use return for actual output

Recursion >> definition eventually can stop (even using while also can)
Key: NO error

Past paper

>> recursion(3) = 6 because > 3*2

Tricky pass paper

For inputs >> Need to use int(input(

if not number % n: means “if number is divisible by n with no remainder”.


If x >> if x !=0
If not X: >> if X==0

Filter every x days >> %

Reverse list order


[Link]()
mylist=mylist[::-1]

Function >> if no return >> don’t change


This code doesn;t work >> No return result within function //x > integer division e.g.21//2 >10 (remove o.5)
Need to see which line of code is wrong
If x == y
Or response = input("xxxx) > miss jor “
Typos like answer vs anser

Maze complex version

>> For strings * number > repeat that amount of times


E.g. “”*2 >> two blank space, “X”*2 >> XX
Basic loop functions
●​ 14: len(lines_of_code)
●​ 15: range(len(lines_of_code))
Continue & break

Removing a line
●​ Show both [1:int(position)] & [int(position +1):]

Audio slicing

>> i%2, side -1

You might also like