87
Python Basics! data types, strings, indexing CS101 Lecture #3 2016-10-08

Python Basics! - data types, strings, indexing...Administrivia Administrivia 3/44 Lab #2 tomorrow Sunday. Where can you get help in this class? Blackboard forum Instructors in labs

  • Upload
    others

  • View
    4

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Python Basics! - data types, strings, indexing...Administrivia Administrivia 3/44 Lab #2 tomorrow Sunday. Where can you get help in this class? Blackboard forum Instructors in labs

Python Basics!data types, strings, indexing

CS101 Lecture #3

2016-10-08

Page 2: Python Basics! - data types, strings, indexing...Administrivia Administrivia 3/44 Lab #2 tomorrow Sunday. Where can you get help in this class? Blackboard forum Instructors in labs

Administrivia 1/44

Administrivia

Page 3: Python Basics! - data types, strings, indexing...Administrivia Administrivia 3/44 Lab #2 tomorrow Sunday. Where can you get help in this class? Blackboard forum Instructors in labs

Administrivia

Administrivia 2/44

Homework #2–#6 will be later.

Final answer counts.Answers will be released 18 hours later.

Page 4: Python Basics! - data types, strings, indexing...Administrivia Administrivia 3/44 Lab #2 tomorrow Sunday. Where can you get help in this class? Blackboard forum Instructors in labs

Administrivia

Administrivia 2/44

Homework #2–#6 will be later.Final answer counts.

Answers will be released 18 hours later.

Page 5: Python Basics! - data types, strings, indexing...Administrivia Administrivia 3/44 Lab #2 tomorrow Sunday. Where can you get help in this class? Blackboard forum Instructors in labs

Administrivia

Administrivia 2/44

Homework #2–#6 will be later.Final answer counts.Answers will be released 18 hours later.

Page 6: Python Basics! - data types, strings, indexing...Administrivia Administrivia 3/44 Lab #2 tomorrow Sunday. Where can you get help in this class? Blackboard forum Instructors in labs

Administrivia

Administrivia 3/44

Lab #2 tomorrow Sunday.

Where can you get help in this class?Blackboard forumInstructors in labs and office hours

You don’t need to install Python—but if you do,use Python 3.This is not a “weeder” class—you can succeed!

Page 7: Python Basics! - data types, strings, indexing...Administrivia Administrivia 3/44 Lab #2 tomorrow Sunday. Where can you get help in this class? Blackboard forum Instructors in labs

Administrivia

Administrivia 3/44

Lab #2 tomorrow Sunday.Where can you get help in this class?

Blackboard forumInstructors in labs and office hours

You don’t need to install Python—but if you do,use Python 3.This is not a “weeder” class—you can succeed!

Page 8: Python Basics! - data types, strings, indexing...Administrivia Administrivia 3/44 Lab #2 tomorrow Sunday. Where can you get help in this class? Blackboard forum Instructors in labs

Administrivia

Administrivia 3/44

Lab #2 tomorrow Sunday.Where can you get help in this class?

Blackboard forumInstructors in labs and office hours

You don’t need to install Python—but if you do,use Python 3.

This is not a “weeder” class—you can succeed!

Page 9: Python Basics! - data types, strings, indexing...Administrivia Administrivia 3/44 Lab #2 tomorrow Sunday. Where can you get help in this class? Blackboard forum Instructors in labs

Administrivia

Administrivia 3/44

Lab #2 tomorrow Sunday.Where can you get help in this class?

Blackboard forumInstructors in labs and office hours

You don’t need to install Python—but if you do,use Python 3.This is not a “weeder” class—you can succeed!

Page 10: Python Basics! - data types, strings, indexing...Administrivia Administrivia 3/44 Lab #2 tomorrow Sunday. Where can you get help in this class? Blackboard forum Instructors in labs

Quick Review & A Bit New 4/44

Quick Review & A Bit New

Page 11: Python Basics! - data types, strings, indexing...Administrivia Administrivia 3/44 Lab #2 tomorrow Sunday. Where can you get help in this class? Blackboard forum Instructors in labs

