Back

Python Chrestomathics

Ahead


Part 3 — Remembering and manipulating values

Together we learn — To say what we mean to say — A valuable skill

values   learning   values



Section 3.1: Names have power

Rumpelstiltskin - Anne Andersonx original at https://upload.wikimedia.org/wikipedia/commons/thumb/7/77/Rumpelstiltskin-_Anne_Anderson.jpg/256px-Rumplestiltskin_-_Anne_Anderson.jpg

Rumpelstiltskin by Anne Anderson

It was created (and copyrighted) for the music artist Prince. He used this symbol as his representation of his name for several years to antagonize his recording label.

sum

miles_per_hound

week51

meetingTime

2bOrNot2b

family name

:-)



Section 3.2 — Variables and assignment

A variable — Is like the name of a box — Store that in your brain

target = calculation

Identifier Type Identity
distance integer #459720864
hours integer #459721184
speed decimal #459726944

info icon

snake_case

To make the reading of programs easier, programmers often follow coding conventions. They are not rules, but guidelines. A Python coding convention is that variables are written in snake_case. The letters in a snake_case identifier are all lower case and each successive word in the identifier is preceded by a single underscore. So, miles_per_hour is in a snake_case, but meetingTime is not.

""" Purpose: introduce assignment

"""

x = 10

y = x

print( "x:", x )

print( "y:", y )

x = 10

Variable Value
x 10

y = x

Because I see the current value of x is 10 according to my tracking, y is assigned 10.

Variable Value
x 10
y 10

print( "x:", x )

x: 10

print( "y:", y )

y: 10

""" Purpose: demonstrate assignment only effects the being assigned

"""

x = 10

y = x

x = 20

print( "x:", x )

print( "y:", y )

Variable Value
x 10

Variable Value
x 10
y 10

Variable Value
x 20
y 10

x: 20

y: 10

""" Purpose: quick assessment of assignment

"""

x = "Toto"

y = "Dog"

x = y

y = x

print( "x:", x )

print( "y:", y )

x: Dog

y: Dog



Section 3.3: Multi-assignment

""" Purpose: demonstrate multi-assignment

"""

# Letters in love

c1, c2, c3, c4 = "love"

# Start of the new millennium

month, day, year = "January", 1, 2000

# Results

print( c1, c2, c3, c4 )

print()

print( month, day, year )

# Letters in love

c1, c2, c3, c4 = "love"

# Start of the new millennium

month, day, year = "January", 1, 2000

l o v e

January 1 2000

alert

Assignment gotchas

  • While assignment statements seem simple, many beginning programmers have difficulty with them. I think the biggest problem is that the assignment operator is often read by beginning programmers as the equals operator, when in fact it is the assignment operator.
  • If you say equals operator, you might get confused where the target variable can be situated. There is no choice — the target variable is always to the left of the assignment operator.
  • Another misconception sometimes occurs if a variable is used as a target of more than one assignment. Each time a variable is the target of an assignment, it becomes associated with the new value that just been calculated and only that value. Previous associations are forgotten and cannot be re-remembered. If you need to remember a prior calculation, it needs its own variable.


Section 3.4: Arithmetic

# Name some variables and print their values

a = 20

b = 3

print( "a:", a )

print( "b:", b )

print()

# Perform calculations

total = a + b

difference = a - b

product = a * b

dec_quotient = a / b

int_quotient = a // b

remainder = a % b

power = a ** b

# Print results

print( "a + b: ", total )

print( "a - b: ", difference )

print( "a * b: ", product )

print( "a / b: ", dec_quotient )

print( "a // b:", int_quotient )

print( "a ** b:", power )

print( "a % b: ", remainder )

a = 20

b = 3

a + b: 23

a - b: 17

a * b: 60

a / b: 6.666666666666667

a // b: 6

a ** b: 8000

a % b: 2


# Name some variables and print their values

x = 2.71

y = 3.14

print( "x:", x )

print( "y:", y )

print()

# Perform calculations

total = x + y

difference = x - y

product = x * y

dec_quotient = x / y

int_quotient = x // y

remainder = x % y

power = x ** y

# Print results

print( "x + y: ", total )

print( "x - y: ", difference )

print( "x * y: ", product )

print( "x / y: ", dec_quotient )

print( "x // y:", int_quotient )

print( "x ** y:", power )

print( "x % y: ", remainder )

x = 2.71

y = 3.14

x + y: 5.85

x - y: -0.43000000000000016

x * y: 8.5094

x / y: 0.8630573248407643

x // y: 0.0

x ** y: 22.883559193263366

x % y: 2.71



Section 3.5: Case study — hip-hopping

rabbit rabbit

Generation: 1

Rabbits: 6

Generation: 2

Rabbits: 12

Generation: 3

Rabbits: 24

Generation: 4

Rabbits: 48

# Simulation set up

starting_number_of_rabbits = 6

growth_rate = 2

Variable Value
starting_number_of_rabbits 6
growth_rate 2

# First generation

number_of_rabbits = starting_number_of_rabbits

generation = 1

print( "Generation:", generation )

print( "Rabbits: ", number_of_rabbits )

Variable Value
starting_number_of_rabbits 6
growth_rate 2
number_of_rabbits 6
generation 1

# Next generation (second)

number_of_rabbits = growth_rate * number_of_rabbits

generation = generation + 1

print( "Generation:", generation )

print( "Rabbits: ", number_of_rabbits )

Variable Value
starting_number_of_rabbits 6
growth_rate 2
number_of_rabbits 12
generation 2

# Next generation (third)

number_of_rabbits = growth_rate * number_of_rabbits

generation = generation + 1

print( "Generation:", generation )

print( "Rabbits: ", number_of_rabbits )

Variable Value
starting_number_of_rabbits 6
growth_rate 2
number_of_rabbits 24
generation 3

# Next generation (fourth)

number_of_rabbits = growth_rate * number_of_rabbits

generation = generation + 1

print( "Generation:", generation )

print( "Rabbits: ", number_of_rabbits )

Variable Value
starting_number_of_rabbits 6
growth_rate 2
number_of_rabbits 48
generation 4

flashlight

Experimenting

  • To see the value of what we have done, try making changes to hip_hopping.py by altering the starting number of rabbits and/or and the growth rate.
  • The program will correctly handle the new scenario.


Section 3.6: Casting

Castings are like spells — New kinds of values from old — But no wands needed

string string string

""" Purpose: demonstrate some casting functions

"""

# Set and print values of interest

i = 1

f = 2.9

c = "33"

d = "4.5"

print( "Values"

print( "i:", i )

print( "f:", f )

print( "c:", "'" + c + "'" )

print( "d:", "'" + d + "'" )

print()

# Perform conversions

int_f = int( f )

float_i = float( i )

int_c = int( c )

float_d = float( d )

# Display results

print( "Conversions" )

print( "int( f ):", int_f )

print( "float( i ):", float_i )

print()

print( "int( c ):", int_c )

print( "float( d ):", float_d )

Values

i: 1

f: 2.9

c: '33'

d: '4.5'

Conversions

int( f ): 2

float( i ): 1.0

int( c ): 33

float( d ): 4.5

print( "c:", "'" + c + "'" )

print( "d:", "'" + d + "'" )


Section 3.7: What’s next


That Black Lives Matter — Should be so easy to grasp — People are people

 

Black Lives Matter Plaza captured by JP Cohoon. Its use must follow Creative Commons rules



Back

Ahead

 


© Jim Cohoon. For now, all rights reserved.