Python supports all the basic arithmetic calculations
print( 2 + 2 ) print( 1.5 + 2.25 ) print( 7 – 2 ) print( 3 * 4 ) print( 10 / 2 ) print( 4 ** 3 ) # Exponent operator
A note about division & modulus
print( 13 / 5 ) # Real number divison print( 13 // 5 ) # Integer divison print( 13 % 5 ) # Modulus
Variables are just a named memory location
Defining a variable in Python is as simple as assigning a value to a name.
a = 10
Variableis not the same as
var = 10 print(Var) # Will not work!
Calculations can be assigned on a right goes into left basis.
val = 5 + 3 print(val)
Variables can be used as part of a calculation as well
a = 5 + 3 b = a * 4 c = a - b print( c ** a )
It is possible to denote the value of a variable to be a real number simply by adding a decimal element.
To convert a real number to integer, use the
int() command to truncate, or
round() to round.
a = 13.6 b = int(a) c = round(a) print(a,b,c)
To convert an integer to real, use the
a = 13 b = float(a) print(a,b)
import math answer = math.pi # π = 3.141592 answer = math.e # the natural number, e = 2.718281 answer = math.sqrt(100) # Square root answer = math.gcd(104,64) # Greatest common divisor answer = math.log(1024,2) # Log of base 2 answer = math.hypot(6,8) # Hypothenus of triangle with sides 6, 8 answer = math.cos( angle ) # Cosine of angle (radians) answer = math.sin( angle ) # Sine of angle (radians) answer = math.tan( angle ) # Tangent of angle (radians) answer = math.acos( adj/hypot ) # Arc-cosine in radians answer = math.asin( opp/hypot ) # Arc-sine in radians answer = math.atan( opp/adj ) # Arc-tan in radians answer = math.degrees( rad ) # Convert radians to degrees answer = math.radians( deg ) # Convert degrees to radians answer = abs( val ) # Absolute value import random num = random.randint(0,100) # Random number between 0 and 99 inclusive
The following questions assume you will use variables as the inputs into the problem, so the problems can re-caclulate solutions by changing the value assigned to the variable. You should also print the given information in your answer.
The tens digit in 1234 is 3.
height, print the area.
70500seconds should print a time of
band angle in degrees
C, return the area of the triangle (remember you will have to convert degrees to radians first).
c, will provide the solutions to the quadratic formula (you may assume both solutions are required). Be careful with your order of precedence. Here is an example solution set for testing: If
y=2x^2-4x-10then the solutions are