Py-Tut-003

__**Back to Learning Python**__

Simple Operators
Some of the simple operators for basic math include:
 * Addition, "+"
 * Subtraction, "-"
 * Multiplication, "*"
 * Division, "/"
 * Exponent, "**"

Please note that Python recognizes proper Order of Operations (BEDMAS) and that it is your responsibility to ensure the order is correct.

These operators can be used without variables:

code format="python" print 6 * 7 code

They can also be used with variables: code format="python" myAge = 25 print "Double my age is", myAge * 2 code

User Input
What if the value you want needs to be typed in by the user? This is where we use "raw_input" To save user input as a string:

code format="python" myName = raw_input("Please enter your name ") code

If you want to save the input as an integer (int) or decimal value (float) you would need to use "int" or "float around "raw_input". The following example asks the user for their age and prints it back to them:

code format="python" userAge = int( raw_input( "Please enter your age: ") ) print "You entered", userAge, "for your age." code

Assignment 3a
Write a program that uses all five of the math operators listed above. Print a statement that tells the user how the operator is being used similar to the example above.

Assignment 3b
Write a program that asks a user for their name as a string, their age as an integer, and their favourite colour as a string. Print a statement that prints back what the user typed.

Assignment 3c
Modify Assignment 3b so that the age is stored as a float. What do you notice that is different about the data that is printed back? Why do you think it is different?

__**Back to Learning Python**__