Decision Structures, String Comparison, Nested Structures

Slides:



Advertisements
Similar presentations
CATHERINE AND ANNIE Python: Part 3. Intro to Loops Do you remember in Alice when you could use a loop to make a character perform an action multiple times?
Advertisements

Strings Testing for equality with strings.
ECS 15 if and random. Topic  Testing user input using if statements  Truth and falsehood in Python  Getting random numbers.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 4- 1.
Geography 465 Assignments, Conditionals, and Loops.
Line Continuation, Output Formatting, and Decision Structures CS303E: Elements of Computers and Programming.
A453 Exemplar Password Program using VBA
CS0004: Introduction to Programming Relational Operators, Logical Operators, and If Statements.
Introduction to Python
STRINGS CMSC 201 – Lab 3. Overview Objectives for today's lab:  Obtain experience using strings in Python, including looping over characters in strings.
PYTHON: PART 2 Catherine and Annie. VARIABLES  That last program was a little simple. You probably want something a little more challenging.  Let’s.
1 Data Comparisons and Switch Data Comparisons Switch Reading for this class: L&L 5.3,
Agenda Exam #1 Review Modulus Conditionals Boolean Algebra Reading: Chapter Homework #5.
CONTENTS Processing structures and commands Control structures – Sequence Sequence – Selection Selection – Iteration Iteration Naming conventions – File.
Decision Structures and Boolean Variables. Sequence Structures Thus far, we’ve been programming “sequence structures” Thus far, we’ve been programming.
Chapter Making Decisions 4. Relational Operators 4.1.
Decision Structures, String Comparison, Nested Structures
Introduction to Python Dr. José M. Reyes Álamo. 2 Three Rules of Programming Rule 1: Think before you program Rule 2: A program is a human-readable set.
CSC 1010 Programming for All Lecture 3 Useful Python Elements for Designing Programs Some material based on material from Marty Stepp, Instructor, University.
Last Week Modules Save functions to a file, e.g., filename.py The file filename.py is a module We can use the functions in filename.py by importing it.
Python Let’s get started!.
CECS 5020 Computers in Education Visual Basic Variables and Constants.
These Guys? Wait, What? Really?  Branching is a fundamental part of programming  It means taking an action based on decision  The decision is dependent.
Exception Handling and String Manipulation. Exceptions An exception is an error that causes a program to halt while it’s running In other words, it something.
Introduction to Programming Python Lab 7: if Statement 19 February PythonLab7 lecture slides.ppt Ping Brennan
Winter 2016CISC101 - Prof. McLeod1 CISC101 Reminders Quiz 3 this week – last section on Friday. Assignment 4 is posted. Data mining: –Designing functions.
 Type Called bool  Bool has only two possible values: True and False.
CSC 108H: Introduction to Computer Programming Summer 2011 Marek Janicki.
Discussion 4 eecs 183 Hannah Westra.
Introduction to Decision Structures and Boolean Variables
Basic concepts of C++ Presented by Prof. Satyajit De
String Manipulation Part 1
Whatcha doin'? Aims: To start using Python. To understand loops.
Introduction to Python
Line Continuation, Output Formatting, and Decision Structures
Formatting Output.
Debugging and Random Numbers
IF statements.
Introduction to Programming
Variables, Expressions, and IO
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,
Javascript Conditionals.
Engineering Innovation Center
While loops The while loop executes the statement over and over as long as the boolean expression is true. The expression is evaluated first, so the statement.
Imperative Programming
Line Continuation, Output Formatting, and Decision Structures
String Manipulation Part 2
Introduction to Programming
Decision Structures, String Comparison, Nested Structures
Decision Structures, String Comparison, Nested Structures
Strings.
Introduction to Programming
Introduction to Programming
Logical Operations In Matlab.
3. Decision Structures Rocky K. C. Chang 19 September 2018
Selection Statements.
Introduction to Decision Structures and Boolean Variables
A look at Python Programming Language 2018.
VB Decisions & Conditions
Introduction to Computer Science
Making decisions with code
The Selection Structure
Introduction to Programming
Introduction to Programming
Module 3 Selection Structures 6/25/2019 CSE 1321 Module 3.
Hardware is… Software is…
Imperative Programming
Presentation transcript:

Decision Structures, String Comparison, Nested Structures Best Powerpoint Layout Ever!

The IF-ELSE Statement Previously, we wrote “IF” statements to check for a condition, and our programs followed the order of the program according to whether the conditions held “True” or “False” Now, we will add in another branch of execution based on the value of the Boolean expression

The IF-ELSE Statement We can do this by adding the reserved word “else” This tells the program to check for the “if” condition and if it is not satisfied, run the code branched under the “else” statement You need to add a colon at the end of each else statement

The IF-ELSE Statement Previously, we had to use two “if” statements like this: if temperature < 32: print(“It’s freezing outside”) if temperature >= 32: print(“It’s a normal day in October … ”)

The IF-ELSE Statement Now, with the “else”, we can write our codes like this: if temperature < 32: print(“It’s freezing outside”) else: print(“It’s a normal day in October … ”)

The IF-ELSE Statement Notice that the “else” statement works kind of like a safety net, that “catches” all other possibilities. In other words, the first “if” condition checks for one type of possibility and all other possibilities fall into the “else” block For example, any temperature that is NOT less than 32 degrees, must be equal to or greater than 32, and that is summed up in the “else” Another example might be if something is NOT green, it can be any other color whether purple, red or blue and all of those colors will fall under an “else” statement

The IF-ELSE Statement

The IF-ELSE Statement hours = float(input(“how many hours did you work?”)) rate = float(input(“what’s your hourly rate?”)) if hours > 40: print(“Your total is”, (40*rate) + ( (hours - 40)*1.5*rate) ) else: print(“Your total is”, hours*rate)

String Comparison So far, we’ve been comparing data as numeric types We can also compare strings The way we do this is to convert all characters back to their basic data type, all one’s and zero’s using the ASCII/UNICODE table

ASCII Table

String Comparison When comparing strings, Python will compare one letter at a time, in the order that they appear Example: “animal” < “banana” # True Because the letter “a” in animal has a lower ASCII value than the letter “b” in banana, this Boolean expression holds True

String Comparison “dog” > “cat” # is “dog” greater than “cat”? “fish” < “alligator” # is “fish” less than “alligator”? “elephant” == “tiger” # are “elephant” and “tiger” equal? “Mr. Seok” != “Donald # are these different strings?

Practice: Passwords Write a program that asks the user to input a new password and store it in a variable Then, ask the user to confirm the password they just typed If they are exactly the same, then print out “Great! Password was saved!” Otherwise, print out “Sorry, the two passwords did not match.”

Basic String Manipulation Python has a huge list of string manipulation commands written in a package. We will take a look at many of them, but for now we’ll take a look at: lower() # this changes all characters into lower case upper() # this one changes into upper case

Basic String Manipulation These two commands are not actually built into Python’s function library, therefore we need to refer to a separate module We call this the “str” module Examples: string_lower = str.lower (“Hello, World!”) # hello, world! string_upper = str.upper (“Hello, World!”) # HELLO, WORLD!

Basic String Manipulation A module is a file containing functions defined in Python and other statements string_lower = str.lower (“Hello, World!”) The characters “ str. ” calls on a module entitled “str” that Python can access but is not directly built into the shell The word “lower()” calls the function within that specific module

Basic String Manipulation Keep in mind that the str.lower() function is one that “returns” a value and therefore must be stored in some place, or variable name new_word = str.lower (“Hello, World!”) print(new_word) >> hello, world!

Practice: Passwords Rewrite your password program so that it is not case sensitive Such that, “password” and “PaSsWoRd” are considered valid entries

Practice: Alphabetizing Strings Write a program that asks the user to input two names Then print back these names in alphabetical order Note: Keep in mind that lower case letters hold “greater” values than that of the capital letters

String Length Python also has a function that counts the number of characters in a string We call it the len() function and it returns an integer Example: count = len(“Donald”) # count will hold value 6 # as an integer

Practice: Compare Size of Strings Write a program that asks the user for two names Sort them in size order and print them out