Review: A Structural View program modules -> main program -> functions -> libraries statements -> simple statements -> compound statements expressions.

Slides:



Advertisements
Similar presentations
Container Types in Python
Advertisements

CHAPTER 4 AND 5 Section06: Sequences. General Description "Normal" variables x = 19  The name "x" is associated with a single value Sequence variables:
Chapter 6 Lists and Dictionaries CSC1310 Fall 2009.
Chapter 7 Strings F To process strings using the String class, the StringBuffer class, and the StringTokenizer class. F To use the String class to process.
Ch 8. Characters and Strings Timothy Budd 2 Characters and Literals Strings Char in C++ is normally an 8-bit quantity, whereas in Java it is a 16-bit.
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.
DICTIONARIES. The Compound Sequence Data Types All of the compound data types we have studies in detail so far – strings – lists – Tuples They are sequence.
Lists Introduction to Computing Science and Programming I.
What is a scripting language? What is Python?
CMPT 120 Lists and Strings Summer 2012 Instructor: Hassan Khosravi.
Lilian Blot CORE ELEMENTS COLLECTIONS & REPETITION Lecture 4 Autumn 2014 TPOP 1.
January 24, 2006And Now For Something Completely Different 1 “And Now For Something Completely Different” A Quick Tutorial of the Python Programming Language.
CS 100: Roadmap to Computing Fall 2014 Lecture 01.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 9 More About Strings.
CS190/295 Programming in Python for Life Sciences: Lecture 3 Instructor: Xiaohui Xie University of California, Irvine.
Hossain Shahriar Announcement and reminder! Tentative date for final exam need to be fixed! Topics to be covered in this lecture(s)
I Power Int 2 Computing Software Development High Level Language Constructs.
Built-in Data Structures in Python An Introduction.
Getting Started with Python: Constructs and Pitfalls Sean Deitz Advanced Programming Seminar September 13, 2013.
Data Collections: Lists CSC 161: The Art of Programming Prof. Henry Kautz 11/2/2009.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 8 Lists and Tuples.
Lists CS303E: Elements of Computers and Programming.
More Strings CS303E: Elements of Computers and Programming.
Lecture 04 – Models of memory Mutable and immutable data.
10 – Java Script (3) Informatics Department Parahyangan Catholic University.
C++ String Class nalhareqi©2012. string u The string is any sequence of characters u To use strings, you need to include the header u The string is one.
Loops & List Intro2CS – week 3 1. Loops -- Motivation Sometimes we want to repeat a certain set of instructions more than once. The number of repetitions.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
Python Mini-Course University of Oklahoma Department of Psychology Day 3 – Lesson 11 Using strings and sequences 5/02/09 Python Mini-Course: Day 3 – Lesson.
Guide to Programming with Python Chapter Five Lists and dictionaries (data structure); The Hangman Game.
A Tutorial on the Python Programming Language. Overview Running Python and Output Data Types Input and File I/O Control Flow Functions.
LISTS and TUPLES. Topics Sequences Introduction to Lists List Slicing Finding Items in Lists with the in Operator List Methods and Useful Built-in Functions.
Python Data Structures By Greg Felber. Lists An ordered group of items Does not need to be the same type – Could put numbers, strings or donkeys in the.
Strings CSE 1310 – Introduction to Computers and Programming Alexandra Stefan University of Texas at Arlington 1.
Strings … operators Up to now, strings were limited to input and output and rarely used as a variable. A string is a sequence of characters or a sequence.
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.
PYTHON PROGRAMMING. WHAT IS PYTHON?  Python is a high-level language.  Interpreted  Object oriented (use of classes and objects)  Standard library.
Indentations makes the scope/block Function definition def print_message (): print “hello” Function usages print_message () hubo.move ()// hubo is a class.
Section06: Sequences Chapter 4 and 5. General Description "Normal" variables x = 19 – The name "x" is associated with a single value Sequence variables:
String and Lists Dr. José M. Reyes Álamo.
Section06: Sequences Chapter 4 and 5.
IGCSE 4 Cambridge Data types and arrays Computer Science Section 2
CS Computer Science IA: Procedural Programming
Computer Programming Fundamentals
CS 100: Roadmap to Computing
Lesson 07: Strings Topic: Introduction to Programming, Zybook Ch 6, P4E Ch 6. Slides on website.
Section 6: Sequences Chapter 4 and 5.
Python Data Types Expressions, Variables, and Assignments Strings
Chapter 7: Strings and Characters
Basic Input and Output C++ programs can read and write information using streams A simple input stream accepts typed data from a keyboard A simple output.
Data types Numeric types Sequence types float int bool list str
8 – Lists and tuples John R. Woodward.
4. sequence data type Rocky K. C. Chang 16 September 2018
String and Lists Dr. José M. Reyes Álamo.
Topics Sequences Introduction to Lists List Slicing
Basic String Operations
CS190/295 Programming in Python for Life Sciences: Lecture 3
CS1110 Today: collections.
Topics Sequences Lists Copying Lists Processing Lists
Topics Introduction to File Input and Output
Topics Basic String Operations String Slicing
Introduction to Computer Science
Topics Sequences Introduction to Lists List Slicing
CS 100: Roadmap to Computing
Topics Basic String Operations String Slicing
For loop Using lists.
Python Open source Many applications Python 3 jupyter notebook
Topics Basic String Operations String Slicing
Introduction to PYTHON
Introduction to Computer Science
Presentation transcript:

