More examples

You can run all of these in Spyder. Copy the code, and paste into the code editor window.

Here's the first program.

  • toes_per_foot = 5
  • feet = 2
  • total_toes = toes_per_foot * 2
  • print(total_toes)

Try it in Spyder. Any questions about this one?

Ray
Ray

You didn't use an input here.

Right. The = puts the value on the right into the variable on the left. Python doesn't care where the value comes from. Python would be happy with all of these:

  • toes_per_foot = 5
  • toes_per_foot = float(input('How many toes on a foot'))
  • toes_per_foot = 3 + 2

As long as the thing on the right of the = sends back a number, Python will put the number into toes_per_foot, no matter where the number comes from.

Another example.

  • jan = 31
  • feb = 28
  • mar = 31
  • april = 30
  • may = 31
  • june = 30
  • july = 31
  • aug = 31
  • sept = 30
  • octbr = 31
  • nov = 30
  • dec = 31
  •  
  • winter = dec + jan + feb
  • spring = mar + april + may
  • summer = june + july + aug
  • fall = sept + octbr + nov
  •  
  • year = winter + spring + summer + fall
  •  
  • print(year)

OK, I know it's not accurate. Just go with it.

winter is an intermediate variable. Computed from a few months, the used to work out year. It only exists to make the code easier to think about.

Adela
Adela

So you could have done:

  • year = jan + feb + ...

Aye. But the code would be harder to scan for errors. Simple is good.

Another.