And now for something completely different . . .

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

 2000 Prentice Hall, Inc. All rights reserved. Chapter 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line.
Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a.
1 9/15/06CS150 Introduction to Computer Science 1 Combined Assignments, Relational Operators, and the If Statement.
School of Computing Science CMT1000 Ed Currie © Middlesex University 1 CMT1000: Introduction to Programming Ed Currie Lecture 5B: Branch Statements - Making.
Chapter 4: Basic C Operators
The University of Texas – Pan American
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 4 Decision Structures and Boolean Logic.
Decision Structures and Boolean Logic
Programming Fundamentals. Today’s lecture Decisions If else …… Switch Conditional Operators Logical Operators.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C 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.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 4 Decision.
Pseudocode When designing an ALGORITHM to solve a problem, Pseudocode, can be used. –Artificial, informal language used to develop algorithms –Similar.
Lecture 2: Introduction to C Programming. OBJECTIVES In this lecture you will learn:  To use simple input and output statements.  The fundamental data.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 5: Introduction to C: More Control Flow.
1 Compound Assignment C++ has a large set of operators for applying an operation to an object and then storing the result back into the object Examples.
Python Conditionals chapter 5
Principles of Programming Chapter 4: Basic C Operators  In this chapter, you will learn about:  Arithmetic operators  Unary operators  Binary operators.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
Principles of Programming - NI July Chapter 4: Basic C Operators In this chapter, you will learn about: Assignment operators Arithmetic operators.
GCSE COMPUTER SCIENCE Practical Programming using Python Lesson 4 - Selection.
Fundamentals of Programming I Overview of Programming
Control Structures I Chapter 3
Chapter 3 Selection Statements
GCSE COMPUTER SCIENCE Practical Programming using Python
Chapter 10 Programming Fundamentals with JavaScript
CIS3931 – Intro to JAVA Lecture Note Set 2 17-May-05.
Introduction to Python
Selection (also known as Branching) Jumail Bin Taliba by
Chapter 2 - Introduction to C Programming
Topic: Conditional Statements – Part 2
Data Types, Identifiers, and Expressions
Expressions An expression is a portion of a C++ statement that performs an evaluation of some kind Generally requires that a computation or data manipulation.
IF statements.
Variables A piece of memory set aside to store data
The Selection Structure
CSC113: Computer Programming (Theory = 03, Lab = 01)
Topics The if Statement The if-else Statement Comparing Strings
Chapter 2 - Introduction to C Programming
Control Structures – Selection
And now for something completely different . . .
Chapter 10 Programming Fundamentals with JavaScript
Topics The if Statement The if-else Statement Comparing Strings
Selection CIS 40 – Introduction to Programming in Python
Data Types, Identifiers, and Expressions
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
Bools and simple if statements
Introduction to C++ Programming
Relational Operators Operator Meaning < Less than > Greater than
Chapter 2 - Introduction to C Programming
Chapter 3: Introduction to Problem Solving and Control Statements
3 Control Statements:.
Chapter 2 - Introduction to C Programming
Summary Two basic concepts: variables and assignments Basic types:
Comparing Strings Strings can be compared using the == and != operators String comparisons are case sensitive Strings can be compared using >, =, and.
Expressions An expression is a portion of a C++ statement that performs an evaluation of some kind Generally requires that a computation or data manipulation.
Chapter 2 Programming Basics.
Boolean Expressions to Make Comparisons
Chapter 2 - Introduction to C Programming
CHAPTER 5: Control Flow Tools (if statement)
Winter 2019 CISC101 4/28/2019 CISC101 Reminders
DATA TYPES There are four basic data types associated with variables:
Introduction to C Programming
Introduction to Python
PYTHON - VARIABLES AND OPERATORS
Presentation transcript:

And now for something completely different . . . 20-Nov-18 AHD c 2010

Part 4 More on Data Processing and Introducing Selection 20-Nov-18 AHD c 2010

Python Data Processing and Selection 4.1 Python Program Structure 4.2 Memory Concepts 4.3 Arithmetic Expression Operators 4.4 Relational and Logical Operators 4.5 Selection 4.6 De Morgan’s Laws 20-Nov-18 AHD c 2010

