lesson1: getting started with idle, , strings, variables

42
Lesson1: Getting Started with IDLE, print, Strings, Variables Fundamentals of Text Processing for Linguists Na-Rae Han

Upload: others

Post on 15-Nov-2021

3 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Lesson1: Getting Started with IDLE, , Strings, Variables

Lesson1: Getting Started with

IDLE, print, Strings, Variables

Fundamentals of Text Processing for Linguists

Na-Rae Han

Page 2: Lesson1: Getting Started with IDLE, , Strings, Variables

Objectives

Introduction to Python 2.7

Try out Python IDLE (Interactive DeveLopment Environment)

Learn Python basic syntax

print command

Strings

Variables

Learn to get around in Python IDLE

Restarting IDLE session

Saving IDLE session into a file

Configure IDLE command-history key shortcuts

1/8/2014 2

Page 3: Lesson1: Getting Started with IDLE, , Strings, Variables

Your first code

1/8/2014 3

Type up into your IDLE shell window, followed by ENTER:

print 'Hello, world'

What you get:

Try it again.

This time, print out 'Hello, world!!!!'

>>> print 'Hello, world'

Hello, world

>>>

Page 4: Lesson1: Getting Started with IDLE, , Strings, Variables

Every command you type in is stored in a buffer as command history.

You can quickly bring up and edit a previously entered command using these shortcuts:

Mac: Ctrl+p / Ctrl+n (previous/next)

Windows: Alt+p / Alt+n

>>> print 'Hello, world!'

Hello, world!

>>> print 'Hello, worl

Command history

1/8/2014 4

You can edit command line

Page 5: Lesson1: Getting Started with IDLE, , Strings, Variables

Customize command history keys

1/8/2014 5

Default IDLE key bindings assigned to previous- and next- command: Ctrl+p and Ctrl+n (Alt key in Win)

These are hard to use! Let's change them to: (Up arrow: previous command)

(Down arrow: next command)

Instructions: 1. From the menu go to "Options Configure IDLE"

2. Click "Keys" tab

3. Scroll down and click on the line starting with "history-next". Click button "Get New Keys for Selection"

4. Scroll down to find "Down Arrow" and click on it.

5. The new key is now set to "<Key-Down>". Press OK.

Using arrows is a common practice

Page 6: Lesson1: Getting Started with IDLE, , Strings, Variables

1/8/2014 6

6. You are prompted to name your custom key scheme. Give any name.

7. Now repeat above process for "history-previous". But this time select "Up Arrow".

Page 7: Lesson1: Getting Started with IDLE, , Strings, Variables

More commands

1/8/2014 7

Try:

print 'Hello', 'world!'

4*17

print 4*17

4 * 17 + 2

4*17+2

(4 * 17 + 2) / 10

10 / 3

10.0 / 3

Page 8: Lesson1: Getting Started with IDLE, , Strings, Variables

1/8/2014 8

>>> print 'Hello', 'world!'

Hello world!

>>> 4*17

68

>>> print 4*17

68

>>> 4 * 17 + 2

70

>>> 4*17+2

70

>>> (4 * 17 + 2) / 10

7

>>>

prints value

returns value

white space is optional here

prints two strings as two words

( ) specifies scope

Page 9: Lesson1: Getting Started with IDLE, , Strings, Variables

1/8/2014 9

>>> 10 / 3

3

>>> 10.0 / 3

3.3333333333333335

>>>

Integer division: An integer is returned

Floating point number division: A floating point number (rounded)

is returned

Page 10: Lesson1: Getting Started with IDLE, , Strings, Variables

The print command

1/8/2014 10

>>> print 'Hello, world!'

Hello, world!

>>> print "Hello, world!"

Hello, world!

>>> print 'Hello', 'world!'

Hello world!

>>> print 'cat', 'dog', 'fox'

cat dog fox

>>> print 4500*12

54000

>>> print 'Homer makes', 4500*12, 'dollars a year.'

Homer makes 54000 dollars a year.

Page 11: Lesson1: Getting Started with IDLE, , Strings, Variables