How Assignment Works

Quick Review & A Bit New 5/44

x = 10

y = x * xx * x = y

x,y = y,x # a neat trick

Page 12: Python Basics! - data types, strings, indexing...Administrivia Administrivia 3/44 Lab #2 tomorrow Sunday. Where can you get help in this class? Blackboard forum Instructors in labs

How Assignment Works

Quick Review & A Bit New 5/44

x = 10y = x * x

x * x = y

x,y = y,x # a neat trick

Page 13: Python Basics! - data types, strings, indexing...Administrivia Administrivia 3/44 Lab #2 tomorrow Sunday. Where can you get help in this class? Blackboard forum Instructors in labs

How Assignment Works

Quick Review & A Bit New 5/44

x = 10y = x * xx * x = y

x,y = y,x # a neat trick

Page 14: Python Basics! - data types, strings, indexing...Administrivia Administrivia 3/44 Lab #2 tomorrow Sunday. Where can you get help in this class? Blackboard forum Instructors in labs

How Assignment Works

Quick Review & A Bit New 5/44

x = 10y = x * xx * x = y

x,y = y,x # a neat trick

Page 15: Python Basics! - data types, strings, indexing...Administrivia Administrivia 3/44 Lab #2 tomorrow Sunday. Where can you get help in this class? Blackboard forum Instructors in labs

Warmup Quiz 6/44

Warmup Quiz

Page 16: Python Basics! - data types, strings, indexing...Administrivia Administrivia 3/44 Lab #2 tomorrow Sunday. Where can you get help in this class? Blackboard forum Instructors in labs

Reminder

Warmup Quiz 7/44

You will have graded quiz starting from theupcoming Monday lecture!

Page 17: Python Basics! - data types, strings, indexing...Administrivia Administrivia 3/44 Lab #2 tomorrow Sunday. Where can you get help in this class? Blackboard forum Instructors in labs

Our execution model

Warmup Quiz 8/44

Page 18: Python Basics! - data types, strings, indexing...Administrivia Administrivia 3/44 Lab #2 tomorrow Sunday. Where can you get help in this class? Blackboard forum Instructors in labs

Question #1

Warmup Quiz 9/44

x = 10y = x + 1y = x * y

What is the value of y?A 11B 100C 110D None of the above

Page 19: Python Basics! - data types, strings, indexing...Administrivia Administrivia 3/44 Lab #2 tomorrow Sunday. Where can you get help in this class? Blackboard forum Instructors in labs

Question #2

Warmup Quiz 10/44

x = 10y = x + 1y = x * y

What do we call x?A a literalB a variableC an expressionD a statement

Page 20: Python Basics! - data types, strings, indexing...Administrivia Administrivia 3/44 Lab #2 tomorrow Sunday. Where can you get help in this class? Blackboard forum Instructors in labs

Question #3

Warmup Quiz 11/44

x = 10y = x + 1y = x * y

What do we call 10?A a literalB a variableC an expressionD a statement

Page 21: Python Basics! - data types, strings, indexing...Administrivia Administrivia 3/44 Lab #2 tomorrow Sunday. Where can you get help in this class? Blackboard forum Instructors in labs

Question #2

Warmup Quiz 12/44

x = 10y = x + 1y = x * y

What do we call y = x * y?A a literalB a variableC an expressionD a statement

Page 22: Python Basics! - data types, strings, indexing...Administrivia Administrivia 3/44 Lab #2 tomorrow Sunday. Where can you get help in this class? Blackboard forum Instructors in labs

Question #5

Warmup Quiz 13/44

x = 10y = xx = 5

What is the value of y?A 10B 5

Page 23: Python Basics! - data types, strings, indexing...Administrivia Administrivia 3/44 Lab #2 tomorrow Sunday. Where can you get help in this class? Blackboard forum Instructors in labs

Data Types 14/44

Data Types

Page 24: Python Basics! - data types, strings, indexing...Administrivia Administrivia 3/44 Lab #2 tomorrow Sunday. Where can you get help in this class? Blackboard forum Instructors in labs

