SlideServe
Menu
Préférences
© SlideServe · v2026

Input and Output

robertsonn avatar
22 diapositives · 278 Views · Jan 03, 2020

Understand basic string operations and manipulations, from concatenation to conversion, to enhance data processing and analysis efficiency in programming.

Input and Output - slide 1
01 / 22Input and Output
1 / 17

Télécharger la présentation

Input and Output
Une image / un lien ci-dessous est fourni (tel quel) pour télécharger la présentation

Politique de téléchargement : le contenu du site vous est fourni TEL QUEL pour votre information et votre usage personnel ; il ne peut être vendu, concédé sous licence ni partagé sur d’autres sites sans l’accord de son auteur.Le contenu vous est fourni TEL QUEL, pour votre information et votre usage personnel uniquement. Téléchargez la présentation en cliquant sur ce lien. Lors du téléchargement, si vous ne parvenez pas à télécharger une présentation, l’éditeur a peut-être supprimé le fichier de son serveur.Pendant le téléchargement, si vous ne parvenez pas à obtenir une présentation, le fichier a peut-être été supprimé par l’éditeur.

F I N

Plus de contenu associé

Transcription de la présentation
  1. Input and Output CMSC 120: Visualizing Information Lecture 4/10

  2. Computing • Input Data • Store • Manipulate Data • Output Data Types of Data Numbers Logic Objects Sequences Strings Input - Output Dynamic (User) Stored (Text File) Input Memory Output CPU

  3. Text • string data type • Sequence of characters • 'my string'

  4. Basic String Operations >>> 'spam' + 'eggs' 'spameggs' >>> 3 * 'spam' 'spamspamspam ' >>> (3 * 'spam') + (5 * 'eggs') 'spamspamspameggseggseggseggseggs' >>> len('spam') 4 >>> forchin'Spam!' print ch S p a m !

  5. Basic String Operations >>> breakfast = 'SpamAndEggs' >>> breakfast[0] >>> breakfast[4:7] >>> breakfast[-2] >>> breakfast[:] >>> breakfast[:4] >>> breakfast[4:]

  6. Basic String Operations

  7. String Representation • Numbers: • Stored in binary notation • Computer CPU circuitry designed to manipulate 0s and 1s • Text: • Encoded as numbers • ASCII (American Standard): • A-Z = 65-90 • a-z = 97-122 • Unicode

  8. String Representation • Switching from character to encoded ordinal >>> ord('a') 97 >>> ord('A') 65 >>> chr(97) 97

  9. Interactive Input and Output >>> fname = input('Enter name: ') Enter name: 'Emily' >>> print'Hello',fname Hello Emily

  10. 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

  11. raw_input >>> fname = raw_input('Enter name: ') Enter name: Emily • raw_input • Exactly like input, except... • it does not evaluate the expression • input is treated like a string of text >>> print'Hello',fname Hello Emily

  12. 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

  13. Simple String Processing # generate a user name def main(): first = raw_input('Enter first name: ') last = raw_input('Enter last name: ') # concatenate first initial with # 7 characters of the last name uname = first[0] + last[:7] printuname main() emilygreenfest egreenfe

  14. Simple String Processing # generate a user name def main(): first = raw_input('Enter first name: ') last = raw_input('Enter last name: ') # concatenate first initial with # 7 characters of the last name uname = first[0] + last[:7] print User name is: uname main() Enter first name: emily Enter last name: greenfest User name is: egreenfe

  15. Simple String Processing • Strings 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']

  16. 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'

  17. Simple String Processing >>> s = 'Spam and Eggs' >>> s.replace('and','or') 'Spam or Eggs' >>> s.count('a') 2 >>> s.find('E') 9

  18. Input/Output String Maninpulation

  19. Date Conversion • User 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

  20. 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 strings monthStr, dayStr, yearStr = dateStr.split('/') Enter date (mm/dd/ yyyy): 05/07/1977 >>> print monthStr, dayStr, yearStr 05 07 1977 monthStr '05'

  21. String Conversion >>> int('5') 5 >>> float('5') 5.0 >>> string(2.8) '2.8' >>> eval('5 + 2') 7

  22. Date Conversion # convert month string to month name months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'] monthName = months[int(monthStr) – 1] # output in form Month Day, Year print monthName, dayStr + ',', yearStr Enter date (mm/dd/ yyyy): 05/07/1977 May 7, 1977