Docsity

Python Numeric Types, Conversion, Lists, and Strings: CSSE 120 Course Outline, Study notes of Software Engineering

Partial preview of the text

Download Python Numeric Types, Conversion, Lists, and Strings: CSSE 120 Course Outline and more Study notes Software Engineering in PDF only on Docsity!

TYPES, CONVERSION,LISTS, ANDSTRINGS CSSE 120 – Rose-Hulman Institute of Technology

Outline

^ More on Numeric Types ^ Long integers vs floats ^ Type Conversion ^ Lists and Strings

Integer Representations

^ An

int

is represented by a fixed-length sequence of bits.^ †^ A

bit

is a

bi nary digi

t : its value is either 0 or 1.

†^ On typical 2007 operating systems, that length is 32. †^ How many different values can be represented by nbits? Thus there is a largest

int

value.

^ How to deal with larger integer values?^ †

Use floats? † Do what other languages do? (overflow)

Python’s long integer type

^ Allows arbitrarily large integers ^ Automatically created when needed:^ >>>

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?

Sequences in Python

^ A sequence is an ordered collection of data items.There are two kinds:^ †

List: mutable

[3, 4, 6]

†^ Tuple: immutable

(3, 4, 6)

^ Simple examples of generating lists and tuples:^ †

>>>

range(4, 11, 2) [4, 6, 8, 10] † >>>

3*4, 3-4, 3+4, 3/ (12, -1, 7, 0)

Accessing & Changing List Elements

^ What will each statement do or return?Assume executed in order, beginning with^ >>> myList = [3, 6, 8, 11, 7, 4, 5, 9, 10, 0,

2]

>>> len(myList) >>> myList[0] >>> myList[3] >>> myList[-3] >>> myList[1:5]

**# A slice of the list

myList[1:11:3] >>> myList[2:] >>> myList[4] = 12 >>> myList >>> myTuple = (2, 3, 4) >>**^

myTuple[1] = 6 # oops!

>>> myList = myList + [40, 50] >>> myList >>> myList[1:5] = [0, 0] >>> myList >>> [1, 2, 4] * 3 >>> [ii for i in range(6)] >>> range(4) + range(5) >>> myList.index(9) >>> myList[myList.index(9)] >>> myList[0:myList.index(9)] >>>[2, 3, 4, 5][2]*

Strings (character strings)

^ String literals: ^ "One\nTwo\nThree" ^ "Can’t Buy Me Love" ^ ′

I say, "Yes."

You say, "No."

′

^ """I don't know why you say "Goodbye,\”

I say "Hello." """

String Operations

^ Many of the operations listed in the book, while they workin 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.'

Lists of Strings >>>

beatles

=^

['John',

'Paul']

beatles.append('George')

beatles ['John', 'Paul', 'George'] >>>

beatles

+^

['Ringo']

['John', 'Paul', 'George', 'Ringo'] >>>

beatles ['John', 'Paul', 'George'] >>>

beatles

=^

beatles

+^

['Ringo']

beatles ['John', 'Paul', 'George', 'Ringo'] >>>

beatles[1] 'Paul' >>>

beatles[1][2] 'u'

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

Geometric exercise

^ Start in class, finish for homework. ^ Ask user for an odd integer N that is at least 3. ^ Draw the following pictures:^ †

A “pizza pie” with N slices. † A circle with an inscribed regular polygon with N sides. † A circle with an inscribed star generated by connectingevery other vertex of the regular polygon.

^ See next slide for sample pictures and the followingslide for hints. ^ You can do a separate program for each (copy andpaste are your friends, but get the first one right first).

Pictures for geometric exercise

^ N=5: ^ N=7:

Turn-in for Geometric Exercise

^ In the Homework 4 folder on ANGEL, there arethree Drop Boxes named Pizza, Poly, and Star. ^ Place your Python code files in the correspondingdrop boxes before the beginning of your section’sSession 5 class meeting.