What is an encoding?

Data Types 15/44

01001000 01000101 01001100 01001100What does a binary data value like this represent?

What does binary data represent?

How does the processor know?

Page 25: Python Basics! - data types, strings, indexing...Administrivia Administrivia 3/44 Lab #2 tomorrow Sunday. Where can you get help in this class? Blackboard forum Instructors in labs

What is an encoding?

Data Types 15/44

01001000 01000101 01001100 01001100What does a binary data value like this represent?

What does binary data represent?How does the processor know?

Page 26: Python Basics! - data types, strings, indexing...Administrivia Administrivia 3/44 Lab #2 tomorrow Sunday. Where can you get help in this class? Blackboard forum Instructors in labs

Our execution model

Data Types 16/44

Page 27: Python Basics! - data types, strings, indexing...Administrivia Administrivia 3/44 Lab #2 tomorrow Sunday. Where can you get help in this class? Blackboard forum Instructors in labs

What is an encoding?

Data Types 17/44

01001000 01000101 01001100 01001100What does a binary data value like this represent?

What does binary data represent?How does the processor know?The encoding interprets the value.

Page 28: Python Basics! - data types, strings, indexing...Administrivia Administrivia 3/44 Lab #2 tomorrow Sunday. Where can you get help in this class? Blackboard forum Instructors in labs

Our execution model

Data Types 18/44

Page 29: Python Basics! - data types, strings, indexing...Administrivia Administrivia 3/44 Lab #2 tomorrow Sunday. Where can you get help in this class? Blackboard forum Instructors in labs

What is a data type?

Data Types 19/44

A data type defines an encoding rule.All values have a type.

The type defines how data is represented inmemory.The type defines allowed operations and how theywork.

Page 30: Python Basics! - data types, strings, indexing...Administrivia Administrivia 3/44 Lab #2 tomorrow Sunday. Where can you get help in this class? Blackboard forum Instructors in labs

What is a data type?

Data Types 19/44

A data type defines an encoding rule.All values have a type.The type defines how data is represented inmemory.

The type defines allowed operations and how theywork.

Page 31: Python Basics! - data types, strings, indexing...Administrivia Administrivia 3/44 Lab #2 tomorrow Sunday. Where can you get help in this class? Blackboard forum Instructors in labs

What is a data type?

Data Types 19/44

A data type defines an encoding rule.All values have a type.The type defines how data is represented inmemory.The type defines allowed operations and how theywork.

Page 32: Python Basics! - data types, strings, indexing...Administrivia Administrivia 3/44 Lab #2 tomorrow Sunday. Where can you get help in this class? Blackboard forum Instructors in labs

Example

Data Types 20/44

01100111 can be the number 103, the letter g,hexadecimal 67, 3.5, etc.

So what are these data types?

Page 33: Python Basics! - data types, strings, indexing...Administrivia Administrivia 3/44 Lab #2 tomorrow Sunday. Where can you get help in this class? Blackboard forum Instructors in labs

Numeric Data Types 21/44

Numeric Data Types

Page 34: Python Basics! - data types, strings, indexing...Administrivia Administrivia 3/44 Lab #2 tomorrow Sunday. Where can you get help in this class? Blackboard forum Instructors in labs

How do binary numbers work?

Numeric Data Types 22/44

Numeric types can be represented in binary:000 0 100 4001 1 101 5010 2 110 6011 3 111 7

If we add more, the number overflows.Negative numbers? Add a sign bit.

Page 35: Python Basics! - data types, strings, indexing...Administrivia Administrivia 3/44 Lab #2 tomorrow Sunday. Where can you get help in this class? Blackboard forum Instructors in labs

How do binary numbers work?

Numeric Data Types 22/44

Numeric types can be represented in binary:000 0 100 4001 1 101 5010 2 110 6011 3 111 7

If we add more, the number overflows.

Negative numbers? Add a sign bit.

