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

csc 1010 lecture 2
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

CSC 1010 Lecture 2 1

CSC 1010 Programming for All

Lecture 2 Working with Python

2

What do we know so far?

 Class – lecture, lab, Rephactor, Quick Checks, R&R  Program to solve problems, make computers useful  User vs. Programmer – think like both!  Program – sequence of instructions  Python is 3rd most popular language, core principles  Algorithm is step-by-step procedure to do something  syntax, runtime, & logic errors, testing & debugging  hardware vs. software  flow – step-by-step, function call, conditional, loop  IDLE shell >>> or editor for saving and running  Lab 1 – visual programming, install Python, Hello World

Interpreting vs. Compiling

3

An interpreter goes one instruction at a time, translating into machine code and executing each

  • n the machine (Python)

A compiler translates all instructions first into machine code, then executing all of them on the machine (Python)

Python Standard Library

4

The Python Standard Library is a toolbox of ready‐to‐use, built‐ in components for your program.

  • built‐in functions ‐ frequently used functions such as print
  • built‐in types – numbers, sequences, and more
  • built‐in constants – fixed values like True, False and math.pi
  • built‐in exceptions ‐ represent errors and warnings

Some modules require the import statement to use them.

i m por t m at h pr i nt ( ' The val ue of PI i s' , m at h. pi ) The val ue of PI i s 3. 141592653589793

BUILDING BLOCKS

5

Variables

A variable is a name used to refer to a value stored in the computer's memory

total = 500 count = 7.5 name value

Print out the value inside a variable like this:

print(total) print("The value of count is", count)

1 2 3 4 5 6

slide-2
SLIDE 2

CSC 1010 Lecture 2 2

Assignment Statements

An assignment statement changes the value of a variable The assignment operator is the = sign

total = 55

  • The expression on the right is evaluated and the

result is stored in the variable on the left

  • The value that was in total is overwritten

Numeric Expressions

An expression is a combination of one or more operators and

  • perands

Numeric expressions compute numeric results and make use

  • f the arithmetic operators:

Addition Subtraction Multiplication Division Floor Division Modulus Exponentiation +

  • *

/ // % **

Division and Remainder

The result of division (/) is always floating point The modulus operator (%) returns the remainder after dividing the second operand into the first

14 / 3 equals 8 / 16 equals 4.666666666666667 0.5 14 % 3 equals 8 % 12.0 equals 2 8.0 If either operand of modulus is floating point, the result is floating point, too

Floor Division and Exponentiation

