Introduction to Programming with Python

Slides:



Advertisements
Similar presentations
ECS 15 if and random. Topic  Testing user input using if statements  Truth and falsehood in Python  Getting random numbers.
Advertisements

Conditional Statements Introduction to Computing Science and Programming I.
Overview Program flow Decision / branching / selection structures True & False in Python Comparison operators & Boolean expressions if … if … else if …
Python (yay!) November 16, Unit 7. Recap We can store values in variables using an assignment statement >>>x = We can get input from the user using.
Decisions in Python elif. A new keyword elif A contraction of “else if” Used to tie two if statements (or more) together into one structure Syntax – elif,
Python – Making Decisions Lecture 02. Control Structures A program that only has one flow is useful but limited. We can use if statements to make these.
This Week The string type Modules print statement Writing programs if statements (time permitting) The boolean type (time permitting)
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 4 Decision.
Decision Making CMSC 201. Overview Today we will learn about: Boolean expressions Decision making.
Chapter 3: Branching and Program Flow CSCI-UA 0002 – Introduction to Computer Programming Mr. Joel Kemp.
Midterm Review Important control structures Functions Loops Conditionals Important things to review Binary Boolean operators (and, or, not) Libraries (import.
Decision Making CMSC 201 Chang (rev ).
1 Computer Science of Graphics and Games MONT 105S, Spring 2009 Session 3 Decision Trees Conditionals.
The If Statement There are no switch statements in Python. You need to use just if statements. There are no switch statements in Python. You need to use.
Control Flow (Python) Dr. José M. Reyes Álamo. 2 Control Flow Sequential statements Decision statements Repetition statements (loops)
Flow Control in Imperative Languages. Activity 1 What does the word: ‘Imperative’ mean? 5mins …having CONTROL and ORDER!
Control Flow (Python) Dr. José M. Reyes Álamo. 2 Control Flow Sequential statements Decision statements Repetition statements (loops)
CMSC201 Computer Science I for Majors Lecture 07 – While Loops
Introduction to Decision Structures and Boolean Variables
Control Flow (Python) Dr. José M. Reyes Álamo.
Control Structures I Chapter 3
ANALYSIS AND ALGORITHM DESIGN - III
Introduction to Python
Selection and Python Syntax
C-Language Lecture By B.S.S.Tejesh, S.Neeraja
IF statements.
Programming 101 Programming for non-programmers.
Selection UCT Department of Computer Science Computer Science 1015F
Microsoft Visual Basic 2005 BASICS
පාඨමාලා මාතෘකා Microsoft SQL Server Databases සැකසීම
Lesson 8: Boolean Expressions and "if" Statements
Selection CIS 40 – Introduction to Programming in Python
Introduction to Programming with Python
Introduction to Programming with Python
Introduction to Programming with Python
Introduction to Programming with Python
Introduction to Programming with Python
Class 12.
Customizing Views Views Customize කර ගැනීම
Multiple Selections (ELIF Statements)
ETI Base Code ETI මුලධර්මයන් හදුනාගනිමු
Microsoft Azure Fundamentals Microsoft Azure මූලික දැනුම
මුහම්මද් සල්ලල්ලාහු අලෙයිහි වසල්ලම්
Basics of MVC MVC වල මුලික කොටස්
Practice with loops! What is the output of each function below?
Microsoft Azure Fundamentals Microsoft Azure මූලික දැනුම
Creating and Configuring Models Models create කිරීම සහ config කර ගැනීම
වී පොහොර සහනාධාර මුදල් ප්‍රදාන වැඩසටහන 2017/18 මහ කන්නය
Logical Operations In Matlab.
Customizing Controllers Controllers Customizing කර ගැනීම
Repetition In today’s lesson we will look at:
Introduction to Decision Structures and Boolean Variables
Conditional and iterative statements
‘Shifters’ in bilingual education : Problems and challenges of learning through a second language Marie Perera 1Department of Humanities Education, University.
Introduction to Programming with Python
Electoral system in Sri lanka
Introduction to Programming with Python
Class 13 function example unstring if if else if elif else
CS2011 Introduction to Programming I Selections (I)
Introduction to Programming with Python
Microsoft Azure Fundamentals Microsoft Azure මූලික දැනුම
Nate Brunelle Today: Conditional Decision Statements
Introduction to Programming with Python
Introduction to Programming with Python
මොඩියුල විශ්ලේෂණය Stored Procedure හඳුන්වා දීම.
Introduction to Programming with Python
Python Conditionals: The if statement
Control Flow statements
Lecture 5 – Unit 1 – Chatbots Python – More on Conditional statements
Flow Control I Branching and Looping.
Presentation transcript:

Introduction to Programming with Python Code එක මගින් සංකීර්ණ තීරණ ගැනීම Dileepa S. Rajapaksa | http://www.windowsgeek.lk @dsrajapaksa Microsoft Virtual Academy

කලින් Video එකේ අපි කතා කලේ මොනවාද? If condition භාවිතයෙන් තීරණ ගැනීම Coding ලිවීමේදී indentation තැබීම If Conditions භාවිතයෙන් සංඛ්‍යා සංසන්ඳනය Else conditions භාවිතය Boolean Variables භාවිතා කර ඒවා If conditions සමඟ යෙදීම Microsoft Virtual Academy

Demo කලින් Video එකේ අභියෝගයට පිළිතුරු Microsoft Virtual Academy

Code එකෙන් සංකීර්ණ තීරණ ගැනීම and/or, nested if, elif Last part Practice your language Microsoft Virtual Academy

සමහර වෙලාවට අවසාන තීරණයට බලපාන condition ගොඩක් තියෙන්න පුළුවන්. ඔබ ඉන්නේ එංගලන්තයේ නම් Hello කියන්න, ස්පාඤ්ඤයේ නම් Hola කියන්න, ප්‍රංශයේනම් bonjour කියන්න. ඔබට lottery එකක් ඇදුනොත් හා එහි ත්‍යාගය ඩොලර් මිලියනයට වැඩිනම් සුඛෝපභෝගී ජිවිතයක් ගත කරන්න. අද සඳුදා නම් පරීක්ෂා කර බලන්න නැවුම් කෝපි තියෙනවද නැතිද බව. නැතිනම් ලඟම තියෙන cafe එකට යන්න Microsoft Virtual Academy

If, then, otherwise… Microsoft Virtual Academy Last part Practice your language Microsoft Virtual Academy

ඔබ ඉන්නේ කැනඩාවේ නම් Hello කියන්න, ස්පාඤ්ඤයේ නම් Hola කියන්න, ප්‍රංශයේනම් bonjour කියන්න. මේක පොඩ්ඩක් වැදගත් අවස්ථාවක්.ඔයාලට එක Condition එකයි පරීක්ෂාකරන්න තියෙන්නේ.ඒත් ඒ එක Condition එකට විවිධ අගයන් තියෙන්න පුළුවන්. Microsoft Virtual Academy

වෙනස් අගයන් පරීක්ෂා කිරීමට “elif” මගින් පුළුවන්. country = input("Where are you from? " ) if country == "CANADA" : print("Hello") elif country == “SPAIN" : print(“Hola") elif country == "FRANCE" : print("Bonjour") බලන්න, elif statement එක indent වෙලා නැහැ !. “elif” කියන්නේ else if කියන එක කෙටියෙන්. Microsoft Virtual Academy

