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

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
KosmikTech1
 
python introduction initial lecture unit1.pptx
python introduction initial lecture unit1.pptxpython introduction initial lecture unit1.pptx
python introduction initial lecture unit1.pptx
ChandraPrakash715640
 
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
sharanyarashmir5
 
Python by Rj
Python by RjPython by Rj
Review Python
Review PythonReview Python
Review Python
ManishTiwari326
 
lecture 2.pptx
lecture 2.pptxlecture 2.pptx
lecture 2.pptx
Anonymous9etQKwW
 
Ch-3.pdf
Ch-3.pdfCh-3.pdf
a9855c3532e13484ee6a39ba30218896d7c0d863-1676987272842.pptx
a9855c3532e13484ee6a39ba30218896d7c0d863-1676987272842.pptxa9855c3532e13484ee6a39ba30218896d7c0d863-1676987272842.pptx
a9855c3532e13484ee6a39ba30218896d7c0d863-1676987272842.pptx
cigogag569
 
Mastering python lesson2
Mastering python lesson2Mastering python lesson2
Mastering python lesson2
Ruth Marvin
 
Spsl iv unit final
Spsl iv unit  finalSpsl iv unit  final
Spsl iv unit final
Sasidhar Kothuru
 
Spsl iv unit final
Spsl iv unit  finalSpsl iv unit  final
Spsl iv unit final
Sasidhar Kothuru
 
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
 
Core python programming tutorial
Core python programming tutorialCore python programming tutorial
Core python programming tutorial
Amarjeetsingh Thakur
 
Python introduction towards data science
Python introduction towards data sciencePython introduction towards data science
Python introduction towards data science
deepak 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 learning
sherinjoyson
 
Python Programming - III. Controlling the Flow
Python Programming - III. Controlling the FlowPython Programming - III. Controlling the Flow
Python Programming - III. Controlling the Flow
Ranel 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
 
Vks python
Vks pythonVks python
Vks python
Vinod Srivastava
 
علم البيانات - 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.pptx
azha Affi11108
 
Halal ingredients in food processing
Halal ingredients in food processingHalal ingredients in food processing
Halal ingredients in food processing
azha Affi11108
 
Introduction
IntroductionIntroduction
Introduction
azha 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 treatment
azha 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 final
azha 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

BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
Nguyen Thanh Tu Collection
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
JosvitaDsouza2
 
Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
TechSoup
 
Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345
beazzy04
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
camakaiclarkmusic
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
Celine George
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
EverAndrsGuerraGuerr
 
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdfAdversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Po-Chuan Chen
 
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
Levi Shapiro
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
Jisc
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
Celine George
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
Pavel ( NSTU)
 
Palestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxPalestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptx
RaedMohamed3
 
Polish students' mobility in the Czech Republic
Polish students' mobility in the Czech RepublicPolish students' mobility in the Czech Republic
Polish students' mobility in the Czech Republic
Anna Sz.
 
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th SemesterGuidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Atul Kumar Singh
 
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCECLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
BhavyaRajput3
 
Francesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptxFrancesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptx
EduSkills OECD
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
Vivekanand Anglo Vedic Academy
 
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
MIRIAMSALINAS13
 
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
TechSoup
 

Recently uploaded (20)

BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
 
Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
 
Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
 
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdfAdversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
 
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
 
Palestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxPalestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptx
 
Polish students' mobility in the Czech Republic
Polish students' mobility in the Czech RepublicPolish students' mobility in the Czech Republic
Polish students' mobility in the Czech Republic
 
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th SemesterGuidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th Semester
 
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCECLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
 
Francesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptxFrancesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptx
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
 
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
 
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
 

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