Python Data Processing and Selection 4.1 Python Program Structure 4.2 Memory Concepts 4.3 Arithmetic Expression Operators 4.4 Relational and Logical Operators 4.5 Selection 4.6 De Morgan’s Laws 20-Nov-18 AHD c 2010

1. Programs are composed of modules Python Program Structure 1. Programs are composed of modules 2. Modules contain statements 3. Statements contain expressions 4. Expressions create and process objects 20-Nov-18 AHD c 2010

Python Data Processing and Selection 4.1 Python Program Structure 4.2 Memory Concepts 4.3 Arithmetic Expression Operators 4.4 Relational and Logical Operators 4.5 Selection 4.6 De Morgan’s Laws 20-Nov-18 AHD c 2010

Memory Concepts in Python A variable is a name that refers to a value. The assignment statement creates new variables and gives them values: number1 = input("Enter first number:\n") 20-Nov-18 AHD c 2010

Variables refer to Objects Variable names such as number1 actually refer to Python objects. Every object has a data type (e.g. int), a size (e.g. 4 bytes), a value (e.g. 10) and a location in the computer's memory. . . 20-Nov-18 AHD c 2010

Creation of an object number1 = input("Enter first number:\n") Python first creates an object to hold the user-entered string and places the object into a memory location "10" 20-Nov-18 AHD c 2010

Creation of an object number1 "10" number1 = input("Enter first number:\n") The assignment symbol (=) then associates the name number1 with the newly created object. number1 "10" 20-Nov-18 AHD c 2010

A program cannot change an object's type or location In Python, a program cannot change an object's type or location. 20-Nov-18 AHD c 2010

A program cannot change an object's type or location In Python, a program cannot change an object's type or location. Only with a small selection of object types can the value of the object be changed. . . 20-Nov-18 AHD c 2010

These objects are said to be immutable. Python program statements cannot change the value of a number or string object. These objects are said to be immutable. 20-Nov-18 AHD c 2010

Creating a string object 10 is entered number1 = input("Enter first number:\n") print (number1, type(number1)) The output is: 10 <class 'str'> number1 "10" 04-01.py 20-Nov-18 AHD c 2010

Converting from one data type to another number1 = input("Enter first number:\n") print (number1, type(number1)) The int function creates a new object to store the integer value of the string entered in the first line. number1 = int(number1) number1 "10" 10 20-Nov-18 AHD c 2010

Converting from one data type to another number1 = input("Enter first number:\n") print (number1, type(number1)) number1 = int(number1) The new object has a different address which is then associated with the variable name number1 . number1 "10" 10 20-Nov-18 AHD c 2010

Converting from one data type to another number1 = input("Enter first number:\n") print (number1, type(number1)) Once an object no longer has a reference to it, its memory is released. number1 = int(number1) print (number1, type(number1)) number1 "10" 10 04-02.py 20-Nov-18 AHD c 2010

Converting from one data type to another number1 = input("Enter first number:\n") print (number1, type(number1)) number1 = int(number1) print (number1, type(number1)) Once an object no longer has a reference to it, its memory is released. number1 10 20-Nov-18 AHD c 2010

Displaying an objects memory location number1 = input("Enter first number:\n") print (number1, type(number1), id(number1)) number1 = int(number1) print (number1, type(number1) ), id(number1)) A representation of the memory location of an object can be obtained by using the id function. 04-03.py 20-Nov-18 AHD c 2010

Python Data Processing and Selection 4.1 Python Program Structure 4.2 Memory Concepts 4.3 Arithmetic Expression Operators 4.4 Relational and Logical Operators 4.5 Selection 4.6 De Morgan’s Laws 20-Nov-18 AHD c 2010

Numeric Expressions (int) 2 + 4 6 - 4 6 * 3 6 / 3 6 % 3 6 // 3 -5 3**2 04-04.py 20-Nov-18 AHD c 2010