මොකද වෙන්නේ Japan හෝ Italy ඇතුලත් කලොත්. අපි දක්වා නැති conditions පරීක්ෂා කිරීමට අපි else statement එකක් යෙදිය යුතුයි. country = input("Where are you from? " ) if country == "CANADA" : print("Hello") elif country == “SPAIN" : print(“Hola") elif country == "FRANCE" : print("Bonjour") else : print("Aloha/Ciao/G’Day") Microsoft Virtual Academy

Demo elif Last part Practice your language Microsoft Virtual Academy

Conditions කිහිපයක් සම්බන්ධ කිරීම Last part Practice your language Microsoft Virtual Academy

ඔබට lottery එකක් ඇදුනොත් සහ ත්‍යාගය මිලියනයට වැඩිනම් ඔබට lottery එකක් ඇදුනොත් සහ ත්‍යාගය මිලියනයට වැඩිනම්.විශ්‍රාම ගිහින් luxury ජීවිතයක් ගෙවන්න. සමහර වෙලාවට ඊළඟ පියවර කරනවාද නැද්ද කියන එක සාධක ගොඩක් මත රඳාපවතිනවා. මම Lottery එකක් දින්නොත්, ඒත් දීනේ ඩොලර් 5යි නම් , විශ්‍රාම ගන්න බැහැ. Lottery එකේ ත්‍යාගය ඩොලර් මිලියනයයි එහෙත් මම දිනලා නැත්නම් , මට විශ්‍රාම ගන්න බැහැ. මට විශ්‍රාම ගන්න පුළුවන් මම Lottery එක දිනලා නම් සහ ( and ) ත්‍යාගය ඩොලර් මිලියනයක් නම් පමණයි. Microsoft Virtual Academy