The print command

1/8/2014 11

>>> print 'Hello, world!'

Hello, world!

>>> print "Hello, world!"

Hello, world!

>>> print 'Hello', 'world!'

Hello world!

>>> print 'cat', 'dog', 'fox'

cat dog fox

>>> print 4500*12

54000

>>> print 'Homer makes', 4500*12, 'dollars a year.'

Homer makes 54000 dollars a year.

1 string, enclosed in ' ' or " "

2 strings, separated by a comma

3 strings

Page 12: Lesson1: Getting Started with IDLE, , Strings, Variables

>>> print 'Hello, world!'

Hello, world!

>>> print "Hello, world!"

Hello, world!

>>> print 'Hello', 'world!'

Hello world!

>>> print 'cat', 'dog', 'fox'

cat dog fox

>>> print 4500*12

54000

>>> print 'Homer makes', 4500*12, 'dollars a year.'

Homer makes 54000 dollars a year.

Practice

1/8/2014 12

1 minute

Page 13: Lesson1: Getting Started with IDLE, , Strings, Variables

>>> salary = 4500*12

>>> print salary

54000

Variable assignment

1/8/2014 13

VALUE is what’s stored, not the

expression itself

Variable assignment operator

Variable name

Expression: variable content

Page 14: Lesson1: Getting Started with IDLE, , Strings, Variables

>>> salary = 4500*12

>>> print salary

54000

Variable assignment

1/8/2014 14

VALUE is what’s stored, not the

expression itself

Variable assignment operator

Variable name

Expression: variable content

Page 15: Lesson1: Getting Started with IDLE, , Strings, Variables

1/8/2014 15

>>> who = 'Homer'

>>> print who

Homer

>>> who

'Homer'

>>> salary = 4500 *12

>>> salary

54000

>>> print who, 'makes', salary, 'dollars a year.'

Homer makes 54000 dollars a year.

>>> who = 'Mr. Burns'

>>> salary = 50000*12

>>> print who, 'makes', salary, 'dollars a year.'

Mr. Burns makes 600000 dollars a year.

Page 16: Lesson1: Getting Started with IDLE, , Strings, Variables

Printing vs. Returning

1/8/2014 16

>>> who = 'Homer'

>>> print who

Homer

>>> who

'Homer'

>>> salary = 4500 *12

>>> salary

54000

>>> print who, 'makes', salary, 'dollars a year.'

Homer makes 54000 dollars a year.

>>> who = 'Mr. Burns'

>>> salary = 50000*12

>>> print who, 'makes', salary, 'dollars a year.'

Mr. Burns makes 600000 dollars a year.

Prints string

content Evaluates expression,

returns the value. Value is a string: note the quotes!

Page 17: Lesson1: Getting Started with IDLE, , Strings, Variables

Changing variable value

1/8/2014 17

>>> who = 'Homer'

>>> print who

Homer

>>> who

'Homer'

>>> salary = 4500 *12

>>> salary

54000

>>> print who, 'makes', salary, 'dollars a year.'

Homer makes 54000 dollars a year.

>>> who = 'Mr. Burns'

>>> salary = 50000*12

>>> print who, 'makes', salary, 'dollars a year.'

Mr. Burns makes 600000 dollars a year.

Variable value is declared and then changed

Page 18: Lesson1: Getting Started with IDLE, , Strings, Variables

Practice

1/8/2014 18

Try:

greet = 'Hello,'

nameis = 'my name is'

name = 'Homer Simpson'

print greet, nameis, name

### change name to 'Abraham Lincoln'

### then print again

### change greet to 'Good morning everyone,'

### then print again

Same command. Use command history

and!

1 minute

Page 19: Lesson1: Getting Started with IDLE, , Strings, Variables

Variables, variables

1/8/2014 19

>>> greet = 'Hello,'

>>> nameis = 'my name is'

>>> name = 'Homer Simpson'

>>> print greet, nameis, name

Hello, my name is Homer Simpson

>>>

