Lists and More About Strings CS303E: Elements of Computers and Programming.

Slides:



Advertisements
Similar presentations
ThinkPython Ch. 10 CS104 Students o CS104 n Prof. Norman.
Advertisements

Strings Testing for equality with strings.
Introduction to PHP MIS 3501, Fall 2014 Jeremy Shafer
CS0007: Introduction to Computer Programming Introduction to Classes and Objects.
String and Lists Dr. Benito Mendoza. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list List.
IT151: Introduction to Programming
AAA. Today’s lecture Review of Chapter 6 Go over examples.
Lists Introduction to Computing Science and Programming I.
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
Intro to Java Part II. Calling an Objects Methods Use qualified names to call the objects methods. To form – you append the method name to an object reference.
CSE 2501 Review Declaring a variable allocates space for the type of datum it is to store int x; // allocates space for an int int *px; // allocates space.
CS1061: C Programming Lecture 23: Review; Examination Details A. O’Riordan, 2005.
Comparing Data Comparing More than Numbers. Comparing Data When comparing data using boolean expressions, it's important to understand the nuances of.
Pointers and Dynamic Variables. Objectives on completion of this topic, students should be able to: Correctly allocate data dynamically * Use the new.
Garbage Collection CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
CH1 – A 1 st Program Using C#. Program Set of instructions which tell a computer what to do. Machine Language Basic language computers use to control.
1 Chapter One A First Program Using C#. 2 Objectives Learn about programming tasks Learn object-oriented programming concepts Learn about the C# programming.
3.1 Documentation & Java Language Elements Purpose of documentation Assist the programmer with developing the program Assist other programers who.
A First Program Using C#
Lists in Python.
Handling Lists F. Duveau 16/12/11 Chapter 9.2. Objectives of the session: Tools: Everything will be done with the Python interpreter in the Terminal Learning.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
CSC 253 Lecture 2. Some differences between Java and C  Compiled C code is machine specific, whereas Java compiles for a virt. machine.  Virtual machines.
Structured programming 3 Day 33 LING Computational Linguistics Harry Howard Tulane University.
Chapter 4 Procedural Methods. Learning Java through Alice © Daly and Wrigley Objectives Identify classes, objects, and methods. Identify the difference.
1 Data Comparisons and Switch Data Comparisons Switch Reading for this class: L&L 5.3,
COMPSCI 101 Principles of Programming Lecture 25 – Nested loops, passing mutable objects as parameters.
Lists CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Examples of comparing strings. “ABC” = “ABC”? yes “ABC” = “ ABC”? No! note the space up front “ABC” = “abc” ? No! Totally different letters “ABC” = “ABCD”?
Object-Oriented Programming in C++
COM S 207 Method Instructor: Ying Cai Department of Computer Science Iowa State University
Introducing Python CS 4320, SPRING Lexical Structure Two aspects of Python syntax may be challenging to Java programmers Indenting ◦Indenting is.
Session 7 Methods Strings Constructors this Inheritance.
1 Workin’ with Pointas An exercise in destroying your computer.
Identifiers Identifiers in Java are composed of a series of letters and digits where the first character must be a letter. –Identifiers should help to.
Lists CS303E: Elements of Computers and Programming.
More about Strings. String Formatting  So far we have used comma separators to print messages  This is fine until our messages become quite complex:
More Strings CS303E: Elements of Computers and Programming.
Finalizers, this reference and static Sangeetha Parthasarathy 06/13/2001.
Coding Bat: Ends in ly Given a string of even length, return a string made of the middle two chars, so the string "string" yields "ri". The string.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
COMPE 111 Introduction to Computer Engineering Programming in Python Atılım University
OCR Computing GCSE © Hodder Education 2013 Slide 1 OCR GCSE Computing Python programming 3: Built-in functions.
Lists in Python Lists as Arguments/Parameters. Lists as arguments to functions Just like other data types, lists can be sent to functions as arguments.
LECTURE 5 Strings. STRINGS We’ve already introduced the string data type a few lectures ago. Strings are subtypes of the sequence data type. Strings are.
Functions with Arguments and Return Values, Oh My! CS303E: Elements of Computers and Programming.
Winter 2016CISC101 - Prof. McLeod1 CISC101 Reminders Quiz 3 this week – last section on Friday. Assignment 4 is posted. Data mining: –Designing functions.
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.
Objects and Memory Mehdi Einali Advanced Programming in Java 1.
Recap Resizing the Vector Push_back function Parameters passing Mechanism Primitive Arrays of Constants Multidimensional Arrays The Standard Library string.
String and Lists Dr. José M. Reyes Álamo.
Fundamentals of Programming I Overview of Programming
Computer Organization and Design Pointers, Arrays and Strings in C
String Methods Programming Guides.
Unit-2 Objects and Classes
Representing Characters
Chapter 4 Procedural Methods.
Chapter 12: Text Processing
Chapter 12: Text Processing
4. sequence data type Rocky K. C. Chang 16 September 2018
CEV208 Computer Programming
String and Lists Dr. José M. Reyes Álamo.
Assessment – Java Basics: Part 1
Chapter 7 Procedural Methods.
Functions By Anand George.
String Class.
Introduction to Computer Science
Introduction to Computer Science
PYTHON - VARIABLES AND OPERATORS
Presentation transcript:

Lists and More About Strings CS303E: Elements of Computers and Programming

Functions vs. Methods We’ve been looking at string functions We’ve been looking at string functions –take the string as an argument: string. (, ) Now, we are going to look at methods Now, we are going to look at methods Methods are called on objects: Methods are called on objects:<objectName>.<methodName>(<args>) So a string method… So a string method…

String Methods Strings methods are called on a string: Strings methods are called on a string:s=“hello” s.isalnum() #isalnum() is a method

Sample String Methods MethodDescription s.isalnum()Returns True if s contains only letters and digits s.isdigit()Returns True if s contains only digits s.isalpha()Returns True if s contains only letters s.isspace()Returns True if s contains only whitespace s.islower()Returns True if all letters in s are lowercase s.isupper()Returns True if all letters in s are uppercase s.endswith( )Returns True if s ends with s.startswith( )Returns True if s ends with s.count( )Returns number of times appears in s s.strip()Returns a new string with leading and trailing whitespace characters removed s.rstrip()Returns a new string with trailing whitespace characters removed s.lstrip()Returns a new string with leading whitespace characters removed

Question: (Do not call out your answer!) What is the difference between a method and a function? A. Methods are called on objects; functions are not B. Functions perform a set of tasks C. Methods are for the madness

Example: String Methods Write a segment of code that prints “all lower case letters” if a string, s, consists of only letters and all are lower case, “all upper case letters” if s consists of only letters and all letters are uppercase, and prints “whitespace” is s is all whitespace.

Lists as Parameters When a list is a parameter to a function, that function has the ability to change its value in the calling function When a list is a parameter to a function, that function has the ability to change its value in the calling function Let’s see why… Let’s see why…

Lists as Parameters: Summary Lists are not passed into the function in the typical way Lists are not passed into the function in the typical way Instead, a pointer to the list is passed Instead, a pointer to the list is passed As a result, the function has access to the same list As a result, the function has access to the same list

Question: If lists, files, and strings are all objects, and objects can be modified in functions, why can’t strings be modified in functions? A. Strings aren’t objects B. Functions are irrelevant, strings are immutable C. They can be!

Memory Management Variables and their values take up space in memory Variables and their values take up space in memory Memory space is limited Memory space is limited To save memory, a program should clean up any values it has finished using To save memory, a program should clean up any values it has finished using –Many languages: the programmer must do this explicitly, using delete or free

Garbage Collection Other languages manage the memory for you Other languages manage the memory for you Garbage collectors collect any values no longer being used Garbage collectors collect any values no longer being used –Python, Java

Group Exercise Write a function that takes a list and sorts it in ascending order. Do not use the Python sort() method.