1/22
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced |
---|
No study sessions yet.
If statement
The if statement will evaluate whether a statement is true or false, and run code only in the case that the statement is true.
grade = 70
if grade >= 65: print (‘Passing Grade’)
Passing Grade
grade = 60
if grade >= 65: print (‘Passing Grade’)
ERROR
balance = -5
if balance < 0: print("Balance is below 0, add funds now or you will be charged a penalty.")
Balance is below 0, add funds now or you will be charged a penalty.
grade = 70
if grade < 60: print("\n Failing")
if grade < 70: print("\n Average")
if grade < 80: print("\n Good")
if grade < 90: print("\n Very Good")
if grade <= 100: print("\n Excellent ")
Good
Very Good
Excellent
Else Statement
If you would like the program to do something even when an if statement evaluates to false then use the else statement
grade = 60
if grade >= 65: print("Passing grade")
else: print("Failing grade")
Failing Grade
Else if statement ELIF
-If you would like for the program to evaluates more than two possible outcomes
-The elif or else if statement looks like the if statement and will evaluate another condition.
-The elif statement will be placed between the if statement and the else statement.S
balance = 50
if balance < 0:
print("Balance is below 0, add funds now or you will be charged a penalty.")
elif balance == 0:
print("Balance is equal to 0, add funds soon.")
else:
print("Your balance is above 0.")
Your balance is 0 or above.
balance = 0
if balance < 0:
print("Balance is below 0, add funds now or you will be charged a penalty.")
elif balance == 0:
print("Balance is equal to 0, add funds soon.")
else:
print("Your balance is above 0.")
Balance is equal to 0, add funds soon
balance = -5
if balance < 0:
print("Balance is below 0, add funds now or you will be charged a penalty.")
elif balance == 0:
print("Balance is equal to 0, add funds soon.")
else:
print("Your balance is above 0.")
Balance is below 0, add funds now or you will be charged a penalty
In the grade.py program, there are a few letter grades corresponding to ranges of numerical grades:
90 or above is equivalent to an A grade
80-89 is equivalent to a B grade
70-79 is equivalent to a C grade
65-69 is equivalent to a D grade
64 or below is equivalent to an F grade
Write a code to implement the above requirement
grade = int( input("Enter Your Grade ") )
if grade >= 90: print("A grade")
elif grade >=80: print("B grade")
elif grade >=70: print("C grade")
elif grade >= 65: print("D grade")
else: print("Failing grade")
Sample Run
Enter Your Grade 70
C grade
>>>
grade = int( input("Enter Your Grade ") )
if grade >= 90: print("A grade")
elif grade >=80: print("B grade")
elif grade >=70: print("C grade")
elif grade >= 65: print("D grade")
else: print("Failing grade")
Sample Run
Enter Your Grade -10
…
Failing grade
grade = int( input("Enter Your Grade ") )
if grade >= 90: print("A grade")
elif grade >=80: print("B grade")
elif grade >=70: print("C grade")
elif grade >= 65: print("D grade")
else: print("Failing grade")
Sample Run
Enter Your Grade A
ERROR
grade = int( input("Enter Your Grade ") )
if grade <= 90: print("A grade")
elif grade <=80: print("B grade")
elif grade <=70: print("C grade")
elif grade <= 65: print("D grade")
else: print("Failing grade")
Sample Run
Enter Your Grade 95
…
Enter Your Grade 65
…
Enter Your Grade -10
…
Failing Grade
A Grade
A grade
my_int = int(input("Enter a number between 1 and 50 --- > "))
list = range(0 , 50)
result = my_int in list
if result == 1:
print( my_int , " is in the list\n")
else:
print( my_int , " is not in the list\n")
Sample run
Enter a number between 1 and 50 --- > 40
…
Enter a number between 1 and 50 --- > 49
…
Enter a number between 1 and 50 --- > 50
…
Enter a number between 1 and 50 --- > 0
…
Enter a number between 1 and 50 --- > A
…
40 is in the list
49 is in the list
50 is not in the list
0 is in the list
ERROR
my_int = int(input("Enter a number between 1 and 50 --- > "))
list = range(0 , 50 , 2) # Only Even Numbers
result = my_int in list
if result == 1:
print( my_int , " is in the list\n")
else:
print( my_int , " is not in the list\n")
SAMPLE RUN
Enter a number between 1 and 50 --- > 0
…
Enter a number between 1 and 50 --- > 50
…
Enter a number between 1 and 50 --- > 48
…
Enter a number between 1 and 50 --- > 19
…
0 is in the list
50 is not in the list
48 is in the list
19 is not in the list
Nested If Statements
used for situations if you would like to check for a secondary condition if the first condition executes as true.
Thus , you can have an if-else statement inside of another if else statement
if statement1:
print("true")
if nested_statement:
print("yes")
else:
print("no")
else:
print("false")
-If statement1 evaluates to true, the program will then evaluate whether the nested_statement also evaluates to true. If both cases are true, the output will be:
-If, however, statement1 evaluates to true, but nested_statement evaluates to false, then the output will be:
-And if statement1 evaluates to false, the nested if-else statement will not run, so the else statement will run alone, and the output will be:
true
yes
true
no
false
s=input("Enter any character: ")
if s.isalnum():
print("Alpha Numeric Character")
if s.isalpha():
print("Alphabet character")
if s.islower():
print("Lower case alphabet character")
else: print("Upper case alphabet character")
else: print("it is a digit")
elif s.isspace():
print("It is space character")
SAMPLE RUN
A, 2, ,a
Enter any character: A
Alpha Numeric Character
Alphabet character
Upper case alphabet character
>>>
Enter any character: 2
Alpha Numeric Character
it is a digit
>>
Enter any character:
It is space character
>>>
Enter any character: a
Alpha Numeric Character
Alphabet character
Lower case alphabet character
>>>
credits = float(input('How many units of credit do you have? '))
GPA = float(input('What is your GPA? '))
if credits >= 120 and GPA >=2.0:
print('You are eligible to graduate!')
else:
print('You are not eligible to graduate.')
SAMPLE RUN
How many units of credit do you have? 135
What is your GPA? 1.25
….
How many units of credit do you have? 125
What is your GPA? 3.5
….
You are not eligible to graduate
You are eligible to graduate
isSunday = True
isMonday = True
isHoliday = False
if isHoliday and isSunday:
print('\nSunday is a Funday!!')
else:
print('\nNot holiday !! Please start working :(')
if isHoliday or isSunday:
print('\nSunday is a Funday!!')
else:
print('\nNot holiday !! Please start working :(')
isHoliday = True
if isHoliday and isSunday:
print('\nSunday is a Funday!!')
else:
print('\nNot holiday !! Please start working :(')
isHoliday = False
if isHoliday and isSunday and isMonday:
print('\nSunday is a Funday!!')
else:
print('\nNot holiday !! Please start working :(')
if isHoliday or isSunday or isMonday:
print('\nSunday is a Funday!!')
else:
print('\nNot holiday !! Please start working :(')
Not holiday !! Please start working :(
Sunday is a Funday!!
Sunday is a Funday!!
Not holiday !! Please start working :(
Sunday is a Funday!!
num = 3
if num > 0:
print(num, "is a positive number.")
print("This is always printed.")
num = -1
if num > 0:
print(num, "is a positive number.")
print("This is also always printed.")
Is a positive number
This is always printed
This is also always printed