In the computer data are all binary digits (usually 0 and +5.5 volts). At a higher level of abstraction data are typed into integers, real, or alphanumeric representation.
The type affects the kind of arithmetic operations that are allowed (as well as the kind of arithmetic -- integer versus real arithmetic; lexicographical ordering of alphanumeric , etc.)
In scientific programming, a common (and really difficult to detect) source of slight inaccuracies (that tend to snowball as the program runs) is mixed mode arithmetic required because two numeric values are of different types (integer and real).
Integers are numbers without any fractional portion (nothing after the decimal point -- which is not used in integers). Numbers like -3, -2, -1, 0, 1, 2, 200 are integers. A number like 1.1 is not an integer, and 1.0 is also not an integer (the presence of the decimal point makes the number a real).
To declare an integer in Python, just assign the variable name to an integer for example
MyPhoneNumber = 14158576309
# Integer Assignment
MyPhoneNumber = 14158576309
print(MyPhoneNumber)
A real or float is a number that has (or can have) a fractional portion -- the number has decimal parts. The numbers 3.14159, -0.001, 11.11, 1., are all floats. The last one is especially tricky, if you don't notice the decimal point you might think it is an integer but the inclusion of the decimal point in Python tells the program that the value is to be treated as a float. To declare a float in Python, just assign the variable name to a float for example
MyMassInKilos = 74.8427
# Float Assignment
MyMassInKilos = 74.8427
print(MyMassInKilos)
A string is a data type that is treated as text elements. The usual letters are strings, but numbers can be included. The numbers in a string are simply characters and cannot be directly used in arithmetic. There are some kinds of arithmetic that can be performed on strings but generally we process string variables to capture the text nature of their contents. To declare a string in Python, just assign the variable name to a string value { the trick is the value is enclosed in quotes. The quotes are delimiters that tell the program that the characters between the quotes are characters and are to be treated as literal representation. For example
MyName = 'Theodore'
MyCatName = "Dusty"
DustyMassInKilos = "7.48427"
are all string variables. The last assignment is made a string on purpose. String variables
can be combined using an operation called concatenation
. The symbol for concatenation is
the plus symbol +.
Strings can also be converted to all upper case using the upper()
function. The syntax for
the upper()
function is 'string to be upper case'.upper(). Notice the "dot" in the
syntax. The operation passes everything to the left of the dot to the function which then
operates on that content and returns the result all upper case (or an error if the input stream
is not a string).
MyName = 'Theodore'
MyCatName = "Dusty"
DustyMassInKilos = "7.48427"
print("all about me")
print("My name is : ",MyName,"My Mass is :",MyMassInKilos," kilograms")
print("My Phone Number is :",MyPhoneNumber)
print("My cat's name is : ",MyCatName,"My cat's mass is :",DustyMassInKilos," kilograms")
print("Concatenation Examples")
print("Silly String :",MyCatName + MyName + DustyMassInKilos)
print("SILLY STRING :",(MyCatName + MyName + DustyMassInKilos).upper())
Notice how Dusty's mass appears to be a number, but in reality it is a string variable -- if it were used in ordinary arithmetic it would cause an error.
Strings can be formatted using the %
operator or the format()
function. The concepts will
be introduced later on as needed, you can Google search for examples of
how to do such formatting. My personal preference would be with the format()
function
because it is clear what is going on, whereas the %
operator is hard to interpret when
maintaining code, its also a modulo operator in arithmetic contexts.
A variable type can be changed. This activity is called type casting. Three functions allow type casting: int(), float(), and str(). The function names indicate the result of using the function, hence int() returns an integer, float() returns a float, and str() returns a string.
The easiest way to understand is to see an example. We cannot convert arbitrary strings (with letters) into numeric variables using the functions as-is, there would be some coding involved. Strings have a numeric value called their lexographical value -- its a really advanced concept and used in sorting.
MyInteger=234
MyFloat=876.543
MyString="what is your name?"
print(MyInteger,MyFloat,MyString)
print("Convert integer to float",float(MyInteger))
print("Convert float to integer",int(MyFloat))
print("Convert integer to string",str(MyInteger))
A list is a collection of data that are somehow related. It is a convenient way to refer to a
collection of similar things by a single name, and using an index (like a subscript in math)
to identify a particular item.
Consider the variable x
:
[$x_0 = 7$,
$x_1 = 11$,
$x_2 = 5$,
$x_3 = 9$,
$x_4 = 13$,
$\dots$,
$x_N = 223$]
The variable name is x
and the subscripts correspond to different values. Thus the value of
the variable named x
associated with subscript 3 is the number 9.
A visual representation of the concept that treats a variable as a collection of cells is shown below
In the figure, the variable name is MyList
, the subscripts are replaced by an index
which identifies which cell is being referenced. The value is the cell content at the particular
index. So in the figure the value of MyList
at Index = 1 is the number 11.
In engineering programming we use lists a lot -- we usually call then vectors, arrays, matrices and such, but they are ultimately just lists. To declare a list you can write the list name and assign it values. The square brackets areused to identify that the variable is a list. Like:
MyList = [7,11,5,9,13,66,99,223]
One can also declare a null list and use the append() method to fill it as needed.
MyOtherList = [ ]
Ok in Python, indices always start at ZERO. This tends to be a source of confusion because many other languages start counting at ONE. Otherwise its just the convention. The first element in a list has an index of 0, the second an index of 1, and so on.We access the contents of a list by referring to its name and index. For example
MyList[3] has a value of 9.
The script below shows some of these list operations. The example creates
MyList
then prints its contents. That shows as the list printed across the line. Then
the 4th element (index == 3) is printed.
Lastly a special kind of list is built using the range(start,end,step)
constructor function. This function returns a list of numbers that starts at "start", ends at "end", and increments in units of "step." The result is assigned to a
variable named HowMany
and this variable is itself just a list. We could have put the range
function directly into the "for" loop -- but it makes the parts of the example harder to keep
track of.
We have seen a "for" loop earlier in an exercise. Engineering programming uses loops a lot (in fact most programming uses loops { the benefit of a computer is the ability for it do do stuff over and over again; this process is called iteration (or repetition)). There are tricks to doing loops fast, but that's for another time.
# Lists
MyList = [7,11,5,9,13,66,99,223]
print("MyList : ", MyList)
print("MyList[3] : ",MyList[3])
# lets go for a loop
print("Loopa del Mare")
HowMany = range(0,len(MyList),1)
for i in HowMany:
print("MyList[",i,"]= ",MyList[i])
A tuple is a special kind of list where the values cannot be changed after the list is created. It is useful for list-like things that are static -- like days in a week, or months of a year. You declare a tuple like a list, except use round brackets instead of square brackets.
MyTupleName = ("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec")
MyTupleName = ("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec")
MyTupleName[3]
MyTupleName[3]='fred'
A dictionary is a special kind of list where the items are related data PAIRS. It is a lot like a relational database (it probably is one in fact) where the first item in the pair is called the key, and must be unique in a dictionary, and the second item in the pair is the data. The second item could itself be a list, so a dictionary would be a meaningful way to build a database in Python. One declares a dictionary using curly brackets
MyPetsNamesAndMass = { "Dusty":7.8 , "Aspen":6.3, "Merrimee":0.03}
or by using the dict()
method
MyPetsNamesAndMassToo = dict(Dusty = 7.8 , Aspen = 6.3, Merrimee = 0.03)
The script below shows some examples of a Tuple and Dictionary constructs. The last row in the program is an attempt to clobber the contents of a Tuple and the related error message.
MyPetsNamesAndMass = { "Dusty":7.8 , "Aspen":6.3, "Merrimee":0.03} #create dictionary
print("Aspen's Mass",MyPetsNamesAndMass['Aspen'])
print("Merrimee's Mass",MyPetsNamesAndMass['Merrimee'])
# update entry
MyPetsNamesAndMass['Merrimee'] = 31.0
print("Merrimee's Mass",MyPetsNamesAndMass['Merrimee']) # she ate a lot!
MyPetsNamesAndMassToo = dict(Dusty = 7.8 , Aspen = 6.3, Merrimee = 0.03) # alternate constructor
print("Merrimee's Mass",MyPetsNamesAndMassToo['Merrimee'])
Getting data into a program { the input() function
We this notebook with introduction to the input()
function so that we can start
some meaningful exercises. The input function has the following structure it looks like
MyVariable = input("Message to prompt input")
The script below is an example of using the input()
function for very simplistic input.
Later on
when we learn about formatted strings for the message body, much more complicated input
can be used.
# input
MyVariable = input("message to prompt user")
print(MyVariable)
MyInteger = 0
MyFloat = 0.0
MyString = "G"
MyInteger = input("enter a number")
MyFloat = input("enter a number too")
MyString = input("enter some text")
print("MyInteger = ",MyInteger)
print("MyFloat = ",MyFloat)
print("MyString = ",MyString)
Notice how the input function clobbers our integer variable and wrote the value to the variable as type == float. As presented here the input function will read the input stream and make a guess as to the data type and assign that to the destination variable, clobbering the existing variable type if necessary.
A simple way to control behavior is to type cast the input something like
MyInteger = int(input("enter a number"))
or use a try/except structure and explicitly trap exceptions.
1) Run the script below.
print ('integers and reals')
x1 = 1.0
y1 = 1.
z1 = 1
x2 = 5.0
y2 = 5.
z2 = 5
print ('x1 = ', x1, ' y1 = ', y1, ' z1 = ', z1)
print ('x2 = ', x2, ' y2 = ', y2, ' z2 = ', z2)
print ('x1/x2 = ',x1/x2,' y1/y2 = ',y1/y2,' z1/z2 = ',z1/z2)
(a) Of the six variables, which are integers?
(b) What is the difference (in effect) between x1=1.0 and y1=1.?
(c) Examine the division results; Why does z1/z2 return a value of 0?
2) Using the for loop example, we can have the script square each element in the list and place that result in a second list.
# list of squares
x = [-1,0,1,2]
y = [0,0,0,0]
HowMany = range(0,len(x),1)
for i in HowMany:
y[i]=x[i]**2
print ("x: ",x)
print ("y: ",y)
Now modify the code to do the same exercise except with the list of six elements $x_0 = -1$,$ x_1 = 0 $,$x_2 = 1$,$ x_3 = 2$,$ x_4 = 3 $,$x_5 = 4$
(a) Why is the variable y
declared as a list of zeros instead of a null list?
(b) Employ a different way to build y
so the programmer doesn't have to modify
both lists for the longer mathematical list.
# list of squares
y = []
x = [-1,0,1,2,3,4]
HowMany = range(0,len(x),1)
for i in HowMany:
y.append(x[i]**2)
print ("x: ",x)
print ("y: ",y)
3) Use the input function to write a script that asks the user for a number, and returns the cube of the number.