Page 36: Python Basics! - data types, strings, indexing...Administrivia Administrivia 3/44 Lab #2 tomorrow Sunday. Where can you get help in this class? Blackboard forum Instructors in labs

How do binary numbers work?

Numeric Data Types 22/44

Numeric types can be represented in binary:000 0 100 4001 1 101 5010 2 110 6011 3 111 7

If we add more, the number overflows.Negative numbers? Add a sign bit.

Page 37: Python Basics! - data types, strings, indexing...Administrivia Administrivia 3/44 Lab #2 tomorrow Sunday. Where can you get help in this class? Blackboard forum Instructors in labs

Integers,Z

Numeric Data Types 23/44

Integers have been our only type thus far....,−4,−3,−2,−1, 0,+1,+2,+3, ...

What are limits?

Page 38: Python Basics! - data types, strings, indexing...Administrivia Administrivia 3/44 Lab #2 tomorrow Sunday. Where can you get help in this class? Blackboard forum Instructors in labs

Integer operations

Numeric Data Types 24/44

Evaluating an expression of integers will generallyresult in an integer answer

3 + 5

EXCEPTION: DIVISION!3 / 4 → 0.753 // 4 → 0 (floor division)

Page 39: Python Basics! - data types, strings, indexing...Administrivia Administrivia 3/44 Lab #2 tomorrow Sunday. Where can you get help in this class? Blackboard forum Instructors in labs

Integer operations

Numeric Data Types 24/44

Evaluating an expression of integers will generallyresult in an integer answer

3 + 5EXCEPTION: DIVISION!

3 / 4 → 0.753 // 4 → 0 (floor division)

Page 40: Python Basics! - data types, strings, indexing...Administrivia Administrivia 3/44 Lab #2 tomorrow Sunday. Where can you get help in this class? Blackboard forum Instructors in labs

Integer operations

Numeric Data Types 24/44

Evaluating an expression of integers will generallyresult in an integer answer

3 + 5EXCEPTION: DIVISION!3 / 4 → 0.75

3 // 4 → 0 (floor division)

Page 41: Python Basics! - data types, strings, indexing...Administrivia Administrivia 3/44 Lab #2 tomorrow Sunday. Where can you get help in this class? Blackboard forum Instructors in labs

Integer operations

Numeric Data Types 24/44

Evaluating an expression of integers will generallyresult in an integer answer

3 + 5EXCEPTION: DIVISION!3 / 4 → 0.753 // 4 → 0 (floor division)

Page 42: Python Basics! - data types, strings, indexing...Administrivia Administrivia 3/44 Lab #2 tomorrow Sunday. Where can you get help in this class? Blackboard forum Instructors in labs

Floating-point numbers,R

Numeric Data Types 25/44

Floating-point numbers include a fractional part.

(Anything with a decimal point—2.4, 3.0.)What are limits?

Overflow/underflowArbitrary precision (π, e)

Page 43: Python Basics! - data types, strings, indexing...Administrivia Administrivia 3/44 Lab #2 tomorrow Sunday. Where can you get help in this class? Blackboard forum Instructors in labs

Floating-point numbers,R

Numeric Data Types 25/44

Floating-point numbers include a fractional part.(Anything with a decimal point—2.4, 3.0.)

What are limits?Overflow/underflowArbitrary precision (π, e)

Page 44: Python Basics! - data types, strings, indexing...Administrivia Administrivia 3/44 Lab #2 tomorrow Sunday. Where can you get help in this class? Blackboard forum Instructors in labs

Floating-point numbers,R

Numeric Data Types 25/44

Floating-point numbers include a fractional part.(Anything with a decimal point—2.4, 3.0.)What are limits?

Overflow/underflowArbitrary precision (π, e)

Page 45: Python Basics! - data types, strings, indexing...Administrivia Administrivia 3/44 Lab #2 tomorrow Sunday. Where can you get help in this class? Blackboard forum Instructors in labs

Floating-point numbers,R

Numeric Data Types 25/44

Floating-point numbers include a fractional part.(Anything with a decimal point—2.4, 3.0.)What are limits?

