Building Simple Projects in Python
In my quest to deepen my grasp of Python fundamentals, I embarked on a journey of creation, delving into the world of simple projects.
Project 1: A Simple Calculator
I chose a calculator as my first project because it’s straightforward and covers basic Python concepts like functions, user input, and conditional statements.
Step by Step
First, declare variables
# Program Kalkulator Sederhana
print("Kalkulator Sederhana")
angka_pertama = int(input("Input angka pertama : "))
angka_kedua = int(input("Input angka kedua : "))
a = angka_pertama
b = angka_kedua
Then write the function and output display
def penjumlahan(a, b):
return a + b
def pengurangan(a, b):
return a - b
def perkalian(a, b):
return a * b
def pembagian(a, b):
if a != 0 and b != 0:
return round(a / b, 4)
else:
return "Bilangan tidak bisa dibagi 0"
print("")
print(f"Hasil Penjumlahan : {penjumlahan(a, b)}")
print(f"Hasil Pengurangan : {pengurangan(a, b)}")
print(f"Hasil Perkalian : {perkalian(a, b)}")
print(f"Hasil Pembagian : {pembagian(a, b)}")
After that run the program in your terminal or IDE.
Project 2: Biodata
A simple program that emphasizes user input and then displays it again to the user.
Step by Step
First, declare variables for input data
nama = input("Masukkan nama Anda : ")
pekerjaan = input("Masukkan pekerjaan Anda : ")
hobi = input("Masukkan hobi Anda : ")
Then write the output display
print("\nBerikut detail informasi Anda")
print("Nama :", nama)
print("Pekerjaan :", pekerjaan)
print("Hobi :", hobi)
After that run the program in your terminal or IDE.
Project 3: Graduation Status
If the minimum grade is met, then it will be passed
Step by Step
First, declare variable
print("Cek Kelulusan")
nilai = int(input("Masukkan nilai Anda : "))
Then make some condition, here are the rules:
- If the score is over 80, the message “Selamat Anda Lulus!” will appear, which means you passed
- If the score is below 80, you failed
if nilai > 80:
print("\nSelamat Anda Lulus!")
else:
print("\nMaaf Anda Belum Lulus. Silahkan Coba Lagi di Lain Kesempatan")
After that Run the program in your terminal or IDE.
Project 4: Flat Shapes Calculator
This program calculates the area and perimeter of flat shapes based on the user’s choice of flat shapes.
Step by Step
First, create function, here I created 3 functions to calculate the area and perimeter of a square, rectangle, and isosceles triangle.
def LuasPersegi():
print("\nHitung Luas dan Keliling Persegi")
a = int(input("Input Nilai Sisi : "))
luas_persegi = a * a
keliling_persegi = a * 4
print("\nLuas Persegi :", luas_persegi)
print("Keliling Persegi :", keliling_persegi)
def LuasPersegiPanjang():
print("\nHitung")
a = int(input("Input Panjang : "))
b = int(input("Input Lebar : "))
luas_persegi_panjang = a * b
keliling_persegi_panjang = (2 * a) + (2 * b)
print("\nLuas Persegi Panjang :", luas_persegi_panjang)
print("Keliling Persegi Panjang :", keliling_persegi_panjang)
def LuasSegitiga():
a = int(input("\nInput Alas : "))
t = int(input("Input Tinggi : "))
k = int(input("Input Panjang Kaki : "))
luas_segitiga = 1/2 * a * t
keliling_segitiga = a + (k * 2)
print("\nLuas Segitiga :", luas_segitiga)
print("Keliling Segitiga :", keliling_segitiga)
Then create user input. The running flat calculation will be according to the user’s choice:
- If the user enters 1, then the square calculation will begin
- If the user enters 2, then the rectangle calculation will begin
- If the user enters 3, then the isosceles triangle calculation will begin.
- If the user enters a number other than 1/2/3, the program will display the message “Input Invalid” and stop.
print("Hitung Luas dan Keliling Bangun Datar")
print("1. Persegi")
print("2. Persegi Panjang")
print("3. Segitiga Sama Sisi")
pilihan = int(input("Pilih Bangun Datar 1/2/3:"))
if pilihan == 1:
LuasPersegi()
elif pilihan == 2:
LuasPersegiPanjang()
elif pilihan == 3:
LuasSegitiga()
else:
print("\nInput Invalid")
After that run the program in your terminal or IDE.
User enters 1, then the square calculation will begin:
User enters 2, then the rectangle calculation will begin:
User enters 3, then the isosceles triangle calculation will begin:
User enters a number other than 1/2/3, the program will display the message “Input Invalid” and stop.
Project 5: Calculate Total Payment
The last program calculates the total payment after the 25% discount.
Step by Step
First, create variables to store the price of each item.
nasi = 5000
sate_ayam = 15000
soto_kambing = 2000
es_jeruk = 3000
Then, calculate and display the total pay before and after discount
total_tagihan = nasi + sate_ayam + soto_kambing + es_jeruk
diskon = 25/100
total_bayar = total_tagihan * diskon
print("Total Pembelian Sebelum Diskon :", total_tagihan)
print("Total Tagihan Setelah Diskon :", total_bayar)
After that run the program in your terminal or IDE.
Thank you for staying with me in this long tutorial.
I hope you liked it and learned something new. If you have any comments or need more details don’t hesitate to type your thoughts.
Originally published at https://abdiel.hashnode.dev/building-simple-projects-in-python on May 15, 2024.