1 COAN COMPUTER CLASSES
With the while loop we can execute a set of statements as long as a
condition is true.
Example 1:
Print i as long as i is less than 6:
Python Code
i=1
while i < 6:
print(i)
i += 1
Python For Loops
A for loop is used for iterating over a sequence.
This is less like the for keyword in other programming languages, and works more like an
iterator method as found in other object-orientated programming languages.
With the for loop we can execute a set of statements, once for each item in a list, tuple, set
etc.
Example 2:
Print each fruit in a fruit list:
Python Code
fruits = ["apple", "banana", "cherry"]
for x in fruits:
print(x)
1|Page
NCOAN 59253887 Diploma Information Systems / Bsc (Hons) Business
Informatics / Post Graduate Certificate Computer Science/ Post Graduate
Certificate Computer Education / MSc Project Management
2 COAN COMPUTER CLASSES
What is an Array?
An array is a special variable, which can hold more than one value at a time.
If you have a list of items (a list of car names, for example), storing the cars in single
variables could look like this:
Example 3
Create an array containing car names:
Python Code
cars = ["Ford", "Volvo", "BMW"]
print(cars)
2|Page
NCOAN 59253887 Diploma Information Systems / Bsc (Hons) Business
Informatics / Post Graduate Certificate Computer Science/ Post Graduate
Certificate Computer Education / MSc Project Management
3 COAN COMPUTER CLASSES
Creating a Comment
Comments starts with a #, and Python will ignore them:
Example 4
Python Code
#This is a comment
print("Hello, World!")
Example 5
Python Code
print("Hello, World!") #This is a comment
Further Examples:
(a)
#Add two numbers
num1=7
num2=10
addition=(num1+num2)
print("First number :",num1)
print("Second number :",num2)
print("Addition : ",addition)
3|Page
NCOAN 59253887 Diploma Information Systems / Bsc (Hons) Business
Informatics / Post Graduate Certificate Computer Science/ Post Graduate
Certificate Computer Education / MSc Project Management
4 COAN COMPUTER CLASSES
(b)
#Addition of two numbers inputted by a user
num1=int(input("Enter first number. :"))
num2=int(input("Enter second number. :"))
addition=(num1+num2)
#displaying the user input values
print("First number is :",num1)
print("Second number is :",num2)
#displaying addition of user input
print("Addition is : ",addition)
4|Page
NCOAN 59253887 Diploma Information Systems / Bsc (Hons) Business
Informatics / Post Graduate Certificate Computer Science/ Post Graduate
Certificate Computer Education / MSc Project Management
5 COAN COMPUTER CLASSES
SEQUENCE Classwork in Python
Program 1:
Write a program that is capable to calculate and displays sum of three numbers. All
numbers will be entered by user.
Program 2:
Given the employee’s working hour, a program will calculate salary for an employee. The pay
rate for an hour is 6.50. The program will display the total salary of the employee.
Program 3:
Write a program that can prompts user to enter total number of umbrellas he/she wants to
buy. Price of one umbrella is 3.00. This program will calculate and displays total price of
these umbrellas to user.
5|Page
NCOAN 59253887 Diploma Information Systems / Bsc (Hons) Business
Informatics / Post Graduate Certificate Computer Science/ Post Graduate
Certificate Computer Education / MSc Project Management
6 COAN COMPUTER CLASSES
SELECTION Classwork in Python
Program 1:
Ask user to enter weight. Program will only display status “You’re obese” if the weight is
more than 100kg.
Program 2:
Write a program that will calculate discount, based on the quantity of book purchased. 15%
discount will be given if customer pays for more than 30 books, where cost of each book is
entered by user. Display total price and discount to user. Also display whether user have
been entitled to discount or not.
Program 3:
Modify program 1. Program will display status based on below condition:
• weight >100kg, status= “Obese”
• weight < 30kg, status= “Underweight”
• others, status = “Normal”
Program 4:
Subtract 2 numbers. All numbers will be entered by user.
If the result is positive, say “Positive”; if the result is negative, say “Negative”. If the result is
0, say “Zero”
6|Page
NCOAN 59253887 Diploma Information Systems / Bsc (Hons) Business
Informatics / Post Graduate Certificate Computer Science/ Post Graduate
Certificate Computer Education / MSc Project Management