26 Apr 2012

School

Department

Course

Professor

Variables

Strings, Floats, Integers

print "*** Adding Program ***"

x = raw_input("Enter the 1st number to add:")

y = raw_input("Enter the 2nd number to add:")

sum = x + y

print "Sum:" + sum

•And the corresponding program output (user input in bold)

*** Adding Program ***

Enter the 1st number to add:2

Enter the 2nd number to add:3

Sum:23

Our program is telling us that 2 + 3 = 23

•When we added the variables x and y together with the + operator, they were not added

mathematically, but rather joined together

•The reason this happens is because what we get from the user via raw_input is stored as a

string and not as a number

•A string is a sequence of characters

•Characters are anything that you can type on a keyboard:

-Letters

-Digits

-Spaces

-Punctuation

•When you add two strings together with the + operator, the strings are attached together

•So, if we type two words into our calculator (e.g. "pot" and "ash"), then the result will be the

two words joined together (e.g. "potash")

*** Adding Program ***

Enter the 1st number to add:pot

Enter the 2nd number to add:ash

Sum:potash

•If we want Python to treat x and y as numbers, we will have to tell it to do this

•To treat x and y as numbers, we will have to convert them from strings to numbers

-That is, we have to change their data type

•All variables have a specific data type

•The data type of a variable lets Python know how to treat the variable

E.g. Adding two strings together will attach them together, whereas adding two numbers

together will mathematically add them

•String (str)

-Stores a sequence of characters

•Integer (int)

-Stores a whole number (no decimal point)

•Floating point (float)

-Stores a number with a decimal point

•Boolean (bool)

-Stores either True or False

•For the calculator, x and y are numbers, so we could store them in either integers or floats

Here's how to convert x and y to floats

x = float(raw_input("Enter the 1st number to add:"))

y = float(raw_input("Enter the 2nd number to add:"))

•Similarly, if we wanted to convert them to integers, we would do this:

x = int(raw_input("Enter the 1st number to add:"))

y = int(raw_input("Enter the 2nd number to add:"))

•Now, our full program looks like this:

print "*** Adding Program ***"

x = float(raw_input("Enter the 1st number to add:"))

y = float(raw_input("Enter the 2nd number to add:"))

sum = x + y

print "Sum:" + sum

•And the program output is now this:

*** Adding Program ***

Enter the 1st number to add:2

Enter the 2nd number to add:3

Traceback (most recent call last):

File "D:\Program Files\Python25\calculator.py",

line 5, in <module>

print "Sum:" + sum TypeError: cannot concatenate 'str' and 'float' objects

•Python cannot join a string and a float together; it can only join two strings together

•Here is the corrected code

print "*** Adding Program ***"

x = float(raw_input("Enter the 1st number to add:"))

y = float(raw_input("Enter the 2nd number to add:"))

sum = x + y

print "Sum:" + str(sum)

•The corresponding output:

*** Adding Program ***

Enter the 1st number to add:2

Enter the 2nd number to add:3

Sum:5.0

•Our corrected calculator could still produce a runtime error – for example, if the user types in

a word instead of a number

*** Adding Program ***

Enter the 1st number to add:2

Enter the 2nd number to add:bob