Docsity

Notes on Types, Conversion Lists and Strings | CSSE 120, Study notes of Software Engineering

Partial preview of the text

Download Notes on Types, Conversion Lists and Strings | CSSE 120 and more Study notes Software Engineering in PDF only on Docsity!

TYPES, CONVERSION,

LISTS, AND

STRINGS

CSSE 120 – Rose-Hulman Institute of Technology

Outline

 More on Numeric Types

 Long Integers vs. Floats

 Type Conversion

 Lists and Strings

 Lab Time

Seeing Your Grades in ANGEL

 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.

Numeric Types - Recap

 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

Python’s long integer type

 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?

Type Conversions

 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()

Slices of a List

 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].

Two More Sequence Operations

len( )

 Returns length of the sequence

.index( )

 Returns the index of the first occurrence of the expression in the sequence

String Operations

 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.'

Strings as Sequences

 A string is an immutable sequence of characters

 >>> alpha = "abcdefg "

 >>> alpha[2]

 >>> alpha[1:4]

 >>> alpha[3] = "X " # illegal!

A Loop to Make a List

 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?

A List of Points

**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)