Overflow/underflowArbitrary precision (π, e)

Page 46: Python Basics! - data types, strings, indexing...Administrivia Administrivia 3/44 Lab #2 tomorrow Sunday. Where can you get help in this class? Blackboard forum Instructors in labs

Floating-point operations

Numeric Data Types 26/44

Evaluating an expression of floating-point valueswill result in a floating-point answer.

3.0 + 5.5 → 8.53.0 + 5.0 → 8.03 + 5.5 → ? (what happens here?)

Engineers and scientists need to think carefullyabout the precision of answers.

Page 47: Python Basics! - data types, strings, indexing...Administrivia Administrivia 3/44 Lab #2 tomorrow Sunday. Where can you get help in this class? Blackboard forum Instructors in labs

Floating-point operations

Numeric Data Types 26/44

Evaluating an expression of floating-point valueswill result in a floating-point answer.

3.0 + 5.5 → 8.5

3.0 + 5.0 → 8.03 + 5.5 → ? (what happens here?)

Engineers and scientists need to think carefullyabout the precision of answers.

Page 48: Python Basics! - data types, strings, indexing...Administrivia Administrivia 3/44 Lab #2 tomorrow Sunday. Where can you get help in this class? Blackboard forum Instructors in labs

Floating-point operations

Numeric Data Types 26/44

Evaluating an expression of floating-point valueswill result in a floating-point answer.

3.0 + 5.5 → 8.53.0 + 5.0 → 8.0

3 + 5.5 → ? (what happens here?)Engineers and scientists need to think carefullyabout the precision of answers.

Page 49: Python Basics! - data types, strings, indexing...Administrivia Administrivia 3/44 Lab #2 tomorrow Sunday. Where can you get help in this class? Blackboard forum Instructors in labs

Floating-point operations

Numeric Data Types 26/44

Evaluating an expression of floating-point valueswill result in a floating-point answer.

3.0 + 5.5 → 8.53.0 + 5.0 → 8.03 + 5.5 → ? (what happens here?)

Engineers and scientists need to think carefullyabout the precision of answers.

Page 50: Python Basics! - data types, strings, indexing...Administrivia Administrivia 3/44 Lab #2 tomorrow Sunday. Where can you get help in this class? Blackboard forum Instructors in labs

Floating-point operations

Numeric Data Types 26/44

Evaluating an expression of floating-point valueswill result in a floating-point answer.

3.0 + 5.5 → 8.53.0 + 5.0 → 8.03 + 5.5 → ? (what happens here?)

Engineers and scientists need to think carefullyabout the precision of answers.

Page 51: Python Basics! - data types, strings, indexing...Administrivia Administrivia 3/44 Lab #2 tomorrow Sunday. Where can you get help in this class? Blackboard forum Instructors in labs

Complex numbers,C

Numeric Data Types 27/44

Represent numbers with an imaginary component.

Use j for i:1.0 + 1jThink of ”jmaginary” numbers, I suppose.

Page 52: Python Basics! - data types, strings, indexing...Administrivia Administrivia 3/44 Lab #2 tomorrow Sunday. Where can you get help in this class? Blackboard forum Instructors in labs

Complex numbers,C

Numeric Data Types 27/44

Represent numbers with an imaginary component.Use j for i:1.0 + 1j

Think of ”jmaginary” numbers, I suppose.

Page 53: Python Basics! - data types, strings, indexing...Administrivia Administrivia 3/44 Lab #2 tomorrow Sunday. Where can you get help in this class? Blackboard forum Instructors in labs

Complex numbers,C

Numeric Data Types 27/44

Represent numbers with an imaginary component.Use j for i:1.0 + 1jThink of ”jmaginary” numbers, I suppose.

Page 54: Python Basics! - data types, strings, indexing...Administrivia Administrivia 3/44 Lab #2 tomorrow Sunday. Where can you get help in this class? Blackboard forum Instructors in labs

Example

Numeric Data Types 28/44

x = 4y = 3 + 1jz = 33.3333print( x + y + z )

What is printed to the screen?

