Please check the codes in the menu on the left on the sample codes for my presentation
FUNCTIONS & PROCEDURE
PROCEDURE example
(Mind the proper indentation, please arrange each line of code.)
#Procedure in Python:
def greet(name):
print("Hello, " + name + "!")
greet("Alice")
FUNCTION CALL example
(Mind the proper indentation, please arrange each line of code.)
# Function Call in Python that returns a value
def add_numbers(a, b):
return a + b
# This will return a value of the function call.
print("Please enter your first number: ")
a=int(input())
print("Please enter your second number: ")
b=int(input())
result = add_numbers(a, b)
print("\nThe result is: ", result)
CASE SELECTON Example 1
(Mind the proper indentation, please arrange each line of code.)
# Using Python function and switch to implement CASE statement
def vowel(num):
switch={
1:'a',
2:'e',
3:'i',
4:'o',
5:'u'
}
return switch.get(num,"Invalid input")
print("What is your choice ?\n from 1 to 5")
num = input()
print ("You have chosen", num, "and it is", vowel(int(num)))
CASE SELECTON Example 2
(Mind the proper indentation, please arrange each line of code.)
import os #This import os, is a built-in function to allow the os.system('clear') to work.
def select(num):
switch={
1: "Cash Register",
2: "Monthly Report",
3: "Menu of the day",
4: "Exit"
}
return switch.get(num,"Please try again")
num=int(0)
while int(num) < 4:
print("\n\n\n")
print ("Please enter a choice \n 1: Cash Register \n 2: Monthly Report \n 3: Menu of the day \n 4: Exit")
num = input ()
print("\n")
print(" You have chosen ", num, " as the ", select(int(num)))
input()
os.system('clear')
print("You have exited")
ARRAY Example 1
(Mind the proper indentation, please arrange each line of code.)
import array
NumberofItems=int(10)
k=0
ProductPrice=array.array('i', range(NumberofItems))
while (k<10):
ProductPrice[k]=int(input("Enter the product price: " ))
k=k+1
k=0
print(""The product prices are: ")
while (k<10):
print(ProductPrice[k])
k=k+1
2-DIMENSIONAL ARRAY Example 2
(Mind the proper indentation, please arrange each line of code.)
import array
Array_2d = [[10, 2, 15, 12], [3, 12 ,8], [10, 8, 12, 5], [12,15,8,6]]
for row in Array_2d:
for element in row:
print(element, end = " ")
print()
XXX
2-DIMENSIONAL ARRAY Example 3
(Mind the proper indentation, please arrange each line of code.)
Example on having 10 inputted numbers and segregating them of either the number is ODD or EVEN
#declaring an array
import array
a=array.array('i',range(10))
#entering data into array
i=0
while(i<10):
a[i]=int(input("Enter element:"))
i=i+1
even=array.array('i',range(10))
odd=array.array('i',range(10))
j=0
k=0
l=0
#separating even and odd numbers into separate list
while (j<10):
if(a[j]%2==0):
even[k]=a[j]
k=k+1
else:
odd[l]=a[j]
l=l+1
j=j+1
#printing both the lists
print("The even list")
x=0
while (x<k):
print(even[x],end=" ")
x=x+1
print("\nThe odd list")
y=0
while (y<l):
print(odd[y],end=" ")
y=y+1