Lab1. Introduction to Python. Lab 4: Selection Statement. Eng. Mai Z. Alyazji

Similar documents
Selection Statement ( if )

Variable and Data Type I

Variable and Data Type I

Flow Control. So Far: Writing simple statements that get executed one after another.

Lecture 02 Making Decisions: Conditional Execution

4. Write the output that would be printed from each of the following code fragments. (8pts) x = 9 y = x / 2 print('y ==', y) +1 4.

CS100: CPADS. Decisions. David Babcock / Don Hake Department of Physical Sciences York College of Pennsylvania

Computer Programming: C++

Lecture 4 8/24/18. Expressing Procedural Knowledge. Procedural Knowledge. What are we going to cover today? Computational Constructs

Lab # 6. Using Subqueries and Set Operators. Eng. Alaa O Shama

Chapter 5 Conditional and Iterative Statements. Statement are the instructions given to the computer to perform any kind of action.

Eng. Mohammed S. Abdualal

Object Oriented Programming Lecture (2.1) Supervisor Ebtsam AbdelHakam Department of Computer Science Najran University

\n is used in a string to indicate the newline character. An expression produces data. The simplest expression

Hacettepe University Computer Engineering Department. Programming in. BBM103 Introduction to Programming Lab 1 Week 4. Fall 2018

Lecture 4. Conditionals and Boolean Expressions

Introduction to Computers. Laboratory Manual. Experiment #3. Elementary Programming, II

Conditional Expressions and Decision Statements

Flow of Control Branching 2. Cheng, Wei COMP May 19, Title

Python for Informatics

Chapter 7: Programming in MATLAB

CS 112: Intro to Comp Prog

Computer Programming: C++

Algorithms and Data Structures

Python The way of a program. Srinidhi H Asst Professor Dept of CSE, MSRIT

Intro to Programming. Unit 7. What is Programming? What is Programming? Intro to Programming

Fundamentals of Programming (Python) Control Structures. Sina Sajadmanesh Sharif University of Technology Fall 2017

Assembly Language Lab # 4 Data Transfer & Arithmetic (1)

Python Activity 5: Boolean Expressions and Selection Statements

9/18/11. Type casting, logical operators and if/else statement. Explicit Type Conversion. Example of Type Casting

CSc 372. Comparative Programming Languages. 36 : Scheme Conditional Expressions. Department of Computer Science University of Arizona

Lecture 3 (02/06, 02/08): Condition Statements Decision, Operations & Information Technologies Robert H. Smith School of Business Spring, 2017

JAVA PROGRAMMING LAB. ABSTRACT In this Lab you will learn to define and invoke void and return java methods

Loops. Eng. Mohammed Abdualal. Islamic University of Gaza. Faculty of Engineering. Computer Engineering Department

Introduction to Python

Conditionals and Recursion. Python Part 4

XQ: An XML Query Language Language Reference Manual

Variable and Data Type 2

Overview: Programming Concepts. Programming Concepts. Names, Values, And Variables

Overview: Programming Concepts. Programming Concepts. Chapter 18: Get With the Program: Fundamental Concepts Expressed in JavaScript

Loops and Conditionals. HORT Lecture 11 Instructor: Kranthi Varala

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Fall 2016 Howard Rosenthal

CSc 520 Principles of Programming Languages

egrapher Language Reference Manual

cs1114 REVIEW of details test closed laptop period

CHAPTER 2: Introduction to Python COMPUTER PROGRAMMING SKILLS

Recursion and Induction: Haskell; Primitive Data Types; Writing Function Definitions

LAB 4.1 Relational Operators and the if Statement

Chapter 5 : Informatics practices. Conditional & Looping Constructs. Class XI ( As per CBSE Board)

Chapter 1 Summary. Chapter 2 Summary. end of a string, in which case the string can span multiple lines.

Python Programming: An Introduction to Computer Science

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Spring 2016 Howard Rosenthal

Test #2 October 8, 2015

LAB 5: SELECTION STATEMENTS

Computer Programming: C++

Topics. Chapter 5. Equality Operators

Control Structures. Lecture 4 COP 3014 Fall September 18, 2017

Comp 151. Control structures.

DATA 301 Introduction to Data Analytics Python. Dr. Ramon Lawrence University of British Columbia Okanagan

PRG PROGRAMMING ESSENTIALS. Lecture 2 Program flow, Conditionals, Loops

Python Boot Camp. Day 3

Decision Making in C

Python review. 1 Python basics. References. CS 234 Naomi Nishimura

Objectives. Chapter 4: Control Structures I (Selection) Objectives (cont d.) Control Structures. Control Structures (cont d.) Relational Operators

LAB 13: ARRAYS (ONE DIMINSION)

Chapter 4: Control Structures I (Selection)

Control flow statements

Lecture 5 Tao Wang 1

Lecture Programming in C++ PART 1. By Assistant Professor Dr. Ali Kattan

SE 3S03. Zahra Ali. Week of Feb 22, 2016

Control Structures in Java if-else and switch

Introduction to Programming

Lab # 6. Data Manipulation Language (DML)

61A Lecture 3. Friday, September 5

conditional statements

Decision Making. <statements before if statement > if <condition >: <then statements > <statements after if statement >

Algorithms and Data Structures

Motivation of Loops. Loops. The for Loop (1) Learning Outcomes

Chapter 4: Control Structures I (Selection) Objectives. Objectives (cont d.) Control Structures. Control Structures (cont d.

Loops. EECS1021: Object Oriented Programming: from Sensors to Actuators Winter 2019 CHEN-WEI WANG

Islamic University Gaza Engineering Faculty Department of Computer Engineering ECOM 4111: Digital Systems Design Lab. Lab # 5. Concurrent Statements

Faculty of Engineering Computer Engineering Department Islamic University of Gaza C++ Programming Language Lab # 6 Functions

[Ch 6] Set Theory. 1. Basic Concepts and Definitions. 400 lecture note #4. 1) Basics

CSE 115. Introduction to Computer Science I

Relational & Logical Operators, Selection Statements

Algorithms and Conditionals

In this lab, you will learn more about selection statements. You will get familiar to

CSCE 110: Programming I

Computer Programming ECIV 2303 Chapter 6 Programming in MATLAB Instructor: Dr. Talal Skaik Islamic University of Gaza Faculty of Engineering

Fall 2018 Discussion 8: October 24, 2018 Solutions. 1 Introduction. 2 Primitives

SE 3S03 - Tutorial 10. Helen Brown. Week of Mar 23, 2015

Multi-Way Decisions. The newest program is great, but it still has some quirks! This program finds the real solutions to a quadratic

Assembly Language LAB

GE PROBLEM SOVING AND PYTHON PROGRAMMING. Question Bank UNIT 1 - ALGORITHMIC PROBLEM SOLVING

Introduction to Programming

Sequence types. str and bytes are sequence types Sequence types have several operations defined for them. Sequence Types. Python

Selection statements. CSE 1310 Introduction to Computers and Programming Alexandra Stefan University of Texas at Arlington

Spring Semester 08, Dr. Punch. Exam #1 (2/12), form 1 B

Introduction to Visual Basic and Visual C++ Arithmetic Expression. Arithmetic Expression. Using Arithmetic Expression. Lesson 4.

CONTROL AND HIGHER ORDER FUNCTIONS 1

Transcription:

Islamic University of Gaza Faculty of Engineering Computer Engineering Dept. Introduction to Computers Lab (ENGG 1003) Lab1 Introduction to Python Lab 4: Selection Statement Eng. Mai Z. Alyazji

In this lab we will constructs program that allow statements to be optionally executed, depending on the selection execution Boolean Data Type and Operations: The python values True and False are special inbuilt objects which is Boolean type. You can give variable the value directly or via Boolean expressions. Boolean expression: an expression that computes to True or false. It may contain relational (Comparison) operators and logical operators, the simplest Boolean expressions in Python are True and False. Comparison Operators: Operator Description == Equal to!= Not equal to <> Not equal to > Greater than < Less than >= Greater than or equal to <= Less than or equal to Logical Operators: We combined more complex boolean expressions using the logical operators and, or, and not Operator and or not Description If both the operands are true then condition becomes true If any of the two operands are non-zero then condition becomes true Used to negate the logical state Logical operators e1 and e2 are Boolean expressions e1 e2 e1 and e2 e1 or e2 not e1 True True True True False True False False True False False True False True True False False False False True Membership Operators: Python s membership operators test for membership in a sequence, such as strings, lists, or tuples. For example list=[2,6,2,2] Operator Description Example in Check element belong to sequence 5 in list------ False not in Check element not belong to sequence 5 not in list---true

Decision structures evaluate multiple expressions which produce TRUE or FALSE as outcome. You need to determine which action to take and which statements to execute if outcome is TRUE or FALSE otherwise. Following is the general form of a typical decision making structure: If Statement: if Boolean Expression: statements Syntax of if statment Condition False True Statements Example 1 a = 24 if a > 0: print "a is positive "

If else Statement: if Boolean Expression: statements(1) else statements(2) Syntax of if / else statment Condition False True Statements (1) Statements (2) Example 2 a = 24 if a > 0: print "a is positive " else : print "a is non - positive"

The elif Statement: The elif statement allows you to check multiple expressions for TRUE and execute a block of code as soon as one of the conditions evaluates to TRUE if expression1 : Syntax of elif statment statements(1) elif expression2: statements(2) elif expression3: statements(3) else statements(4) Example 3 a = 24 if a == 0: print "a is zero " elif a < 0: print "a is negative " else : print "a is positive " If a=24 a=0 a=-3

Single Statement: If the suite of an if clause consists only of a single line, it may go on the same line as the header statement. Here is an example of a one-line if clause: var = 100 Single statment if ( var == 100 ) : print "Value of expression is 100" print "Done!" Conditional Expressions: Python provides an alternative to the if/else construct called a conditional expression. A conditional expression evaluates to one of two values depending on a Boolean condition The general form of the conditional expression is: expression1 if condition else expression2 For example here variable c is assigned one of two possible values: if a!= b: c = d else: c = e if/else example Rewritten as c = d if a!= b else e The form of the conditional expression

Exmple 4: Small program to get the absolute value of a number n = input("enter a number: ") absvalue=(-n if n < 0 else n) print ' ', n, ' = ', absvalue Nested Conditionals: The statements in the block of the if or the else may be any Python statements, including other if/else statements. Determines if a number is between 0 and 10, inclusive value = input("please enter an integer value in the range 0...10: ") if value >= 0: # First check if value <= 10: # Second check print "In range" else : print "Out of range" print("done") The program can be rewritten to behave the same way with only one if statement: Another way value = input("please enter an integer value in the range 0...10: ") if value >= 0 and value <= 10: # Only one print("in range") else: print "Out of range" print("done")

Lab work: Example 4: check number is even or odd n = input("enter a number: ") if n%2==0 : print "Even number" else : print "Odd number" print "Done" Output: Example 5 value = input("please enter an integer in the range 0...5: ") if value < 0: print("too small") elif value == 0: print("zero") elif value == 1: print("one") elif value == 2: print("two") elif value == 3: print("three") elif value == 4: print("four") elif value == 5: print("five") else: print("too large") print("done") Output:

Q) Write a program that prompts the user to enter an integer and checks whether the number is divisible by both 5 and 6, or neither of them, or just one of them. Here are some sample runs for inputs 10, 30, and 23. 10 is divisible by 5 or 6, but not both 30 is divisible by both 5 and 6 23 is not divisible by either 5 or 6 Example 6 num=input("enter number : ") if num%5==0 and num%6==0: print num,"is divisible by both 5 and 6" elif num%5==0 or num%6==0: print num, "is divisible by 5 or 6, but not both " else: print num,"is not divisible by either 5 or 6" Output:

Exercises: 1) Write a Python program that requests three integer values from the user. It then prints one of two things: if any of the values entered are duplicates, it prints "DUPLICATES"; otherwise, it prints "ALL UNIQUE". 2) Write a Python program that use an if statement to find the smallest of three given integers without using the min function. 3) Write a Python program that sorts three integers. The integers are entered from user using input function. For example if the user enters: 6, 2, 6 then the output should be 2<=6 <=6 7, 3, 5 then the output should be 3<=5 <=7.