Variables and Types

From Training Material
Revision as of 20:08, 5 February 2013 by Kristian Rother (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Variables

Variable names may be composed of letters, underscores and, after the first position, also digits.

Lowercase letters are common, but uppercase is also allowed (usually used for constants).

invitation = 'Hello World'
a = 10
b = 3.0
PI = 3.1415

Arithmetical operations

Basic operations and variable assignments:

a = 7
b = 4
c = a-b # 3
d = a*b # 28
e = a/b # 1
f = a%b # modulo, 3
g = a**2 # 49
h = 3.0/b # 0.75
i = 7.0//b # floor div, 1.0

Types

The basic data types in Python are:

  • Boolean (True / False).
  • Integer (0, 1, -3).
  • Float (1.0, -0.3, 1.2345)
  • Strings ('apple', ”banana”) - both single and double quotes are equivalent.
  • None (aka an empty variable).

Type conversions

Variables can be converted using:

  • int(value) → from a float or string.
  • float(value) → from a int or string.
  • str(value) → from any Python variable, including None, any function or object.