SlideShare a Scribd company logo
1 of 19
Creative Commons License
This work is licensed under a Creative Commons Attribution 4.0 International License.
BS GIS Instructor: Inzamam Baig
Lecture 3
Fundamentals of Programming
Boolean Expressions
A Boolean expression is an expression that is either true or
false
>>> 5 == 5
True
>>> 5 == 6
False
True and False are special values that belong to the class bool
Boolean Expressions
>>> type(True)
>>> type(False)
x != y # x is not equal to y
x > y # x is greater than y
x < y # x is less than y
x >= y # x is greater than or equal to y
x <= y # x is less than or equal to y x is y # x is the
same as y
x is not y # x is not the same as y
Logical Operators
There are three logical operators: and, or, and not
x > 0 and x < 10
is true only if x is greater than 0 and less than 10
n%2 == 0 or n%3 == 0
is true if either of the conditions is true
not (x > y)
is true if x > y is false;that is, if x is less than or equal to y
>>> 17 and True
>>> bool(-2)
>>> bool(0)
>>> bool('a ')
Conditional Execution
To check conditions and change the behavior of the program
accordingly, Conditional statements are used
if x > 0 :
print('x is positive')
If Condition
There is no limit on the number of statements that can appear in
the body, but there must be at least one.
You can use the pass statement, which does nothing
if x < 0 :
pass # need to handle negative values!
If Condition in Interactive Mode
>>> x = 3
>>> if x < 10:
... print('Small')
... Small
>>>
Alternative Execution
A second form of the if statement is alternative execution
there are two possibilities and the condition determines which
one gets executed
if x%2 == 0 :
print('x is even')
else :
print('x is odd')
Chained Conditions
Sometimes there are more than two possibilities and we need more
than two branches.
One way to express a computation like that is a chained conditional
if x < y:
print('x is less than y')
elif x > y:
print('x is greater than y')
else:
print('x and y are equal')
Chained Conditions
if choice == 'a':
print('Bad guess')
elif choice == 'b':
print('Good guess')
elif choice == 'c':
print('Close, but not correct')
Nested Conditionals
One conditional can also be nested within another.
if x == y:
print('x and y are equal')
else:
if x < y:
print('x is less than y')
else:
print('x is greater than y')
if 0 < x:
if x < 10:
print('x is a positive single-digit number.')
if 0 < x and x < 10:
print('x is a positive single-digit number.')
Debugging
The traceback Python displays when an error occurs contains a
lot of information, but it can be overwhelming.
The most useful parts are usually:
• What kind of error it was, and
• Where it occurred.
>>> x = 5
>>> y = 6
File "", line 1
y = 6
^
IndentationError: unexpected indent

More Related Content

Similar to Python Lecture 3

Control structures in C++ Programming Language
Control structures in C++ Programming LanguageControl structures in C++ Programming Language
Control structures in C++ Programming LanguageAhmad Idrees
 
Conditional_and_Recursion.pptx
Conditional_and_Recursion.pptxConditional_and_Recursion.pptx
Conditional_and_Recursion.pptxYagna15
 
Control Structures, If..else, switch..case.pptx
Control Structures, If..else, switch..case.pptxControl Structures, If..else, switch..case.pptx
Control Structures, If..else, switch..case.pptxdoncreiz1
 
Python unit 2 M.sc cs
Python unit 2 M.sc csPython unit 2 M.sc cs
Python unit 2 M.sc csKALAISELVI P
 
GE3151 PSPP UNIT III QUESTION BANK.docx.pdf
GE3151 PSPP UNIT III QUESTION BANK.docx.pdfGE3151 PSPP UNIT III QUESTION BANK.docx.pdf
GE3151 PSPP UNIT III QUESTION BANK.docx.pdfAsst.prof M.Gokilavani
 
Javascript comparison and logical operators
Javascript comparison and logical operatorsJavascript comparison and logical operators
Javascript comparison and logical operatorsJesus Obenita Jr.
 
Introduction to limits
Introduction to limitsIntroduction to limits
Introduction to limitsTyler Murphy
 
Ch5 Selection Statements
Ch5 Selection StatementsCh5 Selection Statements
Ch5 Selection StatementsSzeChingChen
 
Absolute Java 5e Savitch Test Bank
Absolute Java 5e Savitch Test BankAbsolute Java 5e Savitch Test Bank
Absolute Java 5e Savitch Test BankLauriewest24
 