If both operands to the floor division operator (//) are integers, the result is integer (the fractional part isn't shown) The exponentiation operator (**) returns the result of raising the first number to the power of the second

14 // 3 equals 17.0 // 5 equals 4 3.0 2 ** 3.0 equals 5 ** 2 equals 8.0 25.0

Operator Precedence

Operators can be combined into complex expressions

result = total + count / max - offset

Operators have a well‐defined precedence which determines the order in which they are evaluated, which is:

  • 1. exponentiation
  • 2. multiplication, division, floor division, modulus

3. addition, subtraction

Same precedence? Go left to right Use parentheses to force precedence

Operator Precedence

What is the order of evaluation in the following expressions?

a + b + c + d + e 1 4 3 2 a + b * c - d / e 3 2 4 1 a // b ** c - d % e 2 3 4 1 a / (b * (c + (d - e))) 4 1 2 3

7 8 9 10 11 12

slide-3
SLIDE 3

CSC 1010 Lecture 2 3

The print Function

The print function writes text output to the console window

hei ght = 24. 56832 pr i nt ( " The hei ght i s" , hei ght ) The hei ght i s 24. 56832 pr i nt ( ' Hey t her e, W

  • r l d! Sup?' )

Hey t her e, W

  • r l d! Sup?

Multiple arguments print on the same line with spaces between

The print Function

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 ! ' ) I f eel pr et t y and br i ght !

You can have any type of argument and perform calculations, too

pr i nt ( ' The t ot al i s' , 77 + 11) The t ot al i s 88

Optional print arguments

15

The end argument overrides what prints at the end, which is normally a "newline" (next print goes on the next line)

pr i nt ( ' Hel l o ' , end=' ' ) pr i nt ( ' W

  • r l d! ' )

Hel l o W

  • r l d!

The sep argument overrides what goes between values, normally a space

pr i nt ( ' one' , ' t wo' , ' t hr ee' , ' f our ' , sep=' $$$' )

  • ne$$$t wo$$$t hr ee$$$f our

The print Function in Action

16

pr i nt ( ' O ne, ' , end=' ' ) pr i nt ( " Two, " , end=' ' ) pr i nt ( ' Buckl e m y shoe. ' ) pr i nt ( ) pr i nt ( ' Thr ee, ' , end=' ' ) pr i nt ( ' Four , ' , end=' ' ) pr i nt ( ' Cl ose t he door . ' ) O ne, Two, Buckl e m y shoe. Thr ee, Four , Cl ose t he door . No argument required Result: blank line

Strings

A character string is a sequence of characters, surrounding by single or double quotes

17

ki l l er = ' Lee Har vey O swal d' vi ct i m = " John F. Kennedy"

To include one type of quote inside a string, use the other type of quotes on the ends

sent ence = " Kennedy' s ki l l er i s O swal d"

pr i nt ( ' Ther e ar e m any " assassi nat i on conspi r aci es" out t her e' )

String Concatenation

String concatenation allows two or more strings to be joined using the + operator

18

val ue = ' The ki l l er of ' + vi ct i m + ' i s ' + ki l l er

To concatenate numbers to strings, use the str function around them or you'll get an error message

nam e = ' Rober t W adl ow' f eet = 8 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

13 14 15 16 17 18

slide-4
SLIDE 4

CSC 1010 Lecture 2 4

Long Strings

Character strings cannot be broken across lines:

19

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

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 also surround long strings with triple quotes

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! ' ' ' )

String Indexes

Each character in a string has a position or index

20

You can access an individual character using its index and the index operator (square brackets []). Suppose the above string is in a variable called title:

pr i nt ( ' Char act er 6 i n t i t l e i s ' + t i t l e[ 6] ) Char act er 6 i n t i t l e i s t

String Length

You can determine the length of a string using len

21

If you compare the length to the last index, you'll see it is

  • different. That's because indexes start counting at 0.

t i t l e = ' Rephact or Pyt hon' pr i nt ( l en( t i t l e) ) 16

Iterating Through a String

You can step through the characters or iterate a string like this

22

l anguage = ' Pyt hon' f or l et t er i n l anguage: pr i nt ( l et t er ) P y t h

  • n

This iteration uses a for loop

String Containment

To see if one string is contained inside another string, use the in operator

23

t i t l e = ' Rephact or Pyt hon' i f ' act or ' i n t i t l e: pr i nt ( ' Found i t ! ' ) Found i t !

Repeat a String

To repeat any string some number of times, use the repetition

  • perator (*)

24

books = ' Pyt hon' * 7 pr i nt ( books) Pyt honPyt honPyt honPyt honPyt honPyt honPyt hon

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

19 20 21 22 23 24

slide-5
SLIDE 5

CSC 1010 Lecture 2 5

Comments

Comments explain a program's purpose and processing They are intended for the human reader – they have no effect on a program A Python comment begins with a # and continues until the end of the line

25

# Thi s i s a com m ent

It might be put on the end of a line of code

bal ance = bal ance – f ees # deduct m

  • nt hl y f ees

A block comment has as many lines of comments as needed

Comments Example

26

# # 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 # Bar t vs. Thanksgi vi ng. # def pr i nt _si m psons_quot e( ) : 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' ) # M ai n pr ogr am pr i nt _si m psons_quot e( )

O per at or , what ' s t he num ber f or 9- 1- 1?

  • Hom

er Si m pson

25 26