Python Programming Language

Slides:



Advertisements
Similar presentations
The If Then Else Statement Using JavaScript TIP The If Statement is the fundamental control structure that allows branches in the flow of control in a.
Advertisements

Making Decisions in Python Sec 9-10 Web Design. Objectives The student will: Understand how to make a decision in Python Understand the structure of an.
Python Programming Language
Conditional statements and Boolean expressions. The if-statement in Java (1) The if-statement is a conditional statement The statement is executed only.
Conditional Statements Introduction to Computing Science and Programming I.
Week 10 Recap CSE 115 Spring For-each loop When we have a collection and want to do something to all elements of that collection we use the for-each.
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.
ECE122 L7: Conditional Statements February 20, 2007 ECE 122 Engineering Problem Solving with Java Lecture 7 Conditional Statements.
Tutorial 4 Decision Making with Control Structures and Statements Section A - Decision Making JavaScript Tutorial 4 -Decision Making with Control.
Decision Making George Mason University. Today’s topics 2 Review of Chapter 2: Decision Making Go over exercises Decision making in Python.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Geography 465 Assignments, Conditionals, and Loops.
INTRODUCTION TO PYTHON PART 3 - LOOPS AND CONDITIONAL LOGIC CSC482 Introduction to Text Analytics Thomas Tiahrt, MA, PhD.
Chapter 5 Conditionals and Loops. © 2004 Pearson Addison-Wesley. All rights reserved2/33 Conditionals and Loops Now we will examine programming statements.
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.
Programming in Java (COP 2250) Lecture 11 Chengyong Yang Fall, 2005.
Lecture 10 – Boolean expressions, if statements COMPSCI 101 Principles of Programming.
Conditions. Objectives  Understanding what altering the flow of control does on programs and being able to apply thee to design code  Look at why indentation.
If…else statements. Boolean Expressions Boolean expression - An expression whose value is either true or false true = 1 false = 0 Datatype: boolean.
1 2. Program Construction in Java. 2.4 Selection (decisions)
15-Nov-15 All the Operators. operators.ppt 2 Precedence An operator with higher precedence is done earlier (precedes) one with lower precedence A higher.
Python Conditionals chapter 5
Flow of Control Unless indicated otherwise, the order of statement execution through a method is linear: one after the other in the order they are written.
Boolean Expressions and Logical Operators CSE 1310 – Introduction to Computers and Programming Alexandra Stefan University of Texas at Arlington Credits:
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)
Today… Operators, Cont. Operator Precedence Conditional Statement Syntax. Winter 2016CISC101 - Prof. McLeod1.
CS 115 Lecture 8 Conditionals, if statements Taken from notes by Dr. Neil Moore.
IST 210: PHP Logic IST 210: Organization of Data IST2101.
 Type Called bool  Bool has only two possible values: True and False.
JavaScript Controlling the flow of your programs with ‘if’ statements
Control Flow (Python) Dr. José M. Reyes Álamo.
Control Structures I Chapter 3
CS 115 Lecture Conditionals and if statements
COMP 14 Introduction to Programming
Selection (also known as Branching) Jumail Bin Taliba by
Making Choices with if Statements
WELCOME to….. The If Statement.
Debugging and Random Numbers
Operator Precedence Operators Precedence Parentheses () unary
Computer Programming If Statements Trade & Industrial Education
Topics The if Statement The if-else Statement Comparing Strings
If, else, elif.
The order in which statements are executed is called the flow of control. Most of the time, a running program starts at the first programming statement,
Conditionals & Boolean Expressions
Arithmetic operations, decisions and looping
And now for something completely different . . .
Computers & Programming Languages
All the Operators 22-Nov-18.
Types, Truth, and Expressions (Part 2)
Coding Concepts (Sub- Programs)
All the Operators 4-Dec-18.
Types, Truth, and Expressions (Part 2)
Conditions and Boolean Expressions
Java Programming Control Structures Part 1
Summary Two basic concepts: variables and assignments Basic types:
Types, Truth, and Expressions (Part 2)
SE1H421 Procedural Programming LECTURE 4 Operators & Conditionals (1)
Logic of an if statement
Chapter 3: Selection Structures: Making Decisions
Boolean Expressions to Make Comparisons
All the Operators 6-Apr-19.
Relational Operators.
All the Operators 13-Apr-19.
CHAPTER 5: Control Flow Tools (if statement)
Javascript Chapter 19 and 20 5/3/2019.
Conditionals and Loops
Controlling Program Flow
Types, Truth, and Expressions (Part 2)
Presentation transcript:

Python Programming Language

Control Flow - Comparators There are 6 Comparators: - Equal to (==) Not equal to (!=) Less than (<) Less than or equal to (<=) Greater than (>) Greater than or equal to (>= Note that == is used to compare whether two things are equal, and = is used to assign a value to a variable

Control Flow - Comparators bool_one = True bool_two = True bool_three = False bool_four = False bool_five = False

Boolean operators. and, or, not Boolean operators (or logical operators) are words used to connect Python statements in a grammatically correct way—almost exactly as in regular English. There are three boolean operators in Python: and, which means the same as it does in English; or, which means "one or the other or BOTH" (it's not exclusively one or the other); not, which means the same as it does in English. Boolean operators result in Boolean values—True or False. 

The AND boolean operator The boolean operator AND only results in True when the expressions on either side of AND are both true. An expression is any statement involving one or more variables and operators (arithmetic, logical, or boolean). For instance: 1 < 2 and 2 < 3 results in True because it is true that one is less than two and that two is less than three. 1 < 2 and 2 > 3 results in False because it is not true that both statements are true—one is less than two, but two is not greater than three.

The OR boolean operator The boolean operator OR only returns True when either (meaning one, the other or both!) of the expressions on each side of OR are true. (It's only False when both expressions are False.) For example: 1 < 2 or 2 > 3 is True, even though two is not greater than three; 1 > 2 or 2 > 3 is False, because it is neither the case that one is greater than two nor that two is greater than three.

The NOT boolean operator The boolean operator NOT returns True for false statements and False for true statements. Remember, the only two boolean values are True and False! For example: not False will evaluate to True, as will not 40 > 41. Applying not to expressions that would otherwise be true makes them False

This and That (or This, But Not That!) Boolean operators can be chained together! It's important to know that boolean operators are not evaluated straight across from left to right all the time; just like with arithmetic operators, where / and * are evaluated before + and -  There is an order of precedence or order of operations for boolean operators. The order is as follows: not is evaluated first; and is evaluated next; or is evaluated last. This order can be changed by including parentheses (()). Anything in parentheses is evaluated as its own unit.

Conditional Statement Syntax Remember whitespace in Python is significant? In JavaScript, the block of code an ifstatement executes is bound by curly braces ({}). In Python, whitespace (tabs or spaces) does this work for us. Here's an example of if statement syntax in Python: if 8 < 9: print "Eight is less than nine!" if is always followed by an expression, which is followed by a colon (:). The code block (the code to be executed if the expression evaluates to True) is indented four spaces. This is also true for elif and else(which we'll get to in a moment). The full syntax would look something like this: if 8 < 9: print "I get printed!" elif 8 > 9: print "I don't get printed." else: print "I also don't get printed”

Else conditional statement The else statement in Python is the complement to the if statement. While an if statement will return control of the program to the next line of code after the if code block even if there's no else statement, it's considered a good habit to pair each if with an else. An if/else pair says to Python: "If this expression is true, run this indented code block; otherwise, run this code after the else statement." else is always written alone. Unlike if, else should have nothing after it except a colon.

Elif conditional statement "Elif" is short for "else if." It means exactly what it sounds like: "otherwise, if the following code is true, do this!"

Here's what you've learned in this unit: Basics of control flow; Comparators (such as >, <, and==); Boolean operators (and, or, and not); And conditional statements (if, else, and elif).

And finally def teachers_question(): print "You've just spent some time learning Conditionals and Control Flow in Python" print "Did you enjoy it and learn a lot?" answer = raw_input("Type Yes or No and hit 'Enter'.").lower() if answer == "yes" or answer == "y": print "Brilliant, I'm glad you enjoyed it" elif answer == "no" or answer == "n": print "I'm sorry to hear that. Perhaps you'll need to start again and have another go!" else: print "You didn't pick Yes or No! Try again." teachers_question()