SlideShare a Scribd company logo
1 of 11
Download to read offline
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

python-online&offline-training-in-kphb-hyderabad (1) (1).pdf
python-online&offline-training-in-kphb-hyderabad (1) (1).pdfpython-online&offline-training-in-kphb-hyderabad (1) (1).pdf
python-online&offline-training-in-kphb-hyderabad (1) (1).pdfKosmikTech1
 
python introduction initial lecture unit1.pptx
python introduction initial lecture unit1.pptxpython introduction initial lecture unit1.pptx
python introduction initial lecture unit1.pptxChandraPrakash715640
 
Python Programming-1.pptx of python by computer
Python Programming-1.pptx of python by computerPython Programming-1.pptx of python by computer
Python Programming-1.pptx of python by computersharanyarashmir5
 
a9855c3532e13484ee6a39ba30218896d7c0d863-1676987272842.pptx
a9855c3532e13484ee6a39ba30218896d7c0d863-1676987272842.pptxa9855c3532e13484ee6a39ba30218896d7c0d863-1676987272842.pptx
a9855c3532e13484ee6a39ba30218896d7c0d863-1676987272842.pptxcigogag569
 
Mastering python lesson2
Mastering python lesson2Mastering python lesson2
Mastering python lesson2Ruth Marvin
 
Mastering Python : 100+ Solved and Commented Exercises to Accelerate Your Lea...
Mastering Python : 100+ Solved and Commented Exercises to Accelerate Your Lea...Mastering Python : 100+ Solved and Commented Exercises to Accelerate Your Lea...
Mastering Python : 100+ Solved and Commented Exercises to Accelerate Your Lea...Lucky Gods
 
Python introduction towards data science
Python introduction towards data sciencePython introduction towards data science
Python introduction towards data sciencedeepak teja
 
Advance Python programming languages-Simple Easy learning
Advance Python programming languages-Simple Easy learningAdvance Python programming languages-Simple Easy learning
Advance Python programming languages-Simple Easy learningsherinjoyson
 
Python Programming - III. Controlling the Flow
Python Programming - III. Controlling the FlowPython Programming - III. Controlling the Flow
Python Programming - III. Controlling the FlowRanel Padon
 
UNIT-1 : 20ACS04 – PROBLEM SOLVING AND PROGRAMMING USING PYTHON
UNIT-1 : 20ACS04 – PROBLEM SOLVING AND PROGRAMMING USING PYTHON UNIT-1 : 20ACS04 – PROBLEM SOLVING AND PROGRAMMING USING PYTHON
UNIT-1 : 20ACS04 – PROBLEM SOLVING AND PROGRAMMING USING PYTHON Nandakumar P
 
PART - 1 Python Introduction- Variables- Data types - Numeric- String- Boole...
PART - 1  Python Introduction- Variables- Data types - Numeric- String- Boole...PART - 1  Python Introduction- Variables- Data types - Numeric- String- Boole...
PART - 1 Python Introduction- Variables- Data types - Numeric- String- Boole...manikamr074
 
علم البيانات - Data Sience
علم البيانات - Data Sience علم البيانات - Data Sience
علم البيانات - Data Sience App Ttrainers .com
 

Similar to _PYTHON_CHAPTER_2.pdf (20)

python-online&offline-training-in-kphb-hyderabad (1) (1).pdf
python-online&offline-training-in-kphb-hyderabad (1) (1).pdfpython-online&offline-training-in-kphb-hyderabad (1) (1).pdf
python-online&offline-training-in-kphb-hyderabad (1) (1).pdf
 
python introduction initial lecture unit1.pptx
python introduction initial lecture unit1.pptxpython introduction initial lecture unit1.pptx
python introduction initial lecture unit1.pptx
 
Python Programming-1.pptx of python by computer
Python Programming-1.pptx of python by computerPython Programming-1.pptx of python by computer
Python Programming-1.pptx of python by computer
 
Python by Rj
Python by RjPython by Rj
Python by Rj
 
Review Python
Review PythonReview Python
Review Python
 
lecture 2.pptx
lecture 2.pptxlecture 2.pptx
lecture 2.pptx
 
Ch-3.pdf
Ch-3.pdfCh-3.pdf
Ch-3.pdf
 
a9855c3532e13484ee6a39ba30218896d7c0d863-1676987272842.pptx
a9855c3532e13484ee6a39ba30218896d7c0d863-1676987272842.pptxa9855c3532e13484ee6a39ba30218896d7c0d863-1676987272842.pptx
a9855c3532e13484ee6a39ba30218896d7c0d863-1676987272842.pptx
 
Mastering python lesson2
Mastering python lesson2Mastering python lesson2
Mastering python lesson2
 
Spsl iv unit final
Spsl iv unit  finalSpsl iv unit  final
Spsl iv unit final
 
Spsl iv unit final
Spsl iv unit  finalSpsl iv unit  final
Spsl iv unit final
 
Mastering Python : 100+ Solved and Commented Exercises to Accelerate Your Lea...
Mastering Python : 100+ Solved and Commented Exercises to Accelerate Your Lea...Mastering Python : 100+ Solved and Commented Exercises to Accelerate Your Lea...
Mastering Python : 100+ Solved and Commented Exercises to Accelerate Your Lea...
 
Core python programming tutorial
Core python programming tutorialCore python programming tutorial
Core python programming tutorial
 
Python introduction towards data science
Python introduction towards data sciencePython introduction towards data science
Python introduction towards data science
 