Week8finalexamlivelecture april2012
Week8finalexamlivelecture april2012Week8finalexamlivelecture april2012
Week8finalexamlivelecture april2012Brent Heard
 
Week8finalexamlivelecture dec2012
Week8finalexamlivelecture dec2012Week8finalexamlivelecture dec2012
Week8finalexamlivelecture dec2012Brent Heard
 
[ITP - Lecture 09] Conditional Operator in C/C++
[ITP - Lecture 09] Conditional Operator in C/C++[ITP - Lecture 09] Conditional Operator in C/C++
[ITP - Lecture 09] Conditional Operator in C/C++Muhammad Hammad Waseem
 

Similar to Python Lecture 3 (20)

Control structures in C++ Programming Language
Control structures in C++ Programming LanguageControl structures in C++ Programming Language
Control structures in C++ Programming Language
 
Conditional_and_Recursion.pptx
Conditional_and_Recursion.pptxConditional_and_Recursion.pptx
Conditional_and_Recursion.pptx
 
Control Structures, If..else, switch..case.pptx
Control Structures, If..else, switch..case.pptxControl Structures, If..else, switch..case.pptx
Control Structures, If..else, switch..case.pptx
 
Python unit 2 M.sc cs
Python unit 2 M.sc csPython unit 2 M.sc cs
Python unit 2 M.sc cs
 
GE3151 PSPP UNIT III QUESTION BANK.docx.pdf
GE3151 PSPP UNIT III QUESTION BANK.docx.pdfGE3151 PSPP UNIT III QUESTION BANK.docx.pdf
GE3151 PSPP UNIT III QUESTION BANK.docx.pdf
 
Javascript comparison and logical operators
Javascript comparison and logical operatorsJavascript comparison and logical operators
Javascript comparison and logical operators
 
Introduction to limits
Introduction to limitsIntroduction to limits
Introduction to limits
 
Ch5 Selection Statements
Ch5 Selection StatementsCh5 Selection Statements
Ch5 Selection Statements
 
Ch05.pdf
Ch05.pdfCh05.pdf
Ch05.pdf
 
Selection
SelectionSelection
Selection
 
ICP - Lecture 7 and 8
ICP - Lecture 7 and 8ICP - Lecture 7 and 8
ICP - Lecture 7 and 8
 
Ch05-converted.pptx
Ch05-converted.pptxCh05-converted.pptx
Ch05-converted.pptx
 
04 Conditions.pptx
04 Conditions.pptx04 Conditions.pptx
04 Conditions.pptx
 
Fekra c++ Course #2
Fekra c++ Course #2Fekra c++ Course #2
Fekra c++ Course #2
 
Absolute Java 5e Savitch Test Bank
Absolute Java 5e Savitch Test BankAbsolute Java 5e Savitch Test Bank
Absolute Java 5e Savitch Test Bank
 
Java Programmin: Selections
Java Programmin: SelectionsJava Programmin: Selections
Java Programmin: Selections
 
Php
PhpPhp
Php
 
Week8finalexamlivelecture april2012
Week8finalexamlivelecture april2012Week8finalexamlivelecture april2012
Week8finalexamlivelecture april2012
 
Week8finalexamlivelecture dec2012
Week8finalexamlivelecture dec2012Week8finalexamlivelecture dec2012
Week8finalexamlivelecture dec2012
 
[ITP - Lecture 09] Conditional Operator in C/C++
[ITP - Lecture 09] Conditional Operator in C/C++[ITP - Lecture 09] Conditional Operator in C/C++
[ITP - Lecture 09] Conditional Operator in C/C++
 

More from Inzamam Baig

More from Inzamam Baig (13)

Python Lecture 8
Python Lecture 8Python Lecture 8
Python Lecture 8
 
Python Lecture 13
Python Lecture 13Python Lecture 13
Python Lecture 13
 
Python Lecture 12
Python Lecture 12Python Lecture 12
Python Lecture 12
 
Python Lecture 11
Python Lecture 11Python Lecture 11
Python Lecture 11
 
Python Lecture 10
Python Lecture 10Python Lecture 10
Python Lecture 10
 
Python Lecture 9
Python Lecture 9Python Lecture 9
Python Lecture 9
 
Python Lecture 7
Python Lecture 7Python Lecture 7
Python Lecture 7
 
Python Lecture 6
Python Lecture 6Python Lecture 6
Python Lecture 6
 
Python Lecture 5
Python Lecture 5Python Lecture 5
Python Lecture 5
 
Python Lecture 4
Python Lecture 4Python Lecture 4
Python Lecture 4
 
