22
Bell Ringer What types are numbers are there is the python programming language?

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

Embed Size (px)

Citation preview

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

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

String helpers: Concatenating

• “ “ + “ “

• print “cat” + “ dog”

• catdog

Repeating strings

print “dog” * 10

Suppressing a new lineprint “How are you”,

print “I am fine.”

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.

How to do it?

• Float to int int()

>>>c = 38.0

>>> d= int (c)

>>>c

38.0

>>>d

38

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’>

Of Course!!

Remember

Int and float can only be numbers!!!!!

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?”

Ends up with

Enter your name:

Henry

Hi! Henry 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

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.