>>> name = 'Abraham Lincoln'

>>> print greet, nameis, name

Hello, my name is Abraham Lincoln

>>>

>>> greet = 'Good morning everyone,'

>>> print greet, nameis, name

Good morning everyone, my name is Abraham Lincoln

Page 20: Lesson1: Getting Started with IDLE, , Strings, Variables

Can anything be a variable name?

1/8/2014 20

Answer: NO.

You can use: Any letter

Number, but not initially. ex: name1 is good, 1name is not

Underscore "_". ex: student_name

Variable names are case-sensitive! greet and Greet are two different variables.

Following are built-in Python keywords and cannot be used as a variable name: and assert break class continue def del elif else except exec finally for from global if import in is lambda not or pass print raise return try while yield

Page 21: Lesson1: Getting Started with IDLE, , Strings, Variables

Ctrl+F6 restarts IDLE shell

>>> greet = 'Hello,'

>>> nameis = 'my name is'

>>> name = 'Homer Simpson'

>>> print greet, nameis, name

Hello, my name is Homer Simpson

>>> name = 'Abraham Lincoln'

>>> print greet, nameis, name

Hello, my name is Abraham Lincoln

>>> ================================ RESTART ================

>>> print greet, nameis, name

Traceback (most recent call last):

File "<pyshell#3>", line 1, in <module>

print greet, nameis, name

NameError: name 'greet' is not defined

Restarting shell

1/8/2014 21

After RESTART, the 3 variables are no longer

defined!!

Page 22: Lesson1: Getting Started with IDLE, , Strings, Variables

String vs. number: '+' and len()

1/8/2014 22

>>> 4 + 15

19

>>> '4 + 15'

'4 + 15'

>>> '4' + '15'

'415'

>>> 'ab' + 'cde'

'abcde'

>>> len('abcde')

5

>>> len('4' + '15')

3

>>> len('ab' + 'cde')

5

>>> len(4 + 15)

Traceback (most recent call last):

File "<pyshell#12>", line 1, in <module>

len(4 + 15)

TypeError: object of type 'int' has no len()

Page 23: Lesson1: Getting Started with IDLE, , Strings, Variables

String vs. number: '+' and len()

1/8/2014 23

>>> 4 + 15

19

>>> '4 + 15'

'4 + 15'

>>> '4' + '15'

'415'

>>> 'ab' + 'cde'

'abcde'

>>> len('abcde')

5

>>> len('4' + '15')

3

>>> len('ab' + 'cde')

5

>>> len(4 + 15)

Traceback (most recent call last):

File "<pyshell#12>", line 1, in <module>

len(4 + 15)

TypeError: object of type 'int' has no len()

ERROR: Integers do not inherently have

length!

Page 24: Lesson1: Getting Started with IDLE, , Strings, Variables

len()

1/8/2014 24

>>> 4 + 15

19

>>> '4 + 15'

'4 + 15'

>>> '4' + '15'

'415'

>>> 'ab' + 'cde'

'abcde'

>>> len('abcde')

5

>>> len('4' + '15')

3

>>> len('ab' + 'cde')

5

>>> len(4 + 15)

Traceback (most recent call last):

File "<pyshell#12>", line 1, in <module>

len(4 + 15)

TypeError: object of type 'int' has no len()

len() returns the length of a string

measured in # of characters

Page 25: Lesson1: Getting Started with IDLE, , Strings, Variables

+: number vs. string operation

1/8/2014 25

>>> 4 + 15

19

>>> '4 + 15'

'4 + 15'

>>> '4' + '15'

'415'

>>> 'ab' + 'cde'

'abcde'

>>> len('abcde')

5

>>> len('4' + '15')

3

>>> len('ab' + 'cde')

5

>>> len(4 + 15)

Traceback (most recent call last):

File "<pyshell#12>", line 1, in <module>

len(4 + 15)

TypeError: object of type 'int' has no len()

4 and 15 are integers. +: addition operator

'4' and '15' are strings of letters. + is a concatenation operator

