strings, if/else, user input

Slides:



Advertisements
Similar presentations
Strings in Java 1. strings in java are handled by two classes String &
Advertisements

Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 8 Strings.
Java Programming Strings Chapter 7.
Strings An extension of types A class that encompasses a character array and provides many useful behaviors Chapter 9 Strings are IMMUTABLE.
Java Strings in 10 minutes
1 BUILDING JAVA PROGRAMS CHAPTER 4 CONDITIONAL EXECUTION.
Week 5 while loops; logic; random numbers; tuples Special thanks to Scott Shawcroft, Ryan Tucker, and Paul Beck for their work on these slides. Except.
Week 4 Strings, if/else, return, user input Special thanks to Scott Shawcroft, Ryan Tucker, and Paul Beck for their work on these slides. Except where.
Week 4 gur zntvp jbeqf ner fdhrnzvfu bffvsentr Strings, if/else, return, user input Special thanks to Scott Shawcroft, Ryan Tucker, and Paul Beck for their.
Strings, if/else, return, user input
HW 1: Problems 3 & 4 EC 1 & 2.
INTRODUCTION TO PYTHON PART 3 - LOOPS AND CONDITIONAL LOGIC CSC482 Introduction to Text Analytics Thomas Tiahrt, MA, PhD.
CS305j Introduction to Computing Conditional Execution 1 Topic 12 Conditional Execution "We flew down weekly to meet with IBM, but they thought the way.
1 Introduction to Programming with Python. 2 Some influential ones: FORTRAN science / engineering COBOL business data LISP logic and AI BASIC a simple.
Chapter 9 IF Statement Bernard Chen. If Statement The main statement used for selecting from alternative actions based on test results It’s the primary.
Unit 5 while loops; logic; random numbers; tuples Special thanks to Roy McElmurry, John Kurkowski, Scott Shawcroft, Ryan Tucker, Paul Beck for their work.
Introduction to Python Lecture 1. CS 484 – Artificial Intelligence2 Big Picture Language Features Python is interpreted Not compiled Object-oriented language.
Line Continuation, Output Formatting, and Decision Structures CS303E: Elements of Computers and Programming.
Building Java Programs Chapter 4 Conditional Execution Copyright (c) Pearson All rights reserved.
Programming for Linguists An Introduction to Python 24/11/2011.
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. Chapter 8 More on Strings and Special Methods 1.
Python for Informatics: Exploring Information
Python Programming in Context Chapter 3. Objectives To introduce the string data type To demonstrate the use of string methods and operators To introduce.
Building Java Programs
Python Mini-Course University of Oklahoma Department of Psychology Day 2 – Lesson 7 Conditionals and Loops 4/18/09 Python Mini-Course: Day 2 - Lesson 7.
Building Java Programs Chapter 4 Conditional Execution Copyright (c) Pearson All rights reserved.
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.
Copyright 2008 by Pearson Education Building Java Programs Chapter 4 Lecture 4-1: if and if/else Statements reading: 4.2 self-check: #4-5, 7, 10, 11 exercises:
1 Introduction to Programming with Python: overview.
1 CS 177 Week 6 Recitation Slides Review for Midterm Exam.
If/else, return, user input, strings
Strings, if/else, user input
Strings CSE 1310 – Introduction to Computers and Programming Alexandra Stefan University of Texas at Arlington 1.
Python Arithmetic Operators OperatorOperationDescription +AdditionAdd values on either side of the operator -SubtractionSubtract right hand operand from.
Introduction to programming in java
CSc 110, Autumn 2016 Lecture 9: input ; if/else Adapted from slides by Marty Stepp and Stuart Reges.
Python Basics.
Lecture 8: Python Writing Scripts in GIS
Building Java Programs
Introduction to Python
Basic operators - strings
Building Java Programs Chapter 4
Building Java Programs
Building Java Programs
CSc 110, Spring 2017 Lecture 8: input; if/else
Week 1 Review Special thanks to Scott Shawcroft, Ryan Tucker, and Paul Beck for their work on these slides. Except where otherwise noted, this work is.
Imperative Programming
Week 4 gur zntvp jbeqf ner fdhrnzvfu bffvsentr
Building Java Programs
Chapter 8 More on Strings and Special Methods
Chapter 8 More on Strings and Special Methods
Python - Strings.
CSc 110, Autumn 2016 Lecture 9: input; if/else
Building Java Programs
Chapter 8 More on Strings and Special Methods
Topic 1: Problem Solving
while loops; logic; random numbers; tuples
Building Java Programs
Building Java Programs
Python Basics with Jupyter Notebook
Building Java Programs
Building Java Programs
Building Java Programs
Control Statements.
Building Java Programs
Building Java Programs
Building Java Programs Chapter 4
Introduction to Programming with Python
Building Java Programs
COMPUTING.
Presentation transcript:

