You are on page 1of 22

HFEE 122 COMPUTER PROGRAMING

FUNCTIONS IN PYTHON, CONDITIONS AND LOOPS LESSON


By
LAURENCE MAREGEDZE +263773467505
maregedzel@staff.msu.ac.zw
OBJECTIVES

• IDENTIFY COMMON FUCTIONS IN PYTHON, CONDITION STATEMENTS AND

LOOPS

• EXPLAIN AND USE THE FUCTIONS, CONDITION STATEMENTS AND LOOPS

• DEVELOP PROGRAMS USING IDENTIFIED FUNCTIONS, CONDITION

STATEMENTS AND LOOPS


OPERATORS IN PYTHON
Python divides the operators in the following groups:
• Arithmetic operators
• Assignment operators
• Comparison operators
• Logical operators
• Identity operators
• Membership operators
• Bitwise operators
PYTHON ASSIGNMENT OPERATORS
PYTHON IDENTITY OPERATORS
• Identity operators are used to compare the objects, not if
they are equal, but if they are actually the same object,
with the same memory location:
COMMON FUCTIONS IN PYTHON
COMMON FUCTIONS IN PYTHON

Different types of Python Functions:

• Python Built-in Functions.


• Python User-defined Functions.
COMMON FUCTIONS IN PYTHON
• print( ) function
• type( ) function
• round( ) function
• input( ) function • divmod( ) function
• abs( ) function • id( ) function
• pow( ) function • ord( ) function
• dir( ) function • len( ) function
• sorted( ) function • sum( ) function
• max( ) function • help( ) function
• append() function
User-defined Functions
• Python User-defined Functions
• Functions that we define ourselves to do certain specific task.

• We write on our own.

• Advantages of user-defined functions

1. User-defined functions help to decompose a large program into small

segments which makes program easy to understand, maintain and


debug.
2. If repeated code occurs in a program. Function can be used to include

those codes and execute when needed by calling that function.


3. Programmers working on large project can divide the workload by

making different functions.


CONDITION STATEMENTS
import math
a=int(input("Enter the value of a: "))
b=int(input("Enter the value of b: "))
c=int(input("Enter the value of a: "))

d=b*b-4*a*c
if(d < 0):
print("The roots are imaginary")
else:
r1=(-b+math.sqrt(d))/(2*a) #to calculate square root */
r2=(-b-math.sqrt(d))/(2*a)
print("The first roots is: ",r1, "The first roots is: ",r2)
print("The first roots is: ", int(r1), "The first roots is: ", int(r2))
print("The first roots is: ", math.ceil(r1), "The first roots is: ", math.ceil(r2))
print("The first roots is: ", math.floor(r1), "The first roots is: ", math.floor(r2))
import random
from array import*
import numpy as np
from numpy import shape
LOOPS
count=0
count1=0
i=0
m=int(input("Enter number of days for the temperature records: "))
a=array('i',[])
a_new=array('i',[])
for i in range(0, m):
x=int(input(f"Enter the temperature value {i+1}: "))
if x <360:
count=count + 1
if x >530:
count1=count1 + 1
a.append(x)
if x < 360:
x = 360
if x > 530:
x = 530
a_new.append(x)
sum=0
for e in a_new:
sum = sum + e
o = np.reshape(a,(12,6))
p = np.reshape(a_new,(12,6))
f=np.delete(p,(0,1,6),axis=0) #delete row values in the 1st, 2nd and 7th row
g=np.delete(p,(2,4),axis=1) #delete row values in the 3rd and 5th columns
q=np.reshape(f,(54))
q1= np.reshape(q,(9,6))
r=np.reshape(g,(48))
r1= np.reshape(g,(12,4))
sum1=0
for e in q:
sum1 = sum1 + e

sum2=0
for e in r:
sum2 = sum2 + e

print(a)
print("\n",a_new)
print(f"\nThe number temperature data below 460 is: {count} and temperature data below 460 is {count1}")
print("The total temperature is: ",sum)
print("\n",o)
print("\n",p)
print("\n",f)
print("\n",q)
print("\n",r)
print("\n",q1)
print("\n",r1)
print(f"\nThe total sample temperature is: {sum1} for subtracted rows and {sum2} for subtracted columns")
print("\nThe average daily coal usage is: ",sum1/54)
print("\nThe average daily coal usage is: ",sum2/48)
THANK YOU

You might also like