chapter-3
Condition execution
python
if...else
syntax
if test expression:
statement(s)
example 1
a= int(input("enter first numbers .."))
b=int(input("enter second number.."))
if a>b:
print("a is greatest")
if b>a:
print("b is greatest")
output
enter first number...2
enter second numbers...4
b is greatest
enter first number
...23
enter second number...12
a is greatest
Example 2
Given Number Is
Positive Negative Or Zero
num =int(input("enter a
number.."))
if num>0:
print(num,
"is a positive number.")
if num<0:
print(num,
"is a negative number..")
if num==0:
print("the
given number is zero")
output
enter a number..4
4, 'is a positive number.'
enter a number..-6
-6, 'is a negative number..'
enter a number..0
the given number is zero
IF...ELSE statement
syntax
if test expression
body of if stament(s)
else:
body of else
statemet(s)
example:-
num=int(input("enter a number
.."))
if num %3==0:
print(num,
"is divisible by 3.")
else:
print(num, "is
not divisible by 3")
output
enter a number ..5
5, 'is not divisible by 3'
enter a number ...12
12, 'is divisible by 3.'
program 2
num ==int(input("enter a
number.."))
if num >=0:
print("positive
number")
else:
print("negative number")
output
enter a number ...34
positive number
enter a number..-56
negative number
python IF...ELIF...ELSE
syntax
if test expression:
body of
if statement(s)
elif test expresion:
body of
elif statement(s)
else:
body of
else statement(s)
program:-
a=int(input("enter first
number.."))
b=int(input("enter second
number.."))
c=int(input("enter 3rd
number,,,"))
if a>b and a>c:
print("greatest
is a ")
elif b>c:
print("greatest
is b")
else:
print("greatest is
c")
output
enter first number..1
enter second number....3
enter 3rd number..2
greatest is b
enter first number..1
enter second number....2
enter 3rd number..3
greatest is c
enter first number..4
enter second number....2
enter 3rd number..3
greatest is a
Nested if statements
program example
num=float(input("enter a number:
"))
if num >=0:
if num ==0:
print("zero")
else:
print("positive number")
else:
print("negative
number")
output
enter a number:5
positive number
enter a number: -1
negative number
enter a number: 0
zero
Iteration
for loop structure
syntax
for val in sequences:
[body of for loop
statement(s)]
example program
for i in "HAI":
print(i)
output
H
A
I
The range () function
example
for i in range(5,50,5):
print(i)
output
5
10
15
20
25
30
35
40
45
For loop with else
example
digits=[1,2,3,4,5,6,7,8,9,10]
for i digits:
print(i)
else:
print("no items left.")
output
1
2
3
4
5
6
7
8
9
10
no items left
While loop structure
syntax
while <test_expression>:
body of
while loop
example:
n=int(input("enter n:"))
sum=0
i=1
while i<=n:
sum= sum +i
i = i+1
print("the sum
is",sum)
output
enter n: 10
the sum is 55
While loop with else
program
i=1
while i<=3:
print("\n
inside loop")
i= i+1
else:
print("\n finally print while
condition is flase")
output
inside loop
inside loop
inside loop
finally print while condition is false
Break and continue
example
for val in "string":
if val ==
"i":
break
print(val)
print("The end")
output
S
T
R
the end
python continue
statements
for val in "string":
if val ==
"i":
continue
print(val)
print("the end")
output
s
t
r
n
g
the end
Pass
sequence ={'p','a','s','s'}
for val in sequence:
pass
print(val)
output
>>>
RESTART:
C:/Users/SERVER/AppData/Local/Programs/Python/Python37-32/jhgkjds.py
p
>>>
RESTART:
C:/Users/SERVER/AppData/Local/Programs/Python/Python37-32/jhgkjds.py
s
>>>
RESTART:
C:/Users/SERVER/AppData/Local/Programs/Python/Python37-32/jhgkjds.py
a
>>>
RESTART:
C:/Users/SERVER/AppData/Local/Programs/Python/Python37-32/jhgkjds.py
p
>>>
0 comments:
Post a Comment