In Python variables are not objects; they are pointers to objects. For example, if we use the assignment statement x=5 , the variable x points to an object (the integer 5). We begin defining Python objects by introducing three types of numbers.
- integer
- float
- complex
Variable names in Python can be any length and can contain uppercase and lowercase letters (A to Z, a to z), digits (0-9), and the underscore character (_). Although a variable name can contain digits, the first character of a variable name cannot be a digit.
If you learned computing in the late 50's and 60's (as did I) you are familiar with the notion of declaring variable types. In Fortran, for example, any variable beginning with i,j,k,l,m,n was automatically declared integer. Good programming style required a declarative statement to define the type of each variable at the beginning of a program. This is very different from Python. First, variables are not predestined to hold a particular type of number, or other object. When a number is assigned to a variable, it is dynamically typed. Therefore, assigning z=7.6 establishes the variable z as a float.
We can interrogate the variable type by using the built in Python function type(). We will explore this in EX1.1. Another set of built Python functions can be used to type cast. These functions include: int() and float().
References:
https://www.w3schools.com/python/python_numbers.asp https://www.pythonlikeyoumeanit.com/Module2_EssentialsOfPython/Basic_Objects.html \ https://www.bogotobogo.com/python/python_numbers_strings.php \
Updated 8/23/23
Donald Falkenburg
Using type() and type cast functions.
# EX1.1.1
x=7
x1=float(x)
y=8.76
y1=int(y)
print(type(x),type(x1))
print('x=',x,'x1=',x1)
print(type(y),type(y1))
print('y=',y,'y1=',y1)
<class 'int'> <class 'float'> x= 7 x1= 7.0 <class 'float'> <class 'int'> y= 8.76 y1= 8
When we print type(x) it identifies x as pointing to an integer object. Notice the format of the output of the type() function. It identifies the class of object to which x,y,x1, and y1 point. The type() function can be used on any Python object. More later. Note what happens when you type case a float to an integer.
Many mathematical operators in Python are the same as in other computer languages.
# ex1.1.2 Multiplying Integers
x=500
y=12345
z=x*y
print(z)
print(x*y)
z
6172500 6172500
6172500
The first two outputs above come from lines 5 and 6. Note, you can perform arithmetic operations within a print statement The last output labeled Out[1] may appear a bit mystreous. If you simply type a variable name in a script, it will cause the program to create an output of the value of that variable
When doing arithmetic operations with integers, the number of digits in the result will adjust, as needed. In the following example, we square the integer 12345, then raise it to the 10th power, and finally to the 20th power.
#ex1.1.3 Precision in integer arithmetic
z=12345**2
print("\n",z)
z=12345**10
print("\n",z)
z=123245**20
print("\n",z)
152399025 82207405646327461794954634291560556640625 653711587317690717935599252587686866434493332386916755952509974509090222850382171300125217437744140625
Notice the number of digits in the printouts. Note: "/n" is a string character that places a line feed before each print output.
Artimetic operations mixing integer and floating point numbers will produce a floating point number.
# ex1.1.4
x=500
y=1.2345
print(x*y)
617.25
Let's perform a similar operation as we did in in ex1.1.2 Here we raise 1.2345 the power 2,20, and 200. Notice that unlike the case with integers, each of the results has the same precision (number of significant figures). This precision is based on the system you are using and the number of bits used to represent floating point numbers.
#ex1.1.5 Precision with floating point numbers
x=1.2345**2
print(x)
x=1.2345**20
print(x)
x=1.2345**200
print(x)
1.5239902499999998 67.58057543099825 1.9870960204327767e+18
Normally, complex numbers are expressed in terms of $i=\sqrt{-1}$ such as 1+2i. DO NOT USE i IN PYTHON. PYTHON USES j to represent the imaginary part. Alternatively, you can use complex(1,2) to specify a complex number.DO NOT USE 1+2*j. You will get an error that j is undefined. 2j is recognized by Python.
# ex1.1.6
x=1+2j
y=4+3j
print(x*y)
z= complex(4,3)*complex(1,-1)
print(z)
(-2+11j) (7-1j)
In order to find the real and imaginary parts of an imaginary number we use a Python function. If x points to a complex number we get the real part by writing x.real, while the imaginary part is given by x.imag
# ex1.1.7
x=(1+2j)*(5-3j)
print('x=',x)
y=x.real
z=x.imag
print('The real component is',y)
print('The imaginary component is',z)
x= (11+7j) The real component is 11.0 The imaginary component is 7.0
Notice that while we entered the components of the factors of the complex number x, with integer components, when we applied constructed the real and imaginary components, they are floats. The reason for this is that all complex numbers are represented by two floating point numbers--real and imaginary part. Wen we specified the factors, internally these were turned into floats.
The precedence of operations is the same as for most computing languages. In the next example, we will do a couple of simple problems involving integer division // and % (the remainder after integer division)
Operator Operation
Operator | Operation | x+y | Sum |
---|---|
x-y | Difference | x*y | Product |
x/y | Quotient | x//y | Integer Division |
x%y | Remainder from Integer Division |
x**y | Exponentiation |
abs(x) | Absolute Value |
# ex1.1.8
x=9
y=2
print(x//y)
print(x%y)
4 1
Rather than endow Python with every conceivable function you might need in program development, the environment uses packages that must be imported to your program. One of these packages is the Python math Module.
If you will use these built in functions in your program, you must begin your program importing the package, including the statement import math (line 2).
# ex1.1.8
import math
r=10
area=math.pi*r**2
print("area of circle=",area)
z=cos(math.pi)
print("cos(\u03C0)=",z)
y=e**(-1)
print(y)
area of circle= 314.1592653589793 cos(π)= -1.0 0.36787944117144233
In EX1.1.8 we calculate the area of a circle whose radius in 10. Note that all functions and mathematical constants defined in the math module must be preceded by math dot, in order to designate that they are from the math package.
In the print statement (line 7) I have inserted the greek letter 𝞹 in the output string using unicode \u03C0. \
In some cases it is inconvenient to type out the entire name of the package. In that case you can use import math as m, and refer to the functions as m.cos().Try it.
There is a separate package for math functions of complex numbers. Use import cmath as cm. Line 4 cm.polar converts the complex number in rectangular coordinates into polar coordinates. Line 5 print(y) produces the pair (magnitude, angle in radians). We can also convert from polar to rectangular coordinates. The method has two inputs: magnitude=1 and the angle=𝞹/2, and returns the rectangular coordinates. Notice the error in precision here.
# ex1.1.9
import cmath as cm
x=1+1j
y=cm.polar(x)
print(y)
z=cm.rect(1,cm.pi/2)
print(z)
(1.4142135623730951, 0.7853981633974483) (6.123233995736766e-17+1j)