1 / 22

Bell Ringer

Bell Ringer. What types are numbers are there is the python programming language?. Objective: Converting data to other forms of data and inputting data. Agenda Housekeeping/Bell ringer How to convert data and inputting data notes Programming assignments Wrap up. Escape Sequences.

hovan
Download Presentation

Bell Ringer

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Bell Ringer • What types are numbers are there is the python programming language?

  2. Objective: Converting data to other forms of data and inputting data Agenda Housekeeping/Bell ringer How to convert data and inputting data notes Programming assignments Wrap up

  3. Escape Sequences \\ Backslash. Prints one backslash. \’ Single Quote. Prints a single quote. \” Double quote. Prints a double quote. \a Bell. Sounds the bell system. \b Backspace. Moves cursor back one space. \n Newline. Moves cursor to beginning of next line. \t Horizontal tab. Moves cursor forward to tab.

  4. String helpers: Concatenating • “ “ + “ “ • print “cat” + “ dog” • catdog

  5. Repeating strings print “dog” * 10 Suppressing a new line print “How are you”, print “I am fine.”

  6. Using functions to change data types: float to int • Float() How to use it? a = 24 b = float (a) Print a Output 24 Print b Output 24.

  7. How to do it? • Float to intint() >>>c = 38.0 >>> d= int (c) >>>c 38.0 >>>d 38

  8. One issue with float to int >>>e= 54.99 >>>f= int(e) >>> print e 54.99 >>>print f 54 The int() function always rounds down.

  9. Changing string to float >>>a = “76.3” >>>b = float (a) >>>print a “76.3” >>>print b 76.299999999999997

  10. Get type from program with type() >>>a = “44.2” >>> b = 44.2 >>>type (a) <type ‘str’> >>>type(b) <type ‘float’>

  11. Of Course!! Remember Int and float can only be numbers!!!!!

  12. Using Type Conversions • In our Temp Conversion Program, you could have used: • Celsius = float(5)/9 * (F-32) • Or • Celsius = 5/float(9) * (F-32)

  13. Input function • raw_input() Gets input from user >>>print “What is your name?” >>>someName= raw_input() >>>print “Hi!,” someName, “ what are you doing?”

  14. Ends up with Enter your name: Henry Hi! Henry What are you doing?

  15. Get question and answer on one line—use comma >>>print “Enter your name:”, >>>yourName=raw_input() Output: Enter your name: Harold

  16. Can also use this for >>>print “My ”, >>>print “dog’s ”, >>>print “name ”, >>>print “is skipper.” Ouput: My dog’s name is Skipper.

  17. Short cut for raw_input() prompts >>>youName = raw_input (“Enter your name: ”) Raw input function will print Enter your name

  18. Inputting numbers >>>temperature_string =raw_input() >>>fahrenheit = float(temperature_string) Or shortcut: fahrenheit= float(raw input())

  19. Example >>>print “Fahrenheit to Celsius Conversion Program.” >>>print “Type in a temperature(f): ”, >>>f= float(raw_input()) >>>celsius = (f-32)*5.0/9 >>>print “That is”, >>>print celsius, >>>print “degrees celsius.”

  20. Input from the web >>>import urllib >>>file = urllib.urlopen(http://helloworldbook.com/data/message.txt) >>>message = file.read() >>>print message (Need direct internet connection)

  21. String methods • upper () Returns the uppercase version • lower () Returns lowercase version • swapcase() Returns a new string where the case of each character switches • capitalize() Returns a new string were the first letter is capitalized and the rest are lowercase. • title() First letter of each word is capitalized

  22. More string methods • Strip() Returns a string where all the white space at the beginning and end is removed. • Replace(old, new[.max]) Returns a string where occurrances of the string old are replaced with the sring new. The optional max limits the number of replacements.

More Related