sum, count and len

Posted under » PHP » Python on 14 March 2022

I'm a noob in excel and the only thing I use is the sum function.

In php it may look like this.

if ($result = $dbhandle -> query("SELECT * FROM question")) {
while($row = mysqli_fetch_array($result)) {
    $sum += $row['value'];
    }
    echo $sum."
"; } $dbhandle -> close();

This is the same with python django count.

If you want to just increment or +1 use ++ just like this example.

For arrays, you can do this in Python

salah = [1,2,1,4,100]

for cat in salah:
    print(sum(salah))

Or you can append to the array and do a len or length of an array, if want to count the value of 1s.

 betul = []
 for cat in x["stage107f"]:
        band = cat["band"]
 	kuching = cat["correctness"]
	if band == 2 and kuching == 1 :
                	betul.append(kuching) 
 print(len(betul))

A basic sum of var in python is like this

total = int(angsa) + int(answer)

If you don't put the int, it is assumed that it is string so you will not get the correct total.

web security linux ubuntu python django git Raspberry apache mysql php drupal cake javascript css AWS data