Chapter 8 - Conditional Statements

0.0(0)
studied byStudied by 0 people
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Card Sorting

1/22

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

23 Terms

1
New cards

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.

<p>The if statement will evaluate whether a statement is true or false, and run code only in the case that the statement is true.</p>
2
New cards

grade = 70

if grade >= 65: print (‘Passing Grade’)

Passing Grade

3
New cards

grade = 60

if grade >= 65: print (‘Passing Grade’)

ERROR

4
New cards

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.

5
New cards

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

6
New cards

Else Statement

If you would like the program to do something even when an if statement evaluates to false then use the else statement

<p>If you would like the program to do something even when an if statement evaluates to false then use the else statement</p>
7
New cards

grade = 60

if grade >= 65: print("Passing grade")

else: print("Failing grade")

Failing Grade

8
New cards

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

<p>-If you would like for the program to evaluates more than two possible outcomes</p><p>-The elif or else if statement looks like the if statement and will evaluate another condition.</p><p>-The elif statement will be placed between the if statement and the else statement.S</p>
9
New cards

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.

10
New cards

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

11
New cards

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

12
New cards

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

>>>

13
New cards

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

14
New cards

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

15
New cards

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

16
New cards

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

17
New cards

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

18
New cards

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

19
New cards

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

20
New cards

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

>>>

21
New cards

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

22
New cards

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!!

23
New cards

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