Professional Documents
Culture Documents
A) IF-ELSE
a)
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.")
output
3 is a positive number
This is always printed
This is also always printed.
b) if else
num = 3
if num >= 0:
print("Positive or Zero")
else:
print("Negative number")
Output
Positive or Zero
c) if-else-if
Output 1
Enter a number: 5
Positive number
Output 2
Enter a number: -1
Negative number
Output 3
Enter a number: 0
Zero
2) ITERATIVE STATEMENTS
a) for loop
# List of numbers
numbers = [6, 5, 3, 8, 4, 2, 5, 4, 11]
output
The sum is 48
b) while loop
c) # Program to add natural
d) # numbers up to
e) # sum = 1+2+3+...+n
f)
g) # To take input from the user,
h) # n = int(input("Enter n: "))
i)
j) n = 10
k)
l) # initialize sum and counter
m) sum = 0
n) i = 1
o)
p) while i <= n:
q) sum = sum + i
r) i = i+1 # update counter
s)
t) # print the sum
u) print("The sum is", sum)
output
v) Enter n: 10
w) The sum is 55
3) JUMPING STATEMENTS
a) Return
def absolute_value(num):
"""This function returns the absolute
value of the entered number"""
if num >= 0:
return num
else:
return -num
print(absolute_value(2))
print(absolute_value(-4))
Output
2
4
b) Break
# Use of break statement inside the loop
print("The end")
Output
s
t
r
The end
c) Pass
def function(args):
pass