Sequences Sequences are collections of data values that are ordered by position A string is a sequence of characters A list is a sequence of any Python.

Slides:



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

CHAPTER 4 AND 5 Section06: Sequences. General Description "Normal" variables x = 19  The name "x" is associated with a single value Sequence variables:
Strings An extension of types A class that encompasses a character array and provides many useful behaviors Chapter 9 Strings are IMMUTABLE.
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.
Computer Science 111 Fundamentals of Programming I Sequences: Strings.
October 4, 2005ICP: Chapter 4: For Loops, Strings, and Tuples 1 Introduction to Computer Programming Chapter 4: For Loops, Strings, and Tuples Michael.
Lists in Python.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 9 More About Strings.
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.
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. Chapter 8 More on Strings and Special Methods 1.
Python Programming Chapter 7: Strings Saad Bani Mohammad Department of Computer Science Al al-Bayt University 1 st 2011/2012.
Strings CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Strings The Basics. Strings can refer to a string variable as one variable or as many different components (characters) string values are delimited by.
Strings CS303E: Elements of Computers and Programming.
Built-in Data Structures in Python An Introduction.
Introduction to Strings Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg 1.
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.
Mr. Fowler Computer Science 14 Feb 2011 Strings in Python.
Strings The Basics. Strings a collection data type can refer to a string variable as one variable or as many different components (characters) string.
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.
COMPE 111 Introduction to Computer Engineering Programming in Python Atılım University
Python Strings. String  A String is a sequence of characters  Access characters one at a time with a bracket operator and an offset index >>> fruit.
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.
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.
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.
PYTHON PROGRAMMING. WHAT IS PYTHON?  Python is a high-level language.  Interpreted  Object oriented (use of classes and objects)  Standard library.
Guide to Programming with Python Chapter Four Strings, and Tuples; for Loops: The Word Jumble Game.
Unit 4 – Chapter 4 Strings and Tuples. len() Function You can pass any sequence you want to the len() function and it will return the length of the sequence.
Section06: Sequences Chapter 4 and 5. General Description "Normal" variables x = 19 – The name "x" is associated with a single value Sequence variables:
The Methods and What You Need to Know for the AP Exam
String and Lists Dr. José M. Reyes Álamo.
Python unit_4 review Tue/Wed, Dec 1-2
Generating Random Numbers
Tuples and Lists.
Containers and Lists CIS 40 – Introduction to Programming in Python
Section 6: Sequences Chapter 4 and 5.
Strings Part 1 Taken from notes by Dr. Neil Moore
Lists Part 1 Taken from notes by Dr. Neil Moore & Dr. Debby Keen
Introduction to Strings
Lists in Python.
Manipulating Characters
Fundamentals of Programming I Number Systems
Chapter 8 More on Strings and Special Methods
Chapter 8 More on Strings and Special Methods
Python - Strings.
Data types Numeric types Sequence types float int bool list str
Chapter 8 More on Strings and Special Methods
CEV208 Computer Programming
String and Lists Dr. José M. Reyes Álamo.
Topics Sequences Introduction to Lists List Slicing
Fundamentals of Python: First Programs
Introduction to Strings
functions: argument, return value
CS1110 Today: collections.
15-110: Principles of Computing
CISC101 Reminders Assignment 2 due today.
Topics Basic String Operations String Slicing
Introduction to Computer Science
Topics Sequences Introduction to Lists List Slicing
Introduction to Strings
Python Review
Topics Basic String Operations String Slicing
By Himanshi dixit 11th ’B’
Introduction to Strings
CMPT 120 Lecture 10 – Unit 2 – Cryptography and Encryption –
Strings Taken from notes by Dr. Neil Moore & Dr. Debby Keen
Topics Basic String Operations String Slicing
Introduction to PYTHON
Presentation transcript:

Sequences Sequences are collections of data values that are ordered by position A string is a sequence of characters A list is a sequence of any Python data values A tuple is like a list but cannot be modified Retrieved from: http://home.wlu.edu/~lambertk/classes/111/

Examples Strings contains characters a = 'apple' b = 'banana' print(a, b) # Displays apple banana fruits = (a, b) # A tuple print(fruits) # Displays ('apple', 'banana') veggies = ['bean', 'lettuce'] # A list print(veggies) # Displays ['bean', 'lettuce'] Strings contains characters Tuples and lists can contain anything

