Strings, if/else, return, user input

Slides:



Advertisements
Similar presentations
Building Java Programs
Advertisements

Return values.
Arithmetic in Pascal (2) Arithmetic Functions Perform arithmetic calculations Gives an argument to the function and it returns the result.
Unit 6 File processing Special thanks to Roy McElmurry, John Kurkowski, Scott Shawcroft, Ryan Tucker, Paul Beck for their work. Except where otherwise.
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.
Introduction to Programming with Python Marty Stepp University of Washington Special thanks to Scott Shawcroft, Ryan Tucker,
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.
Week 3 parameters, return, math, graphics Special thanks to Scott Shawcroft, Ryan Tucker, and Paul Beck for their work on these slides. Except where otherwise.
Week 3 parameters and graphics Special thanks to Scott Shawcroft, Ryan Tucker, and Paul Beck for their work on these slides. Except where otherwise noted,
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 3: Parameters, Return, and Interactive Programs with Scanner.
Copyright 2008 by Pearson Education Building Java Programs Chapter 3 Lecture 3-2: Return; double ; System.out.printf reading: 3.2, 3.5, 4.4 videos: Ch.
Week 3 parameters, return, math, graphics Special thanks to Scott Shawcroft, Ryan Tucker, and Paul Beck for their work on these slides. Except where otherwise.
Week 7 Lists Special thanks to Scott Shawcroft, Ryan Tucker, and Paul Beck for their work on these slides. Except where otherwise noted, this work is licensed.
Week 6 review; file processing Special thanks to Scott Shawcroft, Ryan Tucker, and Paul Beck for their work on these slides. Except where otherwise noted,
Week 6 review; file processing Special thanks to Scott Shawcroft, Ryan Tucker, and Paul Beck for their work on these slides. Except where otherwise noted,
Copyright 2008 by Pearson Education Building Java Programs Chapter 3 Lecture 3-2: Return; double ; System.out.printf reading: 3.2, 3.5, 4.4 videos: Ch.
INTRODUCTION TO PYTHON PART 4 – TEXT AND FILE PROCESSING CSC482 Introduction to Text Analytics Thomas Tiahrt, MA, PhD.
12-2 Know how if and switch C statements control the sequence of execution of statements. Be able to use relational and logical operators in the conditional.
INTRODUCTION TO PYTHON PART 3 - LOOPS AND CONDITIONAL LOGIC CSC482 Introduction to Text Analytics Thomas Tiahrt, MA, PhD.
INTRODUCTION TO PYTHON PART 2 INPUT AND OUTPUT CSC482 Introduction to Text Analytics Thomas Tiahrt, MA, PhD.
Microsoft® Small Basic The Math Object Estimated time to complete this lesson: 1 hour.
INTRODUCTION TO PYTHON PART 1 CSC482 Introduction to Text Analytics Thomas Tiahrt, MA, PhD.
Lecture 6 29/1/15. Number functions Number functions take numbers as input, change them, and output the results as numbers. 2.
1 CS101 Introduction to Computing Lecture 38 String Manipulations (Web Development Lecture 13)
1 Introduction to Programming with Python. 2 Some influential ones: FORTRAN science / engineering COBOL business data LISP logic and AI BASIC a simple.
Week #2 Java Programming. Enable Line Numbering in Eclipse Open Eclipse, then go to: Window -> Preferences -> General -> Editors -> Text Editors -> Check.
Unit 5 while loops; logic; random numbers; tuples Special thanks to Roy McElmurry, John Kurkowski, Scott Shawcroft, Ryan Tucker, Paul Beck for their work.
Computer Science 111 Fundamentals of Programming I Basic Program Elements.
1 Text Processing. 2 Type char char : A primitive type representing single characters. –A String is stored internally as an array of char String s = "Ali.
Warm-Up: Monday, March 24 List as many commands in Java as you can remember (at least 10)
Calculations Chapter 11 Library of math functions, and constants cos, sin, tan, abs, min, max, log, random, sqrt, pow, exp Constants.PI,.E Use care with.
Unit 6 File processing Special thanks to Roy McElmurry, John Kurkowski, Scott Shawcroft, Ryan Tucker, Paul Beck for their work. Except where otherwise.
Week 3 parameters and graphics Special thanks to Scott Shawcroft, Ryan Tucker, and Paul Beck for their work on these slides. Except where otherwise noted,
Introduction As programmers, we don’t want to have to implement functions for every possible task we encounter. The Standard C library contains functions.
Week 2 expressions, variables, for loops Special thanks to Scott Shawcroft, Ryan Tucker, and Paul Beck for their work on these slides. Except where otherwise.
Week 2 expressions, variables, for loops Special thanks to Scott Shawcroft, Ryan Tucker, Paul Beck, Hélène Martin, Kim Todd, John Kurkowski, and Marty.
Exploration Seminar 4 Basics Special thanks to Scott Shawcroft, Ryan Tucker, Paul Beck and Roy McElmurry for their work on these slides. Except where otherwise.
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.
Introduction to Programming with Python
COP 2510 Chapter 6 - Functions. Random function (randint) #import the desired function import random #integer random number in the range of 1 through.
1 Introduction to Programming with Python: overview.
29 January 2016Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems.
Let’s Learn 3. Modules Saenthong School, January – February 2016
Week 7 : String and photo processing. Today’s Tasks  Practice list and string  Convert Decimal to any radix base number  Between Binary and Hexadecimal.
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.
Strings CSE 1310 – Introduction to Computers and Programming Alexandra Stefan University of Texas at Arlington 1.
CSc 110, Autumn 2016 Lecture 9: input ; if/else Adapted from slides by Marty Stepp and Stuart Reges.
strings, if/else, user input
Adapted from slides by Marty Stepp and Stuart Reges
Introduction to Programming with Python
Introduction to Programming
parameters, return, math, graphics
CSc 110, Spring 2017 Lecture 8: input; if/else
review; file processing
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.
Week 4 gur zntvp jbeqf ner fdhrnzvfu bffvsentr
Chapter 8 More on Strings and Special Methods
CSc 110, Autumn 2016 Lecture 9: input; if/else
Chapter 8 More on Strings and Special Methods
while loops; logic; random numbers; tuples
4TC00 Model-based systems engineering 2.2 Types and values
Introduction to Programming with Python
functions: argument, return value
Terminal-Based Programs
COMPUTER PROGRAMMING SKILLS
Introduction to Programming with Python
Presentation transcript:

Strings, if/else, return, user input 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 otherwise noted, this work is licensed under: http://creativecommons.org/licenses/by-nc-sa/3.0

Math commands from math import * Function name Description abs(value) absolute value ceil(value) rounds up cos(value) cosine, in radians degrees(value) convert radians to degrees floor(value) rounds down log(value, base) logarithm in any base log10(value) logarithm, base 10 max(value1, value2, ...) larger of two (or more) values min(value1, value2, ...) smaller of two (or more) values radians(value) convert degrees to radians round(value) nearest whole number sin(value) sine, in radians sqrt(value) square root tan(value) tangent Constant Description e 2.7182818... pi 3.1415926...

Strings Accessing character(s): variable [ index ] variable [ index1:index2 ] index2 exclusive index1 or index2 can be omitted (end of string) index 1 2 3 4 5 6 7 or -8 -7 -6 -5 -4 -3 -2 -1 character P . D i d y >>> name = "P. Diddy" >>> name[0] 'P' >>> name[7] 'y' >>> name[-1] >>> name[3:6] 'Did' >>> name[3:] 'Diddy' >>> name[:-2] 'P. Did'

String Methods Java Python length len(str) startsWith, endsWith toLowerCase, toUpperCase upper, lower, isupper, islower, capitalize, swapcase indexOf find trim strip >>> name = "Martin Douglas Stepp" >>> name.upper() 'MARTIN DOUGLAS STEPP' >>> name.lower().startswith("martin") True >>> len(name) 20

for Loops and Strings A for loop can examine each character in a string in order. for name in string: statements >>> for c in "booyah": ... print c ... b o y a h

input input : Reads a number from the user's keyboard. You can store the result of input into a variable. Example: age = input("How old are you? ") print "Your age is", age print "You have", 65 - age, "years until retirement" Output: How old are you? 53 Your age is 53 You have 12 years until retirement

raw_input raw_input : Reads a string from the user's keyboard. reads and returns an entire line of input >>> name = raw_input("Howdy. What's yer name?") Howdy. What's yer name? Paris Hilton >>> name 'Paris Hilton'

Exercise Write a program that reads two employees' hours and displays each's total and the overall total. Cap each day at 8 hours. Employee 1: How many days? 3 Hours? 6 Hours? 12 Hours? 5 Employee 1's total hours = 19 (6.33 / day) Employee 2: How many days? 2 Hours? 11 Employee 2's total hours = 14 (7.00 / day) Total hours for both = 33

Formatting Text "format string" % (parameter, parameter, ...) Placeholders insert formatted values into a string: %d an integer %f a real number %s a string %8d an integer, 8 characters wide, right-aligned %08d an integer, 8 characters wide, padding with 0s %-8d an integer, 8 characters wide, left-aligned %12f a real number, 12 characters wide %.4f a real number, 4 characters after decimal %6.2f a real number, 6 total characters wide, 2 after decimal >>> x = 3; y = 3.14159; z = "hello" >>> print "%-8s, %04d is close to %.3f" % (z, x, y) hello , 0003 is close to 3.142

if if condition: statements Example: gpa = input("What is your GPA? ") if gpa > 2.0: print "Your application is accepted."

if/else if condition: statements elif condition: else: Example: gpa = 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!" print "Your application is denied."

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)

Returning Values def name(parameters): statements ... return value >>> def ftoc(temp): ... tempc = 5.0 / 9.0 * (temp - 32) ... return tempc >>> ftoc(98.6) 37.0

Exercise Write a program that "encrypts" a secret message by shifting the letters of the message by 1: e.g. "Attack" when rotated by 1 becomes "buubdl"

Strings and Integers ord(text) - Converts a string into a number. ord("a") is 97 ord("b") is 98 Uses standard mappings such as ASCII and Unicode. chr(number) - Converts a number into a string. chr(97) is "a" chr(99) is "c"