Review: A Structural View program modules -> main program -> functions -> libraries statements -> simple statements -> compound statements expressions -> operators -> expressions objects -> data model

Data Model -> data is an object that has: a memory location a type a value (could be “empty”) -> type defines: the possible values for that type the operations that the type supports the object’s mutability mutable -> values can change in place immutable -> values can not be changed in place

Data Types TYPE (constructor) EXAMPLES Numbers: Boolean (bool)True, False Integer (int)-99, 0, 156 Floating point (float)-34.65, 0.0, Sequences: Range (range) range(stop); range(start, stop[, step]) - generates a sequence of numbers Strings (str)"", '123', "Bob's", '''abc's and "yes" ''' Lists (list) -> mutable [], [1,2,3], ['abc', 53, yes] Other: NoneNone - used to signify the absence of a value Filesopen('data.txt', 'r'), open('sales.txt', 'a') type_variable = constructor(compatible items)

Numbers integer - have no fractional part - unlimited precision int_variable = int(compatible items) float - have a fractional part - precision depends on system float_variable = float(compatible items) boolean - True, False

Numeric Tools Built-in Functions abs(number)return absolute value of number Math Library math.sqrt(number)return square root of number math.pimathematical constant, , to available precision … others as required Random Library random.randint(a, b)return a random integer n such that a <= n <= b random.random()return a random float n such that 0.0 <= n < 1.0 random.uniform(a, b)return a random float n such that a <= n <= b

Sequences -items have a left-to-right positional order -index numbering starts at 0 from left -index numbering starts at -1 from right name = "Fred" print(name[0]) fruit = ["apple", "orange", "kiwi"] print(fruit[-1]) data = range(10,101,10) print(data[1])

Sequences type_variable = constructor(compatible items) string_variable = str(compatible items) - returns the printable version of items list_variable = list(compatible items) range_variable = range(compatible items)

Common Sequence Operations OperationResult x in seq x not in seq True if x is equal to item in seq; ** False if x is equal to item in seq item1 + item2concatenation *** item * nconcatenate n copies of item *** (repetition) seq[ i ]item i of seq, numbering starts at 0 seq[ i:j ] seq[ i:j:k ] slice of seq from i to j slice of seq from i to j with step k len(seq)length of seq Methods seq.index(item)index of the first occurrence of item in seq seq.count(item)number of occurrences of item in seq ** If seq is str, can be used for substring testing, e.g. “gl” in “glow”. *** Not valid for range type.

Mutable Sequence Operations OperationResult seq[ i ] = itemreplace contents at position i of seq with item seq[ i:j ] = s seq[ i:j:k ] = s replace slice of seq from i to j (step k) with contents of s del seq[ i:j ] del seq[ i:j:k ] delete elements in the slice from seq Methods seq.append(item)appends item to the end of seq seq.extend(s)appends contents of s to the end of seq seq.insert(i,item)insert item at position i in seq item = seq.pop(i)return and remove element at position i from seq seq.remove(item)find item in seq and remove it (first one)

File Operations OperationResult fvn = open(name, 'r')-opens name (str) for reading -file must exist -file pointer points to start of file -returns file variable name fvn = open(name, 'w')-opens/creates name (str) for writing -if file exists, it is overwritten -file pointer points to start of file -returns file variable name fvn = open(name, 'a')-opens/creates name (str) for append -if file exists, add to end of file -file pointer points to end of file -returns file variable name

File Operations MethodsResult fvname.close()flushes output buffer to disk and closes the file line = fvn.readline()return next line including newline; file pointer points to next line fvname.seek(n) fvname.seek(0) move to the nth character in file - position file pointer to start of file