String Assignment, Concatenation, and Comparisons a = 'apple' b = 'banana' print(a + b) # Displays applebanana print(a == b) # Displays False print(a < b) # Displays True Strings can be ordered like they are in a dictionary

Positions or Indexes 'Hi there!' 0 1 2 3 4 5 6 7 8 Each character in a string has a unique position called its index We count indexes from 0 to the length of the string minus 1 A for loop automatically visits each character in the string, from beginning to end for ch in 'Hi there!': print(ch)

Traversing with a for Loop 'Hi there!' H i t h e r e ! 0 1 2 3 4 5 6 7 8 A for loop automatically visits each character in the string, from beginning to end for ch in 'Hi there!': print(ch, end='') # Prints Hi there!

Summing with Strings 'Hi there!' 0 1 2 3 4 5 6 7 8 Start with an empty string and add characters to it with + noVowels = '' for ch in 'Hi there!': if not ch in ('a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'): noVowels += ch print(noVowels) # Prints H thr!

The Subscript Operator 'Hi there!' H i t h e r e ! 0 1 2 3 4 5 6 7 8 Alternatively, any character can be accessed using the subscript operator [] This operator expects an int from 0 to the length of the string minus 1 Example: Syntax: 'Hi there!'[0] # equals 'H' <a string>[<an int>]

The len Function 'Hi there!' 0 1 2 3 4 5 6 7 8 The len function returns the length of any sequence >>> len('Hi there!') 9 >>> s = 'Hi there!' >>> s[len(s) - 1] '!'

An Index-Based Loop 'Hi there!' 0 1 2 3 4 5 6 7 8 If you need the positions during a loop, use the subscript operator s = 'Hi there!' for ch in s: print(ch) for i in range(len(s)): print(i, s[i])

Oddball Indexes 'Hi there!' To get to the last character in a string: 0 1 2 3 4 5 6 7 8 To get to the last character in a string: s = 'Hi there!' print(s[len(s) - 1]) # Displays !

Oddball Indexes 'Hi there!' To get to the last character in a string: 0 1 2 3 4 5 6 7 8 To get to the last character in a string: s = 'Hi there!' print(s[len(s) - 1]) # or, believe it or not, print(s[-1]) A negative index counts backward from the last position in a sequence

Slicing Strings Extract a portion of a string (a substring) s = 'Hi there!' print(s[0:]) # Displays Hi there! print(s[1:]) # Displays i there! print(s[:2]) # Displays Hi (two characters) print(s[0:2]) # Displays Hi (two characters) The number to the right of : equals one plus the index of the last character in the substring

String Methods s = 'Hi there!' print(s.find('there')) # Displays 3 print(s.upper()) # Displays HI THERE! print(s.replace('e', 'a')) # Displays Hi thara! print(s.split()) # Displays ['Hi', 'there!'] A method is like a function, but the syntax for its use is different: <a string>.<method name>(<any arguments>)

String Methods A sequence of items in [ ] is a Python list s = 'Hi there!' print(s.split()) # Displays ['Hi', 'there!'] A sequence of items in [ ] is a Python list

Characters in Computer Memory Each character translates to a unique integer called its ASCII value (American Standard for Information Interchange) Basic ASCII ranges from 0 to 127, for 128 keyboard characters and some control keys

The Basic ASCII Character Set

The ord and chr Functions ord converts a single-character string to its ASCII value chr converts an ASCII value to a single-character string print(ord('A')) # Displays 65 print(chr(65)) # Displays A for ascii in range(128): # Display 'em all print(ascii, chr(ascii))

Data Encryption A really simple (and quite lame) encryption algorithm replaces each character with its ASCII value and a space source = "I won't be here Friday!" code = "" for ch in source: code = code + str(ord(ch)) + " " print(code) # Displays 73 32 119 111 110 39 116 32 98 101 32 104 101 114 101 32 70 # 114 105 100 97 121 33

Data Decryption To decrypt an encoded message, we split it into a list of substrings and convert these ASCII values to the original characters source = "" for ascii in code.split(): source = source + chr(int(ascii)) print(source) # Displays I won't be here Friday!