DiTEC Unit 08 - Python Programming
DiTEC Unit 08 - Python Programming
DiTEC Unit 08 - Python Programming
UNIT 08
PYTHON PROGRAMMING
8.1 - INTRODUCTION
• Please refer to Python™ 3 Installation Guide to make your computer ready to run Python™
programs.
#Numeric Data
mark01 = 85 #Non-Decimal Values
Go to Python Numbers Again
mark02 = 85.64 #Decimal Values
#Non-Numeric Values
name = ‘Dilan’
address = “No 57, Main Street, Battaramulla”
• Special symbols that can perform arithmetic computations, logical comparisons or value
assignments.
• Typical Operation:
OPERATOR
If A = 10 and B = 3
VALUE INSIDE
OPERATOR NAME EXAMPLE VARIABLE ‘C’
+ Addition C=A+B 13
- Subtraction C=A–B 7
* Multiplication C=A*B 30
/ Division C=A/B 3.33
% Modulus C=A%B 1
** Exponent C = A ** B 1000
// Floor Division C = A // B 3
If A = 10, B = 10 and C = 3
VALUE INSIDE
OPERATOR NAME EXAMPLE VARIABLE ‘C’
== Equals C = (A == B) True
!= C = (A != B) False
Not Equals
<> C = (A <> C) True
> Greater Than C = (A > C) True
>= Greater Than or Equals C = (A >= C) True
< Lesser Than C = (A < B) False
<= Lesser Than or Equals C = (A <= B) True
VALUE INSIDE
OPERATOR EXAMPLE VARIABLE ‘A’
= A=B 4
+= A += B 14
-= A -= B 6
*= A *= B 40
/= A /= B 2.5
%= A %= B 2
**= A **= B 10000
//= A //= B 2
• Syntax: • Example:
if(condition): Mark = 85
statement(s)
if(Mark > 50):
print(“Passed”)
Reading Keyboard
Input
• Syntax: • Example:
Mark = 85
if (condition):
if (Mark > 50):
statement(s) print(“Passed”)
else:
else:
print(“Failed”)
statement(s)
• Syntax: • Example:
Mark = 85
if (condition_01): if (Mark > 75):
statement(s) print(“A”)
elif (condition_02): elif (Mark > 50):
statement(s) print(“B”)
…………………………………………………… elif (Mark > 35):
else: print(“C”)
statement(s) else:
print(“Failed”)
Monday, April 15, 2024 ESOFT METRO COLLEGE - BATTARAMULLA 28
8.5 – LOOPS IN PYTHON™
• Syntax:
X = 0
while (condition): while (X < 5):
statement(s) print(X)
X += 1
• Example:
Monday, April 15, 2024 ESOFT METRO COLLEGE - BATTARAMULLA 30
8.5.2 - FOR LOOP
• Syntax:
for X in range(5):
for(variable in sequence): print(X)
statement(s)
• Example:
Monday, April 15, 2024 ESOFT METRO COLLEGE - BATTARAMULLA 31
8.6 – SPECIAL DATA TYPES
• Following illustration shows how Python string values are saved in main memory.
• Example:
• name01 = “Gihan Samaranayaka”
name01 Values
(Name of the variable)
G i h a n S a m a r a n a y a k a
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
Index Numbers
• However, if you try to access an index number, that do not exist in the specified variable,
following error will occur:
• However, if you try to access an index number, that do not exist in the specified variable,
there will not be an error:
• Example:
Examples\PythonStrings01.py
Click Here
• Examples:
• names01 = [“Sahan”, “Amith”, “Jehan”]
• Following illustration shows how Python list items are saved in main memory.
• Example:
• names01 = [“Sahan”, “Amith”, “Jehan”]
names01 Values
(Name of the list)
Index Numbers
• However, if you try to access an index number that do not exist in the specified variable,
following error will occur:
• However, if you try to access an index number that do not exist in the specified variable,
there will not be an error:
• Example:
Examples\PythonLists01.py
Click Here
• Examples:
• student01 = (“Sahan”, “Saparamadu”, “DIT-111”, “Battaramulla”)
• Following illustration shows how Python tuple items are saved in main memory.
• Example:
• student01 = (“Sahan”, “Saparamadu”, “DIT-111”, “Battaramulla”)
student01 Values
(Name of the tuple)
Index Numbers
• However, if you try to access an index number that do not exist in the specified variable,
following error will occur:
• However, if you try to access an index number that do not exist in the specified variable,
there will not be an error:
• Example:
Examples\PythonTuples01.py
Click Here
• Dictionary_Name = {item01:value01,……,itemX:valueX}
• Following illustration shows how Python dictionary items are saved in main memory.
• Example:
• studentDict1 = {'FirstName':'Saman' , 'Age':35 , 'Hometown':'Nugegoda'}
studentDict01
(Name of the dictionary)
FirstName Saman Age 35 Hometown Nugegoda
X - Key
• However, if you try to access a key that do not exist in the specified dictionary, following error
will occur:
• Example:
Examples\PythonDictionaries01.py
Click Here
• A function is a grouped set of statements written to achieve a specific task inside a program.
• Rules of creating a function in Python:
• Function should begin with the keyword def. Then you should provide a name for the
function.
• Example: Docstring
Arguments (Optional)
Name
• Another Example:
Examples\PythonFunctions01.py
Click Here
• We can use the in-built input() function of Python to read keyboard input from the user.
• By default, the value taken by the input() is of type string.
• If we need to convert that string value into a numeric value (for calculations), we can use in-
built int() and float() functions.
Go again to IF STATEMENTS