ECS 15 Strings, input. Outline  Strings, string operation  Converting numbers to strings and strings to numbers  Getting input  Running programs by.

Slides:



Advertisements
Similar presentations
Fundamentals of Python: From First Programs Through Data Structures Chapter 2 Software Development, Data Types, and Expressions.
Advertisements

Python November 14, Unit 7. Python Hello world, in class.
Introduction to Python
 2002 Prentice Hall. All rights reserved. 1 Chapter 2 – Introduction to Python Programming Outline 2.1 Introduction 2.2 First Program in Python: Printing.
Data Types. Every program must deal with data The data is usually described as a certain type This type determines what you can do with the data and how.
Copyright © 2001 by Wiley. All rights reserved. Chapter 3: Variables, Assignment Statements, and Arithmetic Variables Assignment Statements Arithmetic.
Guide to Programming with Python Chapter Two Basic data types, Variables, and Simple I/O: The Useless Trivia Program.
Introduction to Python Lecture 1. CS 484 – Artificial Intelligence2 Big Picture Language Features Python is interpreted Not compiled Object-oriented language.
Introduction to Python
General Computer Science for Engineers CISC 106 Lecture 02 Dr. John Cavazos Computer and Information Sciences 09/03/2010.
CS161 Topic #21 CS161 Introduction to Computer Science Topic #2.
Instructor: Chris Trenkov Hands-on Course Python for Absolute Beginners (Spring 2015) Class #002 (January 17, 2015)
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
1 CSC 221: Introduction to Programming Fall 2012 Functions & Modules  standard modules: math, random  Python documentation, help  user-defined functions,
Sep 12, 2007Sprenkle - CS1111 Objectives Review  Linux  Why programming languages? Compiled vs. Interpreted Languages Programming in Python  Data types.
Chapter 3 Processing and Interactive Input. 2 Assignment  The general syntax for an assignment statement is variable = operand; The operand to the right.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 2 Input,
OCR Computing GCSE © Hodder Education 2013 Slide 1 OCR GCSE Computing Python programming 6: Variables and constants.
Input, Output, and Processing
Computer Science 101 Introduction to Programming.
Lesson 4 Using Variables in Python – Creating a Simple ChatBot Program.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
COMP 171: Data Types John Barr. Review - What is Computer Science? Problem Solving  Recognizing Patterns  If you can find a pattern in the way you solve.
1 Computer Science of Graphics and Games MONT 105S, Spring 2009 Session 1 Simple Python Programs Using Print, Variables, Input.
Variables, Expressions and Statements
ECS 15 Variables. Outline  Using IDLE  Building blocks of programs: Text Numbers Variables!  Writing a program  Running the program.
© 2004 Pearson Addison-Wesley. All rights reserved ComS 207: Programming I Instructor: Alexander Stoytchev
Python Conditionals chapter 5
My Python Programmes NAME:.
CSC 1010 Programming for All Lecture 7 Input, Output & Graphics.
Operators and Expressions. 2 String Concatenation  The plus operator (+) is also used for arithmetic addition  The function that the + operator performs.
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.
Python Let’s get started!.
Data Types and Conversions, Input from the Keyboard CS303E: Elements of Computers and Programming.
1 Computer Graphics and Games MONT 105S, Spring 2009 Lecture 2 Simple Python Programs Working with numbers and strings.
Data Types and Conversions, Input from the Keyboard If you can't write it down in English, you can't code it. -- Peter Halpern If you lie to the computer,
© 2007 Pearson Addison-Wesley. All rights reserved2-1 Character Strings A string of characters can be represented as a string literal by putting double.
Introduction to Programming
Course A201: Introduction to Programming 09/09/2010.
Strings CSE 1310 – Introduction to Computers and Programming Alexandra Stefan University of Texas at Arlington 1.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
Input, Output and Variables GCSE Computer Science – Python.
ENGINEERING 1D04 Tutorial 2. What we’re doing today More on Strings String input Strings as lists String indexing Slice Concatenation and Repetition len()
Introduction to Programming
Fundamentals of Programming I Overview of Programming
Topics Designing a Program Input, Processing, and Output
Introduction to Python
Data Types and Conversions, Input from the Keyboard
ECS10 10/10
Introduction to Programming
Presented By S.Yamuna AP/IT
Variables, Expressions, and IO
Useful String Methods Cont…
Introduction to Programming
Thinking about programming
Topics Introduction to File Input and Output
Introduction to Programming
Topics Designing a Program Input, Processing, and Output
What are variables? Using input()
Introduction In today’s lesson we will look at: why Python?
Topics Designing a Program Input, Processing, and Output
Topics Designing a Program Input, Processing, and Output
Thinking about programming
Introduction to Programming
What are variables? Using input()
Input and Output Python3 Beginner #3.
General Computer Science for Engineers CISC 106 Lecture 03
Topics Introduction to File Input and Output
PYTHON - VARIABLES AND OPERATORS
Presentation transcript:

ECS 15 Strings, input

Outline  Strings, string operation  Converting numbers to strings and strings to numbers  Getting input  Running programs by clicking on them

Strings in English

Dictionary: string ( strĭng ) n. 1.A cord usually made of fiber, used for fastening, tying, or lacing. 2.Something configured as a long, thin line: limp strings of hair. 3.A plant fiber. 4.A set of objects threaded together: a string of beads. 5.A series of similar or related acts, events, or items arranged or falling in or as if in a line. See synonyms at series.series 6.Computer Science. A set of consecutive characters.

Topic  Writing a form letter ``Parts of speech” in programming Using variables and strings

“Parts of speech”  Dark red – comments! Good idea to start off with a comment describing purpose of program and its overall strategy.  Orange – Python commands. If you misspell it, it’s not orange.  Green – strings  Black – numbers, variables

Expressions and values  is an expression.  Its value is 91  “Coca-cola” is also an expression  Its value is “Coca-cola”  Either kind of expression can be assigned to variables.

IDLE vs script window  Type an expression in IDLE window – it types back the value.  Type an expression (alone) in script window and the value does NOT appear when you run the program.  Has no effect unless it’s assigned to a variable, explicitly printed, etc.

Strings and quotes  “Coca-cola” and ‘Coca-cola’ are two ways of writing the same string.  “Macy’s” and ‘Macy\’s’ are two ways of writing a string containing an apostrophe.  Can use \’ or \” to make strings with apostrophes or quotes.

Concatenation  An operator on strings  “My “+”cat” has the value “My cat”

A few fun string operators  a+b  a*i  a.split(sep)  a.upper()  a.lower()  a.capitalize()  a.count(‘sub’)  a.replace(‘sub1’,’sub2’,n)

Different ways to skin a…  Here are three ways to do the same thing print (“My cat“,catName) print (“My cat “+catName) print (“My cat“, end=‘ ‘) print(catName) (this one works only in programs, not in the IDLE window).

Newline  “\n” produces a carriage return  Two ways to do the same thing: print (“A rose\nis a rose\nis a rose.”) print (“A rose”) print (“is a rose”) print (‘is a rose.’)

Quick review  is a number (or numeric) expression.  It uses addition  Its value is 37  “drive”+”way” is a string expression  It uses concatenation  It’s value is “driveway”

Integers vs strings 5 is an integer. “5” is a string. Rover is a dog. “Rover” is a string.

Convert integer to string  int(“5”) is the integer 5  str(5) is the string “5”

Functions  int() and str() are functions  Like in algebra, f(x).  Python computes the value of a function, just like it computes the value of an expression.  The value of int(“5”) is the integer 5.  The value of str(6) is the string “6”.

Converting numbers  5.0 and 6.33 are floating-point numbers  5 and 2 are integers  The value of float(2) is the floating-point number 2.0  The value of int(6.0) is the integer 6  The value of int(“3.8”) is…

Getting input  Use the function input() (used to be raw_input in Python 2)  Example: color = input(“ Favorite color: “) Python evaluates the function input… by printing the prompt “Favorite color: “ the user types in “red” the string “red” becomes the value of the input function the variable color gets the value “red”

The ID program  First line: x = input(“Four-digit number: “)  x gets the string “7554”, not the integer  Python gets confused if we try to do arithmetic with a string.  We need to convert “7554” to an integer

Running program by clicking  Why does it close up?

Keep window open  Add final line to program: input(“Type enter to exit: “)  We do not do anything with the value of this last raw_input function; we are happy to throw it away!

Handling bad input  Crazy input causes our program to crash!  This is very bad.  We do not have a good way to fix this yet…