0% found this document useful (0 votes)
5 views1 page

Conditional Statements

The document provides an overview of conditional statements in programming, specifically focusing on 'if', 'if-else', and 'if-elif-else' structures. It includes examples demonstrating how to check conditions, handle user input, and execute corresponding code blocks based on the evaluation of those conditions. Additionally, it covers practical applications such as age eligibility for voting and string manipulations.

Uploaded by

Satya Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views1 page

Conditional Statements

The document provides an overview of conditional statements in programming, specifically focusing on 'if', 'if-else', and 'if-elif-else' structures. It includes examples demonstrating how to check conditions, handle user input, and execute corresponding code blocks based on the evaluation of those conditions. Additionally, it covers practical applications such as age eligibility for voting and string manipulations.

Uploaded by

Satya Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

IF

if : it executes a block of code if the condition is true syntax : if : code block


In [1]: a = 20
b = 30

In [2]: if b > a:
print(' b is greater value')

b is greater value

In [13]: if a>b:
print('a is smaller value')
print('execution is done')

print('Im done with if condition')

Im done with if condition

In [14]: #w.a.p to check age eligibility for voting of a candidate. take age
#as a dynamic input

In [16]: age = int(input('Enter Candidate age:' ))


if age >=18:
print('Eligible for voting, Happy Voting')

Enter Candidate age:17

In [17]: a,b

Out[17]: (20, 30)

If -else
If the condition is true , code block associated to true will get executed ,else, code block associated to else will get executed syntax: if : block of code else: block of code
In [19]: if a>b:
print('a is greater value:',a)
print('Execution is done')
else:
print('b is greater value',b)
print('execution is done')

b is greater value 30
execution is done

In [20]: age = int(input('Enter Candidate age:' ))


if age >=18:
print('Eligible for voting, Happy Voting')
else:
print('Not elible for voting')

Enter Candidate age:17


Not elible for voting

if -elif -else
if : block 1 elif : block2 elif : block3 . . . else: defalut block
In [21]:

In [26]: a = int(input('Enter a value: '))


b = int(input('Enter b value'))

if a > b:
print('a is greater value: ',a)
print('Execution is done')
elif b>a:
print('b is greater value',b)
print('Execution is done')
elif a == b:
print('a and b are equal',a,b)
print('Execution is done')
else:
print('No condition is true')
print('Execution is done')

Enter a value: 20
Enter b value34
b is greater value 34
Execution is done

In [28]: # w.a.p to find the largest number among three given numbers , take dynamic inputs

In [1]: a = int(input('Enter a value: '))


b = int(input('Enter b value: '))
c = int(input('Enter c value: '))

if a >= b and a >= c:


if a == b ==c:
print(' a and b and c are equal:')
elif a == b:
print('a and b are largest numbers')
elif a == c:
print(' a and c are largest numbers:')
else:
print('a is larest number: ',a)

elif b >= a and b >= c:


if b == c:
print(' b and c are largest numbers:')
else:
print('b is largest number: ',b)
else:
print('c is largest number: ',c)

Enter a value: 10
Enter b value: 10
Enter c value: 23
c is largest number: 23

In [51]: #w.a.p to check eligibility of candidate for voting. if candidate is not eligible for
# voting find the difference between candidates age and voting eligibility age ,
#print a message that wait for # number of year to get eligibility for voting

In [52]: age = int(input('Enter Person age: '))


if age>18:
print('Eligible for voting, Happy Voting')
else:
d = 18 - age
print('Please wait for ', d , 'years for voting')

Enter Person age: 16


Please wait for 2 years for voting

In [53]: #w.a.p to check if a given string starts with a character 's', if true print 'welcome' else
#print 'thank you'

# take dynamic string

In [56]: s = input('Enter a string: ')


if s.lower().startswith('s'):
print('welcome')
else:
print('Thank you')

Enter a string: learning


Thank you

In [57]: #w.a.p to check if a string is a title or not , if true print ' given string is a title',
#else convert your string into title and print converted string

In [58]: #s = input('Enter a string: ')


s = 'Programming Is Good'

In [59]: s.istitle()

Out[59]: True

In [60]: s = input('Enter a string: ')


if s.istitle():
print('given string is a title')
else:
print(s.title())

Enter a string: programming is good


Programming Is Good

In [61]: #w.a.p to check whether given numbers is positive or not ? if not add 10 and print the
#number

In [63]: Number = int(input('Enter a number: '))


if Number>0:
print('Given number is positive')
else:
Number= Number+10
print(Number)

Enter a number: 10
Given number is positive

In [ ]:

You might also like