Input and Output CMSC 120: Visualizing Information Lecture 4/10.
-
Upload
steven-waters -
Category
Documents
-
view
222 -
download
1
description
Transcript of Input and Output CMSC 120: Visualizing Information Lecture 4/10.
![Page 1: Input and Output CMSC 120: Visualizing Information Lecture 4/10.](https://reader035.fdocuments.in/reader035/viewer/2022062905/5a4d1ade7f8b9ab0599762e3/html5/thumbnails/1.jpg)
Input and Output
CMSC 120: Visualizing InformationLecture 4/10
![Page 2: Input and Output CMSC 120: Visualizing Information Lecture 4/10.](https://reader035.fdocuments.in/reader035/viewer/2022062905/5a4d1ade7f8b9ab0599762e3/html5/thumbnails/2.jpg)
ComputingInput DataStoreManipulate DataOutput Data
Input Memory Output
CPU
Types of DataNumbers
LogicObjects
SequencesStrings
Input - OutputDynamic (User)
Stored (Text File)
![Page 3: Input and Output CMSC 120: Visualizing Information Lecture 4/10.](https://reader035.fdocuments.in/reader035/viewer/2022062905/5a4d1ade7f8b9ab0599762e3/html5/thumbnails/3.jpg)
Textstring data type
Sequence of characters
'my string'
![Page 4: Input and Output CMSC 120: Visualizing Information Lecture 4/10.](https://reader035.fdocuments.in/reader035/viewer/2022062905/5a4d1ade7f8b9ab0599762e3/html5/thumbnails/4.jpg)
Basic String Operations>>> 'spam' + 'eggs''spameggs'
>>> 3 * 'spam''spamspamspam '
>>> (3 * 'spam') + (5 * 'eggs') 'spamspamspameggseggseggseggseggs'
>>> len('spam')4
>>> for ch in 'Spam!' print ch
S p a m !
![Page 5: Input and Output CMSC 120: Visualizing Information Lecture 4/10.](https://reader035.fdocuments.in/reader035/viewer/2022062905/5a4d1ade7f8b9ab0599762e3/html5/thumbnails/5.jpg)
Basic String Operations
>>> breakfast = 'SpamAndEggs'
>>> breakfast[0]>>> breakfast[4:7]>>> breakfast[-2]
>>> breakfast[:]>>> breakfast[:4]>>> breakfast[4:]
![Page 6: Input and Output CMSC 120: Visualizing Information Lecture 4/10.](https://reader035.fdocuments.in/reader035/viewer/2022062905/5a4d1ade7f8b9ab0599762e3/html5/thumbnails/6.jpg)
Basic String Operations
Operator Meaning+ Concatenation* Repetition
<string>[] Indexing<string>[:] Slicelen(<string>) Length
for <var> in <string> Iteration through characters
![Page 7: Input and Output CMSC 120: Visualizing Information Lecture 4/10.](https://reader035.fdocuments.in/reader035/viewer/2022062905/5a4d1ade7f8b9ab0599762e3/html5/thumbnails/7.jpg)
String RepresentationNumbers:
Stored in binary notationComputer CPU circuitry designed to manipulate 0s and 1s
Text:Encoded as numbers
ASCII (American Standard): • A-Z = 65-90• a-z = 97-122
Unicode
![Page 8: Input and Output CMSC 120: Visualizing Information Lecture 4/10.](https://reader035.fdocuments.in/reader035/viewer/2022062905/5a4d1ade7f8b9ab0599762e3/html5/thumbnails/8.jpg)
String RepresentationSwitching from character to encoded ordinal
>>> ord('a')97
>>> ord('A')65
>>> chr(97)97
![Page 9: Input and Output CMSC 120: Visualizing Information Lecture 4/10.](https://reader035.fdocuments.in/reader035/viewer/2022062905/5a4d1ade7f8b9ab0599762e3/html5/thumbnails/9.jpg)
Interactive Input and Output
>>> fname = input('Enter name: ')Enter name: 'Emily'
>>> print 'Hello', fnameHello Emily
![Page 10: Input and Output CMSC 120: Visualizing Information Lecture 4/10.](https://reader035.fdocuments.in/reader035/viewer/2022062905/5a4d1ade7f8b9ab0599762e3/html5/thumbnails/10.jpg)
Raw Input
>>> fname = input('Enter name: ')Enter name: Emily
Traceback (most recent call last): File "<pyshell#0>", line 1, in <module>
input('Enter name: ') File "<string>", line 1, in <module>NameError: name 'Emily' is not defined
![Page 11: Input and Output CMSC 120: Visualizing Information Lecture 4/10.](https://reader035.fdocuments.in/reader035/viewer/2022062905/5a4d1ade7f8b9ab0599762e3/html5/thumbnails/11.jpg)
raw_input>>> fname = raw_input('Enter name: ')Enter name: Emily
raw_inputExactly like input, except...it does not evaluate the expressioninput is treated like a string of text
>>> print 'Hello', fnameHello Emily
![Page 12: Input and Output CMSC 120: Visualizing Information Lecture 4/10.](https://reader035.fdocuments.in/reader035/viewer/2022062905/5a4d1ade7f8b9ab0599762e3/html5/thumbnails/12.jpg)
raw_input>>> fname = raw_input('Enter name: ')Enter name: 5
>>> fname'5'
>>> fname = raw_input('Enter name: ')Enter name: Emily Allen
fname'Emily Allen'
String Processing: translating raw input strings into appropriate types of data
![Page 13: Input and Output CMSC 120: Visualizing Information Lecture 4/10.](https://reader035.fdocuments.in/reader035/viewer/2022062905/5a4d1ade7f8b9ab0599762e3/html5/thumbnails/13.jpg)
Simple String Processing# generate a user namedef main():
first = raw_input('Enter first name: ')last = raw_input('Enter last name: ')
# concatenate first initial with # 7 characters of the last nameuname = first[0] + last[:7]
print uname
main()
emily greenfestegreenfe
![Page 14: Input and Output CMSC 120: Visualizing Information Lecture 4/10.](https://reader035.fdocuments.in/reader035/viewer/2022062905/5a4d1ade7f8b9ab0599762e3/html5/thumbnails/14.jpg)
Simple String Processing# generate a user namedef main():
first = raw_input('Enter first name: ')last = raw_input('Enter last name: ')
# concatenate first initial with # 7 characters of the last nameuname = first[0] + last[:7]
print User name is: uname
main()
Enter first name: emily Enter last name: greenfestUser name is: egreenfe
![Page 15: Input and Output CMSC 120: Visualizing Information Lecture 4/10.](https://reader035.fdocuments.in/reader035/viewer/2022062905/5a4d1ade7f8b9ab0599762e3/html5/thumbnails/15.jpg)
Simple String ProcessingStrings are objects!
>>> myName = raw_input('Enter whole name: ')Enter whole name: Emily Greenfest-Allen
>>> myName'Emily Greenfest-Allen'
>>> myName.split()['Emily', 'Greenfest-Allen']
>>> myName.split('-' )['Emily Greenfest', 'Allen']
![Page 16: Input and Output CMSC 120: Visualizing Information Lecture 4/10.](https://reader035.fdocuments.in/reader035/viewer/2022062905/5a4d1ade7f8b9ab0599762e3/html5/thumbnails/16.jpg)
Simple String Processing>>> s = 'Spam and Eggs'
>>> s.capitalize()'Spam and eggs'
>>> s.capwords()'Spam And Eggs'
>>> s.upper()'SPAM AND EGGS'
>>> s.lower ()'spam and eggs'
![Page 17: Input and Output CMSC 120: Visualizing Information Lecture 4/10.](https://reader035.fdocuments.in/reader035/viewer/2022062905/5a4d1ade7f8b9ab0599762e3/html5/thumbnails/17.jpg)
Simple String Processing>>> s = 'Spam and Eggs'
>>> s.replace('and','or')'Spam or Eggs'
>>> s.count('a')2
>>> s.find('E')9
![Page 18: Input and Output CMSC 120: Visualizing Information Lecture 4/10.](https://reader035.fdocuments.in/reader035/viewer/2022062905/5a4d1ade7f8b9ab0599762e3/html5/thumbnails/18.jpg)
Input/Output
String Maninpulation
![Page 19: Input and Output CMSC 120: Visualizing Information Lecture 4/10.](https://reader035.fdocuments.in/reader035/viewer/2022062905/5a4d1ade7f8b9ab0599762e3/html5/thumbnails/19.jpg)
Date ConversionUser enters a date in mm/dd/yyyy format (dateStr)
Split dateStr into month, day, and year strings
Convert the month string into a month number
Use the month to look up the month name
Create a new date string in form Month Day, Year
Output new date string
![Page 20: Input and Output CMSC 120: Visualizing Information Lecture 4/10.](https://reader035.fdocuments.in/reader035/viewer/2022062905/5a4d1ade7f8b9ab0599762e3/html5/thumbnails/20.jpg)
Date Conversion# User enters a date in mm/dd/yyyy format (dateStr)dateStr = raw_input('Enter date (mm/dd/ yyyy): ')
# Split dateStr into month, day, and year stringsmonthStr, dayStr, yearStr = dateStr.split('/')
Enter date (mm/dd/ yyyy): 05/07/1977>>> print monthStr, dayStr, yearStr05 07 1977
monthStr'05'
![Page 21: Input and Output CMSC 120: Visualizing Information Lecture 4/10.](https://reader035.fdocuments.in/reader035/viewer/2022062905/5a4d1ade7f8b9ab0599762e3/html5/thumbnails/21.jpg)
String Conversion
>>> int('5')5
>>> float('5')5.0
>>> string(2.8)'2.8'
>>> eval('5 + 2')7
![Page 22: Input and Output CMSC 120: Visualizing Information Lecture 4/10.](https://reader035.fdocuments.in/reader035/viewer/2022062905/5a4d1ade7f8b9ab0599762e3/html5/thumbnails/22.jpg)
Date Conversion# convert month string to month namemonths = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']
monthName = months[int(monthStr) – 1]
# output in form Month Day, Yearprint monthName, dayStr + ',', yearStr
Enter date (mm/dd/ yyyy): 05/07/1977May 7, 1977