1. mhs;ka NdIdj iuÕ lD;%su nqoaêh - AI with Python Language
8
Dishant Yapa | 077 295 3717
www.srilankarobotics.lk
Python programs can used to write and run through the many editor software. But, what we
are considering here IDLE and PyCharm editor software. mhs;ka jevigyka fndfyda editor
uDÿldx. yryd ,sùug yd l%shd;aul lsÍug Ndú;d l< yelsh' kuq;a" wms fuys i,ld n,kafka
IDLE editor iy PyCharm uDÿldx.hhs.
IDLE editor is lightweight, simple, and provides basic functionality such as syntax highlighting
in shell and Python files.
To use IDLE, simply install Python and type “IDLE” into your operating system search bar.
This should work for Linux, Mac, and Windows operating systems.
If you want to execute a small program or script with one or two files using default libraries,
You can use the IDLE project.
If you want to execute a larger program and use different external dependencies specifically for
this project, You can use PyCharm with a virtual environment to handle the different libraries.
02
IDLE Software - Integrated DeveLopment Environment
IDLE kñka yeÈkafjk interactive mode fhdod .ksñka l%shd;aul
jk integrated development environment uDÿldx.h" edit, run browse
and debug lrñka programs ,sùu fuys § isÿ lrkq ,nhs'
How to open IDLE application
2. mhs;ka NdIdj iuÕ lD;%su nqoaêh - AI with Python Language
9
Dishant Yapa | 077 295 3717
www.srilankarobotics.lk
The >>> is called the Python prompt, The interpreter uses the prompt to indicate that it is ready
for instructions.
">>>" - ixfla;h u.ska python command (úOdkhla& wfmalaId lrk nj weÕùu isÿ lrkq
we;'
How to use Python to show “Hello world “.
How to use Python to calculate 2+2
We typed 2 + 2, and the interpreter evaluated our expression, and replied 4, and on the next line
it gave a new prompt, indicating that it is ready for more input.
We type Python expression / statement / command after the prompt and Python immediately
responds with the output of it. Let's start with typing print “How are you” after the prompt.
>>>print (“How are you?”)
How are you?
Write and run Python program via PyCharm Software
Let’s do the coding to show the output of “Hello world “. This “Hello world “ is string data type.
This sentence is an instruction(one), which contains a single statement. The statement is ‘write
the words’.
In Python (3.x), the equivalent statement is print.
print("Hello world")
How to input print command.
3. mhs;ka NdIdj iuÕ lD;%su nqoaêh - AI with Python Language
10
Dishant Yapa | 077 295 3717
www.srilankarobotics.lk
How to Run the Application.
How to see the Output.
Activity-1
What we get is Python’s response. We may try the following and check the response:
(i) print (5+7) (ii) 5+7 (iii) 6*250/9 (iv) print (5-7)
It is also possible to get a sequence of instructions executed through interpreter.
(v) A cinema is offering discount tickets to anyone who is under 15. Decomposing this problem,
gives this algorithm:
1. find out how old the person is
2. if the person is younger than 15 then say “You are eligible for a discount ticket.”
3. otherwise, say “You are not eligible for a discount ticket.”
4. mhs;ka NdIdj iuÕ lD;%su nqoaêh - AI with Python Language
11
Dishant Yapa | 077 295 3717
www.srilankarobotics.lk
Note the similarities between the flowchart and pseudocode, and the finished program.
Now we are good to write a small code on our own in Python, While writing in Python,
remember Python is case sensitive. That means x & X are different in Python. tkï
jevigykla ,sùfï § x iy X w;r fjki b;d jeo.;a fõ'
fuu software tl ;=,ska bj;a ùug wjYH kï"
Keyboard ys we;s Ctrl+D press lsÍfu fyda
quit ^& f,i lsÍu yryd th l%shd;aul lr .; yelsh (used to leave the interpreter).
Algorithm would look like this:
OUTPUT "How old are you?"
INPUT User inputs their age STORE the user's input in the age variable
IF age < 15 THEN OUTPUT "You are eligible for a discount.
"ELSE OUTPUT "You are not eligible for a discount."
In a flowchart, this algorithm would look like this:
Creating the program in Python
A Python (3.x) program to meet this
algorithm would be:
age = int(input("How old are you?"))
if age < 15:
print("You are eligible for a discount.")
else:
print("You are not eligible for a discount.")
Drawing a Shape
fuys § úúO yev;, mß.Klh ;=, o¾Ykh lr .ekSu i|yd Python Ndú;hg .kq ,nhs'
Following example is giving you to understand how to display a triangle.
Input Output
5. mhs;ka NdIdj iuÕ lD;%su nqoaêh - AI with Python Language
12
Dishant Yapa | 077 295 3717
www.srilankarobotics.lk
Keywords in Python
They are the words used by Python interpreter
to recognize the structure of program. As these
words have specific meaning for interpreter,
they cannot be used for any other purpose.
Statements in Python
Different programming languages
use different statements. A few of
these are listed in this table. úúO
l%uf,aLk NdId úúO m%ldY Ndú;d
lrhs' fïjdhska lsysmhla fuu
j.=fõ ,ehsia;= .; lr we;'
Statement Purpose
print Output a message on the screen
input Get data from the user
if…else A decision
while A loop controlled by a decision
for A loop controlled by a counter
def Create a procedure or function
Operators and Operands
Operators are special symbols which represents computation
(.Kkh lsÍu ksfhdackh lrk úfYaI ixfla;). They are applied
on operand(s), which can be values or variables. Same
operator can behave differently on different data types. Operators
when applied on operands form an expression.
Operators are categorized as Arithmetic, Relational, Logical and
Assignment. Value and variables when used with operator are
known as operands.
6. mhs;ka NdIdj iuÕ lD;%su nqoaêh - AI with Python Language
13
Dishant Yapa | 077 295 3717
www.srilankarobotics.lk
Basic arithmetic ^wxl .Ks;h&
Arithmetic is used all the time in computer programs, so an understanding of how computers
use arithmetic is useful' mß.Kl jevigyka j, wxl .Ks;h ks;ru Ndú;d lrhs" tneúka
mß.Kl wxl .Ks;h Ndú;d lrk wdldrh ms<sn| wjfndaOhla m%fhdackj;a fõ'
This table lists the common arithmetic processes with their programming equivalents.
Arithmetic process Programming equivalent
Addition (plus) +
Subtraction (minus) -
Multiplication *
Division /
Activity2
Do the following mathematical examples in Python (3.x).
(i) >>> print(5 + 7)
12
(ii) >>> print(7 - 5)
2
(iii) >>> print(5 * 7)
35
(iv) >>> print(35 / 7)
5.0
More complicated calculations:
(v) >>> print((5 * 2) + (4 - 3))
11
(vi) >>> print((8 / 4) + (2 - 1))
3.0
Mathematical/Arithmetic Operators
Python Numbers
Two primary (m%d:ñl) types of numbers mhs;ka Ndú;hg .kq ,nhs.
• Integers (whole numbers fyj;a iïmQ¾K ixLHd, including negatives, like 7, -9, or 0)
• Floating point numbers (numbers with decimals, like 1.0, 2.5, 0.999, or 3.14159265)
Python Operators
The math symbols (.Ks; ixfla;) like + (plus) and - (minus) are called operators because they
operate, or perform calculations, on the numbers in our equation.
7. mhs;ka NdIdj iuÕ lD;%su nqoaêh - AI with Python Language
14
Dishant Yapa | 077 295 3717
www.srilankarobotics.lk
Relational Operators
Logical Operators
Symbol Description
or If any one of the operand is true, then the condition becomes true.
and If both the operands are true, then the condition becomes true.
not Reverses the state of operand/condition.
Example:
8. mhs;ka NdIdj iuÕ lD;%su nqoaêh - AI with Python Language
15
Dishant Yapa | 077 295 3717
www.srilankarobotics.lk
Python Indentation
Most of the programming languages like C, C++, and Java use braces { } to define a block of
code. Python, however, uses indentation.
A code block (body of a function, loop, etc.) starts with indentation and ends with the first un-
indented line. The amount of indentation is up to you, but it must be consistent throughout that
block.
Generally, four whitespaces are used for indentation and are preferred over tabs. Here is an
example.
Python Comments
Comments are very important while writing a program. They describe what is going on inside a
program, so that a person looking at the source code does not have a hard time figuring it out.
You might forget the key details of the program you just wrote in a month's time. So taking the
time to explain these concepts in the form of comments is always fruitful.
In Python, we use the hash (#) symbol to start writing a comment.
It extends up to the newline character. Comments are for programmers to better understand a
program. Python Interpreter ignores comments.
for i in range(1,11):
print(i)
if i == 5:
break
The enforcement of indentation in Python makes the code look neat and clean. This results in
Python programs that look similar and consistent. Python ys bkafvkafÜIka n,d;aul lsÍu fla;h
ms<sfj<g iy meyeÈ,s fmkqula we;s lrhs' fuys m%;sM,hla f,i mhs;ka jevigyka iudk iy ia:djr f,i
o¾Ykh jkq we;'
9. mhs;ka NdIdj iuÕ lD;%su nqoaêh - AI with Python Language
16
Dishant Yapa | 077 295 3717
www.srilankarobotics.lk
Answer all the questions.
1. What is a variable?
1. A box(memory location) where you store values 2. a type of graphics
3. Data type 4. a type of memory
2. What symbol is used in python to assign values to a variable?
1. equals = 2. plus +
3. forward slash / 4. asterisk *
3. What will be the output?
name = "Dave"
print (name)
1. Dave 2. 'Dave' 3. name 4. (name)
4. What is Python?
1. text based programming language 2. word processor
3. spreadsheet 4. block based programming language
5. What is Algorithm?
1. Fixing bugs 2. Functions
3. A sequence of instructions 4. Game design
6. Fixing errors in programming is called ...
1. Optimization 2. Debugging
3. Looping 4. Cleaning
7. A command that lets us repeat an action multiple times in your code
1. Algorithm 2. Loop 3. Variable 4. Method
8. Which line of code is used to make a loop?
1. for i in range(5) 2. # Code normally executes in the order it's written.
3. hero.build("upArrow") 4. a = "door"
9. Find the object in the following code:
helper.build("upArrow")
1. helper 2. build() 3. upArrow 4. hero
10. mhs;ka NdIdj iuÕ lD;%su nqoaêh - AI with Python Language
17
Dishant Yapa | 077 295 3717
www.srilankarobotics.lk
10. Find the argument in the following code:
hero.moveUp(4)
1. hero 2. moveUp() 3. 4 4. Up()
11. Find the method in the following code:
hero.use("door")
1. hero 2. use() 3. door 4. " "
12. Which python operator means 'less than or equal to'?
1. >= 2. > 3. < 4. <=
13. Python uses indentation to block code into chunks
True False
14. A syntax error means your code has a 'grammar' mistake or you used symbols/operations
incorrectly
True False
15. Which statement correctly assigns the string "Tanner" to the variable name?
1. name = print( "Tanner") 2. input("Tanner")
3. name = "Tanner" 4. name = input("Tanner")
16. Draw with python shape as below,
17. Draw with python shape as below,
11. mhs;ka NdIdj iuÕ lD;%su nqoaêh - AI with Python Language
18
Dishant Yapa | 077 295 3717
www.srilankarobotics.lk
18. Enter the following expressions and see the result,
>>> 2 + 3 * 6
20
>>> (2 + 3) * 6
30
>>> 48565878 * 578453
28093077826734
>>> 2 ** 8
256
>>> 23 / 7
3.2857142857142856
>>> 23 // 7
3
>>> 23 % 7
2
>>> (5 - 1) * ((7 + 1) / (3 - 1))
16.0
>>> 'Alice' * 5
'AliceAliceAliceAliceAlice'