If statements are the backbone of programming. Fortunately they are fairly simple to understand. Watch indentations for If statements.
A simple if statement:
myinteger1=30
myinteger2=40
if myinteger2 > myinteger1:
print('myinteger2 is larger than myinteger1')
#Output
# myinteger2 is larger than myinteger1
myinteger1 = input("Input myinteger1")
myinteger2 = input("Input myinteger2")
if myinteger2 > myinteger1:
print('myinteger2 is larger than myinteger1')
else:
print('myinteger1 is larger than myinteger2')
#Output
# Input myinteger1 8
# Input myinteger2 9
# myinteger2 is larger than myinteger1
An if – elif – else statement. Note that there can be multiple elif.
myinteger1 = input("Input myinteger1")
myinteger2 = input("Input myinteger2")
if myinteger2 > myinteger1:
print('myinteger2 is larger than myinteger1')
elif myinteger1 == myinteger2:
print('myinteger2 equals myinteger1')
else:
print('myinteger1 is larger than myinteger2')
#Output
# Input myinteger1 4
# Input myinteger2 9
# myinteger2 is larger than myinteger1
If statements have the following Condition Operators:
Equals a == b
Not Equals a != b
Less Than a < b
Less Than or Equal To a <= b
Greater Than a > b
Greater Than or Equal To a >= b
Note that == is a comparison operator, where = is an assignment operator in Python.
and returns true if both statements are true
or returns true if one of the statements is true
not reverse results, returns false if the result is true
The following examples use the logical operators.
myint1=30
myint2=40
print(myint2 > myint1) #Result is True
print(myint2 > 3 and myint1 > 5) #Result is True
print(myint2 > 3 and myint1>120) #Result is False
#Output
# True
# True
# False
Table of Contents
Ch1-Starting Out
Ch2-Loops
Ch3-If Statements
Ch4-Functions
Ch5-Variable Scope
Ch6-Bubble Sort
Ch7-Intro to OOP
Ch8-Inheritance
Ch9-Plotting
Ch10-Files
Ch11-Print Format
Ch12-Dict-Zip-Comp
Ch13-Slice Arrays