ඔබ “and” භාවිතා කරනවිට ඔබ කියන්නේ සියළුම conditions සත්‍ය වියයුතු බව. Microsoft Virtual Academy

AND වලින් ලැබෙන පිළිතුර සත්‍ය යැයි සලකන්නේ අවස්තා දෙකම සත්‍ය වූ විට පමණි #Imagine you have code that ran earlier which #set these two variables wonLottery = True bigWin = True #print statement only executes if both conditions are true if wonLottery and bigWin : print("you can retire") Microsoft Virtual Academy

AND භාවිතයේ විය හැකි සියළුම අවස්ථා if firstCondition and secondCondition : First Condition is Second Condition is Statement is True False Microsoft Virtual Academy

Demo and Last part Practice your language Microsoft Virtual Academy

සමහර අවස්ථා වල අපිට සිදුවෙනවා එක් සිද්ධියක් පමණක් සත්‍ය වූ විට යමක් කරන්න. දිනය සෙනසුරාදා හෝ ( or ) ඉරීදානම් මට නිදාගන්න පුළුවන්. වහිනවානම් හෝ ( or ) අහස කළුකරලා නම් Bike එකේ වැඩට යන්න එපා. Microsoft Virtual Academy

ඔබ “or” භාවිතා කරනවා නම් එයින් කියන්නේ condition එකක් හෝ සත්‍යනම් අදාල කොටස් කරන්න කියන එකයි. Microsoft Virtual Academy

OR වලින් ලැබෙන පිළිතුර සත්‍යවන්නේ දෙපසින් එක් හෝ අවස්තාවක් තෘප්ත වනවානම් පමණි. අවස්තා දෙකම සත්‍යවීම අනිවාර්යය නොවේ. #Imagine you have code that ran earlier which #set these two variables saturday = True sunday = False #print statement executes if either condition is true if saturday or sunday : print("you can sleep in") Microsoft Virtual Academy

විය හැකි සියළුම අවස්ථා if firstCondition or secondCondition : First Condition is Second Condition is Statement is True False Microsoft Virtual Academy

Demo or Last part Practice your language Microsoft Virtual Academy

ඔයාට එක if statement එකක් ඇතුලේ “and”සහ “or” කිහිපයක් භාවිතාකරන්න පුළුවන් if month == "Sep" or month =="Apr" \ or month == "Jun" or month == "Nov" : print("There are 30 days in this month") if favMovie == "Star Wars" \ and favBook == "Lord of the Rings" \   and favEvent == "ComiCon" :     print("You and I should hang out") Microsoft Virtual Academy

This one doesn’t seem to work the way you would expect! ඔයාට එක statement එකක් ඇතුලේ “and” සහ “or” කිහිපයක් සම්බඳ කරන්න පුළුවන් if country == "CANADA" and \ pet == "MOOSE" or  pet == "BEAVER" :     print("Do you play hockey too?") මේ වෙනස් අවස්ථා සියල්ලම සලකා බලන්න Country = CANADA, Pet = MOOSE Country = CANADA, Pet = BEAVER Country = VIETNAM, Pet = MOOSE Country = VIETNAM, Pet = BEAVER This one doesn’t seem to work the way you would expect! Microsoft Virtual Academy

Demo and/or සම්බන්ධ කිරීම Microsoft Virtual Academy Last part Practice your language Microsoft Virtual Academy

ගණිත කර්ම කිරීමේ අනුපිළිවල පාසලේදී ඉගෙනගත්තා මතකද? 8+5*2=? වැඩිකිරීම හා බෙදීම , එකතුකිරීම හා අඩුකිරීමට කලින් සිදුවෙනවා. 8+5*2 = 18 Microsoft Virtual Academy

“and”/”or” ක්‍රියාත්මක වීමටත් පිළිවලක් තියෙනවා “and” මුලින් සලකා බලනවා. if country == "CANADA" and pet == "MOOSE" \   or  pet == "BEAVER" :     print("Do you play hockey too?") Microsoft Virtual Academy

ගණිතයේදී කොහොමද වැඩි කිරීමට කලින් එකතු කිරීම කරගන්නේ? වරහන් භාවිතයෙන්! (8+5)*2 = 26 Microsoft Virtual Academy

