










Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!











CSSE 120 – Rose-Hulman Institute of Technology
More on Numeric Types
Long Integers vs. Floats
Type Conversion
Lists and Strings
Lab Time
In the CSSE 120 ANGEL course, choose the REPORTS tab
Under CATEGORY, choose GRADES
Click RUN
You will have to scroll down to see some of your grades.
int : integer type
Exact values – limited range An operation on two ints always yields an int
float : real number type
Approx. values – much larger range An operation on float and int yields a float
**>>> 5/ 1
5.0/
5/ 2 5/2.
5% 2 5% 1 5.0//2. 2.** Do quiz question 1
Allows arbitrarily large integers
Automatically created when needed: >>> 10** 10000000000L
You can specify a long literal >>> 4L/ 2L >>> type(4L) <type 'long‘>
Since long covers all integers (up to the memory limits of the computer) why have an int type at all? Why not use long for all integer calculations?
Sometimes we have a value of one type, but we need the corresponding value of another type. In some cases, conversion is automatic: x = 3 y = x/7. Python provides functions that allow you to explicitly convert data to another type. int() float() str()
list[m:n] returns a new list consisting of [list[m], list[m+1], list[m+2], … list[n-1]]
list[:n] returns a new list consisting of [list[0], list[1], … list[n-1]]
list[m:] returns a new list consisting of all elements of list beginning with list[m].
list[m:n:k], similar to range(m, n, k), returns a new list consisting of every kth^ element of list, starting with list[m].
len(
Returns length of the sequence
Returns the index of the first occurrence of the expression in the sequence
Many of the operations listed in the book, while they work in Python 2.5, have been superseded by newer ones. + is used for String concatenation: "xyz" + "abc" * is used for String duplication: "xyz " * 4 A String method: split breaks up a string into separate words. >>> franklinQuote = 'Who is rich? He who is content. ' + 'Who is content? Nobody.’ >>> franklinQuote.split() ['Who', 'is', 'rich?', 'He', 'who', 'is', 'content.', 'Who', 'is', 'content?', 'Nobody.'] >>> franklinQuote.lower() 'who is rich? he who is content. who is content? nobody.'
A string is an immutable sequence of characters
>>> alpha = "abcdefg "
>>> alpha[2]
>>> alpha[1:4]
>>> alpha[3] = "X " # illegal!
Python’s fancy term for this : list comprehension.
>>> [ii for i in range(6)]* [0, 1, 4, 9, 16, 25]
>>> [[i, ii] for i in range(5)]* [[0, 0], [1, 1], [2, 4], [3, 9], [4, 16]]
Can you write a list comprehension for the value of cosine every 45 degrees around a circle?
**from zellegraphics import ***
win = GraphWin() pointList = [Point(30, 120), Point(150,55), Point(80, 175)] poly = Polygon(pointList) poly.setFill("maroon") poly.draw(win)
for point in pointList: circ = Circle(point, 20) circ.draw(win)