8000 Add files via upload · lee7py/Python-Programming@cb55c32 · GitHub
[go: up one dir, main page]

Skip to content

Commit cb55c32

Browse files
authored
Add files via upload
1 parent 6869e7c commit cb55c32

39 files changed

+547
-0
lines changed

Ch03/3-1-3.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
list1 = [6, 4, -5, 3.5]
2+
list1.sort()
3+
print(list1)
4+
list2 = ["ha", "hi", "B", "7"]
5+
list2.sort()
6+
print(list2)

Ch03/3-1-4.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
list1 = [chr(177), "cat", "car", "Dog", "dog", "8-ball", "5" + chr(162)]
2+
list1.sort()
3+
print(list1)

Ch03/3-1-5.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
monarchs = [("George", 5), ("Elizabeth", 2), ("George", 6), ("Elizabeth", 1)]
2+
monarchs.sort()
3+
print(monarchs)

Ch03/3-2-1.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
## Determine the larger of two numbers.
2+
# Obtain the two numbers from the user.
3+
num1 = eval(input("Enter the first number: "))
4+
num2 = eval(input("Enter the second number: "))
5+
# Determine and display the larger value.
6+
if num1 > num2:
7+
largestValue = num1 # execute this statement if the condition is true
8+
else:
9+
largestValue = num2 # execute this statement if the condition is false
10+
print("The larger value is", str(largestValue) + ".")

Ch03/3-2-10.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
## Illustrate Boolean values.
2+
if 7:
3+
print("A nonzero number is true.")
4+
else:
5+
print("The number zero is false.")
6+
7+
if []:
8+
print("A nonempty list is true.")
9+
else:
10+
print("An empty list is false.")
11+
12+
if ["spam"]:
13+
print("A nonempty list is true.")
14+
else:
15+
print("The empty list is false.")

Ch03/3-2-2.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
## A quiz.
2+
# Obtain answer to question.
3+
answer = eval(input("How many gallons does a ten-gallon hat hold? "))
4+
# Evaluate answer.
5+
if 0.5 <= answer <= 1:
6+
print("Good, ", end="")
7+
else:
8+
print("No, ", end="")
9+
print("it holds about 3/4 of a gallon.")

Ch03/3-2-3.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
## Find the largest of three numbers.
2+
# Input the three numbers.
3+
firstNumber = eval(input("Enter first number: "))
4+
secondNumber = eval(input("Enter second number: "))
5+
thirdNumber = eval(input("Enter third number: "))
6+
# Determine and display the largest value.
7+
max = firstNumber
8+
if secondNumber > max:
9+
max = secondNumber
10+
if thirdNumber > max:
11+
max = thirdNumber
12+
print("The largest number is", str(max) + ".")

Ch03/3-2-4.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
## Interpret weather beacon.
2+
# Get color and mode.
3+
color = input("Enter the color (BLUE or RED): ")
4+
mode = input("Enter the mode (STEADY or FLASHING): ")
5+
color = color.upper()
6+
mode = mode.upper()
7+
result = ""
8+
# Analyze responses and display weather forecast.
9+
if color == "BLUE":
10+
if mode == "STEADY":
11+
result = "Clear View."
12+
else: # mode is FLASHING
13+
result = "Clouds Due."
14+
else: # color is RED
15+
if mode == "STEADY":
16+
result = "Rain Ahead."
17+
else: # mode is FLASHING
18+
result = "Snow Ahead."
19+
print("The weather forecast is", result)

Ch03/3-2-5.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
## Evaluate profit.
2+
# Obtain input from user.
3+
costs = eval(input("Enter total costs: "))
4+
revenue = eval(input("Enter total revenue: "))
5+
# Determine and display profit or loss.
6+
if costs == revenue:
7+
result = "Break even."
8+
else:
9+
if costs < revenue:
10+
profit = revenue - costs
11+
result = "Profit is ${0:,.2f}.".format(profit)
12+
else:
13+
loss = costs - revenue
14+
result = "Loss is ${0:,.2f}.".format(loss)
15+
print(result)

Ch03/3-2-6.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
## Determine the larger of two numbers.
2+
# Get the two numbers from the user.
3+
num1 = eval(input("Enter the first number: "))
4+
num2 = eval(input("Enter the second number: "))
5+
# Determine and display the larger value.
6+
if num1 > num2:
7+
print("The larger value is", str(num1) + ".")
8+
elif num2 > num1:
9+
print("The larger value is", str(num2) + ".")
10+
else:
11+
print("The two values are equal.")

0 commit comments

Comments
 (0)
0