Page 26: Lesson1: Getting Started with IDLE, , Strings, Variables

+ vs. print, * for numbers vs. strings

1/8/2014 26

Try:

'police' + 'man'

print 'police', 'man'

print 'police' + 'man'

14 * 3

'abc' * 3

'x' * 10

'14' * 3

1 minute

Page 27: Lesson1: Getting Started with IDLE, , Strings, Variables

>>> 'police' + 'man'

'policeman'

>>> print 'police', 'man'

police man

>>> print 'police' + 'man'

policeman

1/8/2014 27

prints two strings, separated by a space

prints the concatenated output as a single string

Page 28: Lesson1: Getting Started with IDLE, , Strings, Variables

>>> 14 * 3

42

>>> 'abc' * 3

'abcabcabc'

>>> '14' * 3

'141414'

>>> 'x' * 10

'xxxxxxxxxx'

1/8/2014 28

Integer arguments. *: multiplication operator

String argument on left. * is a multi-copy concatenation

operator

+, *: overloaded operators

Just like +, * also behaves differently for number and string arguments:

overloaded operators

Page 29: Lesson1: Getting Started with IDLE, , Strings, Variables

+ as concatenation operator

1/8/2014 29

>>> w1 = 'school'

>>> w2 = 'bus'

>>> w1+w2

'schoolbus'

>>> print w1, w2

school bus

>>> print w1+w2

schoolbus

>>> w1+' '+w2

'school bus'

>>> print w1+' '+w2

school bus

>>> [w1, w2]

['school', 'bus']

Page 30: Lesson1: Getting Started with IDLE, , Strings, Variables

A string can contain a space

1/8/2014 30

>>> w1 = 'school'

>>> w2 = 'bus'

>>> w1+w2

'schoolbus'

>>> print w1, w2

school bus

>>> print w1+w2

schoolbus

>>> w1+' '+w2

'school bus'

>>> print w1+' '+w2

school bus

>>> [w1, w2]

['school', 'bus']

Page 31: Lesson1: Getting Started with IDLE, , Strings, Variables

A list of strings

1/8/2014 31

>>> w1 = 'school'

>>> w2 = 'bus'

>>> w1+w2

'schoolbus'

>>> print w1, w2

school bus

>>> print w1+w2

schoolbus

>>> w1+' '+w2

'school bus'

>>> print w1+' '+w2

school bus

>>> [w1, w2]

['school', 'bus']

A list containing two strings. Square bracket [ ]

marks the list data type.

Page 32: Lesson1: Getting Started with IDLE, , Strings, Variables

String: a single piece of text, composed of a sequence of characters.

Enclosed in ' ' or " "

String

1/8/2014 32

>>> print "It's me, Homer."

It's me, Homer.

>>> print 'Homer says "hi."'

Homer says "hi."

Page 33: Lesson1: Getting Started with IDLE, , Strings, Variables

String: a single piece of text, composed of a sequence of characters.

Enclosed in ' ' or " "

Enclosed in """ """ (triple double quotes) Can be multi-line!

String

1/8/2014 33

>>> print "It's me, Homer."

It's me, Homer.

>>> print 'Homer says "hi."'

Homer says "hi."

>>> print """This is a long

string that spans

three lines."""

This is a long

string that spans

three lines.

Page 34: Lesson1: Getting Started with IDLE, , Strings, Variables

The trouble with quotation marks

1/8/2014 34

But how to print:

Method 1: Use triple double quotes """.

Method 2: Use backslash \ to "escape".

>>> print """Both ' and " are ok."""

Both ' and " are ok.

>>> print """Both ' and " are ok."""

Both ' and " are ok.

>>> print 'Both \' and " are ok.'

Both ' and " are ok.

>>> print "Both ' and \" are ok."

Both ' and " are ok.

?

Page 35: Lesson1: Getting Started with IDLE, , Strings, Variables

Escaping with backslash \

1/8/2014 35

>>> print 'It\'s snowing.'

It's snowing.

>>> print 'lala\tlala'

lala lala

>>> print 'lala\nlala'

