Skip to main content

3.14) Exercise 3 - Number Drills


Here are some drills using variables, numbers and Arithmetics. These exercises also contain some useful tricks and new syntax.

Type every single command in the prompt in IDLE and make sure you understand the results of the commands. If you’re not sure, go back to the course, search for some information on the web or use the forum to ask a question.

Note that the lines preceded by a hash (#) are comments. Anything after # will not be executed. We will come back to the notion of comments later in this course. Try typing the full lines including the comments so that you get used to typing them to your code.

# Types and type conversion
1 + 3
1.0 + 3.0
1 + 3.0
3/5
3/5.0
3./5.    # shortcut for 3.0/5.0
_ + 3.0  # _ holds the result of the last operation
# Printing results
8./3.        # full precision result in the prompt, no output in a script
print 8./3.  # user friendly printing
# Priorities and brackets
a= 3 - 4 * 7
b=(3 - 4) * 7
print 'results:',a,b  # printing more than one result
# Make sure you type all the brackets
result_2=( ((18.3-16.0) / (32.4 - 45.2)) - 1.0 )* 10**3
print result_2 # should return -1179.6875
A = 1 ; B =3 # you can put two lines on one with the ; seperator
A = A + 1
B +=1        # equivalent to b=b +1
print "A=",A,'and B=',B
# complex numbers
c= 3+ 5j
print c
(1+3j)/(2+5j)
1.8+6.7J
# shortcut operations
a=3
a+=5
print a
a*=2
print a
# Scientific notation
1E5
4.5E-6
234E2