A 40B 40.3333C 40.3333 + 1jD None of the above

Page 55: Python Basics! - data types, strings, indexing...Administrivia Administrivia 3/44 Lab #2 tomorrow Sunday. Where can you get help in this class? Blackboard forum Instructors in labs

Example

Numeric Data Types 28/44

x = 4y = 3 + 1jz = 33.3333print( x + y + z )

What is printed to the screen?A 40B 40.3333C 40.3333 + 1jD None of the above

Page 56: Python Basics! - data types, strings, indexing...Administrivia Administrivia 3/44 Lab #2 tomorrow Sunday. Where can you get help in this class? Blackboard forum Instructors in labs

Attribute operator .

Numeric Data Types 29/44

Reaches inside of a value to access part of itsdata (called an attribute).

Extracts special variables stored “inside” of thetype.print(x.real)print(x.imag)Both of these components are floats.

Page 57: Python Basics! - data types, strings, indexing...Administrivia Administrivia 3/44 Lab #2 tomorrow Sunday. Where can you get help in this class? Blackboard forum Instructors in labs

Attribute operator .

Numeric Data Types 29/44

Reaches inside of a value to access part of itsdata (called an attribute).Extracts special variables stored “inside” of thetype.print(x.real)print(x.imag)

Both of these components are floats.

Page 58: Python Basics! - data types, strings, indexing...Administrivia Administrivia 3/44 Lab #2 tomorrow Sunday. Where can you get help in this class? Blackboard forum Instructors in labs

Attribute operator .

Numeric Data Types 29/44

Reaches inside of a value to access part of itsdata (called an attribute).Extracts special variables stored “inside” of thetype.print(x.real)print(x.imag)Both of these components are floats.

Page 59: Python Basics! - data types, strings, indexing...Administrivia Administrivia 3/44 Lab #2 tomorrow Sunday. Where can you get help in this class? Blackboard forum Instructors in labs

Example

Numeric Data Types 30/44

x = (3.5 + 1j)y = 1z = x + y

What is the value of z.imag?

A 4.5 + 1jB 4.5C 1jD 1.0

Page 60: Python Basics! - data types, strings, indexing...Administrivia Administrivia 3/44 Lab #2 tomorrow Sunday. Where can you get help in this class? Blackboard forum Instructors in labs

Example

Numeric Data Types 30/44

x = (3.5 + 1j)y = 1z = x + y

What is the value of z.imag?A 4.5 + 1jB 4.5C 1jD 1.0

Page 61: Python Basics! - data types, strings, indexing...Administrivia Administrivia 3/44 Lab #2 tomorrow Sunday. Where can you get help in this class? Blackboard forum Instructors in labs

String Data Type 31/44

String Data Type

Page 62: Python Basics! - data types, strings, indexing...Administrivia Administrivia 3/44 Lab #2 tomorrow Sunday. Where can you get help in this class? Blackboard forum Instructors in labs

How does text work?

String Data Type 32/44

Each symbol is stored individually, one byte long:01001000 7201000101 6901001100 7601001100 7601001111 79

Page 63: Python Basics! - data types, strings, indexing...Administrivia Administrivia 3/44 Lab #2 tomorrow Sunday. Where can you get help in this class? Blackboard forum Instructors in labs

ASCII encoding table

String Data Type 33/44

72 69 76 76 79 = H E L L O'HELLO'

Page 64: Python Basics! - data types, strings, indexing...Administrivia Administrivia 3/44 Lab #2 tomorrow Sunday. Where can you get help in this class? Blackboard forum Instructors in labs

ASCII encoding table

String Data Type 33/44

72 69 76 76 79 = H E L L O

'HELLO'

Page 65: Python Basics! - data types, strings, indexing...Administrivia Administrivia 3/44 Lab #2 tomorrow Sunday. Where can you get help in this class? Blackboard forum Instructors in labs

ASCII encoding table

String Data Type 33/44

72 69 76 76 79 = H E L L O'HELLO'

