csc 1010 lecture 2
play

CSC 1010 Lecture 2 What do we know so far? Class lecture, lab, - PDF document

CSC 1010 Lecture 2 What do we know so far? Class lecture, lab, Rephactor, Quick Checks, R&R CSC 1010 Programming for All Program to solve problems, make computers useful User vs. Programmer think like both! Program


  1. CSC 1010 Lecture 2 What do we know so far?  Class – lecture, lab, Rephactor, Quick Checks, R&R CSC 1010 Programming for All  Program to solve problems, make computers useful  User vs. Programmer – think like both!  Program – sequence of instructions  Python is 3 rd most popular language, core principles  Algorithm is step-by-step procedure to do something  syntax, runtime, & logic errors, testing & debugging Lecture 2  hardware vs. software Working with Python  flow – step-by-step, function call, conditional, loop  IDLE shell >>> or editor for saving and running  Lab 1 – visual programming, install Python, Hello World 2 1 2 Interpreting vs. Compiling Python Standard Library The Python Standard Library is a toolbox of ready‐to‐use, built‐ An interpreter goes one in components for your program. instruction at a time , • built‐in functions ‐ frequently used functions such as print translating into machine • built‐in types – numbers, sequences, and more code and executing each • built‐in constants – fixed values like True, False and math.pi on the machine (Python) • built‐in exceptions ‐ represent errors and warnings A compiler translates all Some modules require the import statement to use them. instructions first into i m por t m at h machine code, then pr i nt ( ' The val ue of PI i s' , m at h. pi ) executing all of them on the machine (Python) The val ue of PI i s 3. 141592653589793 3 4 3 4 Variables A variable is a name used to refer to a value stored in the computer's memory name value total = 500 count = 7.5 Print out the value inside a variable like this: BUILDING BLOCKS print(total) print("The value of count is", count) 5 5 6 1

  2. CSC 1010 Lecture 2 Assignment Statements Numeric Expressions An assignment statement changes the value of a variable An expression is a combination of one or more operators and operands The assignment operator is the = sign Numeric expressions compute numeric results and make use of the arithmetic operators: total = 55 Addition + Subtraction - Multiplication * Division / • The expression on the right is evaluated and the Floor Division // result is stored in the variable on the left Modulus % Exponentiation ** • The value that was in total is overwritten 7 8 Division and Remainder Floor Division and Exponentiation If both operands to the floor division operator ( // ) are The result of division (/) is always floating point integers, the result is integer (the fractional part isn't shown) equals 14 / 3 4.666666666666667 equals 14 // 3 4 equals 8 / 16 0.5 equals 17.0 // 5 3.0 The modulus operator (%) returns the remainder after dividing the second operand into the first The exponentiation operator ( ** ) returns the result of raising the first number to the power of the second equals 14 % 3 2 equals 2 ** 3.0 8.0 equals 8 % 12.0 8.0 equals 5 ** 2 25.0 If either operand of modulus is floating point , the result is floating point , too 9 10 Operator Precedence Operator Precedence Operators can be combined into complex expressions What is the order of evaluation in the following expressions? result = total + count / max - offset a + b + c + d + e a + b * c - d / e Operators have a well‐defined precedence which determines 1 2 3 4 3 1 4 2 the order in which they are evaluated, which is: 1. exponentiation a // b ** c - d % e 2. multiplication, division, floor division, modulus 2 1 4 3 3. addition, subtraction a / (b * (c + (d - e))) Same precedence? Go left to right 4 3 2 1 Use parentheses to force precedence 11 12 2

  3. CSC 1010 Lecture 2 The print Function The print Function The print function writes text output to the console window You can pass into it as many arguments as you like pr i nt ( ' I ' , ' f eel ' , ' pr et t y' , ' and' , ' br i ght ! ' ) pr i nt ( ' Hey t her e, W or l d! Sup?' ) Hey t her e, W or l d! Sup? I f eel pr et t y and br i ght ! Multiple arguments print on the same line with spaces between You can have any type of argument and perform calculations, too hei ght = 24. 56832 pr i nt ( ' The t ot al i s' , 77 + 11) pr i nt ( " The hei ght i s" , hei ght ) The t ot al i s 88 The hei ght i s 24. 56832 13 14 Optional print arguments The print Function in Action The end argument overrides what prints at the end , which is pr i nt ( ' O ne, ' , end=' ' ) normally a "newline" (next print goes on the next line) pr i nt ( " Two, " , end=' ' ) pr i nt ( ' Buckl e m y shoe. ' ) pr i nt ( ' Hel l o ' , end=' ' ) No argument pr i nt ( ) pr i nt ( ' W or l d! ' ) required pr i nt ( ' Thr ee, ' , end=' ' ) pr i nt ( ' Four , ' , end=' ' ) Hel l o W or l d! pr i nt ( ' Cl ose t he door . ' ) The sep argument overrides what goes between values, O ne, Two, Buckl e m y shoe. Result: blank normally a space line Thr ee, Four , Cl ose t he door . pr i nt ( ' one' , ' t wo' , ' t hr ee' , ' f our ' , sep=' $$$' ) one$$$t wo$$$t hr ee$$$f our 15 16 15 16 Strings String Concatenation A character string is a sequence of characters, surrounding by String concatenation allows two or more strings to be single or double quotes joined using the + operator ki l l er = ' Lee Har vey O swal d' val ue = ' The ki l l er of ' + vi ct i m + ' i s ' + ki l l er vi ct i m = " John F. Kennedy" To concatenate numbers to strings, use the str function around To include one type of quote inside a string, use the other type of them or you'll get an error message quotes on the ends nam e = ' Rober t W adl ow' sent ence = " Kennedy' s ki l l er i s O swal d" f eet = 8 pr i nt ( ' Ther e ar e m any " assassi nat i on conspi r aci es" out t her e' ) i nches = 11 pr i nt ( nam e+' was ' +st r ( f eet ) +' f eet ' +st r ( i nches) +' i nches t al l ' ) Rober t W adl ow was 8 f eet 11 i nches t al l 17 18 17 18 3

  4. CSC 1010 Lecture 2 Long Strings String Indexes Character strings cannot be broken across lines: Each character in a string has a position or index pr i nt ( ' Thi s i s a ver y l ong st r i ng but i t i s qui t e possi bl e t her e ar e even l onger st r i ngs! ' ) You can use string concatenation for long strings You can access an individual character using its index and the index operator (square brackets [] ). Suppose the above string pr i nt ( ' Thi s i s a ver y l ong st r i ng but i t i s qui t e ' + is in a variable called title : ' possi bl e t her e ar e even l onger st r i ngs! ' ) pr i nt ( ' Char act er 6 i n t i t l e i s ' + t i t l e[ 6] ) You can also surround long strings with triple quotes Char act er 6 i n t i t l e i s t pr i nt ( ' ' ' Thi s i s a ver y l ong st r i ng but i t i s qui t e possi bl e t her e ar e even l onger st r i ngs! ' ' ' ) 19 20 19 20 String Length Iterating Through a String You can determine the length of a string using len You can step through the characters or iterate a string like this t i t l e = ' Rephact or Pyt hon' l anguage = ' Pyt hon' pr i nt ( l en( t i t l e) ) f or l et t er i n l anguage: pr i nt ( l et t er ) 16 P y If you compare the length to the last index , you'll see it is t h different. That's because indexes start counting at 0 . o n This iteration uses a for loop 21 22 21 22 String Containment Repeat a String To see if one string is contained inside another string, To repeat any string some number of times , use the repetition use the in operator operator ( * ) t i t l e = ' Rephact or Pyt hon' books = ' Pyt hon' * 7 i f ' act or ' i n t i t l e: pr i nt ( books) pr i nt ( ' Found i t ! ' ) Pyt honPyt honPyt honPyt honPyt honPyt honPyt hon Found i t ! The order of the two doesn't matter, but there has to be a string and an integer hasht ags = 15 * ' #' pr i nt ( hasht ags) ############### 23 24 23 24 4

  5. CSC 1010 Lecture 2 Comments Comments Example Comments explain a program's purpose and processing # # Funct i on t o pr i nt a quot e f r om The Si m psons epi sode t i t l ed They are intended for the human reader – they have no effect on # Bar t vs. Thanksgi vi ng. a program # def pr i nt _si m psons_quot e( ) : A Python comment begins with a # and continues until the end of pr i nt ( " O per at or , what ' s t he num ber f or 9- 1- 1?" ) # s2, ep7 pr i nt ( ' - Hom er Si m pson' ) the line # M ai n pr ogr am # Thi s i s a com m ent pr i nt _si m psons_quot e( ) It might be put on the end of a line of code O per at or , what ' s t he num ber f or 9- 1- 1? - Hom er Si m pson bal ance = bal ance – f ees # deduct m ont hl y f ees A block comment has as many lines of comments as needed 25 26 25 26 5

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend