2014. 9. 18. 21:43
1. Exponentiation

All that math can be done on a calculator, so why use Python? Because you can combine math with other data types (e.g. booleans) and commands to create useful programs. Calculators just stick to numbers.

Now let's work with exponents.

eight = 2 ** 3

In the above example, we create a new variable called eight and set it to 8, or the result of 2 to the power to 3 (2^3).

Notice that we use ** instead of * or the multiplication operator.

 

2. Bringing It All Together

Nice work! So far, you've learned about:

  • Variables, which store values for later use
  • Data types, such as numbers and booleans
  • Whitespace, which separates statements
  • Comments, which make your code easier to read
  • Arithmetic operations, including +, -, *, /, **, and %
Instructions

Let's put our knowledge to work.

  1. Write a single-line comment on line 1. It can be anything! (Make sure it starts with #)
  2. Set the variable monty equal to True.
  3. Set another variable python equal to 1.234.
  4. Set a third variable monty_python equal to python squared.
?
Stuck? Get a hint! Hint

You can use arithmetic operators directly on variables! Remember, python squared means python ** 2.

Note that the name of the variable monty_python has nothing to do with the name of the variables monty and python. We just decided to use these variable names because Python people love Monty Python's Flying Circus

'Python' 카테고리의 다른 글

Pythons #3 예외처리  (0) 2014.10.10
Posted by 아도니우스