Showing posts with label Python. Show all posts
Showing posts with label Python. Show all posts

Monday, January 3, 2022

Generate Excel File in Python

Install xlsxwriter:


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import xlsxwriter
# Create a workbook and add a worksheet.
workbook = xlsxwriter.Workbook('Expenses01.xlsx')
worksheet = workbook.add_worksheet()

# Some data we want to write to the worksheet.
expenses = (
    ['Rent', 1000],
    ['Gas',   100],
    ['Food',  300],
    ['Gym',    50],
)

# Start from the first cell. Rows and columns are zero indexed.
row = 0
col = 0

# Iterate over the data and write it out row by row.
for item, cost in (expenses):
    worksheet.write(row, col,     item)
    worksheet.write(row, col + 1, cost)
    row += 1

# Write a total using a formula.
worksheet.write(row, 0, 'Total')
worksheet.write(row, 1, '=SUM(B1:B4)')

workbook.close()

Run the python script:

Result:




Change the sheet title:

    



Thursday, December 9, 2021

Installing fast api

 










Kalu pengen ngubah portnya:

  


Terus klu pengen nyobain request yg post:



Buat ngelid docsnya


Tuesday, March 30, 2021

Create Virtual Envinronment in Django Using pipenv

 First thing first install pipenv using this command:
pip install pipenv

then execute this:
pipenv shell


Donee... haha... cool :D

Thursday, March 25, 2021

Konfigurasi Komodo Edit Buat Python 3

 Pertama-tama silahkan download disini:
https://www.filehorse.com/download-komodo-edit/




Setelah selese download, tinggal double click and next next...






























Setelah itu....








Berikutnya ke menu "Advanced Options"





Terus ke Menu "Key Binding" 








Naah skrg mari kita coba.... so buka script python yg sederhana ajjah kek gini





Wednesday, June 1, 2016

Newton-Rapson Finding Root using Python

Newton-Rapson tuuh... metode kyk bisection juga, cuman ini konsepnya pake polinomialnya newton... Klu di implementasi di python buat nyari akar suatu variabel float, tampilannya kyk gini :


1.
2.
3.
4.
5.
6.
7.
8.
epsilon = 0.01
y = 24.0
guess = y/2.0

while abs(guess*guess - y) >= epsilon:
    guess = guess - (((guess**2) - y)/(2*guess))
    print(guess)
print('Square root of ' + str(y) + ' is about ' + str(guess))

Hasilnya :

Jawaban L3 Problem 9 - Intro to Computer Science & Programming Using Python - edx.org



Jawaban raw Hisoka kyk gini :


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import random

low = 0;
high = 100;
ans = 0;

print("Haay.. I'm Hyosoka's awesome guess number");
print("So...Please Choose number between 0 and 100....Then I'm gonna guess your secret number... :D");
print("Done...? y or n");
usr=raw_input();
while(usr == "n"):
    print("Please Choose number between 0 and 100....");
    print("Done...? y or n");
    usr=raw_input();

ans = random.randint(0,100);    
print("Is your secret number = " + str(ans));
print("");
guess = raw_input("Enter 'h' to indicate the guess is too high. Enter 'l' to indicate the guess is too low. Enter 'c' to indicate I guessed correctly");
while (guess != "c"):    
    if( guess == "h"):
        high=ans;
        ans = (low+high)/2;
    elif(guess == "l"):
        low = ans;
        "high = 100;"
        ans = (low+high)/2;
    else:
        while((guess != "h") or (guess != "l")):            
            print("Sorry... I don't understand your input");
            guess = raw_input("Please just enter 'h' to indicate the guess is too high. Enter 'l' to indicate the guess is too low ");
            if(guess == "h" or guess == "l"): break;
    print("Is your secret number = " + str(ans));
    guess = raw_input("Enter 'h' to indicate the guess is too high. Enter 'l' to indicate the guess is too low. Enter 'c' to indicate I guessed correctly");

print("Yeeei... So your secret number is = "+str(ans) +" heheh... :D");                

Yang itu udah ditest... dan berhasil... nah pas dimasukin ke edx-nya diubah jadi kyk gini :


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
low = 0;
high = 100;
ans = 0;

print "Please think of a number between 0 and 100!"

ans = 50;    
print("Is your secret number = " + str(ans));
print("");
guess = raw_input("Enter 'h' to indicate the guess is too high. Enter 'l' to indicate the guess is too low. Enter 'c' to indicate I guessed correctly");
while (guess != "c"):
    if( guess == "h"):
        high=ans;
        ans = (low+high)/2;
    elif(guess == "l"):
        low = ans;
        "high = 100;"
        ans = (low+high)/2;
    else:
        while(guess != "c" or guess != "h" or guess != "l"):
            print("Sorry... I don't understand your input");
            guess = raw_input("Please just enter 'h' to indicate the guess is too high. Enter 'l' to indicate the guess is too low. Enter 'c' to indicate I guessed correctly");        
            if(guess =="h" or guess =="l"): break;
    print("Is your secret number = " + str(ans));
    guess = raw_input("Enter 'h' to indicate the guess is too high. Enter 'l' to indicate the guess is too low. Enter 'c' to indicate I guessed correctly");

print("Game over. Your secret number was: "+str(ans));    

Cuman dianggap salaah.... :v seharusnya udah bener, soalnya udah ditest di canopy... :v


Mmmm.... tapi udah ngelapor... ehehe... :D
Btw... jawaban dari edx-nya seperti ini :


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
print("Please think of a number between 0 and 100!")

# At the start the highest the number could be is 100 and the lowest is 0.
hi = 100
lo = 0
guessed = False

# Loop until we guess it correctly
while not guessed:
    # Bisection search: guess the midpoint between our current high and low guesses
    guess = (hi + lo)/2
    print("Is your secret number " + str(guess)+ "?")
    user_inp = raw_input("Enter 'h' to indicate the guess is too high. Enter 'l' to indicate the guess is too low. Enter 'c' to indicate I guessed correctly. ")

    if user_inp == 'c':
        # We got it right!
        guessed = True
    elif user_inp == 'h':
        # Guess was too high. So make the current guess the highest possible guess.
        hi = guess
    elif user_inp == 'l':
        # Guess was too low. So make the current guess the lowest possible guess.
        lo = guess
    else:
        print("Sorry, I did not understand your input.")

print('Game over. Your secret number was: ' + str(guess))

Sekian dulu... :)

Bisection Search Using Python

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
x = 123
epsilon = 0.01
numGuesses = 0
low = 0.0
high = x
ans = (high + low)/2.0
while abs(ans**2 - x) >= epsilon:
    print('low = ' + str(low) + ' high = ' + str(high) + ' ans = ' + str(ans))
    numGuesses += 1
    if ans**2 < x:
        low = ans
    else:
        high = ans
    ans = (high + low)/2.0
print('numGuesses = ' + str(numGuesses))
print(str(ans) + ' is close to square root of ' + str(x))

Hasil :

Square Approximation Method using Python

Hasil :
Di sini stepnya yg jadi kunci, terlalu kecil berarti teliti, cuman perhitungannya lebih lama,,, terlalu besar cepet, cuman kurang teliti atau malah g' ada jawabanya didapat dari method approximation ini.. :D