Conditional Statements Intro
September 16, 2022
1 Decision Making Using Conditional Statements
• Conditional statements are used to take decisions
• There are three conditional statements in Python 1. if 2. else 3. elif
1.1 if statement
Syntax:
if condition: > block of statements
if condition is True block of statements will be executed
[6]: age = int(input("Enter your age: "))#25
if age > 18: #25 > 18
print('Yes you can vote!')
print('Done')
Enter your age: 12
[9]: age = int(input("Enter your age: "))#25
if age < 18: #25 > 18
print('No you cannot vote!')
print('Done')
Enter your age: 25
[12]: age = int(input("Enter your age: "))#25
if age > 18: #-146 > 18
print('Yes you can vote!')
else:
print('No you cannot vote')
Enter your age: -146
No you cannot vote
[15]: age = int(input("Enter your age: "))#45
if age < 18: #45 < 18
print('No you cannot vote!')
else:
print('You can vote!')
1
Enter your age: 12456
You can vote!
[17]: # Find out the largest of two number
a = int(input())
b = int(input())
if a>b:
print(a, 'is the largest')
else:
print(b, 'is the largest')
100
-100
100 is the largest
1.2 Single line input taking and Multiline input taking
[19]: a = int(input())
b = int(input())
c = int(input())
print(a + b + c)
10 20 30
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
Input In [19], in <cell line: 1>()
----> 1 a = int(input())
2 b = int(input())
3 c = int(input())
ValueError: invalid literal for int() with base 10: '10 20 30'
[22]: # reading two or more integers in a single line
a, b, c = map(int, input().split())
print(a + b + c)
10 20 30
60
[24]: # reading two or more integers in a single line
a, b, c, d = map(int, input().split())
print(a + b + c + d)
10 20 30 40
100
2
[25]: # reading two or more integers in a single line
a, b = map(int, input().split())
print(a + b)
10 20
30
[ ]: # reading two or more floating values in a single line
a, b = map(float, input().split())
print(a + b)
[26]: # reading two or more strings values in a single line
name1, name2, name3 = map(str, input().split())
print(f"Name 1: {name1}")
print(f"Name 2: {name2}")
print(f"Name 3: {name3}")
vikram rolex tina
Name 1: vikram
Name 2: rolex
Name 3: tina