1. Script Python menghitung tanggal dan waktu :
from datetime import datetime
now = datetime.now()
mm = str(now.month)
dd = str(now.day)
yyyy = str(now.year)
hour = str(now.hour)
mi = str(now.minute)
ss = str(now.second)
print (mm + "/" + dd + "/" + yyyy + " " + hour + ":" + mi + ":" + ss)
2. Script Python menghitung 2 angka yang di inputkan :
number1 = input("Angka Pertama: ")
number2 = input("\nANgka Kedua: ")
sum = float(number1) + float(number2)
print("Penjumlahan dari {0} dan {1} adalah {2}" .format(number1, number2, sum))
3. Script Python untuk menampilkan nama :
# Python program showing
# a use of raw_input()
g = raw_input("Enter your name : ")
print g
4. Script Python untuk kalkulator :
# Program make a simple calculator that can add, subtract, multiply and divide using functions
# This function adds two numbers
def add(x, y):
return x + y
# This function subtracts two numbers
def subtract(x, y):
return x - y
# This function multiplies two numbers
def multiply(x, y):
return x * y
# This function divides two numbers
def divide(x, y):
return x / y
print("Select operation.")
print("1.Add")
print("2.Subtract")
print("3.Multiply")
print("4.Divide")
# Take input from the user
choice = input("Enter choice(1/2/3/4):")
num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))
if choice == '1':
print(num1,"+",num2,"=", add(num1,num2))
elif choice == '2':
print(num1,"-",num2,"=", subtract(num1,num2))
elif choice == '3':
print(num1,"*",num2,"=", multiply(num1,num2))
elif choice == '4':
print(num1,"/",num2,"=", divide(num1,num2))
else:
print("Invalid input")
5. Script Python untuk bilangan Fibonacci :
def Fibonacci(n):
if n<0:
print("Incorrect input")
# First Fibonacci number is 0
elif n==1:
return 0
# Second Fibonacci number is 1
elif n==2:
return 1
else:
return Fibonacci(n-1)+Fibonacci(n-2)
# Driver Program
print(Fibonacci(10))
6. Script Python untuk sorting array :
def insertionSort(arr):
# Traverse through 1 to len(arr)
for i in range(1, len(arr)):
key = arr[i]
# Move elements of arr[0..i-1], that are
# greater than key, to one position ahead
# of their current position
j = i-1
while j >=0 and key < arr[j] :
arr[j+1] = arr[j]
j -= 1
arr[j+1] = key
# Driver code to test above
arr = [12, 11, 13, 8, 4]
insertionSort(arr)
print ("Sorted array is:")
for i in range(len(arr)):
print ("%d" %arr[i])
7. Script Python untuk sorting array :
test_string = "Hello World My Name Is : Joo"
# printing original string
print ("The original string is : " + test_string)
# using split()
# to count words in string
res = len(test_string.split())
# printing result
print ("The number of words in string are : " + str(res))
Sekian tutorial dan artikel mengenai Kumpulan script python sederhana yang bisa kamu coba, bagi kamu yang sedang belajar bahasa pemrograman Python. Happy Coding && Hello World.
إرسال تعليق