# Sample Python Program 11 – Simple Math with Variables
john=3
mary=5
adam=6
print(john,mary,adam,sep=”, “)
total_apples=john+mary+adam
print (“Total Apples; “, total_apples)
# Sample Python Program 12 – Simple DO WHILE
sheep=1
while sheep<=20:
print (sheep)
sheep = sheep + 1
# END
# Sample Python Program 13 – Shortcut Operators
- print (i = i + 2 * j ⇒ i += 2 * j)
- print (var = var / 2 ⇒ var /= 2)
- print (rem = rem % 10 ⇒ rem %= 10)
- print (j = j – (i + var + rem) ⇒ j -= (i + var + rem))
- print (x = x ** 2 ⇒ x **= 2)
# Sample Python Program 14 – Input Data from keyboard
print(“Tell me anything…”)
anything = input()
print(“Hmm…”, anything, “… Really?”)
or
anything = input(“Tell me anything…”)
print(“Hmm…”, anything, “…Really?”)
THIS IS A TEXT STRING
anything = input("Enter a number: ")
something = anything ** 2.0
print
(anything, "to the power of 2 is", something)
MUST BE CODED LIKE THIS
anything = input("Enter a number: ")
something = anything ** 2.0
print
(anything, "to the power of 2 is", something)
OR
anything = float(input(“Enter a number: “))
# Sample Python Program 15 – Input of Strings
fnam = input(“May I have your first name, please? “)
lnam = input(“May I have your last name, please? “)
print(“Thank you.”)
print(“\nYour name is ” + fnam + ” ” + lnam + “.”)
# Sample Python Program 16 – Repetition of a String
print (string * number)
# Sample Python Program 17 – Order of Operations
priority table
Priority | Operator | |
---|---|---|
1 | + , - |
unary |
2 | ** |
|
3 | * , / , // , % |
|
4 | + , - |
binary |
5 | < , <= , > , >= |
|
6 | == , != |
# Sample Python Program 18 – IF / ELSE / ELIF
if x=0:
do this
else: # This is done with a single possible outcome
do this
elif: # This replaces the Select Case conditional
do this or several other Elifs – Exits after first positive
else:
Shortcut
If x=0: do this
Else: do this instead
# Sample Python Program 19 – Example Code
x = input(“Enter dog’s name: “)
if x == “Jasmine”:dog_name=”Horray for Jasmine”
elif x == “jasmine”:dog_name=”I want an upper case Jasmine.”
else: x=”This is not Jasmine.” # ALL OTHER CONDITIONS
print(dog_name)
# Sample Python Program 20 – Thaler Tax Program#
cap = 85528
target_tax=14839.02
currency = “Thalers”
income = float(input(“Enter income: “))
if income >= cap:
print(“Tax is due @ target_tax+32% of excess”)
diff=(income-cap)
perc=(diff*.32)
print(perc)
tax=(target_tax + perc)
else:
print(“Tax is due at 18% – 556.02”)
perc=(income*.18)
tax=(perc-556.02)
tax = round(tax, 0)
if tax<0: tax=0
print(“The tax is:”, tax, currency)