Advance Python programming languages-Simple Easy learning
Advance Python programming languages-Simple Easy learningAdvance Python programming languages-Simple Easy learning
Advance Python programming languages-Simple Easy learning
 
Python Programming - III. Controlling the Flow
Python Programming - III. Controlling the FlowPython Programming - III. Controlling the Flow
Python Programming - III. Controlling the Flow
 
UNIT-1 : 20ACS04 – PROBLEM SOLVING AND PROGRAMMING USING PYTHON
UNIT-1 : 20ACS04 – PROBLEM SOLVING AND PROGRAMMING USING PYTHON UNIT-1 : 20ACS04 – PROBLEM SOLVING AND PROGRAMMING USING PYTHON
UNIT-1 : 20ACS04 – PROBLEM SOLVING AND PROGRAMMING USING PYTHON
 
PART - 1 Python Introduction- Variables- Data types - Numeric- String- Boole...
PART - 1  Python Introduction- Variables- Data types - Numeric- String- Boole...PART - 1  Python Introduction- Variables- Data types - Numeric- String- Boole...
PART - 1 Python Introduction- Variables- Data types - Numeric- String- Boole...
 
Vks python
Vks pythonVks python
Vks python
 
علم البيانات - Data Sience
علم البيانات - Data Sience علم البيانات - Data Sience
علم البيانات - Data Sience
 

More from azha Affi11108

NCD with Health balance.pptx
NCD with Health balance.pptxNCD with Health balance.pptx
NCD with Health balance.pptxazha Affi11108
 
Halal ingredients in food processing
Halal ingredients in food processingHalal ingredients in food processing
Halal ingredients in food processingazha Affi11108
 
2020 udunuwara project 1 (1)
2020 udunuwara project 1 (1)2020 udunuwara project 1 (1)
2020 udunuwara project 1 (1)azha Affi11108
 
Pcos with infertility and its ayurveda and unani combined treatment
Pcos with infertility and its ayurveda and unani combined treatmentPcos with infertility and its ayurveda and unani combined treatment
Pcos with infertility and its ayurveda and unani combined treatmentazha Affi11108
 
English pocket book of 5 covid final
English pocket book of 5 covid finalEnglish pocket book of 5 covid final
English pocket book of 5 covid finalazha Affi11108
 

More from azha Affi11108 (6)

NCD with Health balance.pptx
NCD with Health balance.pptxNCD with Health balance.pptx
NCD with Health balance.pptx
 
Halal ingredients in food processing
Halal ingredients in food processingHalal ingredients in food processing
Halal ingredients in food processing
 
Introduction
IntroductionIntroduction
Introduction
 
2020 udunuwara project 1 (1)
2020 udunuwara project 1 (1)2020 udunuwara project 1 (1)
2020 udunuwara project 1 (1)
 
Pcos with infertility and its ayurveda and unani combined treatment
Pcos with infertility and its ayurveda and unani combined treatmentPcos with infertility and its ayurveda and unani combined treatment
Pcos with infertility and its ayurveda and unani combined treatment
 
English pocket book of 5 covid final
English pocket book of 5 covid finalEnglish pocket book of 5 covid final
English pocket book of 5 covid final
 

Recently uploaded

Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptxExploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptxPooja Bhuva
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Pooja Bhuva
 
How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17Celine George
 
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lessonQUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lessonhttgc7rh9c
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSCeline George
 
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...EADTU
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxEsquimalt MFRC
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxJisc
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfDr Vijay Vishwakarma
 
Details on CBSE Compartment Exam.pptx1111
Details on CBSE Compartment Exam.pptx1111Details on CBSE Compartment Exam.pptx1111
Details on CBSE Compartment Exam.pptx1111GangaMaiya1
 
Play hard learn harder: The Serious Business of Play
Play hard learn harder:  The Serious Business of PlayPlay hard learn harder:  The Serious Business of Play
Play hard learn harder: The Serious Business of PlayPooky Knightsmith
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxRamakrishna Reddy Bijjam
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxPooja Bhuva
 
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...Amil baba
 
PANDITA RAMABAI- Indian political thought GENDER.pptx
PANDITA RAMABAI- Indian political thought GENDER.pptxPANDITA RAMABAI- Indian political thought GENDER.pptx
PANDITA RAMABAI- Indian political thought GENDER.pptxakanksha16arora
 
21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptx21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptxJoelynRubio1
 
UGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdf
UGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdfUGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdf
UGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdfNirmal Dwivedi
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxmarlenawright1
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jisc
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and ModificationsMJDuyan
 

Recently uploaded (20)

Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptxExploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
 
How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17
 
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lessonQUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptx
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
 
Details on CBSE Compartment Exam.pptx1111
Details on CBSE Compartment Exam.pptx1111Details on CBSE Compartment Exam.pptx1111
Details on CBSE Compartment Exam.pptx1111
 
Play hard learn harder: The Serious Business of Play
Play hard learn harder:  The Serious Business of PlayPlay hard learn harder:  The Serious Business of Play
Play hard learn harder: The Serious Business of Play
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptx
 
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
 
PANDITA RAMABAI- Indian political thought GENDER.pptx
PANDITA RAMABAI- Indian political thought GENDER.pptxPANDITA RAMABAI- Indian political thought GENDER.pptx
PANDITA RAMABAI- Indian political thought GENDER.pptx
 
21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptx21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptx
 
UGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdf
UGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdfUGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdf
UGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdf
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 

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