lala

lala

>>> multi = """This is a

long string across

multiple lines."""

>>> print multi

This is a

long string across

multiple lines.

>>> multi

'This is a\nlong string across\nmultiple lines.'

Page 36: Lesson1: Getting Started with IDLE, , Strings, Variables

Escaping with backslash \

1/8/2014 36

>>> print 'It\'s snowing.'

It's snowing.

>>> print 'lala\tlala'

lala lala

>>> print 'lala\nlala'

lala

lala

>>> multi = """This is a

long string across

multiple lines."""

>>> print multi

This is a

long string across

multiple lines.

>>> multi

'This is a\nlong string across\nmultiple lines.'

Page 37: Lesson1: Getting Started with IDLE, , Strings, Variables

Special character and \

1/8/2014 37

Prefixing backslash \ (=escaping) turns:

A special character into a normal one

A normal character into a special one

SPECIAL NORMAL

' String delimiter \' Single quote character

" String delimiter \" Double quote character

\ Escape marker \\ Backslash character

NORMAL SPECIAL

n Alphabet n \n New line character

t Alphabet t \t Tab character

Page 38: Lesson1: Getting Started with IDLE, , Strings, Variables

Compose print commands to output the following:

>>> print 'Look in F:\\temp\\note.'

Look in F:\temp\note.

>>> print 'Lisa said \'Look in F:\\temp\\note\'.'

Lisa said 'Look in F:\temp\note'.

>>> print 'Lisa said \'Look in F:\\temp\\note and find "my.txt".\''

Lisa said 'Look in F:\temp\note and find "my.txt"'.

Practice

1/8/2014 38

>>> print 'Roses are red,\nViolets are blue,\nSugar is sweet.'

Roses are red,

Violets are blue,

Sugar is sweet.

2 minutes

Page 39: Lesson1: Getting Started with IDLE, , Strings, Variables

Practice

1/8/2014 39

Compose print commands to output the following:

>>> print 'Look in F:\\temp\\note.'

Look in F:\temp\note.

>>> print 'Lisa said \'Look in F:\\temp\\note\'.'

Lisa said 'Look in F:\temp\note'.

>>> print 'Lisa said \'Look in F:\\temp\\note and find "my.txt".\''

Lisa said 'Look in F:\temp\note and find "my.txt"'.

>>> print 'Roses are red,\nViolets are blue,\nSugar is sweet.'

Roses are red,

Violets are blue,

Sugar is sweet. 2 minutes

Page 40: Lesson1: Getting Started with IDLE, , Strings, Variables

Save IDLE shell session

1/8/2014 40

You can save your IDLE shell session into a text file Ctrl+Shift+s

Every command you typed in and its output is saved into a file

You should save it as a text file (.txt) and not as a python script (.py or .pyw). An interactive IDLE session itself is NOT

a python script! You cannot run this file!!

You can open the file in IDLE or any text editor program Handy for reviewing later.

Will be required for exercise assignments.

Page 41: Lesson1: Getting Started with IDLE, , Strings, Variables

.split()

splits a string into a list.

>>> abc = 'a-b-c-d-e-f-g'

>>> abc.split('-')

['a', 'b', 'c', 'd', 'e', 'f', 'g']

>>> chom = 'colorless green ideas sleep furiously'

>>> chom.split()

['colorless', 'green', 'ideas', 'sleep', 'furiously']

Looking ahead

1/8/2014 41

no delimiter supplied:

splits on space

splits abc on each '-'

>>> rhyme = 'eeny, meeny, miney, mo'

>>> rhyme.split(', ')

['eeny', 'meeny', 'miney', 'mo']

splits rhyme on each ', '

Page 42: Lesson1: Getting Started with IDLE, , Strings, Variables

Wrap-up

1/8/2014 42

If you want to try more commands, visit:

A Beginner's Python Tutorial, Lesson 2 & 3

http://www.sthurlow.com/python/lesson02/

http://www.sthurlow.com/python/lesson03/

Exercise #1

Due Tuesday midnight