1 Quick summary: print print "Kansas" Print string plus a newline print "Kansas", Print string plus a space print "Kansas", "City" Print strings with space.

Slides:



Advertisements
Similar presentations
CS 100: Roadmap to Computing Fall 2014 Lecture 0.
Advertisements

Introduction to Computing Science and Programming I
CS1010 Programming Methodology
ECS15 for and sequence. Comments  Another way to repeat.
Lecture 7 Sept 17 Goals: Complete Chapter 4 Chapters 5 and 6.
Structured programming
Lecture 7 Sept 29 Goals: Chapters 5 and 6. Scripts Sequence of instructions that we may want to run can be stored in a file (known as script). by typing.
Introduction and a Review of Basic Concepts
Python November 14, Unit 7. Python Hello world, in class.
Lecture 7 Sept 29 Goals: Chapters 5 and 6. Chapter 5 - Exercises Exercise 5.2. Write a script swap.m that swaps the values of variables a and b. For example:
JAVA Control Structures: Repetition. Objectives Be able to use a loop to implement a repetitive algorithm Practice, Practice, Practice... Reinforce the.
Lecture 2 - Variables, program execution, calculations, print() COMPSCI 101 Principles of Programming.
Advanced Shell Programming. 2 Objectives Use techniques to ensure a script is employing the correct shell Set the default shell Configure Bash login and.
Chapter 5: Data Input and Output Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills
The if statement and files. The if statement Do a code block only when something is True if test: print "The expression is true"
Programming for Linguists An Introduction to Python 24/11/2011.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
CNG 140 C Programming (Lecture set 9) Spring Chapter 9 Character Strings.
1 CSC 221: Introduction to Programming Fall 2012 Functions & Modules  standard modules: math, random  Python documentation, help  user-defined functions,
COMP 116: Introduction to Scientific Programming Lecture 28: Midterm #2 Review.
Input, Output, and Processing
Lecture 7 Sept 22 Goals: Chapters 5 and 6. Scripts Sequence of instructions that we may want to run can be stored in a file (known as script). by typing.
C STRUCTURES. A FIRST C PROGRAM  #include  void main ( void )  { float height, width, area, wood_length ;  scanf ( "%f", &height ) ;  scanf ( "%f",
Hey, Ferb, I know what we’re gonna do today! Aims: Use formatted printing. Use the “while” loop. Understand functions. Objectives: All: Understand and.
Copyright © Nancy Acemian 2004 For Loops-Break-Continue COMP For loop is a counter controlled loop. For loop is a pretest loop. Used when number.
An Introduction to Java Programming and Object-Oriented Application Development Chapter 7 Characters, Strings, and Formatting.
Lecture 2: Introduction to C Programming. OBJECTIVES In this lecture you will learn:  To use simple input and output statements.  The fundamental data.
CSE1222: Lecture 7The Ohio State University1. logExample.cpp // example of log(k) for k = 1,2,..,8... int main() { cout
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.
Last Week if statement print statement input builtin function strings and methods for loop.
Introducing Python CS 4320, SPRING Lexical Structure Two aspects of Python syntax may be challenging to Java programmers Indenting ◦Indenting is.
Python Conditionals chapter 5
More about Strings. String Formatting  So far we have used comma separators to print messages  This is fine until our messages become quite complex:
Lecture 6: Midterm Review Tami Meredith. Programming Process How do we fill in the yellow box? Text Editor Compiler (javac) Interpreter (JVM: java) User.
Lecture 12: Final Review Tami Meredith. Programming Requires 1. Identification of the problem Understanding it, identifying correct solutions 2. Solving.
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.
Advanced Strings Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg 1.
Data Types and Conversions, Input from the Keyboard CS303E: Elements of Computers and Programming.
CHAPTER 2 PART #3 C++ INPUT / OUTPUT 1 st Semester King Saud University College of Applied studies and Community Service CSC1101 By: Fatimah.
COMP 110: Spring Announcements Lab 1 due Wednesday at Noon Assignment 1 available on website Online drop date is today.
Trinity College Dublin, The University of Dublin GE3M25: Computer Programming for Biologists Python, Class 2 Karsten Hokamp, PhD Genetics TCD, 17/11/2015.
Programming With Java ICS201 University Of Ha’il1 Chapter 11 Recursion.
Midterm Exam Topics (Prof. Chang's section) CMSC 201.
 2007 Pearson Education, Inc. All rights reserved. A Simple C Program 1 /* ************************************************* *** Program: hello_world.
CPSC 233 Tutorial 5 February 9 th /10 th, Java Classes Each Java class contains a set of instance variables and methods Instance Variables: Type.
Midterm Review Tami Meredith. Primitive Data Types byte, short, int, long Values without a decimal point,..., -1, 0, 1, 2,... float, double Values with.
CCSA 221 Programming in C INPUT AND OUTPUT OPERATIONS IN C – PART 1 1.
Program Development and Design Using C++, Third Edition
String and Lists Dr. José M. Reyes Álamo. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list.
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
Input, Output and Variables GCSE Computer Science – Python.
String and Lists Dr. José M. Reyes Álamo.
C++ Memory Management – Homework Exercises
Topic 6 Recursion.
Greatest Common Divisor
Topic: Iterative Statements – Part 1 -> for loop
INC 161 , CPE 100 Computer Programming
ECS10 10/10
Strings Part 1 Taken from notes by Dr. Neil Moore
Python Lesson 6 Mr. Kalmes.
Chapter 7: Strings and Characters
Discrete Math for CS CMPSC 360 LECTURE 12 Last time: Stable matching
Lecture3.
Python Strings.
Data Types Every variable has a given data type. The most common data types are: String - Text made up of numbers, letters and characters. Integer - Whole.
EECE.2160 ECE Application Programming
Strings Taken from notes by Dr. Neil Moore & Dr. Debby Keen
Presentation transcript:

1 Quick summary: print print "Kansas" Print string plus a newline print "Kansas", Print string plus a space print "Kansas", "City" Print strings with space in between print "Kansas " + "City" Print strings right after each other

2 Quick summary: string indexing Create variable pointing to a string: s = "Kansas City" Strings are indexed from 0: s[0] yields 'K' Strings can be indexed from the right with negative indices: s[-4] yields 'C' Create a copy of the substring of s from index 3 to but not including 6: s[3:6] yields 'sas' Create an extended slice: s[0:6:2] yields 'Kna' s[3::-1] yields 'snaK'

3 Quick summary: raw_input, int, float Print query string, wait for user to type something, always returns a string: raw_input("Enter your age: ") Convert the string "37" to the integer 37: int("37") Convert the string "37.54" to the float 37.54: float("37.54") int(raw_input("Enter your age: "))

4 Quick summary: string formatting Using the conversion specifiers and string formatting operator: s = "City" "Kansas %s"%s yields 'Kansas City' a = 50 "USA has %d states"%a yields "USA has 50 states" "Gotham %s has %d villains"%(s, a) yields 'Gotham City has 50 villains' "%.2f dollars"% yields '7.94 dollars'

5 Quick summary: string methods Can be called through any string: "Kansas".find("sas") yields 3 "Kansas".isalnum() yields True "Kansas".islower() yields False "Kansas".count('a', 3) yields 1.. Or through the identifier str : str.find("Kansas ", "sas") yields 3

6 Example: cutting out parentheses Task: Receive text from user, cut out everything inside matching parentheses: Things in parentheses (such as this one) should be ignored. becomes Things in parentheses should be ignored.

7 Solution 1 S: Don't(or do)floss(I mean it)! Don't floss New S:

8 parenthesis_cutter.py

9 Running the program Sentence: Everything in parentheses (like this one) should be ignored. Everything in parentheses should be ignored. Sentence: What about (things inside (nested) parentheses) ? What about parentheses) ?

10 Solution 2 S: Don't(or do)floss (I mean it) ! Don't New S: parenthesis level: 2 f l o s s !

11 parenthesis_cutter2.py Note for char in s:

12 Running the program Sentence: What about (things inside (nested) parentheses) ? What about ?

13 Euclid’s Greatest Common Divisor algorithm 1.Get two integers a, b > 0 from the user 2.If a and b are the same, GCD = a, exit 3.Otherwise, repeat: – If a > b: Replace a with a modulo b (a is now smaller than b). If a is 0, GCD = b, exit. – Case b >= a analogous I.e., repeat as long as a > 0 and b > 0, then output whichever one is > 0.

14 gcd.py Note string formatting in table built-in function max

15 Running the program Input first integer > 0: 81 Input second integer > 0: 45 a b The greatest common divisor of 81 and 45 is 9

16.. on to the exercises - See course homepage to get to the program examples from the lectures