“and” වලට කලින් “or” ක්‍රියාත්මක කිරීම සඳහා වරහන් භාවිතා කරන්න පුළුවන්. if country == "CANADA" and \  (pet == "MOOSE" or  pet == "BEAVER") :     print("Do you play hockey too") එකම statement එකක and/or දෙකම භාවිතා කරලා තියෙන වෙලාවක ප්‍රතිඵලය ගැන සැකයක් තියෙනවානම් වරහන් යොදන්න. එ විට Code එක කියවීමට පහසු වනු ඇති අතර වැරදි සිදුවීමේ ඉඩ ප්‍රස්ථාව අඩුවනු ඇත… Microsoft Virtual Academy

සමහර වෙලාවන් වල conditions කිහිපයක් පරීක්ෂා කිරීමට “and”/”or” පමණක් භාවිතයෙන් වැඩක් වෙන්නේ නැහැ. මේ ප්‍රශ්ණය code එකේන් මගහැරගන්නේ කොහොමද? සඳුදා නම් කෝපි තියනවාදැයි බලන්න. කෝපි නැත්නම් ළඟම තියෙන cafe එකට යන්න. මෙම අවස්ථාවේදී ඔබට එක condition එකක් පරීක්ෂා කරන්න වෙනවා.ඒක සත්‍යනම් ඔබට තවත් condition එකක් පරීක්ෂා කරලා බලන්න වෙනවා. Microsoft Virtual Academy

Nested if statements Microsoft Virtual Academy Last part Practice your language Microsoft Virtual Academy

ඔයාලට if statement එකක් ඇතුලේ එකක් nest කරන්න පුළුවන් monday = True freshCoffee = False if monday :      #you could have code here to check for fresh coffee      # the if statement is nested, so this if statement      # is only executed if the other if statement is true     if not freshCoffee :         print("go buy a coffee!")   print("I hate Mondays") print("now you can start work") ඔබ සැලකිලිමත් වියයුතුයි code එක indent වෙලා තියෙන විදිහ පිළිබඳව ඒ indent කිරීම මගින් එක් එක් if statement එකට අදාල code එක තීරණය වෙනවා. Microsoft Virtual Academy

Demo Nested if statements Microsoft Virtual Academy Last part Practice your language Microsoft Virtual Academy

ඔබේ අභියෝගය… ශ්‍රී ලංකාවේ online store එකකින් ඇණවුම් කරපු දේකට මුළු වියදම ගණනය කරන්න. ඇණවුමේ මුළු වටිනාකම සහ user ගේ රට පිළිව්බඳ විස්තර ... ගෙන් ලබාගන්න. User ශ්‍රී ලංකාවේ නම් පළාත කුමක්ද? user ගෙන් ලබාගන්න. ඇණවුම ශ්‍රී ලංකාව පිටතින් නම් කිසිඳු බද්ධක් අය නොකර සිටින්න. ඇණවුම ශ්‍රී ලංකාවේ නම් බදු පහත පරිදි ගණනය කරන්න. බස්නාහිර පළාත 2% General sales Tax (GST) දකුණු පළාත, නැගෙනහිර පළාත, මධ්‍යම පළාත් වලට අයකිරීම 4% වශයෙන් අය කරන්න. අනෙකුත් සෑම පලාතක් සඳහාම 5%ක් සහ 1% GST වශයෙන් එකතුකරන්න. බදු සමග එකතුව userට ඉදිරිපත් කරන්න. Microsoft Virtual Academy

Code එක පරීක්ෂාකිරීම… Code එක හරියටම වැඩ කරනවාද බලාගන්නේ කොහොමද? ශ්‍රී ලංකාවෙන් පිට කෙනෙකුට (බදු නැත ) බස්නාහිර පළාතේ කෙනෙකුට (බදු 2%) දකුණු පළාත, නැගෙනහිර පළාත, මධ්‍යම පළාත් වල කෙනෙකුට (බදු 4%) අනෙකුත් සෑම පලාතක කෙනෙක් සඳහා ( උදා: උතුරු මැදපළාත) (බදු 6%) Microsoft Virtual Academy

සාරාංශය… If condition භාවිතයෙන් සංකීර්ණ තීරණ ගැනීම elif භාවිතා කර අනෙකුත් අවස්තාවන් හිදී තීරණ ගැනීම විවිධ අවස්ථාවන් හිදී Conditions කීපයක් සැසදීම AND , OR භාවිතය Nested if statements භාවිතා කිරීම Microsoft Virtual Academy

සුභ පැතුම් ! ඔබට දැන් ඉතා සංකීර්ණ තීරණ ගැනීමට සුදුසු වැඩසටහනක් නිර්මාණය කරන්න පුලුවන්. Microsoft Virtual Academy