Upload
brooke-barrett
View
213
Download
0
Embed Size (px)
Citation preview
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
\\ 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.
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.
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.
Changing string to float
>>>a = “76.3”
>>>b = float (a)
>>>print a
“76.3”
>>>print b
76.299999999999997
Get type from program with type()
>>>a = “44.2”
>>> b = 44.2
>>>type (a)
<type ‘str’>
>>>type(b)
<type ‘float’>
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)
Input function
• raw_input() Gets input from user
>>>print “What is your name?”
>>>someName= raw_input()
>>>print “Hi!,” someName, “ what are you doing?”
Get question and answer on one line—use comma
>>>print “Enter your name:”,
>>>yourName=raw_input()
Output:
Enter your name: Harold
Can also use this for
>>>print “My ”,
>>>print “dog’s ”,
>>>print “name ”,
>>>print “is skipper.”
Ouput:
My dog’s name is Skipper.
Short cut for raw_input() prompts
>>>youName = raw_input (“Enter your name: ”)
Raw input function will print Enter your name
Inputting numbers
>>>temperature_string =raw_input()
>>>fahrenheit = float(temperature_string)
Or shortcut:
fahrenheit= float(raw input())
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.”
Input from the web
>>>import urllib
>>>file = urllib.urlopen(http://helloworldbook.com/data/message.txt)
>>>message = file.read()
>>>print message
(Need direct internet connection)
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