Page 66: Python Basics! - data types, strings, indexing...Administrivia Administrivia 3/44 Lab #2 tomorrow Sunday. Where can you get help in this class? Blackboard forum Instructors in labs

Strings

String Data Type 34/44

As a literal: text surrounded by quotes."DEEP"

Each symbol is a character.Unlike numeric types, strings vary in length.

Page 67: Python Basics! - data types, strings, indexing...Administrivia Administrivia 3/44 Lab #2 tomorrow Sunday. Where can you get help in this class? Blackboard forum Instructors in labs

Strings

String Data Type 34/44

As a literal: text surrounded by quotes."DEEP"

Each symbol is a character.

Unlike numeric types, strings vary in length.

Page 68: Python Basics! - data types, strings, indexing...Administrivia Administrivia 3/44 Lab #2 tomorrow Sunday. Where can you get help in this class? Blackboard forum Instructors in labs

Strings

String Data Type 34/44

As a literal: text surrounded by quotes."DEEP"

Each symbol is a character.Unlike numeric types, strings vary in length.

Page 69: Python Basics! - data types, strings, indexing...Administrivia Administrivia 3/44 Lab #2 tomorrow Sunday. Where can you get help in this class? Blackboard forum Instructors in labs

String operations

String Data Type 35/44

Concatenation: combine two stringsUses the + symbol'RACE' + 'CAR'

Repetition: repeat a stringUses the *'HELLO '*10

Formatting: used to encode other data as stringUses % symbol

Page 70: Python Basics! - data types, strings, indexing...Administrivia Administrivia 3/44 Lab #2 tomorrow Sunday. Where can you get help in this class? Blackboard forum Instructors in labs

String operations

String Data Type 35/44

Concatenation: combine two stringsUses the + symbol'RACE' + 'CAR'

Repetition: repeat a stringUses the *'HELLO '*10

Formatting: used to encode other data as stringUses % symbol

Page 71: Python Basics! - data types, strings, indexing...Administrivia Administrivia 3/44 Lab #2 tomorrow Sunday. Where can you get help in this class? Blackboard forum Instructors in labs

String operations

String Data Type 35/44

Concatenation: combine two stringsUses the + symbol'RACE' + 'CAR'

Repetition: repeat a stringUses the *'HELLO '*10

Formatting: used to encode other data as stringUses % symbol

Page 72: Python Basics! - data types, strings, indexing...Administrivia Administrivia 3/44 Lab #2 tomorrow Sunday. Where can you get help in this class? Blackboard forum Instructors in labs

Formatting operator

String Data Type 36/44

Creates string with value inserted

Formats nicelyRequires indicator of type inside of string

x = 100 * 54s = "String is: %i" % xprint(s)

Page 73: Python Basics! - data types, strings, indexing...Administrivia Administrivia 3/44 Lab #2 tomorrow Sunday. Where can you get help in this class? Blackboard forum Instructors in labs

Formatting operator

String Data Type 36/44

Creates string with value insertedFormats nicelyRequires indicator of type inside of string

x = 100 * 54s = "String is: %i" % xprint(s)

Page 74: Python Basics! - data types, strings, indexing...Administrivia Administrivia 3/44 Lab #2 tomorrow Sunday. Where can you get help in this class? Blackboard forum Instructors in labs

Formatting operator

String Data Type 36/44

Creates string with value insertedFormats nicelyRequires indicator of type inside of string

x = 100 * 54s = "String is: %i" % xprint(s)

Page 75: Python Basics! - data types, strings, indexing...Administrivia Administrivia 3/44 Lab #2 tomorrow Sunday. Where can you get help in this class? Blackboard forum Instructors in labs

Example

String Data Type 37/44

name = "Tao"grade = 2 / 3m1 = "Hello, %s!" % namem2 = "Your grade is: %f." % gradeprint(m1)print(m2)

Hello, Tao!Your grade is 0.66667.

Page 76: Python Basics! - data types, strings, indexing...Administrivia Administrivia 3/44 Lab #2 tomorrow Sunday. Where can you get help in this class? Blackboard forum Instructors in labs