strings, if/else, user input http://www.youtube.com/watch?v=uprjmoSMJ-o

strings access a single character with variable[index] 1 2 3 4 5 6 7 or -8 -7 -6 -5 -4 -3 -2 -1 character P . D i d y access a single character with variable[index] access a range of characters with variable[index1:index2] index1 is inclusive, index2 is exclusive

string methods Java Python length len(str) startsWith, endsWith startswith, endswith toLowerCase, toUpperCase upper, lower, isupper, islower, capitalize, swapcase indexOf find trim strip more at http://docs.python.org/library/stdtypes.html#id4

for loops and strings >>> for c in "eggs": ... print c ... e g s a for loop can be used to loop over each character in a string

raw_input reads a line of input and returns it as a string >>> color = raw_input("What is your favorite color? ") What is your favorite color? Blue. No, yellow! >>> color 'Blue. No, yellow!' reads a line of input and returns it as a string

raw_input + numbers >>> age = int(raw_input("What is your age? ")) What is your age? 53 >>> print 65 - age, "years until retirement" 12 years until retirement to read an int, cast the result of raw_input to int

if/else elif instead of else if elif/else branches are optional gpa = int(raw_input("What is your GPA? ")) if gpa > 3.5: print "You have qualified for the honor roll." elif gpa > 2.0: print "Welcome to Mars University!" else: print "Your application is denied." elif instead of else if elif/else branches are optional

if ... in tests to see if sequence contains value if value in sequence: statements tests to see if sequence contains value sequence can be a string, tuple, or list name = raw_input("What is your name? ") name = name.lower() if name[0] in "aeiou": print "Your name starts with a vowel!"

greater than or equal to logical operators Operator Meaning Example Result == equals 1 + 1 == 2 True != does not equal 3.2 != 2.5 < less than 10 < 5 False > greater than 10 > 5 <= less than or equal to 126 <= 100 >= greater than or equal to 5.0 >= 5.0 Operator Example Result and (2 == 3) and (-1 < 5) False or (2 == 3) or (-1 < 5) True not not (2 == 3)

exercise!

caesear cipher abcdefghijklmnopqrstuvwxyz defghijklmnopqrstuvwxyzabc “we are the knights who say ni!” becomes “zh duh wkh nqljkwv zkr vdb ql!”

exercise >>> alphabet1 = "abcdefghijklmnopqrstuvwxyz" >>> alphabet2 = "defghijklmnopqrstuvwxyzabc" >>> substitute("we are the knights who say ni!", alphabet1, alphabet2) 'zh duh wkh nqljkwv zkr vdb ql!' write a function substitute, that takes a message and two alphabets, and returns an encoded message

solution def substitute(text, alphabet1, alphabet2): result = "" for ch in text: if ch in alphabet1: result += alphabet2[alphabet1.find(ch)] else: result += ch return result

exercise >>> make_phrase("zebras") 'zebrascdfghijklmnopqtuvwxy' write a function make_phrase, that takes a phrase and creates an alphabet from it

solution def make_phrase(phrase): result = alphabet for ch in phrase: result = result.replace(ch, "") return phrase + result

exercise make it take user input! text? va zoa qda hkfcdqp vdl pzx kf!passphrase? Zebras would you like to 1) encode or 2) decode? 2 we are the knights who say ni!

cipher.py 1 alphabet = "abcdefghijklmnopqrstuvwxyz" 2 3 def substitute(text, alphabet1, alphabet2): 4 result = "" 5 for ch in text: 6 if ch in alphabet1: 7 result += alphabet2[alphabet1.find(ch)] 8 else: 9 result += ch 10 return result 11 12 def make_phrase(phrase): 13 result = alphabet 14 for ch in phrase: 15 result = result.replace(ch, "") 16 return phrase + result 17 18 # "main" 19 text = raw_input("text? ") 20 phrase = raw_input("passphrase? ") 21 choice = raw_input("would you like to 1) encode or 2) decode? ") 22 code = make_phrase(phrase) 23 24 print 25 26 if choice == "1": 27 print substitute(text, alphabet, code) 28 else: 29 print substitute(text, code, alphabet)

formatting text just like printf in java "format string" % (parameter, parameter, ...) just like printf in java %d integer %f real number %s string more at http://docs.python.org/library/stdtypes.ht ml#string-formatting