TEST 1 REVIEW
CG 120
I have listed somethings you should make certain you know for the test.
This list may not be complete but is a good start for your reviewing.
Be certain to look over the first three labs you have done so that you
are familar with what you did on those assignments.
Here are some reasons that Python is better for secondary math majors
than Pascal
-
The language is completely free for downloading off the web - no charge
when you get out in the schools. Pascal usually costs money.
-
Python allows you to do GUI's and Pascal doesn't
-
Python is interpreted, Pascal is compiled. Interpreted languages are faster
when programs are developed.
-
Python is Object-Oriented. Pascal is not.
-
There are a huge number of functions available to a Python user. Pascal's
functions are fairly limited.
-
There are a large number of data structures: lists, tuples, and dictionaires
in Python. Pascal has some data structures but they aren't as useful to
the programmer
-
There are probably other reasons, but I thought of these.
VARIABLE NAMES
A variable name must start with an alphabetic character and can be
followed by alphabetic characters, digits (0-9), and the underscore character.
A variable name can be as long as you want, however practically I would
suggest not making them longer than 10 or characters. Python distinguishes
between the case of letters so that the variable name, Max, is different
from the variable name max.
ASSIGNMENT OPERATOR
The assignment operator is the equal sign. It does not stand for equality
but rather it says to take the value of the expression to the right of
it and place that into the variable on the left of the assignment operator.
This is the way variables are created in Python.
x = 2
This says created an int variable called x and give it the value 2.
Following this we might have a line:
x = x + 3
This says to take the previous value of x and add 3 to it so that x's
value will now be 5. The previous line of code can also be written as:
x += 3
One can do multiple assignments in Python:
x = y = 7
PRIMITIVE TYPES
There are 3 types of primitive variable types. They are:
-
int - no decimal point - these are whole numbers - 3, -1
-
float - these are numbers with a decimal point - 2.3, -0.456
-
str - these are character(s) that are enclosed in quotes: 'Hello world'
or "Hello world" or '''Hello world''', or """Hello world"""
It is possible to convert between types by giving the commands: int(),
float(), str(). Of course you can't convert 'abc' to an int but you can
'123'.
INPUT/OUTPUT
Input and output for Python is done using the commands:
The command raw_input() returns a string type value and it may be necessary
to convert it to a numeric value if that is what is wanted. Print allows
you to display output. Multiple items can be placed after the print by
using commas between the various items.
OPERATORS
The operators that we have discussed so far are:
-
+ addition if numbers, concatenates if strings
-
- subtraction if numbers
-
* multiplies if numbers, used with strings and an int, it can repeat
the string the int number of times
-
/ division if numbers
-
% returns the remainder if number - sometimes called mod
-
** raise to a power if numbers.
The following are some additional operators that work on boolean values
(true or false):
The next operators generate boolean values (true or false)
MISCELLANEOUS FUNCTIONS
-
del - eliminates a variable
-
type - tells what type a variable is.
CHAPTER 3 - numeric types
Precedence order of operations: (highest precedence first)
-
( ) - parentheses - whatever is enclosed
in the parentheses has the highest precedence
-
- - unary negative - making a
value negative.
-
** - raising to a power
-
*, /, % - multiply, divide, and remainder
-
+, - - add and subtract
There are several built-in functions in Python. See the table in your book
on page 62. Other mathematical functions exist in the math library. These
are made available by giving the command:
from math import *
These are listed in a table on page 64 of your book. Also the random
function is made available by giving the command:
import random
CHAPTER 4 - strings:
There are escape sequences in Python which are accessed by using the
\ symbol:
-
\\ gives a backslash if that needs to be printed.
-
\n gives a new line that will give a carriage return and go to the next
line if placed inside of a print statement
-
\ line continuation. Used when a statement occupies more than one
line.
-
\t gives a tab character.
The following are some string functions that you should know:
-
len(A) - returns the length of the string A.
-
strings can be indexed: A[1] - gives the second character in the string
-
strings can be sliced: A[2:4] - gives characters from 2 through 3
of the string A.
-
A.upper() and A.lower() make a string lower case.
-
A.find(" ") - returns the location of the space in the string A. If not
found then -1 is returned.
-
A.rfind(" ") - returns the location of the space in the string A starting
at the back of the string.
-
A.swapcase() - flips small to caps and caps to small.
-
A.isupper(0 - returns 1 if the string is all upper case
-
A.islower() - same as isupper except tests for lower case
-
A.isalpha() - returns 1 if A is all alphabetic characters
-
A.isdigit() - same as is alpha except tests for digits 0 - 9
-
A.isalnum() - returns 1 if A contains digits and characters only.
-
A.isspace() - returns 1 if the string contains only spaces.
-
A.lstrip() - removes spaces in front of A (on the left side)
-
A.rstrip() - removes spaces at the back of A (on the right side)
-
A.ljust(width) - left justify A in a field of width of width.
-
A.rjust(width) - same as above except right justify.
-
A.center(width) - same as above but centered.
-
A.count(" ") - counts the number of spaces in the string A.
-
A.replace(old, new) - replace all the old strings in A with the new strings.
-
A.split(" ") - split A into pieces at the space character. The pieces are
placed into a list.
-
" ".join(A) - take the list A and join back into a string based on the
blank character.
-
Strings can be compared by using the operators ==, <, <=, >, >=,
!=. Based on the results a 1 or 0 will be returned.
There are more operations given in your book, but we will stop with these.
If we need others later, we will discuss them.