Python Lecture 2
Python Lecture 2Python Lecture 2
Python Lecture 2
 
Python Lecture 1
Python Lecture 1Python Lecture 1
Python Lecture 1
 
Python Lecture 0
Python Lecture 0Python Lecture 0
Python Lecture 0
 

Recently uploaded

Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Disha Kariya
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAssociation for Project Management
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajanpragatimahajan3
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Celine George
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpinRaunakKeshri1
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room servicediscovermytutordmt
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 

Recently uploaded (20)

Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajan
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpin
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room service
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 

Python Lecture 3

  • 1. Creative Commons License This work is licensed under a Creative Commons Attribution 4.0 International License. BS GIS Instructor: Inzamam Baig Lecture 3 Fundamentals of Programming
  • 2. Boolean Expressions A Boolean expression is an expression that is either true or false >>> 5 == 5 True >>> 5 == 6 False True and False are special values that belong to the class bool
  • 3. Boolean Expressions >>> type(True) >>> type(False) x != y # x is not equal to y x > y # x is greater than y x < y # x is less than y x >= y # x is greater than or equal to y x <= y # x is less than or equal to y x is y # x is the same as y x is not y # x is not the same as y
  • 4. Logical Operators There are three logical operators: and, or, and not x > 0 and x < 10 is true only if x is greater than 0 and less than 10 n%2 == 0 or n%3 == 0 is true if either of the conditions is true not (x > y) is true if x > y is false;that is, if x is less than or equal to y
  • 5. >>> 17 and True >>> bool(-2) >>> bool(0) >>> bool('a ')
  • 6. Conditional Execution To check conditions and change the behavior of the program accordingly, Conditional statements are used if x > 0 : print('x is positive')
  • 7.
  • 8. If Condition There is no limit on the number of statements that can appear in the body, but there must be at least one. You can use the pass statement, which does nothing if x < 0 : pass # need to handle negative values!
  • 9. If Condition in Interactive Mode >>> x = 3 >>> if x < 10: ... print('Small') ... Small >>>
  • 10. Alternative Execution A second form of the if statement is alternative execution there are two possibilities and the condition determines which one gets executed if x%2 == 0 : print('x is even') else : print('x is odd')
  • 11.
  • 12. Chained Conditions Sometimes there are more than two possibilities and we need more than two branches. One way to express a computation like that is a chained conditional if x < y: print('x is less than y') elif x > y: print('x is greater than y') else: print('x and y are equal')
  • 13. Chained Conditions if choice == 'a': print('Bad guess') elif choice == 'b': print('Good guess') elif choice == 'c': print('Close, but not correct')
  • 14.
  • 15. Nested Conditionals One conditional can also be nested within another. if x == y: print('x and y are equal') else: if x < y: print('x is less than y') else: print('x is greater than y')
  • 16. if 0 < x: if x < 10: print('x is a positive single-digit number.') if 0 < x and x < 10: print('x is a positive single-digit number.')
  • 17.
  • 18. Debugging The traceback Python displays when an error occurs contains a lot of information, but it can be overwhelming. The most useful parts are usually: • What kind of error it was, and • Where it occurred.
  • 19. >>> x = 5 >>> y = 6 File "", line 1 y = 6 ^ IndentationError: unexpected indent

Editor's Notes

  1. Any nonzero number is interpreted as “true.”
  2. The boolean expression after the if statement is called the condition. We end the if statement with a colon character (:) and the line(s) after the if statement are indented
  3. If the logical condition is true, then the indented statement gets executed. If the logical condition is false, the indented statement is skipped. Statements like this are called compound statements because they stretch across more than one line
  4. In interactive mode prompt will change from three chevrons to three dots to indicate you are in the middle of a block of statements
  5. Since the condition must either be true or false, exactly one of the alternatives will be executed. The alternatives are called branches, because they are branches in the flow of execution
  6. There is no limit on the number of elif statements. If there is an else clause, it has to be at the end, but there doesn’t have to be one
  7. Each condition is checked in order. If the first is false, the next is checked, and so on. If one of them is true, the corresponding branch executes, and the statement ends. Even if more than one condition is true, only the first true branch executes.
  8. The print statement is executed only if we make it past both conditionals, so we can get the same effect with the and operator
  9. In this example, the problem is that the second line is indented by one space. But the error message points to y, which is misleading. In general, error messages indicate where the problem was discovered, but the actual error might be earlier in the code, sometimes on a previous line