LESSON NOTE/GUIDE
|
THE CODE n1 = int(input("Enter
a number")) n2 = int(input("Enter
another number")) largest = max(n1,n2) print(largest) |
EXPLANATION The
first two lines simply get two integers from the user and store them in n1
and n2. On the
third line, the max function is used to find the maximum number between n1
and n2. It’s result is stored in the
variable largest. We then
print largest out in line 4. |
ALTERNATIVE CODE n1 = int(input("Enter a
number")) n2 = int(input("Enter another
number")) print(max(n1,n2)) |
EXPLANATION The
first two lines are the same as in the first code segment. The
third line prints out the result of the max function immediately. This is shorter and doesn’t require a
third variable. |
PROGRAM 2 – BIGGER NUMBER
THE CODE a = int(input("Enter
a number")) b = int(input("Enter
another number")) if a > b:
print(a, "is bigger than", b) else:
print(b, "is bigger than", a) |
EXPLANATION The
first two lines take care of user input. The
third line has the condition a > b which and is read if
a is greater than b When
the condition is true, then the statement(s) that is/are indented under the
if line are executed. If the
condition is false, those statements are skipped. In the
situation where an else line is included, the indented statements under the
else are executed only if the condition in the if above is false. Note: This program fails when the two numbers
are equal. More on this later on. |
PROGRAM 3A – PASS OR FAIL
THE CODE mark = float(input("Enter your mark.")) if mark < 50: print("You
are failing.") else: print("You
are passing.") |
EXPLANATION The
first line deals with the user input.
Notice that this gets a float number from the user. The
condition in the if statement tests if mark is below 50. If it is, the condition is true and the
first print statement is executed. If
the mark is above 50, then the condition is false and the second print
statement is executed. |
PROGRAM 3B – PASS BY OR FAIL BY
THE CODE mark = float(input("Enter your mark.")) if mark < 50: failBy = 50 - mark print("Failing
by", failBy) else: passBy = mark - 50 print("Passing
by", passBy) |
EXPLANATION The
first line deals with the user input. When
the mark is under 50, the two indented statements under the if line are executed. The first of these statements calculates
the amount by which the user is failing and the other outputs a message to
the screen. The indented statements
under the else are skipped. When
the mark is 50 or over, the two lines under the if line are skipped and the
bottom two lines are executed. These
lines calculate the amount by which the user is passing and that is
outputted to screen. |
PROGRAM 4A – RANDOM NUMBER
THE CODE import random num = random.randint(1,10) print(num) |
EXPLANATION The
first line tells Python that this program will be using functions
(commands) that are located inside the library called random. The
second line uses the randit function (which is
short for random integer) to generate a random integer between 1 and
10. That number gets stored in the
variable num. The
third line simply outputs that value. Try
running this program over many times to verify that the value is in fact
random. |
PROGRAM 5 – COIN FLIP
THE CODE import random rn = random.randint(1,2) if rn == 1:
print("heads") else:
print("tails") |
EXPLANATION The
first line tells Python that this program will be using functions
(commands) that are located inside the library called random. The
second line generates a random number between 1 and 2 and stores it in the
variable rn. The if
statement checks if rn is 1, and if it is, it
outputs “heads”. Otherwise, it
outputs “tails”. |
PROGRAM 6 – RANDOM GREETING
THE CODE import random name = input("Enter your name") rn = random.randint(1,4) if rn == 1:
print("Hello", name) if rn == 2: print("Oh
hi", name) if rn == 3: print(name,
"! Go away!") if rn == 4: print("What's
up fool?") |
EXPLANATION The
first line imports the random library. The
second line gets a name from the user and stores it in name. The
third line generates a random number between 1 and 4 and stores it in rn. The
four if statements check which value is stored in rn
and executes the corresponding print statement. It
should be noted that these are four different if structures. Next we will learn how to merge them into
one structure. |
PROGRAM 7 – BIGGER NUMBER II (WITH
EQUALITY)
THE CODE a = int(input("Enter a
number")) b = int(input("Enter another
number")) if a > b: print(a, "is
bigger than", b) elif a == b: print(a,"and",b,"are equal.") else: print(b, "is
bigger than", a) |
EXPLANATION The
first two lines get the user input. The
first condition in the if checks if a is greater than b. If it is, then the first print statement
is executed and the rest of the if structure is skipped. If the
first condition is false, then the second condition, if a is equal to b, is
checked. If that is true, then the
second print statement is executed and the rest of the if statement is
skipped. If the
second condition is false, then we reach the else and the final print
statement is executed. It is
important to note that only one statement block is ever executed in an if
structure. |
PROGRAM 8 – LEGAL VALUE?
THE CODE (USING or) m = int(input("Enter a number
(1 to 25)")) if m < 1 or m >
25:
print("Illegal number") else: print("Legal
number") |
EXPLANATION The
first line of code gets the user input. The if
statement has two conditions merged together with an or operator. For the or
operator, as long as one of the two conditions is true, the overall
condition will be true. So, if
m is less than 1 or if m is greater than 25, the overall condition will be
true and the program will output “Illegal number”. If the
overall condition is false, then the print statement in the else section
will be executed. |
THE CODE (USING and) m = int(input("Enter a number (1 to 25)")) if m >= 1 and m
<= 25: print("Legal
number") else:
print("Illegal number") |
EXPLANATION The
first line of code gets the user input. The if
statement uses an and operator to merge two conditions together. For and operators, both conditions need
to be true for the overall condition to be true. So, if
m is greater or equal to 1 and m is lesser or equal to 25, the overall
condition will be true and “Legal number” will be outputted. Otherwise,
the “Illegal number” will be outputted. |