Numeric Expressions (float) 2.0 + 4.0 6.0 - 4.0 6.0 * 3.0 6.0 / 3.0 6.0 % 3.0 6.0 // 3.0 -5.0 3.0**2.0 04-05.py 20-Nov-18 AHD c 2010

Mixed Numeric Expressions 2 + 4.0 6 - 4.0 6 * 3.0 6 / 3.0 6 % 3.0 6 // 3.0 -5.0 3**2.0 04-06.py 20-Nov-18 AHD c 2010

Python Data Processing and Selection 4.1 Python Program Structure 4.2 Memory Concepts 4.3 Arithmetic Expression Operators 4.4 Relational and Logical Operators 4.5 Selection 4.6 De Morgan’s Laws 20-Nov-18 AHD c 2010

Relational operators relate two operands 7 > 10 4 < 16 4 == 4 4 <= 4 4 >= 4 4 != 4 These are Boolean expressions. The result of these expressions is either true (1) or false (0). 20-Nov-18 AHD c 2010

Boolean expressions result in values true or false 7 > 10 4 < 16 4 == 4 4 <= 4 4 >= 4 4 != 4 04-07.py 20-Nov-18 AHD c 2010

A Boolean Example number = 10 isPositive = (number > 0) a boolean expression 04-08.py 20-Nov-18 AHD c 2010

A Boolean Example number = 10 isPositive = (number > 0) # the value true (1) is # assigned to isPositive 20-Nov-18 AHD c 2010

Combining Boolean expressions You can combine Boolean expressions. For example, if you need to know if a person's age is greater than 21, AND they have a salary greater than 50 thousand dollars….. 20-Nov-18 AHD c 2010

Combining Boolean Expressions with a Logical Operator (and) age = 25 salary = 55000 print ((age > 21) and (salary > 50000)) 04-09.py 20-Nov-18 AHD c 2010

Logical operator: and (age > 21) and (salary > 50000) The and is known as a logical operator. 20-Nov-18 AHD c 2010

Logical (Boolean) Operators and or not 20-Nov-18 AHD c 2010

Truth Tables of Boolean Operators value of A value of B resulting value of A and B false false false false true false true false false true true true 20-Nov-18 AHD c 2010

Truth Tables of Boolean Operators value of A value of B resulting value of A or B false false false false true true true false true true true true 20-Nov-18 AHD c 2010

Truth Tables of Boolean Operators value of A resulting value of not A false true true false 20-Nov-18 AHD c 2010

When writing boolean expressions or arithmetic expressions, it is usually best to indicate the order of operations by using parentheses (brackets). 20-Nov-18 AHD c 2010

If parentheses are not used in an expression, the computer will perform the operations in an order determined by the precedence rules. . . 20-Nov-18 AHD c 2010

Precedence Rules lowest highest logical or logical and logical not relational operators: <, >, <=, etc +, - *, /, %, // -x, + x ( ) highest Note: expressions in parentheses (brackets) are evaluated first. 20-Nov-18 AHD c 2010

Python Data Processing and Selection 4.1 Python Program Structure 4.2 Memory Concepts 4.3 Arithmetic Expression Operators 4.4 Relational and Logical Operators 4.5 Selection 4.6 De Morgan’s Laws 20-Nov-18 AHD c 2010

The if statement The if statement makes use of a Boolean expression to decide which statement(s) to execute. x = 'spam' if x == 'spam': print ('Hi spam') else: print ('not spam') 20-Nov-18 AHD c 2010

The if statement The Boolean expression in this example is: x == 'spam' The expression has a value of true or false (1 or 0). x = 'spam' if x == 'spam': print ('Hi spam') else: print ('not spam') 20-Nov-18 AHD c 2010

The if statement x = 'spam' if x == 'spam': print ('Hi spam') else: print ('not spam') The Boolean expression is also known as the condition of the if statement. 20-Nov-18 AHD c 2010

The if statement x = 'spam' if x == 'spam': print ('Hi spam') else: print ('not spam') If the condition is true, the first print statement is executed and the second one is skipped. 20-Nov-18 AHD c 2010

