Here are the most common conversion functions.
| Conversion Function
| What it Returns |
| int()
| whole numbers (integers) |
| float()
| numbers with decimals |
| str()
| character strings (i.e. words) |
Notice that the variable to which a value is being assigned always goes on the left of
the "=" assignment operator, its new value on the left. You can display the value
contained my the variable using print. Test the following code in a program:
myNumber = 8
print "My number is", myNumber
It will display
You can assign a value to a variable from the keyboard by using the
raw_input() function. In the program below we get a value from the
keyboard and assign it to the age
variable. During the run of the program we will convert the age
variable to an integer using int().
After doing math we display the variable's value to the screen. Then we assign a new
value to the variable and display the new value to the screen.
age = raw_input("Please enter a number: ")
age = int(age)
print "Your age is", age
age = age * 2
print "Twice your age is", age
The asterisk is Python's multiplication operator.