Skip to content

added two basic python programs. #11

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions beginner_python/11-simple calculator
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Program make a simple calculator

# 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 = float(input("Enter first number: "))
num2 = float(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")
31 changes: 31 additions & 0 deletions beginner_python/12 unit convertor
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import math
import time
"""Unit Converter"""
#Welcome and variable setting
print ("Welcome to Sam's Unit Converter")
cat = input("Which category would you like to convert? we support length(l) and Weight(w): ")
if cat == ("l"):
unit1 = input ("Which unit would you like to convert from: ")
unit2 = input ("Which unit would you like to convert to: ")
num1 = input ("Enter your value: " )

##Calculations

if unit1 == "cm" and unit2 == "m":
ans = float(num1)/100
elif unit1 == "mm" and unit2 == "cm":
ans = float(num1)/10
elif unit1 == "m" and unit2 == "cm":
ans = float(num1)*100
elif unit1 == "cm" and unit2 == "mm":
ans = float(num1)*10
elif unit1 == "mm" and unit2 == "m":
ans = float(num1)/1000
elif unit1 == "m" and unit2 == "mm":
ans = float(num1)*1000
elif unit1 == "km" and unit2 == "m":
ans = float(num1)*1000
elif unit1 == "m" and unit2 == "km":
ans = float(num1)/1000
elif unit1 == "mm" and unit2 == "km":
ans = float(num1)/1000000
43 changes: 43 additions & 0 deletions simple calculator
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Program make a simple calculator

# 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 = float(input("Enter first number: "))
num2 = float(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")