The if statement x = 'spam' if x == 'spam': print ('Hi spam') else: print ('not spam') If the condition is false, the first print statement is skipped and the second one is executed. 20-Nov-18 AHD c 2010

The if statement x = 'spam' if x == 'spam': print ('Hi spam') else: print ('not spam') It's possible to have multiple statements in the true or false sections of an if statement... 20-Nov-18 AHD c 2010

The if statement x = 'spam' if x == 'spam': print ('Hi spam') print ('Hi Anne') else: print ('not spam') print ('bye Anne') If the condition is true, the first two print statements are executed and the second set are skipped. 20-Nov-18 AHD c 2010

The if statement - syntax The if statement starts with the keyword if followed by a Boolean expression, followed by a colon (:). x = 'spam' if x == 'spam': print ('Hi spam') print ('Hi Anne') else: print ('not spam') print ('bye Anne') 20-Nov-18 AHD c 2010

The if statement - syntax Beneath the if line, the statements to be run if the condition is true are entered after pressing the Tab key or typing a few space characters. x = 'spam' if x == 'spam': print ('Hi spam') print ('Hi Anne') else: print ('not spam') print ('bye Anne') 20-Nov-18 AHD c 2010

The if statement - syntax The statements to be run must be indented to the same level. It's recommended to press the Tab key before typing the statements. x = 'spam' if x == 'spam': print ('Hi spam') print ('Hi Anne') else: print ('not spam') print ('bye Anne') 20-Nov-18 AHD c 2010

The if statement - syntax The else part of an if statement is optional, but if included, must be followed by a colon (:), and then the indented statement(s) to be executed if the condition is false. x = 'spam' if x == 'spam': print ('Hi spam') print ('Hi Anne') else: print ('not spam') print ('bye Anne') 20-Nov-18 AHD c 2010

The if statement 04-10.py 04-11.py 04-12.py x = 'spam' if x == 'spam': print ('Hi spam') print ('Hi Anne') else: print ('not spam') print ('bye Anne') 04-10.py 04-11.py 04-12.py 20-Nov-18 AHD c 2010

The nested if / elif / else statement Nested if/else statements test for multiple cases by placing if/else selection structures inside other if/else selection structures… score = input("Enter score: ") score = int(score) if score >= 80: grade = 'A' else: if score >= 70 grade = 'B' grade = 'C' print ("\n\nGrade is: " + grade) 04-13.py 04-14.py 20-Nov-18 AHD c 2010

The nested if statement Nested if/else statements can be written using an alternate if/elif/else construct. Program 04-14.py is exactly equivalent to 04-15.py. score = input("Enter score: ") score = int(score) if score >= 80: grade = 'A' elif score >= 70: grade = 'B' elif score >= 55: grade = 'C' elif score >= 50: grade = 'Pass' else: grade = 'Fail' print ("\n\nGrade is: " + grade) 04-15.py 20-Nov-18 AHD c 2010

Python Data Processing and Selection 4.1 Python Program Structure 4.2 Memory Concepts 4.3 Arithmetic Expression Operators 4.4 Relational and Logical Operators 4.5 Selection 4.6 De Morgan’s Laws 20-Nov-18 AHD c 2010

De Morgan’s Laws 1. A not and is equivalent to an or with two negated inputs. 2. A not or is equivalent to an and with two negated inputs. http://www.annedawson.net/DeMorgansLaws.htm http://en.wikipedia.org/wiki/De_Morgan%27s_law 04-16.py 20-Nov-18 AHD c 2010

This presentation uses the following program files: http://www.annedawson.net/Python3Programs.txt 04-01.py 04-02.py 04-03.py 04-04.py 04-05.py 04-06.py 04-07.py 04-08.py 04-09.py 04-10.py 04-11.py 04-12.py 04-13.py 04-14.py 04-15.py 04-16.py 20-Nov-18 AHD c 2010

End of Python3_Processing_Selection.ppt 20-Nov-18 AHD c 2010

Last updated: Friday 15th May 2009, 09:13 PT, AHD 20-Nov-18 AHD c 2010