Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
35 views

L5:Python

The document discusses conditional statements in Python including if, if/else, if/elif/else statements for making decisions in code. Various examples are provided to demonstrate how to use these conditional statements to check conditions, compare values, and execute different code blocks depending on stated logic. Nested conditional statements and validating user input with conditionals are also covered.

Uploaded by

Mayur Kinra
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views

L5:Python

The document discusses conditional statements in Python including if, if/else, if/elif/else statements for making decisions in code. Various examples are provided to demonstrate how to use these conditional statements to check conditions, compare values, and execute different code blocks depending on stated logic. Nested conditional statements and validating user input with conditionals are also covered.

Uploaded by

Mayur Kinra
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Computer Programming with Python

09/08/2018 Debajyoti Ghosh, Asst. Prof, BML Munjal University 1


Decisions Making
Conditional/Selection/Branching statements

if Statement(simplest one)

# Find absolute value of a number without using abs() function


x=eval(input("Enter a number : "))
if x<0:
x=-x
print(x)

09/08/2018 Debajyoti Ghosh, Asst. Prof, BML Munjal University 2


Decisions Making
if. . .else (or two-way selection) Statement

marks = int(input("Enter your marks (0-100) : "))


if marks < 40:
print("Fail")
else:
print("Pass")

09/08/2018 Debajyoti Ghosh, Asst. Prof, BML Munjal University 3


What following code segments prints?
n = 13
result = n
if n > 12:
result = result + 12
if n < 5:
result = result + 5
else:
result = result + 2
print(result)
27

09/08/2018 Debajyoti Ghosh, Asst. Prof, BML Munjal University 4


Decisions Making
Input a number and determine whether it is even or odd as well as whether it is positive
or negative, i.e., output should be like Even and Negative,…
x=eval(input("Enter a number : "))
if x%2 == 0:
print("Even & ")
else:
print("Odd & ")
if x >= 0:
print("Positive")
else:
print("Negative")

Can you write the output of the above


09/08/2018
program in a single/same line?
Debajyoti Ghosh, Asst. Prof, BML Munjal University 5
Decisions Making
if. . .elif (if/elif chain or if-elif-else ladder)statement

There is no switch or case statement in Python. Python lacks a switch statement (if
you haven’t, there is no need to worry about it with Python). Developers
commonly use the switch statement in other languages to create menu-based
applications.
The if...elif statement is generally used for the same purpose in Python.
However, the if...elif statement doesn’t provide quite the same functionality as a
switch statement because it doesn’t enforce the use of a single variable for
comparison purposes. As a result, some developers rely on Python’s dictionary
functionality to stand in for the switch statement.

09/08/2018 Debajyoti Ghosh, Asst. Prof, BML Munjal University 6


Decisions Making
Input a number and determine whether it is positive or negative or zero

num = float(input("Enter a number: "))


if num > 0:
print("Positive number")
elif num == 0:
print("Zero")
else:
print("Negative number")

09/08/2018 Debajyoti Ghosh, Asst. Prof, BML Munjal University 7


What following code segments prints?
n = 13
result = n
if n > 12:
result = result + 12
elif n < 5:
result = result + 5
else:
result = result + 2
print(result)

25
09/08/2018 Debajyoti Ghosh, Asst. Prof, BML Munjal University 8
print("1. Red")
Decisions Making
print("2. Orange")
print("3. Yellow")
print("4. Green")
print("5. Blue")
print("6. Purple")
choice = int(input("Select your favorite color: "))
if (choice == 1):
print("You chose Red!")
elif (choice == 2):
print("You chose Orange!")
elif (choice == 3):
print("You chose Yellow!")
elif (choice == 4):
print("You chose Green!")
elif (choice == 5):
print("You chose Blue!")
elif (choice == 6):
print("You chose Purple!")
else:
print("You made an invalid choice!")
09/08/2018 Debajyoti Ghosh, Asst. Prof, BML Munjal University 9
Decisions Making
For x=10, what will be the output of the following code snippet?
x=int(input("Enter value of x : "))
if x < 0:
print('x is negative')
elif x % 2:
print('x is positive and odd')
else:
print('x is even and non-negative')

x is even and non-negative


09/08/2018 Debajyoti Ghosh, Asst. Prof, BML Munjal University 10
Decisions Making
Nested Decision Statements
Input a number and determine whether it is positive or negative or zero

num = float(input("Enter a number: "))


if num >= 0:
if num == 0:
print("Zero")
else:
print("Positive number")
else:
print("Negative number")
09/08/2018 Debajyoti Ghosh, Asst. Prof, BML Munjal University 11
What following code segments prints?
n = 13
result = n
if n > 12:
result = result + 12
if n < 5:
result = result + 5
else:
result = result + 2
else:
print(result)
print(result)

27

09/08/2018 Debajyoti Ghosh, Asst. Prof, BML Munjal University 12


Write a Python program that ask user to provide two numbers (both in between 1-
10), based on the validity of your input numbers, your program either output the
secret number(multiplication of your two input number) or it will display an error
message telling that whether the first number is incorrect or the second number is
incorrect or both the first and the second number are incorrect.
One = int(input("Type a number between 1 and 10: "))
Two = int(input("Type a number between 1 and 10: "))
if (One >= 1) and (One <= 10):
if (Two >= 1) and (Two <= 10):
print("Your secret number is: ", One * Two)
else:
print("Incorrect second value!")
elif (Two >= 1) and (Two <= 10):
print("Incorrect first value!")
else:
09/08/2018 Debajyoti Ghosh, Asst. Prof, BML Munjal University 13
print("Incorrect both first and second value!")

You might also like