SlideShare a Scribd company logo
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
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.
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.”
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
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.
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.
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:
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;'
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
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,
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'

More Related Content

Similar to _PYTHON_CHAPTER_2.pdf (20)

PPTX
Introduction-to-Python-Programming1.pptx
vijayalakshmi257551
 
PDF
python.pdf
BurugollaRavi1
 
PPTX
made it easy: python quick reference for beginners
SumanMadan4
 
PPTX
Bikalpa_Thapa_Python_Programming_(Basics).pptx
Bikalpa Thapa
 
PPTX
MODULE 1.pptx
KPDDRAVIDIAN
 
PDF
pythonQuick.pdf
PhanMinhLinhAnxM0190
 
PDF
python 34💭.pdf
AkashdeepBhattacharj1
 
PDF
python notes.pdf
RohitSindhu10
 
PPT
python fundamental for beginner course .ppt
samuelmegerssa1
 
PPTX
Unit -1 CAP.pptx
malekaanjum1
 
DOCX
A Introduction Book of python For Beginners.docx
kumarrabinderkumar77
 
PPTX
Python Introduction
vikram mahendra
 
PPT
Spsl iv unit final
Sasidhar Kothuru
 
PPT
Spsl iv unit final
Sasidhar Kothuru
 
PDF
Python for Physical Science.pdf
MarilouANDERSON
 
PPTX
Python-Certification-Training-Day-1-2.pptx
muzammildev46gmailco
 
PDF
Python: Strings & Intro Programming PDFs
SohamSaha49
 
PPT
Python Programming Language
Dr.YNM
 
PDF
Learnpython 151027174258-lva1-app6892
Neeraj Yadav
 
Introduction-to-Python-Programming1.pptx
vijayalakshmi257551
 
python.pdf
BurugollaRavi1
 
made it easy: python quick reference for beginners
SumanMadan4
 
Bikalpa_Thapa_Python_Programming_(Basics).pptx
Bikalpa Thapa
 
MODULE 1.pptx
KPDDRAVIDIAN
 
pythonQuick.pdf
PhanMinhLinhAnxM0190
 
python 34💭.pdf
AkashdeepBhattacharj1
 
python notes.pdf
RohitSindhu10
 
python fundamental for beginner course .ppt
samuelmegerssa1
 
Unit -1 CAP.pptx
malekaanjum1
 
A Introduction Book of python For Beginners.docx
kumarrabinderkumar77
 
Python Introduction
vikram mahendra
 
Spsl iv unit final
Sasidhar Kothuru
 
Spsl iv unit final
Sasidhar Kothuru
 
Python for Physical Science.pdf
MarilouANDERSON
 
Python-Certification-Training-Day-1-2.pptx
muzammildev46gmailco
 
Python: Strings & Intro Programming PDFs
SohamSaha49
 
Python Programming Language
Dr.YNM
 
Learnpython 151027174258-lva1-app6892
Neeraj Yadav
 

More from azha Affi11108 (11)

PPT
best non communicable diseases foods presentation
azha Affi11108
 
PPT
best non communicable diseases presentation
azha Affi11108
 
PPT
best non communicable diseases food presentation
azha Affi11108
 
PPTX
faith-in-the-unseen-3rd-lesson-31-03-20.pptx
azha Affi11108
 
PPTX
99-Names-of-Allah-Asmaul.9199732.powerpoint.pptx
azha Affi11108
 
PPTX
NCD with Health balance.pptx
azha Affi11108
 
PPT
Halal ingredients in food processing
azha Affi11108
 
PPTX
Introduction
azha Affi11108
 
PDF
2020 udunuwara project 1 (1)
azha Affi11108
 
DOCX
Pcos with infertility and its ayurveda and unani combined treatment
azha Affi11108
 
PDF
English pocket book of 5 covid final
azha Affi11108
 
best non communicable diseases foods presentation
azha Affi11108
 
best non communicable diseases presentation
azha Affi11108
 
best non communicable diseases food presentation
azha Affi11108
 
faith-in-the-unseen-3rd-lesson-31-03-20.pptx
azha Affi11108
 
99-Names-of-Allah-Asmaul.9199732.powerpoint.pptx
azha Affi11108
 
NCD with Health balance.pptx
azha Affi11108
 
Halal ingredients in food processing
azha Affi11108
 
Introduction
azha Affi11108
 
2020 udunuwara project 1 (1)
azha Affi11108
 
Pcos with infertility and its ayurveda and unani combined treatment
azha Affi11108
 
English pocket book of 5 covid final
azha Affi11108
 
