CONTROL STATEMENTS
Conditional Statements
if statement:
if statement is used for execute a group of the statements based on the result of the condition
To check number is positive or not
num = 10
if num>0:
print("positive number")
with else
num = -30
if num>0:
print("positive number")
else:
print("negative number")
input() function :- it is a function to read the data from user . such as given below.
num = int(input("enter a number : "))
print(num)
print(type(num))
To check the number is odd or even
n = int(input("enter a number : "))
if n%2==0:
print(n,"is even number")
else:
print(n,"is odd number")'''
To check smallest number of two numbers
a = int(input("enter a number : "))
b = int(input("enter b number :"))
if a<.b:
print("a is smallest number")
elif a>b:
print("b is smallest number")
else:
print("both are equal")'''
To check smallest number of three numbers
a = int(input("enter first number :"))
b = int(input("enter second number :"))
c = int(input("enter third number :"))
if a<.b and a<.c: (remove".")
print("a is smallet number among b and c")
elif b<.a and b<.c:
print("b is smallest number among a and c")
else:
print("c is smallest number among a and b")'''
To chect the date is valid or not
date = input("enter a date : ")
dd,mm,yy = date.split('/')
dd = int(dd)
mm = int(mm)
yy = int(yy)
if mm==1 or mm==3 or mm==5 or mm==7 or mm==8 or mm==10 or mm==12:
max1 = 31
elif mm==4 or mm==6 or mm==9 or mm==11:
max1 = 30
elif yy%4==0 and yy%100!=0 or yy%400==0:
max1 = 29
else:
max1 = 28
if dd<1 or dd>max1:
print("date is invalid")
elif mm<1 or mm>12:
print("date is invalid")
else:
print("date is valid")'''
To check grade of the students
marks = int(input("enter marks :"))
if marks>=35:
if marks>=75 and marks<=100:
print("you are in the list of distinction........!")
elif marks>=60 and marks<.75:
print("you are in the list of first class")
elif marks>=50 and marks<.60:
print("you are in the list of second class")
else:
print("you are in the list of third class")
else:
print("write again exam in next sem ....!")'''
To check year leap or not in single line of IF condition.
year = int(input("enter a year :"))
print(year,"is leap year ") if year%4==0 else print(year,"is not leap year")
Iterating Statements
=======FOR LOOP=====:-------
For loop:- it is used for the executing group of statements multiple lines.
we know the exact number of iterations.
I need to print 10 number from 1 to 10 using for loop
count = 0
for x in range(1,6):
print(x)
count = count+x
print(count)
To print reverse number from 10 to 1.
sum = 0
for x in range(10,0,-1):
print(x)
sum = sum+x
print("sum of all numbers")
print(sum)
To print letters one by one
data = 'harun python programmer'
for x in data:
print(x)
To print tables by taking the number from user
number = int(input("enter a number for table :"))
for x in range(1,11):
print(number,"*",x,"=",number*x)
using function
def table(number):
for x in range(1, 11):
print(number, "*", x, "=", number * x)
table(2)
Display even numbers from one range to another range
x = int(input("enter lower range : "))
y = int(input("enter higher range :"))
for i in range(x,y+1):
if i%2!=0:
print(i)
To display star pattern
for i in range(1,11):
print(i*'*')
Reverse pattern
for i in range(10,0,-1):
print((10-i)*' '+i*'*')# for space (10-i)multiple with spaces like (' ')
To check given number is prime or not
num = int(input("enter a number : "))
sum = 0
for x in range(1,num+1):
if num%x==0:
sum = sum+1
if sum==2:
print("prime number")
else:
print("not a prime number")
To check number is perfect or not
num = int(input("enter a number : "))
sum = 0
for x in range(1,num):
if num%x==0:
sum = sum+x
if sum==num:
print("perfect number")
else:
print("not a perfect number")
To check in the string how many digits and characters
msg = 'harun14321561'
digit = 0
char = 0
for i in msg:
if i.isdigit():
digit = digit+1
char = char+1
print("number of digits in msg",digit)
print("number of character in msg",char)
To check in string how many is lower and upper
name = "harunPYTHONProgrammer"
l = 0
u = 0
for x in name:
if x.islower():
l = l+1
elif x.isupper():
u = u+1
print("lower in name :",l)
print("upper in name",u)
Fibonacci series
num = int(input("enter a number of terms :"))
a=0
b=1
for x in range(1,num+1):
c = a+b
a = b
b = c
print(a)
Factorial of number
num = int(input("enter a number :"))
fact = 1
for x in range(1,num+1):
fact = fact*x
print("the factorial of",num,"is",fact)
=====WHILE LOOP====:=--
while loop:- we don't know exact number of itarations
To print numbers from 1 to 10 using while
i = 1
while i<=10:
print(i)
i = i+1
To print number and know sum of numbers
i =1
n = 0
while i<=10:
print(i)
n = n+i
i = i+1
print("sum of numbers")
print(n)
To sum the numers upto specified capacity
i=1
n=0
capacity=30
while n<=capacity:
print(i)
n = n+i
i = i+1
print(n)
sum of digts by while
n = int(input("enter a number :"))
total =0
while(n>0):
rem = n%10
total = total + rem
n = n//10
print(total)
To check the reverse of number
n = int(input("enter a number :"))
rev =0
while n>0:
rem = n%10
rev = rev*10+rem
n = n//10
print("reverse of number:",rev)
To check given number is palindrome or not
n = int(input("enter a number :"))
rev = 0
num = n
while n>0:
rem =n%10
rev = rev*10+rem
n=n//10
if rev==num:
print(" given number is palindrome number")
else:
print("given number is not a palindrome")
To check number is armstrong or not
n = int(input("enter a number :"))
total =0
temp = n
while n>0:
rem = n%10
total = total+rem**3
n = n//10
if total==temp:
print("given number is armstrong")
else:
print("given number is not a armstrong")'''
program for fibonacci series
nterms=int(input("how many terms :"))
n1=0
n2=1
count=0
if nterms==1:
print("fibonacci sequence upto",nterms,":",n1)
else:
print("fibonacci sequence upto",nterms,":")
while count<.nterms:
print(n1,end=",")
n3=n1+n2
n1=n2
n2=n3
count = count+1
To count number of digits
n = int(input("enter a number :"))
count = 0
while n > 0:
count = count+1 #it counts how many numbers we entered
n = n//10
print(count)
To display prime number using nested loo
for num in range(1,101):
count=0
for x in range(1,num+1):
if num%x==0:
count!=1
if count==2:
print(num)
To display identity matrix
n = int(input("enter matrix :"))
for i in range(0,n):
for j in range(0,n):
if(i==j):
print("1",sep=" ",end=" ")
else:
print("0",sep=" ",end=" ")
print()
multiple two matrices
x = [[1,2,3],
[4,5,6],
[7,8,9]]
y = [[5,1,2],
[6,7,3],
[4,5,1]]
result = [[0,0,0],
[0,0,0],
[0,0,0,]]
for i in range(len(x)):
for j in range(len(y[0])):
for k in range(len(y)):
result[i][j]=result[i][j]+x[i][k]*y[k][j]
for r in result:
print(r)
Transfer statements
break pass continue
=====break========
To print sum of number upto specified capacity
i=1
total=0
capacity=25
while True:
print(i)
total = total + i
i = i+1
if total > capacity:
break
else:
print("we have reached the maximum limit")
print(total)
=====continue=======
print 1 to 10 expect 7
for x in range(1,11):
if x==7:
continue
print(x)
To print a string without consonants
data = 'harun pyhton programmer'
for x in data:
if x=='a' or x=='e' or x=='i' or x=='o' or x=='u':
continue
print(x)
=======pass=======
count the number of character except one letter
data = "harun"
n=0
for x in data:
if(x=="h"):
pass
else:
n=n+1
print(n)
wap to count number of vowels in a string
n=0
data ='harun'
for x in data:
if x == 'a' or x == 'e' or x == 'i' or x == 'o' or x == 'u':
n=n+1
print(n)'''
========LIST============
lis_name=[10,20,30] #first way
print(type(lis_name))'''
list can be empty
data = []
print(type(data))
2nd way of list
name = list()
data = list(range(1,11))
print(data)
making list on strings
msg='harun python programmer'
data =list(msg)
print(data)
CONCATINATION OF LIST := we can create a list by concatenation of two lists
a=[1,2,3]
b=[4,5,6]
print(a+b)
REPETITION OPERATOR ON LIST := we can create list by repeating elements
a=[1,2,3]
print(a*2)
Indexing:= it is a process of accessing an element from list based on index position
data = [1,2,3,'g','h','j',4]
print(data[4])
print(data[-1])'''
Slicing := it is a process of accessing a group of elements
data = [0,1,2,3,4,5,6,7,8,9]
print(data[0:5])
print(data[2:])
print(data[:7])'''
Membership operation on list
data = [1,2,3,4,5,6,7,8,9]
print(10 in data)
print(2 in data)
print("h" in data)
print('h' not in data )