sun = 'down'
if sun == 'down': print('Good Night!')
print('I am here')
Good Night!
I am here
sun = 'up'
if sun == 'down': print('Good Night!')
print('I am here')
I am here
total = 100
sales_tax_rate = 0.065
taxable = True
if taxable:
print(f"Subtotal : ${total:.2f}")
sales_tax = total * sales_tax_rate
print(f"Sales Tax: ${sales_tax:.2f}")
total = total + sales_tax
print(f"Total : ${total:.2f}")
Subtotal : $100.00
Sales Tax: $6.50
Total : $106.50
import datetime as dt
# Get the current date and time
now = dt.datetime.now()
# Make a decision based on hour
if now.hour < 12 :
print('Good Morning')
else:
print('Good afternoon')
print('I hope you are doing well!')
Good afternoon
I hope you are doing well!
light_color = "green"
if light_color == "green":
print("Go")
elif light_color == "red":
print("Stop")
print("This code executes no matter what")
Go
This code executes no matter what
age = 31
if age < 21:
# If under 21, no alcohol
beverage = "milk"
elif age >= 21 and age < 80:
# Ages 21 - 79, suggest beer
beverage = "beer"
else:
# If 80 or older, prune juice might be a good choice.
beverage = "prune juice"
print("Have a " + beverage)
Have a beer
for x in range(7):
print(x)
print("All done")
0
1
2
3
4
5
6
All done
for x in "Eslam":
print(x)
print("Done")
E
s
l
a
m
Done
for x in ["The", "rain", "in", "Spain"]:
print(x)
print("Done")
The
rain
in
Spain
Done
answers = ["A", "C", "", "D"]
for answer in answers:
if answer == "":
print("Incomplete")
break
print(answer)
print("Loop is done")
A
C
Incomplete
Loop is done
answers = ["A", "C", "", "D"]
for answer in answers:
if answer == "":
print("Incomplete")
continue
print(answer)
print("Loop is done")
A
C
Incomplete
D
Loop is done
counter= 65
while counter < 91:
print(str(counter) + "=" + chr(counter))
counter +=1
print("all done")
65=A
66=B
67=C
68=D
69=E
70=F
71=G
72=H
73=I
74=J
75=K
76=L
77=M
78=N
79=O
80=P
81=Q
82=R
83=S
84=T
85=U
86=V
87=W
88=X
89=Y
90=Z
all done
import random
print("Numbers that aren't evenly divisible by 5")
counter = 0
while counter < 10:
# Get a random number
number = random.randint(1,999)
if int(number / 5) == number / 5:
# If it's evenly divisible by 5, bail out.
continue
# Otherwise, print it and keep going for a while.
print(number)
# Increment the loop counter.
counter += 1
print("Loop is done")
Numbers that aren't evenly divisible by 5
687
878
863
227
106
396
441
688
728
821
Loop is done
import random
print("Numbers that aren't evenly divisible by 5")
counter = 0
while counter < 10:
# Get a random number
number = random.randint(1,999)
if int(number / 5) == number / 5:
# If it's evenly divisible by 5, bail out.
break
# Otherwise, print it and keep going for a while.
print(number)
# Increment the loop counter.
counter += 1
print("Loop is done")
Numbers that aren't evenly divisible by 5
78
83
Loop is done