Ad

Recently uploaded (20)

PDF
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
PDF
The History of Phone Numbers in Stoke Newington by Billy Thomas
History of Stoke Newington
 
PPTX
A PPT on Alfred Lord Tennyson's Ulysses.
Beena E S
 
PPTX
I AM MALALA The Girl Who Stood Up for Education and was Shot by the Taliban...
Beena E S
 
PPTX
Soil and agriculture microbiology .pptx
Keerthana Ramesh
 
PDF
Biological Bilingual Glossary Hindi and English Medium
World of Wisdom
 
PDF
Knee Extensor Mechanism Injuries - Orthopedic Radiologic Imaging
Sean M. Fox
 
PPTX
2025 Winter SWAYAM NPTEL & A Student.pptx
Utsav Yagnik
 
PPSX
HEALTH ASSESSMENT (Community Health Nursing) - GNM 1st Year
Priyanshu Anand
 
PPT
Talk on Critical Theory, Part II, Philosophy of Social Sciences
Soraj Hongladarom
 
PDF
The Different Types of Non-Experimental Research
Thelma Villaflores
 
PPTX
HYDROCEPHALUS: NURSING MANAGEMENT .pptx
PRADEEP ABOTHU
 
PDF
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 - GLOBAL SUCCESS - CẢ NĂM - NĂM 2024 (VOCABULARY, ...
Nguyen Thanh Tu Collection
 
PPTX
ASRB NET 2023 PREVIOUS YEAR QUESTION PAPER GENETICS AND PLANT BREEDING BY SAT...
Krashi Coaching
 
PPTX
Cultivation practice of Litchi in Nepal.pptx
UmeshTimilsina1
 
PDF
CONCURSO DE POESIA “POETUFAS – PASSOS SUAVES PELO VERSO.pdf
Colégio Santa Teresinha
 
PPTX
How to Manage Large Scrollbar in Odoo 18 POS
Celine George
 
PDF
SSHS-2025-PKLP_Quarter-1-Dr.-Kerby-Alvarez.pdf
AishahSangcopan1
 
PPTX
grade 5 lesson matatag ENGLISH 5_Q1_PPT_WEEK4.pptx
SireQuinn
 
PPTX
MENINGITIS: NURSING MANAGEMENT, BACTERIAL MENINGITIS, VIRAL MENINGITIS.pptx
PRADEEP ABOTHU
 
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
The History of Phone Numbers in Stoke Newington by Billy Thomas
History of Stoke Newington
 
A PPT on Alfred Lord Tennyson's Ulysses.
Beena E S
 
I AM MALALA The Girl Who Stood Up for Education and was Shot by the Taliban...
Beena E S
 
Soil and agriculture microbiology .pptx
Keerthana Ramesh
 
Biological Bilingual Glossary Hindi and English Medium
World of Wisdom
 
Knee Extensor Mechanism Injuries - Orthopedic Radiologic Imaging
Sean M. Fox
 
2025 Winter SWAYAM NPTEL & A Student.pptx
Utsav Yagnik
 
HEALTH ASSESSMENT (Community Health Nursing) - GNM 1st Year
Priyanshu Anand
 
Talk on Critical Theory, Part II, Philosophy of Social Sciences
Soraj Hongladarom
 
The Different Types of Non-Experimental Research
Thelma Villaflores
 
HYDROCEPHALUS: NURSING MANAGEMENT .pptx
PRADEEP ABOTHU
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 - GLOBAL SUCCESS - CẢ NĂM - NĂM 2024 (VOCABULARY, ...
Nguyen Thanh Tu Collection
 
ASRB NET 2023 PREVIOUS YEAR QUESTION PAPER GENETICS AND PLANT BREEDING BY SAT...
Krashi Coaching
 
Cultivation practice of Litchi in Nepal.pptx
UmeshTimilsina1
 
CONCURSO DE POESIA “POETUFAS – PASSOS SUAVES PELO VERSO.pdf
Colégio Santa Teresinha
 
How to Manage Large Scrollbar in Odoo 18 POS
Celine George
 
SSHS-2025-PKLP_Quarter-1-Dr.-Kerby-Alvarez.pdf
AishahSangcopan1
 
grade 5 lesson matatag ENGLISH 5_Q1_PPT_WEEK4.pptx
SireQuinn
 
MENINGITIS: NURSING MANAGEMENT, BACTERIAL MENINGITIS, VIRAL MENINGITIS.pptx
PRADEEP ABOTHU
 
Ad

_PYTHON_CHAPTER_2.pdf

  • 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'