Added if statement code

main
Edward Bigos 2025-03-18 10:20:16 -04:00
parent c464c77b9d
commit a9b392d3db
8 changed files with 160 additions and 0 deletions

View File

@ -4,4 +4,6 @@
### This is a third header
## This is a new header level 2
Temp repository

32
if/if01.py Normal file
View File

@ -0,0 +1,32 @@
program01 = '''
Program: if01
This program demonstrates the conditional tests used in Python.
'''
print(program01)
a = 4
b = 7
c = 7
print(f"a = {a}\nb = {b}\nc = {c}\n")
print(f"a == b {a==b}")
print(f"a != b {a!=b}")
print(f"a < b {a<b}")
print(f"a <= b {a<=b}")
print(f"a > b {a>b}")
print(f"a >= b {a>=b}")
print("\n")
print(f"c == b {c==b}")
print(f"c != b {c!=b}")
print(f"c < b {c<b}")
print(f"c <= b {c<=b}")
print(f"c > b {c>b}")
print(f"c >= b {c>=b}")

39
if/if02.py Normal file
View File

@ -0,0 +1,39 @@
# if statement example
if 22 > 7:
print("22 is greater than 7")
print("A better method to define the code ...")
# Use variables in the if statement and print
data1 = 22
if data1 > 7:
print(f"{data1} is greater than 7")
# Or this form
print("And another method to define the code ...")
data1 = 22
data2 = 7
if data1 > data2:
print(f"{data1} is greater than {data2}")
data1 = 6
data2 = 7
if data1 > data2:
print(f"{data1} is greater than {data2}")
else:
print(f"{data1} is less than or equal {data2}")
print("Program ended")

16
if/if03.py Normal file
View File

@ -0,0 +1,16 @@
# if..else statement example
x = 7
print(f"{x} is equal to 7?")
if x == 7:
print("Yes")
else:
print("No")
# if..else statement example
x = 5
print(f"{x} is equal to 7?")
if x == 7:
print("Yes")
else:
print("No")

23
if/if04.py Normal file
View File

@ -0,0 +1,23 @@
#!/usr/bin/python3
# Program: if02
# Lists of numbers. These could be integers or floats.
intList = [7, 2, 16, 18, 12, 21]
# Print the list and the length
print(intList," length = ",len(intList))
print(f"Print each value in the list")
counter = 0
for item in intList:
print(f"intList[{counter}] = {item:2d}")
counter += 1
cutoff = 13
print(f"Print each value in the list >= {cutoff}")
counter = 0
for item in intList:
if(item >= cutoff):
print(f"intList[{counter}] = {item:2d}")
counter += 1

25
if/if05.py Normal file
View File

@ -0,0 +1,25 @@
#!/usr/bin/python3
# Program: if03
# Lists of numbers. These could be integers or floats.
intList = [7, 2, 16, 18, 12, 21]
# Print the list and the length
print(intList," length = ",len(intList))
print(f"Print each value in the list")
counter = 0
for item in intList:
print(f"intList[{counter}] = {item:2d}")
counter += 1
cutoff = 13
print(f"Print each value in the list >= {cutoff}")
counter = 0
for item in intList:
if(item >= cutoff):
print(f"intList[{counter}] = {item:2d}")
else:
print(f"========> intList[{counter}] = {item:2d} is less than {cutoff}")
counter += 1

7
if/if06.py Normal file
View File

@ -0,0 +1,7 @@
# consider the following line from the autoloan program:
principal = float(input("Enter a value for the principal: "))
print(f"You entered the value {principal}")

16
if/if07.py Normal file
View File

@ -0,0 +1,16 @@
# consider the following line from the autoloan program:
principal = float(input("Enter a value for the principal: "))
if(principal < 5000.) or (principal >120000.00):
print(f"The loan value {principal} is outside the allowed limits.")
exit(-1)
rate = float(input("Enter a value for the interest rate (7 for 7%): "))
numberOfYears = float(input("Enter the number of years for the loan: "))
print(f"You entered the principal value {principal}")
print(f"You entered the rate value {rate}%")
print(f"You entered the loan term value {numberOfYears} years")