Class 06 – Monday, January 31

Fearlessly we march on — variables and strings

A sign for knowing — what the future asks of us — demands we act now


Look both ways


Agenda


Examples


Inquiring minds want to know


Program being_powerful.py

  • Get inputs
  • Massage inputs into needed values
  • Compute result
  • Print result

Enter base (integer): 2

Enter exponent (decimal): 0.5

1.4142135623730951

Enter base (integer): 11

Enter exponent (decimal): 3.14

1861.971509290233

Enter base (integer): 4

Enter exponent (decimal): 0.333

1.5866676863822857



Program trio.py

Enter three words: peace joy friends

Your words

peace

joy

friends

Enter three words: ready or not

Your words

ready

or

not



Program i_get_around.py

Enter number: 3.1415926

number: 3.1415926

round( number, 6 ): 3.141593

round( number, 5 ): 3.14159

round( number, 4 ): 3.1416

round( number, 3 ): 3.142

round( number, 2 ): 3.14

round( number, 1 ): 3.1

round( number, 0 ): 3.0

round( number ): 3

Enter number: 2.7182818

number: 2.7182818

round( number, 6 ): 2.718282

round( number, 5 ): 2.71828

round( number, 4 ): 2.7183

round( number, 3 ): 2.718

round( number, 2 ): 2.72

round( number, 1 ): 2.7

round( number, 0 ): 3.0

round( number ): 3



Program escaping.py

a: a b c d

b: "Air quotes" are annoying

c: Hello

 

Goodbye

 

len( a ): x

len( a ): 7

len( b ): 25



Program who_what_and_where.py

  • id( n ) identifies where in memory to find n
  • type( n ) supplies what type of value is n

Enter integer and decimal: 3 4.159

reply: 3 4.159 <class 'str'> 140400369925360

n: 3 <class 'int'> 9720640

x: 4.159 <class 'float'> 140400370194512

Enter integer and decimal: 8 8.88

reply: 8 8.88 <class 'str'> 139778356653296

n: 8 <class 'int'> 9720800

x: 8.88 <class 'float'> 139778356922448



Program the_old_switcheroo.py

reply = input( 'Enter two words: ' )

w1, w2 = reply.split()

print( )

print( 'w1 =', w1 )

print( 'w2 =', w2 )

print()

# swap the values of w1 and w2

w1 = w2

w2 = w1

# print results

print( 'After swapping' )

print()

print( 'w1 =', w1 )

print( 'w2 =', w2 )

Enter two words: wahoo wah

w1 = wahoo

w2 = wah

After swapping

w1 = wah

w2 = wah



Homework Program all_consuming.py

What is something you consume daily? paper napkins

How much consumed on a typical weekday: 6

How much consumed on a typical weekend day: 0

You consume 1560 paper napkins per year.

What is something you consume daily? pretzels

How much consumed on a typical weekday: 0

How much consumed on a typical weekend day: 1

You consume 104 pretzels per year.

What is something you consume daily? jelly beans

How much consumed on a typical weekday: 4

How much consumed on a typical weekend day: 12

You consume 2288 jelly beans per year.



To do list

 


  🦆 © 2022 Jim Cohoon   Resources from previous semesters are available.