Example

String Data Type 37/44

name = "Tao"grade = 2 / 3m1 = "Hello, %s!" % namem2 = "Your grade is: %f." % gradeprint(m1)print(m2)

Hello, Tao!Your grade is 0.66667.

Page 77: Python Basics! - data types, strings, indexing...Administrivia Administrivia 3/44 Lab #2 tomorrow Sunday. Where can you get help in this class? Blackboard forum Instructors in labs

Example

String Data Type 38/44

x = 3s = ("%i" % (x+1)) * x**(5%x)print(s)

What does this program print?A 333333333333B 444444444C 9999D %i%i%i%i%i

Page 78: Python Basics! - data types, strings, indexing...Administrivia Administrivia 3/44 Lab #2 tomorrow Sunday. Where can you get help in this class? Blackboard forum Instructors in labs

Indexing operator

String Data Type 39/44

Extracts single character

a = "FIRE"a[0]The integer is the index.We count from zero!If negative, counts down from end.

Page 79: Python Basics! - data types, strings, indexing...Administrivia Administrivia 3/44 Lab #2 tomorrow Sunday. Where can you get help in this class? Blackboard forum Instructors in labs

Indexing operator

String Data Type 39/44

Extracts single charactera = "FIRE"a[0]

The integer is the index.We count from zero!If negative, counts down from end.

Page 80: Python Basics! - data types, strings, indexing...Administrivia Administrivia 3/44 Lab #2 tomorrow Sunday. Where can you get help in this class? Blackboard forum Instructors in labs

Indexing operator

String Data Type 39/44

Extracts single charactera = "FIRE"a[0]The integer is the index.

We count from zero!If negative, counts down from end.

Page 81: Python Basics! - data types, strings, indexing...Administrivia Administrivia 3/44 Lab #2 tomorrow Sunday. Where can you get help in this class? Blackboard forum Instructors in labs

Indexing operator

String Data Type 39/44

Extracts single charactera = "FIRE"a[0]The integer is the index.We count from zero!

If negative, counts down from end.

Page 82: Python Basics! - data types, strings, indexing...Administrivia Administrivia 3/44 Lab #2 tomorrow Sunday. Where can you get help in this class? Blackboard forum Instructors in labs

Indexing operator

String Data Type 39/44

Extracts single charactera = "FIRE"a[0]The integer is the index.We count from zero!If negative, counts down from end.

Page 83: Python Basics! - data types, strings, indexing...Administrivia Administrivia 3/44 Lab #2 tomorrow Sunday. Where can you get help in this class? Blackboard forum Instructors in labs

Question

String Data Type 40/44

s = "ABCDE"i = 3x = s[i]

What is the value of x?A 'A'B 'B'C 'C'D 'D'E 'E'

Page 84: Python Basics! - data types, strings, indexing...Administrivia Administrivia 3/44 Lab #2 tomorrow Sunday. Where can you get help in this class? Blackboard forum Instructors in labs

Question

String Data Type 41/44

s = "ABCDE"i = 25 % 3y = s[i]

What is the value of y?A 'A'B 'B'C 'C'D 'D'E 'E'

Page 85: Python Basics! - data types, strings, indexing...Administrivia Administrivia 3/44 Lab #2 tomorrow Sunday. Where can you get help in this class? Blackboard forum Instructors in labs

Question

String Data Type 42/44

s = "ABCDE"i = (11 % 3) - 7z = s[i]

What is the value of z?A 'A'B 'B'C 'C'D 'D'E 'E'

Page 86: Python Basics! - data types, strings, indexing...Administrivia Administrivia 3/44 Lab #2 tomorrow Sunday. Where can you get help in this class? Blackboard forum Instructors in labs

Reminders 43/44

Reminders

Page 87: Python Basics! - data types, strings, indexing...Administrivia Administrivia 3/44 Lab #2 tomorrow Sunday. Where can you get help in this class? Blackboard forum Instructors in labs

Reminders

Reminders 44/44

Lab #2 tomorrow Sunday.